]> mj.ucw.cz Git - libucw.git/blob - lib/asio.h
45c7fb9f40d1899040814e9b8e9b08f7513833bf
[libucw.git] / lib / asio.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_ASIO_H
11 #define _UCW_ASIO_H
12
13 #include "lib/workqueue.h"
14 #include "lib/clists.h"
15
16 /* FIXME: Comment, especially on request ordering */
17
18 struct asio_queue {
19   uns buffer_size;                      // How large buffers do we use [user-settable]
20   uns max_writebacks;                   // Maximum number of writeback requests active [user-settable]
21   uns allocated_requests;
22   uns running_requests;                 // Total number of running requests
23   uns running_writebacks;               // How many of them are writebacks
24   clist idle_list;                      // Recycled requests waiting for get
25   clist done_list;                      // Finished requests
26   struct work_queue queue;
27 };
28
29 enum asio_op {
30   ASIO_FREE,
31   ASIO_READ,
32   ASIO_WRITE,
33   ASIO_WRITE_BACK,                      // Background write with no success notification
34 };
35
36 struct asio_request {
37   struct work work;                     // asio_requests are internally just work nodes
38   struct asio_queue *queue;
39   byte *buffer;
40   int fd;
41   enum asio_op op;
42   uns len;
43   int status;
44   int returned_errno;
45   int submitted;
46 };
47
48 void asio_init_queue(struct asio_queue *q);                     // Initialize a new queue
49 void asio_cleanup_queue(struct asio_queue *q);
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 (can block if too many writebacks)
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 void asio_sync(struct asio_queue *q);                           // Wait until all requests are finished
55
56 #endif  /* !_UCW_ASIO_H */