2 * QEMU posix-aio emulation
4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
20 #include "posix-aio-compat.h"
22 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
23 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
24 static pthread_t thread_id
;
25 static int max_threads
= 64;
26 static int cur_threads
= 0;
27 static int idle_threads
= 0;
28 static TAILQ_HEAD(, qemu_paiocb
) request_list
;
30 static void *aio_thread(void *unused
)
34 /* block all signals */
36 sigprocmask(SIG_BLOCK
, &set
, NULL
);
39 struct qemu_paiocb
*aiocb
;
43 pthread_mutex_lock(&lock
);
45 while (TAILQ_EMPTY(&request_list
) &&
46 !(ret
== ETIMEDOUT
)) {
47 struct timespec ts
= { 0 };
50 qemu_gettimeofday(&tv
);
51 ts
.tv_sec
= tv
.tv_sec
+ 10;
52 ret
= pthread_cond_timedwait(&cond
, &lock
, &ts
);
58 aiocb
= TAILQ_FIRST(&request_list
);
59 TAILQ_REMOVE(&request_list
, aiocb
, node
);
65 pthread_mutex_unlock(&lock
);
67 while (offset
< aiocb
->aio_nbytes
) {
71 len
= pwrite(aiocb
->aio_fildes
,
72 (const char *)aiocb
->aio_buf
+ offset
,
73 aiocb
->aio_nbytes
- offset
,
74 aiocb
->aio_offset
+ offset
);
76 len
= pread(aiocb
->aio_fildes
,
77 (char *)aiocb
->aio_buf
+ offset
,
78 aiocb
->aio_nbytes
- offset
,
79 aiocb
->aio_offset
+ offset
);
81 if (len
== -1 && errno
== EINTR
)
92 pthread_mutex_lock(&lock
);
95 pthread_mutex_unlock(&lock
);
97 kill(getpid(), aiocb
->ev_signo
);
102 pthread_mutex_unlock(&lock
);
107 static int spawn_thread(void)
115 pthread_attr_init(&attr
);
116 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
117 ret
= pthread_create(&thread_id
, &attr
, aio_thread
, NULL
);
118 pthread_attr_destroy(&attr
);
123 int qemu_paio_init(struct qemu_paioinit
*aioinit
)
125 TAILQ_INIT(&request_list
);
130 static int qemu_paio_submit(struct qemu_paiocb
*aiocb
, int is_write
)
132 aiocb
->is_write
= is_write
;
133 aiocb
->ret
= -EINPROGRESS
;
135 pthread_mutex_lock(&lock
);
136 if (idle_threads
== 0 && cur_threads
< max_threads
)
138 TAILQ_INSERT_TAIL(&request_list
, aiocb
, node
);
139 pthread_mutex_unlock(&lock
);
140 pthread_cond_broadcast(&cond
);
145 int qemu_paio_read(struct qemu_paiocb
*aiocb
)
147 return qemu_paio_submit(aiocb
, 0);
150 int qemu_paio_write(struct qemu_paiocb
*aiocb
)
152 return qemu_paio_submit(aiocb
, 1);
155 ssize_t
qemu_paio_return(struct qemu_paiocb
*aiocb
)
159 pthread_mutex_lock(&lock
);
161 pthread_mutex_unlock(&lock
);
166 int qemu_paio_error(struct qemu_paiocb
*aiocb
)
168 ssize_t ret
= qemu_paio_return(aiocb
);
178 int qemu_paio_cancel(int fd
, struct qemu_paiocb
*aiocb
)
182 pthread_mutex_lock(&lock
);
183 if (!aiocb
->active
) {
184 TAILQ_REMOVE(&request_list
, aiocb
, node
);
185 aiocb
->ret
= -ECANCELED
;
186 ret
= QEMU_PAIO_CANCELED
;
187 } else if (aiocb
->ret
== -EINPROGRESS
)
188 ret
= QEMU_PAIO_NOTCANCELED
;
190 ret
= QEMU_PAIO_ALLDONE
;
191 pthread_mutex_unlock(&lock
);