2 * UCW Library -- Asynchronous I/O
4 * (c) 2006 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
14 #include "lib/threads.h"
20 static uns asio_num_users;
21 static struct worker_pool asio_wpool;
24 asio_init_unlocked(void)
30 asio_wpool.num_threads = 1;
31 worker_pool_init(&asio_wpool);
35 asio_cleanup_unlocked(void)
41 worker_pool_cleanup(&asio_wpool);
45 asio_init_queue(struct asio_queue *q)
51 DBG("ASIO: New queue %p", q);
52 ASSERT(q->buffer_size);
53 q->allocated_requests = 0;
54 q->running_requests = 0;
55 q->running_writebacks = 0;
57 clist_init(&q->idle_list);
58 clist_init(&q->done_list);
59 work_queue_init(&asio_wpool, &q->queue);
63 asio_cleanup_queue(struct asio_queue *q)
65 DBG("ASIO: Removing queue %p", q);
66 ASSERT(!q->running_requests);
67 ASSERT(!q->running_writebacks);
68 ASSERT(!q->allocated_requests);
69 ASSERT(clist_empty(&q->done_list));
71 struct asio_request *r;
72 while (r = clist_head(&q->idle_list))
74 clist_remove(&r->work.n);
75 big_free(r->buffer, q->buffer_size);
79 work_queue_cleanup(&q->queue);
82 asio_cleanup_unlocked();
87 asio_get(struct asio_queue *q)
89 q->allocated_requests++;
90 struct asio_request *r = clist_head(&q->idle_list);
93 r = xmalloc_zero(sizeof(*r));
95 r->buffer = big_alloc(q->buffer_size);
96 DBG("ASIO: Got %p (new)", r);
100 clist_remove(&r->work.n);
101 DBG("ASIO: Got %p", r);
107 r->returned_errno = -1;
113 asio_raw_wait(struct asio_queue *q)
115 struct asio_request *r = (struct asio_request *) work_wait(&q->queue);
119 q->running_requests--;
120 if (r->op == ASIO_WRITE_BACK)
122 DBG("ASIO: Finished writeback %p", r);
124 die("Asynchronous write to fd %d failed: %s", r->fd, strerror(r->returned_errno));
125 if (r->status != (int)r->len)
126 die("Asynchronous write to fd %d wrote only %d bytes out of %d", r->fd, r->status, r->len);
127 q->running_writebacks--;
131 clist_add_tail(&q->done_list, &r->work.n);
136 asio_handler(struct worker_thread *t UNUSED, struct work *w)
138 struct asio_request *r = (struct asio_request *) w;
140 DBG("ASIO: Servicing %p (%s on fd=%d, len=%d)", r,
141 (char*[]) { "?", "READ", "WRITE", "WRITEBACK" }[r->op], r->fd, r->len);
146 r->status = read(r->fd, r->buffer, r->len);
149 case ASIO_WRITE_BACK:
150 r->status = write(r->fd, r->buffer, r->len);
153 die("ASIO: Got unknown request type %d", r->op);
155 r->returned_errno = errno;
156 DBG("ASIO: Finished %p (status=%d, errno=%d)", r, r->status, r->returned_errno);
160 asio_submit(struct asio_request *r)
162 struct asio_queue *q = r->queue;
163 DBG("ASIO: Submitting %p on queue %p", r, q);
164 ASSERT(r->op != ASIO_FREE);
165 ASSERT(!r->submitted);
166 if (r->op == ASIO_WRITE_BACK)
168 while (q->running_writebacks >= q->max_writebacks)
170 DBG("ASIO: Waiting for free writebacks");
171 if (!asio_raw_wait(q))
174 q->running_writebacks++;
176 q->running_requests++;
178 r->work.go = asio_handler;
179 r->work.priority = 0;
180 work_submit(&q->queue, &r->work);
183 struct asio_request *
184 asio_wait(struct asio_queue *q)
186 struct asio_request *r;
187 while (!(r = clist_head(&q->done_list)))
189 DBG("ASIO: Waiting on queue %p", q);
190 if (!asio_raw_wait(q))
193 clist_remove(&r->work.n);
194 DBG("ASIO: Done %p", r);
199 asio_put(struct asio_request *r)
201 struct asio_queue *q = r->queue;
202 DBG("ASIO: Put %p", r);
203 ASSERT(!r->submitted);
204 ASSERT(q->allocated_requests);
205 clist_add_tail(&q->idle_list, &r->work.n);
206 q->allocated_requests--;
210 asio_sync(struct asio_queue *q)
212 DBG("ASIO: Syncing queue %p", q);
213 while (q->running_requests)
214 if (!asio_raw_wait(q))
223 struct asio_request *r;
225 q.buffer_size = 4096;
226 q.max_writebacks = 2;
236 r->len = q.buffer_size;
245 r->op = ASIO_WRITE_BACK;
263 for (uns i=0; i<10; i++)
266 r->op = ASIO_WRITE_BACK;
269 r->buffer[0] = 'A' + i;
286 asio_cleanup_queue(&q);