]> Git Repo - qemu.git/blame - include/block/aio-wait.h
Merge remote-tracking branch 'remotes/cohuck/tags/s390x-20181004' into staging
[qemu.git] / include / block / aio-wait.h
CommitLineData
7719f3c9
SH
1/*
2 * AioContext wait support
3 *
4 * Copyright (C) 2018 Red Hat, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#ifndef QEMU_AIO_WAIT_H
26#define QEMU_AIO_WAIT_H
27
28#include "block/aio.h"
29
30/**
31 * AioWait:
32 *
cfe29d82
KW
33 * An object that facilitates synchronous waiting on a condition. A single
34 * global AioWait object (global_aio_wait) is used internally.
35 *
36 * The main loop can wait on an operation running in an IOThread as follows:
7719f3c9 37 *
7719f3c9
SH
38 * AioContext *ctx = ...;
39 * MyWork work = { .done = false };
40 * schedule_my_work_in_iothread(ctx, &work);
cfe29d82 41 * AIO_WAIT_WHILE(ctx, !work.done);
7719f3c9
SH
42 *
43 * The IOThread must call aio_wait_kick() to notify the main loop when
44 * work.done changes:
45 *
46 * static void do_work(...)
47 * {
48 * ...
49 * work.done = true;
cfe29d82 50 * aio_wait_kick();
7719f3c9
SH
51 * }
52 */
53typedef struct {
7376eda7
SH
54 /* Number of waiting AIO_WAIT_WHILE() callers. Accessed with atomic ops. */
55 unsigned num_waiters;
7719f3c9
SH
56} AioWait;
57
cfe29d82
KW
58extern AioWait global_aio_wait;
59
7719f3c9
SH
60/**
61 * AIO_WAIT_WHILE:
4d22bbf4
KW
62 * @ctx: the aio context, or NULL if multiple aio contexts (for which the
63 * caller does not hold a lock) are involved in the polling condition.
7719f3c9
SH
64 * @cond: wait while this conditional expression is true
65 *
66 * Wait while a condition is true. Use this to implement synchronous
67 * operations that require event loop activity.
68 *
69 * The caller must be sure that something calls aio_wait_kick() when the value
70 * of @cond might have changed.
71 *
72 * The caller's thread must be the IOThread that owns @ctx or the main loop
73 * thread (with @ctx acquired exactly once). This function cannot be used to
74 * wait on conditions between two IOThreads since that could lead to deadlock,
75 * go via the main loop instead.
76 */
cfe29d82 77#define AIO_WAIT_WHILE(ctx, cond) ({ \
7376eda7 78 bool waited_ = false; \
cfe29d82 79 AioWait *wait_ = &global_aio_wait; \
7376eda7 80 AioContext *ctx_ = (ctx); \
48657448
KW
81 /* Increment wait_->num_waiters before evaluating cond. */ \
82 atomic_inc(&wait_->num_waiters); \
4d22bbf4 83 if (ctx_ && in_aio_context_home_thread(ctx_)) { \
1cc8e54a
KW
84 while ((cond)) { \
85 aio_poll(ctx_, true); \
86 waited_ = true; \
7376eda7
SH
87 } \
88 } else { \
89 assert(qemu_get_current_aio_context() == \
90 qemu_get_aio_context()); \
1cc8e54a 91 while ((cond)) { \
4d22bbf4
KW
92 if (ctx_) { \
93 aio_context_release(ctx_); \
94 } \
1cc8e54a 95 aio_poll(qemu_get_aio_context(), true); \
4d22bbf4
KW
96 if (ctx_) { \
97 aio_context_acquire(ctx_); \
98 } \
1cc8e54a 99 waited_ = true; \
7376eda7 100 } \
7376eda7 101 } \
48657448 102 atomic_dec(&wait_->num_waiters); \
7719f3c9
SH
103 waited_; })
104
105/**
106 * aio_wait_kick:
7719f3c9
SH
107 * Wake up the main thread if it is waiting on AIO_WAIT_WHILE(). During
108 * synchronous operations performed in an IOThread, the main thread lets the
109 * IOThread's event loop run, waiting for the operation to complete. A
110 * aio_wait_kick() call will wake up the main thread.
111 */
cfe29d82 112void aio_wait_kick(void);
7719f3c9 113
b89d92f3
SH
114/**
115 * aio_wait_bh_oneshot:
116 * @ctx: the aio context
117 * @cb: the BH callback function
118 * @opaque: user data for the BH callback function
119 *
120 * Run a BH in @ctx and wait for it to complete.
121 *
122 * Must be called from the main loop thread with @ctx acquired exactly once.
123 * Note that main loop event processing may occur.
124 */
125void aio_wait_bh_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
126
7719f3c9 127#endif /* QEMU_AIO_WAIT */
This page took 0.0958 seconds and 4 git commands to generate.