]> mj.ucw.cz Git - libucw.git/blob - lib/asyncio.h
d230f68430da0632b455a10b3dd25ee207cf5ef7
[libucw.git] / lib / asyncio.h
1 /*
2  *      UCW Library -- Asynchronous I/O
3  *
4  *      (c) 2006 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_ASYNCIO_H
11 #define _UCW_ASYNCIO_H
12
13 #include "lib/semaphore.h"
14 #include "lib/clists.h"
15
16 /* FIXME: Comment, especially on request ordering */
17
18 struct asio_queue {
19   uns max_requests;                     // Maximum number of requests allowed on this queue [user-settable]
20   uns buffer_size;                      // How large buffers do we use [user-settable]
21   uns allocated_requests;
22   uns running_requests;
23   clist idle_list;                      // Recycled requests waiting for get
24   clist done_list;                      // Requests returned from the worker threads
25   sem_t *done_sem;                      // ... and how many of them
26   clist wait_list;                      // Requests available for wait()
27 };
28
29 enum asio_op {
30   ASIO_FREE,
31   ASIO_READ,
32   ASIO_WRITE,
33   ASIO_BACKGROUND_WRITE,                // Write with no success notification
34 };
35
36 struct asio_request {
37   cnode n;
38   struct asio_queue *queue;
39   byte *buffer;
40   int fd;
41   enum asio_op op;
42   uns len;
43   int status;
44 };
45
46 void asio_init(void);
47 void asio_cleanup(void);
48
49 void asio_init_queue(struct asio_queue *q);                     // Initialize a new queue
50 struct asio_request *asio_get(struct asio_queue *q);            // Get an empty request
51 void asio_submit(struct asio_request *r);                       // Submit the request
52 struct asio_request *asio_wait(struct asio_queue *q);           // Wait for the first finished request, NULL if no more
53 void asio_put(struct asio_request *r);                          // Return a finished request for recycling
54 struct asio_request *asio_get_bg(struct asio_queue *q);         // Get and if there are no free requests, wait for background writes to finish
55 void asio_sync(struct asio_queue *q);                           // Wait for all requests to finish
56
57 #endif  /* !_UCW_ASYNCIO_H */