Statistics
| Branch: | Revision:

root / include / block / aio.h @ f2e5dca4

History | View | Annotate | Download (7.5 kB)

1
/*
2
 * QEMU aio implementation
3
 *
4
 * Copyright IBM, Corp. 2008
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 */
13

    
14
#ifndef QEMU_AIO_H
15
#define QEMU_AIO_H
16

    
17
#include "qemu-common.h"
18
#include "qemu/queue.h"
19
#include "qemu/event_notifier.h"
20
#include "qemu/thread.h"
21

    
22
typedef struct BlockDriverAIOCB BlockDriverAIOCB;
23
typedef void BlockDriverCompletionFunc(void *opaque, int ret);
24

    
25
typedef struct AIOCBInfo {
26
    void (*cancel)(BlockDriverAIOCB *acb);
27
    size_t aiocb_size;
28
} AIOCBInfo;
29

    
30
struct BlockDriverAIOCB {
31
    const AIOCBInfo *aiocb_info;
32
    BlockDriverState *bs;
33
    BlockDriverCompletionFunc *cb;
34
    void *opaque;
35
};
36

    
37
void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
38
                   BlockDriverCompletionFunc *cb, void *opaque);
39
void qemu_aio_release(void *p);
40

    
41
typedef struct AioHandler AioHandler;
42
typedef void QEMUBHFunc(void *opaque);
43
typedef void IOHandler(void *opaque);
44

    
45
typedef struct AioContext {
46
    GSource source;
47

    
48
    /* The list of registered AIO handlers */
49
    QLIST_HEAD(, AioHandler) aio_handlers;
50

    
51
    /* This is a simple lock used to protect the aio_handlers list.
52
     * Specifically, it's used to ensure that no callbacks are removed while
53
     * we're walking and dispatching callbacks.
54
     */
55
    int walking_handlers;
56

    
57
    /* lock to protect between bh's adders and deleter */
58
    QemuMutex bh_lock;
59
    /* Anchor of the list of Bottom Halves belonging to the context */
60
    struct QEMUBH *first_bh;
61

    
62
    /* A simple lock used to protect the first_bh list, and ensure that
63
     * no callbacks are removed while we're walking and dispatching callbacks.
64
     */
65
    int walking_bh;
66

    
67
    /* Used for aio_notify.  */
68
    EventNotifier notifier;
69

    
70
    /* GPollFDs for aio_poll() */
71
    GArray *pollfds;
72

    
73
    /* Thread pool for performing work and receiving completion callbacks */
74
    struct ThreadPool *thread_pool;
75
} AioContext;
76

    
77
/**
78
 * aio_context_new: Allocate a new AioContext.
79
 *
80
 * AioContext provide a mini event-loop that can be waited on synchronously.
81
 * They also provide bottom halves, a service to execute a piece of code
82
 * as soon as possible.
83
 */
84
AioContext *aio_context_new(void);
85

    
86
/**
87
 * aio_context_ref:
88
 * @ctx: The AioContext to operate on.
89
 *
90
 * Add a reference to an AioContext.
91
 */
92
void aio_context_ref(AioContext *ctx);
93

    
94
/**
95
 * aio_context_unref:
96
 * @ctx: The AioContext to operate on.
97
 *
98
 * Drop a reference to an AioContext.
99
 */
100
void aio_context_unref(AioContext *ctx);
101

    
102
/**
103
 * aio_bh_new: Allocate a new bottom half structure.
104
 *
105
 * Bottom halves are lightweight callbacks whose invocation is guaranteed
106
 * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
107
 * is opaque and must be allocated prior to its use.
108
 */
109
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
110

    
111
/**
112
 * aio_notify: Force processing of pending events.
113
 *
114
 * Similar to signaling a condition variable, aio_notify forces
115
 * aio_wait to exit, so that the next call will re-examine pending events.
116
 * The caller of aio_notify will usually call aio_wait again very soon,
117
 * or go through another iteration of the GLib main loop.  Hence, aio_notify
118
 * also has the side effect of recalculating the sets of file descriptors
119
 * that the main loop waits for.
120
 *
121
 * Calling aio_notify is rarely necessary, because for example scheduling
122
 * a bottom half calls it already.
123
 */
124
void aio_notify(AioContext *ctx);
125

    
126
/**
127
 * aio_bh_poll: Poll bottom halves for an AioContext.
128
 *
129
 * These are internal functions used by the QEMU main loop.
130
 * And notice that multiple occurrences of aio_bh_poll cannot
131
 * be called concurrently
132
 */
133
int aio_bh_poll(AioContext *ctx);
134

    
135
/**
136
 * qemu_bh_schedule: Schedule a bottom half.
137
 *
138
 * Scheduling a bottom half interrupts the main loop and causes the
139
 * execution of the callback that was passed to qemu_bh_new.
140
 *
141
 * Bottom halves that are scheduled from a bottom half handler are instantly
142
 * invoked.  This can create an infinite loop if a bottom half handler
143
 * schedules itself.
144
 *
145
 * @bh: The bottom half to be scheduled.
146
 */
147
void qemu_bh_schedule(QEMUBH *bh);
148

    
149
/**
150
 * qemu_bh_cancel: Cancel execution of a bottom half.
151
 *
152
 * Canceling execution of a bottom half undoes the effect of calls to
153
 * qemu_bh_schedule without freeing its resources yet.  While cancellation
154
 * itself is also wait-free and thread-safe, it can of course race with the
155
 * loop that executes bottom halves unless you are holding the iothread
156
 * mutex.  This makes it mostly useless if you are not holding the mutex.
157
 *
158
 * @bh: The bottom half to be canceled.
159
 */
160
void qemu_bh_cancel(QEMUBH *bh);
161

    
162
/**
163
 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
164
 *
165
 * Deleting a bottom half frees the memory that was allocated for it by
166
 * qemu_bh_new.  It also implies canceling the bottom half if it was
167
 * scheduled.
168
 * This func is async. The bottom half will do the delete action at the finial
169
 * end.
170
 *
171
 * @bh: The bottom half to be deleted.
172
 */
173
void qemu_bh_delete(QEMUBH *bh);
174

    
175
/* Return whether there are any pending callbacks from the GSource
176
 * attached to the AioContext.
177
 *
178
 * This is used internally in the implementation of the GSource.
179
 */
180
bool aio_pending(AioContext *ctx);
181

    
182
/* Progress in completing AIO work to occur.  This can issue new pending
183
 * aio as a result of executing I/O completion or bh callbacks.
184
 *
185
 * If there is no pending AIO operation or completion (bottom half),
186
 * return false.  If there are pending AIO operations of bottom halves,
187
 * return true.
188
 *
189
 * If there are no pending bottom halves, but there are pending AIO
190
 * operations, it may not be possible to make any progress without
191
 * blocking.  If @blocking is true, this function will wait until one
192
 * or more AIO events have completed, to ensure something has moved
193
 * before returning.
194
 */
195
bool aio_poll(AioContext *ctx, bool blocking);
196

    
197
#ifdef CONFIG_POSIX
198
/* Register a file descriptor and associated callbacks.  Behaves very similarly
199
 * to qemu_set_fd_handler2.  Unlike qemu_set_fd_handler2, these callbacks will
200
 * be invoked when using qemu_aio_wait().
201
 *
202
 * Code that invokes AIO completion functions should rely on this function
203
 * instead of qemu_set_fd_handler[2].
204
 */
205
void aio_set_fd_handler(AioContext *ctx,
206
                        int fd,
207
                        IOHandler *io_read,
208
                        IOHandler *io_write,
209
                        void *opaque);
210
#endif
211

    
212
/* Register an event notifier and associated callbacks.  Behaves very similarly
213
 * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
214
 * will be invoked when using qemu_aio_wait().
215
 *
216
 * Code that invokes AIO completion functions should rely on this function
217
 * instead of event_notifier_set_handler.
218
 */
219
void aio_set_event_notifier(AioContext *ctx,
220
                            EventNotifier *notifier,
221
                            EventNotifierHandler *io_read);
222

    
223
/* Return a GSource that lets the main loop poll the file descriptors attached
224
 * to this AioContext.
225
 */
226
GSource *aio_get_g_source(AioContext *ctx);
227

    
228
/* Return the ThreadPool bound to this AioContext */
229
struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
230

    
231
/* Functions to operate on the main QEMU AioContext.  */
232

    
233
bool qemu_aio_wait(void);
234
void qemu_aio_set_event_notifier(EventNotifier *notifier,
235
                                 EventNotifierHandler *io_read);
236

    
237
#ifdef CONFIG_POSIX
238
void qemu_aio_set_fd_handler(int fd,
239
                             IOHandler *io_read,
240
                             IOHandler *io_write,
241
                             void *opaque);
242
#endif
243

    
244
#endif