… | |
… | |
25 | similar functions, as well as less rarely ones such as C<mknod>, C<futime> |
25 | similar functions, as well as less rarely ones such as C<mknod>, C<futime> |
26 | or C<readlink>. |
26 | or C<readlink>. |
27 | |
27 | |
28 | It also offers wrappers around C<sendfile> (Solaris, Linux, HP-UX and |
28 | It also offers wrappers around C<sendfile> (Solaris, Linux, HP-UX and |
29 | FreeBSD, with emulation on other platforms) and C<readahead> (Linux, with |
29 | FreeBSD, with emulation on other platforms) and C<readahead> (Linux, with |
30 | emulation elsewhere>). |
30 | emulation elsewhere). |
31 | |
31 | |
32 | The goal is to enable you to write fully non-blocking programs. For |
32 | The goal is to enable you to write fully non-blocking programs. For |
33 | example, in a game server, you would not want to freeze for a few seconds |
33 | example, in a game server, you would not want to freeze for a few seconds |
34 | just because the server is running a backup and you happen to call |
34 | just because the server is running a backup and you happen to call |
35 | C<readdir>. |
35 | C<readdir>. |
… | |
… | |
45 | Unlike the name component C<stamp> might indicate, it is also used for |
45 | Unlike the name component C<stamp> might indicate, it is also used for |
46 | time differences throughout libeio. |
46 | time differences throughout libeio. |
47 | |
47 | |
48 | =head2 FORK SUPPORT |
48 | =head2 FORK SUPPORT |
49 | |
49 | |
50 | Calling C<fork ()> is fully supported by this module - but you must not |
50 | Usage of pthreads in a program changes the semantics of fork |
51 | rely on this. It is currently implemented in these steps: |
51 | considerably. Specifically, only async-safe functions can be called after |
|
|
52 | fork. Libeio uses pthreads, so this applies, and makes using fork hard for |
|
|
53 | anything but relatively fork + exec uses. |
52 | |
54 | |
53 | 1. wait till all requests in "execute" state have been handled |
55 | This library only works in the process that initialised it: Forking is |
54 | (basically requests that are already handed over to the kernel). |
56 | fully supported, but using libeio in any other process than the one that |
55 | 2. fork |
57 | called C<eio_init> is not. |
56 | 3. in the parent, continue business as usual, done |
|
|
57 | 4. in the child, destroy all ready and pending requests and free the |
|
|
58 | memory used by the worker threads. This gives you a fully empty |
|
|
59 | libeio queue. |
|
|
60 | |
58 | |
61 | Note, however, since libeio does use threads, the above guarantee doesn't |
59 | You might get around by not I<using> libeio before (or after) forking in |
62 | cover your libc, for example, malloc and other libc functions are not |
60 | the parent, and using it in the child afterwards. You could also try to |
63 | fork-safe, so there is very little you can do after a fork, and in fact, |
61 | call the L<eio_init> function again in the child, which will brutally |
64 | the above might crash, and thus change. |
62 | reinitialise all data structures, which isn't POSIX conformant, but |
|
|
63 | typically works. |
|
|
64 | |
|
|
65 | Otherwise, the only recommendation you should follow is: treat fork code |
|
|
66 | the same way you treat signal handlers, and only ever call C<eio_init> in |
|
|
67 | the process that uses it, and only once ever. |
65 | |
68 | |
66 | =head1 INITIALISATION/INTEGRATION |
69 | =head1 INITIALISATION/INTEGRATION |
67 | |
70 | |
68 | Before you can call any eio functions you first have to initialise the |
71 | Before you can call any eio functions you first have to initialise the |
69 | library. The library integrates into any event loop, but can also be used |
72 | library. The library integrates into any event loop, but can also be used |
… | |
… | |
78 | This function initialises the library. On success it returns C<0>, on |
81 | This function initialises the library. On success it returns C<0>, on |
79 | failure it returns C<-1> and sets C<errno> appropriately. |
82 | failure it returns C<-1> and sets C<errno> appropriately. |
80 | |
83 | |
81 | It accepts two function pointers specifying callbacks as argument, both of |
84 | It accepts two function pointers specifying callbacks as argument, both of |
82 | which can be C<0>, in which case the callback isn't called. |
85 | which can be C<0>, in which case the callback isn't called. |
|
|
86 | |
|
|
87 | There is currently no way to change these callbacks later, or to |
|
|
88 | "uninitialise" the library again. |
83 | |
89 | |
84 | =item want_poll callback |
90 | =item want_poll callback |
85 | |
91 | |
86 | The C<want_poll> callback is invoked whenever libeio wants attention (i.e. |
92 | The C<want_poll> callback is invoked whenever libeio wants attention (i.e. |
87 | it wants to be polled by calling C<eio_poll>). It is "edge-triggered", |
93 | it wants to be polled by calling C<eio_poll>). It is "edge-triggered", |
… | |
… | |
98 | This callback is invoked when libeio detects that all pending requests |
104 | This callback is invoked when libeio detects that all pending requests |
99 | have been handled. It is "edge-triggered", that is, it will only be |
105 | have been handled. It is "edge-triggered", that is, it will only be |
100 | called once after C<want_poll>. To put it differently, C<want_poll> and |
106 | called once after C<want_poll>. To put it differently, C<want_poll> and |
101 | C<done_poll> are invoked in pairs: after C<want_poll> you have to call |
107 | C<done_poll> are invoked in pairs: after C<want_poll> you have to call |
102 | C<eio_poll ()> until either C<eio_poll> indicates that everything has been |
108 | C<eio_poll ()> until either C<eio_poll> indicates that everything has been |
103 | handled or C<done_poll> has been called, which signals the same. |
109 | handled or C<done_poll> has been called, which signals the same - only one |
|
|
110 | method is needed. |
104 | |
111 | |
105 | Note that C<eio_poll> might return after C<done_poll> and C<want_poll> |
112 | Note that C<eio_poll> might return after C<done_poll> and C<want_poll> |
106 | have been called again, so watch out for races in your code. |
113 | have been called again, so watch out for races in your code. |
107 | |
114 | |
|
|
115 | It is quite common to have an empty C<done_call> callback and only use |
|
|
116 | the return value from C<eio_poll>, or, when C<eio_poll> is configured to |
|
|
117 | handle all outstanding replies, it's enough to call C<eio_poll> once. |
|
|
118 | |
108 | As with C<want_poll>, this callback is called while locks are being held, |
119 | As with C<want_poll>, this callback is called while locks are being held, |
109 | so you I<must not call any libeio functions form within this callback>. |
120 | so you I<must not call any libeio functions from within this callback>. |
110 | |
121 | |
111 | =item int eio_poll () |
122 | =item int eio_poll () |
112 | |
123 | |
113 | This function has to be called whenever there are pending requests that |
124 | This function has to be called whenever there are pending requests that |
114 | need finishing. You usually call this after C<want_poll> has indicated |
125 | need finishing. You usually call this after C<want_poll> has indicated |
… | |
… | |
170 | { |
181 | { |
171 | loop = EV_DEFAULT; |
182 | loop = EV_DEFAULT; |
172 | |
183 | |
173 | ev_idle_init (&repeat_watcher, repeat); |
184 | ev_idle_init (&repeat_watcher, repeat); |
174 | ev_async_init (&ready_watcher, ready); |
185 | ev_async_init (&ready_watcher, ready); |
175 | ev_async_start (loop &watcher); |
186 | ev_async_start (loop, &watcher); |
176 | |
187 | |
177 | eio_init (want_poll, 0); |
188 | eio_init (want_poll, 0); |
178 | } |
189 | } |
179 | |
190 | |
180 | For most other event loops, you would typically use a pipe - the event |
191 | For most other event loops, you would typically use a pipe - the event |
… | |
… | |
227 | |
238 | |
228 | The C<void *data> member simply stores the value of the C<data> argument. |
239 | The C<void *data> member simply stores the value of the C<data> argument. |
229 | |
240 | |
230 | =back |
241 | =back |
231 | |
242 | |
|
|
243 | Members not explicitly described as accessible must not be |
|
|
244 | accessed. Specifically, there is no guarantee that any members will still |
|
|
245 | have the value they had when the request was submitted. |
|
|
246 | |
232 | The return value of the callback is normally C<0>, which tells libeio to |
247 | The return value of the callback is normally C<0>, which tells libeio to |
233 | continue normally. If a callback returns a nonzero value, libeio will |
248 | continue normally. If a callback returns a nonzero value, libeio will |
234 | stop processing results (in C<eio_poll>) and will return the value to its |
249 | stop processing results (in C<eio_poll>) and will return the value to its |
235 | caller. |
250 | caller. |
236 | |
251 | |
237 | Memory areas passed to libeio must stay valid as long as a request |
252 | Memory areas passed to libeio wrappers must stay valid as long as a |
238 | executes, with the exception of paths, which are being copied |
253 | request executes, with the exception of paths, which are being copied |
239 | internally. Any memory libeio itself allocates will be freed after the |
254 | internally. Any memory libeio itself allocates will be freed after the |
240 | finish callback has been called. If you want to manage all memory passed |
255 | finish callback has been called. If you want to manage all memory passed |
241 | to libeio yourself you can use the low-level API. |
256 | to libeio yourself you can use the low-level API. |
242 | |
257 | |
243 | For example, to open a file, you could do this: |
258 | For example, to open a file, you could do this: |
… | |
… | |
280 | |
295 | |
281 | Cancel the request (and all its subrequests). If the request is currently |
296 | Cancel the request (and all its subrequests). If the request is currently |
282 | executing it might still continue to execute, and in other cases it might |
297 | executing it might still continue to execute, and in other cases it might |
283 | still take a while till the request is cancelled. |
298 | still take a while till the request is cancelled. |
284 | |
299 | |
285 | Even if cancelled, the finish callback will still be invoked - the |
300 | When cancelled, the finish callback will not be invoked. |
286 | callbacks of all cancellable requests need to check whether the request |
|
|
287 | has been cancelled by calling C<EIO_CANCELLED (req)>: |
|
|
288 | |
|
|
289 | static int |
|
|
290 | my_eio_cb (eio_req *req) |
|
|
291 | { |
|
|
292 | if (EIO_CANCELLED (req)) |
|
|
293 | return 0; |
|
|
294 | } |
|
|
295 | |
|
|
296 | In addition, cancelled requests will I<either> have C<< req->result >> |
|
|
297 | set to C<-1> and C<errno> to C<ECANCELED>, or I<otherwise> they were |
|
|
298 | successfully executed, despite being cancelled (e.g. when they have |
|
|
299 | already been executed at the time they were cancelled). |
|
|
300 | |
301 | |
301 | C<EIO_CANCELLED> is still true for requests that have successfully |
302 | C<EIO_CANCELLED> is still true for requests that have successfully |
302 | executed, as long as C<eio_cancel> was called on them at some point. |
303 | executed, as long as C<eio_cancel> was called on them at some point. |
303 | |
304 | |
304 | =back |
305 | =back |
… | |
… | |
586 | =item eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
587 | =item eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
587 | |
588 | |
588 | Calls C<readahead(2)>. If the syscall is missing, then the call is |
589 | Calls C<readahead(2)>. If the syscall is missing, then the call is |
589 | emulated by simply reading the data (currently in 64kiB chunks). |
590 | emulated by simply reading the data (currently in 64kiB chunks). |
590 | |
591 | |
|
|
592 | =item eio_syncfs (int fd, int pri, eio_cb cb, void *data) |
|
|
593 | |
|
|
594 | Calls Linux' C<syncfs> syscall, if available. Returns C<-1> and sets |
|
|
595 | C<errno> to C<ENOSYS> if the call is missing I<but still calls sync()>, |
|
|
596 | if the C<fd> is C<< >= 0 >>, so you can probe for the availability of the |
|
|
597 | syscall with a negative C<fd> argument and checking for C<-1/ENOSYS>. |
|
|
598 | |
591 | =item eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
599 | =item eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) |
592 | |
600 | |
593 | Calls C<sync_file_range>. If the syscall is missing, then this is the same |
601 | Calls C<sync_file_range>. If the syscall is missing, then this is the same |
594 | as calling C<fdatasync>. |
602 | as calling C<fdatasync>. |
595 | |
603 | |
… | |
… | |
613 | |
621 | |
614 | =over 4 |
622 | =over 4 |
615 | |
623 | |
616 | =item eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
624 | =item eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) |
617 | |
625 | |
618 | Reads (C<flags == 0>) or modifies (C<flags == EIO_MT_MODIFY) the given |
626 | Reads (C<flags == 0>) or modifies (C<flags == EIO_MT_MODIFY>) the given |
619 | memory area, page-wise, that is, it reads (or reads and writes back) the |
627 | memory area, page-wise, that is, it reads (or reads and writes back) the |
620 | first octet of every page that spans the memory area. |
628 | first octet of every page that spans the memory area. |
621 | |
629 | |
622 | This can be used to page in some mmapped file, or dirty some pages. Note |
630 | This can be used to page in some mmapped file, or dirty some pages. Note |
623 | that dirtying is an unlocked read-write access, so races can ensue when |
631 | that dirtying is an unlocked read-write access, so races can ensue when |
… | |
… | |
747 | request finish on its own. |
755 | request finish on its own. |
748 | |
756 | |
749 | =item 3) open callback adds more requests |
757 | =item 3) open callback adds more requests |
750 | |
758 | |
751 | In the open callback, if the open was not successful, copy C<< |
759 | In the open callback, if the open was not successful, copy C<< |
752 | req->errorno >> to C<< grp->errorno >> and set C<< grp->errorno >> to |
760 | req->errorno >> to C<< grp->errorno >> and set C<< grp->result >> to |
753 | C<-1> to signal an error. |
761 | C<-1> to signal an error. |
754 | |
762 | |
755 | Otherwise, malloc some memory or so and issue a read request, adding the |
763 | Otherwise, malloc some memory or so and issue a read request, adding the |
756 | read request to the group. |
764 | read request to the group. |
757 | |
765 | |
758 | =item 4) continue issuing requests till finished |
766 | =item 4) continue issuing requests till finished |
759 | |
767 | |
760 | In the real callback, check for errors and possibly continue with |
768 | In the read callback, check for errors and possibly continue with |
761 | C<eio_close> or any other eio request in the same way. |
769 | C<eio_close> or any other eio request in the same way. |
762 | |
770 | |
763 | As soon as no new requests are added the group request will finish. Make |
771 | As soon as no new requests are added, the group request will finish. Make |
764 | sure you I<always> set C<< grp->result >> to some sensible value. |
772 | sure you I<always> set C<< grp->result >> to some sensible value. |
765 | |
773 | |
766 | =back |
774 | =back |
767 | |
775 | |
768 | =head4 REQUEST LIMITING |
776 | =head4 REQUEST LIMITING |
… | |
… | |
770 | |
778 | |
771 | #TODO |
779 | #TODO |
772 | |
780 | |
773 | void eio_grp_limit (eio_req *grp, int limit); |
781 | void eio_grp_limit (eio_req *grp, int limit); |
774 | |
782 | |
775 | |
|
|
776 | =back |
|
|
777 | |
783 | |
778 | |
784 | |
779 | =head1 LOW LEVEL REQUEST API |
785 | =head1 LOW LEVEL REQUEST API |
780 | |
786 | |
781 | #TODO |
787 | #TODO |
… | |
… | |
909 | This symbol governs the stack size for each eio thread. Libeio itself |
915 | This symbol governs the stack size for each eio thread. Libeio itself |
910 | was written to use very little stackspace, but when using C<EIO_CUSTOM> |
916 | was written to use very little stackspace, but when using C<EIO_CUSTOM> |
911 | requests, you might want to increase this. |
917 | requests, you might want to increase this. |
912 | |
918 | |
913 | If this symbol is undefined (the default) then libeio will use its default |
919 | If this symbol is undefined (the default) then libeio will use its default |
914 | stack size (C<sizeof (void *) * 4096> currently). If it is defined, but |
920 | stack size (C<sizeof (void *) * 4096> currently). In all other cases, the |
915 | C<0>, then the default operating system stack size will be used. In all |
|
|
916 | other cases, the value must be an expression that evaluates to the desired |
921 | value must be an expression that evaluates to the desired stack size. |
917 | stack size. |
|
|
918 | |
922 | |
919 | =back |
923 | =back |
920 | |
924 | |
921 | |
925 | |
922 | =head1 PORTABILITY REQUIREMENTS |
926 | =head1 PORTABILITY REQUIREMENTS |