chroot and change user support (Nolan)
[qemu/mini2440/sniper_sniper_test.git] / posix-aio-compat.c
blob6b547f41fd96fd1694735be814270b852ad18b41
1 /*
2 * QEMU posix-aio emulation
4 * Copyright IBM, Corp. 2008
6 * Authors:
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.
14 #include <pthread.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <time.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "osdep.h"
23 #include "posix-aio-compat.h"
25 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
27 static pthread_t thread_id;
28 static pthread_attr_t attr;
29 static int max_threads = 64;
30 static int cur_threads = 0;
31 static int idle_threads = 0;
32 static TAILQ_HEAD(, qemu_paiocb) request_list;
34 static void die2(int err, const char *what)
36 fprintf(stderr, "%s failed: %s\n", what, strerror(err));
37 abort();
40 static void die(const char *what)
42 die2(errno, what);
45 static void mutex_lock(pthread_mutex_t *mutex)
47 int ret = pthread_mutex_lock(mutex);
48 if (ret) die2(ret, "pthread_mutex_lock");
51 static void mutex_unlock(pthread_mutex_t *mutex)
53 int ret = pthread_mutex_unlock(mutex);
54 if (ret) die2(ret, "pthread_mutex_unlock");
57 static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
58 struct timespec *ts)
60 int ret = pthread_cond_timedwait(cond, mutex, ts);
61 if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait");
62 return ret;
65 static void cond_signal(pthread_cond_t *cond)
67 int ret = pthread_cond_signal(cond);
68 if (ret) die2(ret, "pthread_cond_signal");
71 static void thread_create(pthread_t *thread, pthread_attr_t *attr,
72 void *(*start_routine)(void*), void *arg)
74 int ret = pthread_create(thread, attr, start_routine, arg);
75 if (ret) die2(ret, "pthread_create");
78 static void *aio_thread(void *unused)
80 pid_t pid;
81 sigset_t set;
83 pid = getpid();
85 /* block all signals */
86 if (sigfillset(&set)) die("sigfillset");
87 if (sigprocmask(SIG_BLOCK, &set, NULL)) die("sigprocmask");
89 while (1) {
90 struct qemu_paiocb *aiocb;
91 size_t offset;
92 int ret = 0;
93 qemu_timeval tv;
94 struct timespec ts;
96 qemu_gettimeofday(&tv);
97 ts.tv_sec = tv.tv_sec + 10;
98 ts.tv_nsec = 0;
100 mutex_lock(&lock);
102 while (TAILQ_EMPTY(&request_list) &&
103 !(ret == ETIMEDOUT)) {
104 ret = cond_timedwait(&cond, &lock, &ts);
107 if (TAILQ_EMPTY(&request_list))
108 break;
110 aiocb = TAILQ_FIRST(&request_list);
111 TAILQ_REMOVE(&request_list, aiocb, node);
113 offset = 0;
114 aiocb->active = 1;
116 idle_threads--;
117 mutex_unlock(&lock);
119 while (offset < aiocb->aio_nbytes) {
120 ssize_t len;
122 if (aiocb->is_write)
123 len = pwrite(aiocb->aio_fildes,
124 (const char *)aiocb->aio_buf + offset,
125 aiocb->aio_nbytes - offset,
126 aiocb->aio_offset + offset);
127 else
128 len = pread(aiocb->aio_fildes,
129 (char *)aiocb->aio_buf + offset,
130 aiocb->aio_nbytes - offset,
131 aiocb->aio_offset + offset);
133 if (len == -1 && errno == EINTR)
134 continue;
135 else if (len == -1) {
136 offset = -errno;
137 break;
138 } else if (len == 0)
139 break;
141 offset += len;
144 mutex_lock(&lock);
145 aiocb->ret = offset;
146 idle_threads++;
147 mutex_unlock(&lock);
149 if (kill(pid, aiocb->ev_signo)) die("kill failed");
152 idle_threads--;
153 cur_threads--;
154 mutex_unlock(&lock);
156 return NULL;
159 static void spawn_thread(void)
161 cur_threads++;
162 idle_threads++;
163 thread_create(&thread_id, &attr, aio_thread, NULL);
166 int qemu_paio_init(struct qemu_paioinit *aioinit)
168 int ret;
170 ret = pthread_attr_init(&attr);
171 if (ret) die2(ret, "pthread_attr_init");
173 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
174 if (ret) die2(ret, "pthread_attr_setdetachstate");
176 TAILQ_INIT(&request_list);
178 return 0;
181 static int qemu_paio_submit(struct qemu_paiocb *aiocb, int is_write)
183 aiocb->is_write = is_write;
184 aiocb->ret = -EINPROGRESS;
185 aiocb->active = 0;
186 mutex_lock(&lock);
187 if (idle_threads == 0 && cur_threads < max_threads)
188 spawn_thread();
189 TAILQ_INSERT_TAIL(&request_list, aiocb, node);
190 mutex_unlock(&lock);
191 cond_signal(&cond);
193 return 0;
196 int qemu_paio_read(struct qemu_paiocb *aiocb)
198 return qemu_paio_submit(aiocb, 0);
201 int qemu_paio_write(struct qemu_paiocb *aiocb)
203 return qemu_paio_submit(aiocb, 1);
206 ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
208 ssize_t ret;
210 mutex_lock(&lock);
211 ret = aiocb->ret;
212 mutex_unlock(&lock);
214 return ret;
217 int qemu_paio_error(struct qemu_paiocb *aiocb)
219 ssize_t ret = qemu_paio_return(aiocb);
221 if (ret < 0)
222 ret = -ret;
223 else
224 ret = 0;
226 return ret;
229 int qemu_paio_cancel(int fd, struct qemu_paiocb *aiocb)
231 int ret;
233 mutex_lock(&lock);
234 if (!aiocb->active) {
235 TAILQ_REMOVE(&request_list, aiocb, node);
236 aiocb->ret = -ECANCELED;
237 ret = QEMU_PAIO_CANCELED;
238 } else if (aiocb->ret == -EINPROGRESS)
239 ret = QEMU_PAIO_NOTCANCELED;
240 else
241 ret = QEMU_PAIO_ALLDONE;
242 mutex_unlock(&lock);
244 return ret;