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 <ucw/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_remove_head(&q->idle_list))
74 big_free(r->buffer, q->buffer_size);
78 work_queue_cleanup(&q->queue);
81 asio_cleanup_unlocked();
86 asio_get(struct asio_queue *q)
88 q->allocated_requests++;
89 struct asio_request *r = clist_head(&q->idle_list);
92 r = xmalloc_zero(sizeof(*r));
94 r->buffer = big_alloc(q->buffer_size);
95 DBG("ASIO: Got %p (new)", r);
99 clist_remove(&r->work.n);
100 DBG("ASIO: Got %p", r);
106 r->returned_errno = -1;
112 asio_raw_wait(struct asio_queue *q)
114 struct asio_request *r = (struct asio_request *) work_wait(&q->queue);
118 q->running_requests--;
119 if (r->op == ASIO_WRITE_BACK)
121 DBG("ASIO: Finished writeback %p", r);
123 die("Asynchronous write to fd %d failed: %s", r->fd, strerror(r->returned_errno));
124 if (r->status != (int)r->len)
125 die("Asynchronous write to fd %d wrote only %d bytes out of %d", r->fd, r->status, r->len);
126 q->running_writebacks--;
130 clist_add_tail(&q->done_list, &r->work.n);
135 asio_handler(struct worker_thread *t UNUSED, struct work *w)
137 struct asio_request *r = (struct asio_request *) w;
139 DBG("ASIO: Servicing %p (%s on fd=%d, len=%d)", r,
140 (char*[]) { "?", "READ", "WRITE", "WRITEBACK" }[r->op], r->fd, r->len);
145 r->status = read(r->fd, r->buffer, r->len);
148 case ASIO_WRITE_BACK:
149 r->status = write(r->fd, r->buffer, r->len);
152 die("ASIO: Got unknown request type %d", r->op);
154 r->returned_errno = errno;
155 DBG("ASIO: Finished %p (status=%d, errno=%d)", r, r->status, r->returned_errno);
159 asio_submit(struct asio_request *r)
161 struct asio_queue *q = r->queue;
162 DBG("ASIO: Submitting %p on queue %p", r, q);
163 ASSERT(r->op != ASIO_FREE);
164 ASSERT(!r->submitted);
165 if (r->op == ASIO_WRITE_BACK)
167 while (q->running_writebacks >= q->max_writebacks)
169 DBG("ASIO: Waiting for free writebacks");
170 if (!asio_raw_wait(q))
173 q->running_writebacks++;
175 q->running_requests++;
177 r->work.go = asio_handler;
178 r->work.priority = 0;
179 work_submit(&q->queue, &r->work);
182 struct asio_request *
183 asio_wait(struct asio_queue *q)
185 struct asio_request *r;
186 while (!(r = clist_head(&q->done_list)))
188 DBG("ASIO: Waiting on queue %p", q);
189 if (!asio_raw_wait(q))
192 clist_remove(&r->work.n);
193 DBG("ASIO: Done %p", r);
198 asio_put(struct asio_request *r)
200 struct asio_queue *q = r->queue;
201 DBG("ASIO: Put %p", r);
202 ASSERT(!r->submitted);
203 ASSERT(q->allocated_requests);
204 clist_add_tail(&q->idle_list, &r->work.n);
205 q->allocated_requests--;
209 asio_sync(struct asio_queue *q)
211 DBG("ASIO: Syncing queue %p", q);
212 while (q->running_requests)
213 if (!asio_raw_wait(q))
222 struct asio_request *r;
224 q.buffer_size = 4096;
225 q.max_writebacks = 2;
235 r->len = q.buffer_size;
244 r->op = ASIO_WRITE_BACK;
262 for (uns i=0; i<10; i++)
265 r->op = ASIO_WRITE_BACK;
268 r->buffer[0] = 'A' + i;
285 asio_cleanup_queue(&q);