]> git.llucax.com Git - software/ev.d.git/blob - ev/c.d
Don't compile unittests by default.
[software/ev.d.git] / ev / c.d
1 /+
2  + D Programming Language "bindings" to libev
3  + <http://software.schmorp.de/pkg/libev.html>
4  +
5  + Written by Leandro Lucarella (2008).
6  +
7  + Placed under BOLA license <http://auriga.wearlab.de/~alb/bola/> which is
8  + basically public domain.
9  +
10  +/
11
12 module ev.c;
13
14 extern (C):
15 align (4):
16
17 enum: uint
18 {
19         EV_UNDEF    = 0xFFFFFFFFL, // guaranteed to be invalid
20         EV_NONE     =       0x00L, // no events
21         EV_READ     =       0x01L, // ev_io detected read will not block
22         EV_WRITE    =       0x02L, // ev_io detected write will not block
23         EV_IOFDSET  =       0x80L, // internal use only
24         EV_TIMEOUT  = 0x00000100L, // timer timed out
25         EV_PERIODIC = 0x00000200L, // periodic timer timed out
26         EV_SIGNAL   = 0x00000400L, // signal was received
27         EV_CHILD    = 0x00000800L, // child/pid had status change
28         EV_STAT     = 0x00001000L, // stat data changed
29         EV_IDLE     = 0x00002000L, // event loop is idling
30         EV_PREPARE  = 0x00004000L, // event loop about to poll
31         EV_CHECK    = 0x00008000L, // event loop finished poll
32         EV_EMBED    = 0x00010000L, // embedded event loop needs sweep
33         EV_FORK     = 0x00020000L, // event loop resumed in child
34         EV_ERROR    = 0x80000000L, // sent when an error occurs
35 }
36
37 enum: uint
38 {
39         // bits for ev_default_loop and ev_loop_new
40         // the default
41         EVFLAG_AUTO       = 0x00000000UL, // not quite a mask
42         // flag bits
43         EVFLAG_NOENV      = 0x01000000UL, // do NOT consult environment
44         EVFLAG_FORKCHECK  = 0x02000000UL, // check for a fork in each iteration
45         // method bits to be ored together
46         EVBACKEND_SELECT  = 0x00000001UL, // about anywhere
47         EVBACKEND_POLL    = 0x00000002UL, // !win
48         EVBACKEND_EPOLL   = 0x00000004UL, // linux
49         EVBACKEND_KQUEUE  = 0x00000008UL, // bsd
50         EVBACKEND_DEVPOLL = 0x00000010UL, // solaris 8 / NYI
51         EVBACKEND_PORT    = 0x00000020UL, // solaris 10
52 }
53
54 enum
55 {
56         EVLOOP_NONBLOCK = 1, // do not block/wait
57         EVLOOP_ONESHOT  = 2, // block *once* only
58 }
59
60 enum
61 {
62         EVUNLOOP_CANCEL = 0, // undo unloop
63         EVUNLOOP_ONE    = 1, // unloop once
64         EVUNLOOP_ALL    = 2, // unloop all loops
65 }
66
67 version (EV_ENABLE_SELECT)
68 {
69 }
70 else
71 {
72         version = EV_PERIODIC_ENABLE;
73         version = EV_STAT_ENABLE;
74         version = EV_IDLE_ENABLE;
75         version = EV_FORK_ENABLE;
76         version = EV_EMBED_ENABLE;
77 }
78
79 alias double ev_tstamp;
80
81 struct ev_loop_t;
82
83 template EV_COMMON()
84 {
85         void* data;
86 }
87
88 template EV_CB_DECLARE(TYPE)
89 {
90         void function (ev_loop_t*, TYPE*, int) cb;
91 }
92
93 template EV_WATCHER(TYPE)
94 {
95         int active;                 // private
96         int pending;                // private
97         int priority;               // private
98         mixin EV_COMMON;            // rw
99         mixin EV_CB_DECLARE!(TYPE); // private
100 }
101
102 template EV_WATCHER_LIST(TYPE)
103 {
104         mixin EV_WATCHER!(TYPE);
105         ev_watcher_list* next;      // private
106 }
107
108 template EV_WATCHER_TIME(TYPE)
109 {
110         mixin EV_WATCHER!(TYPE);
111         ev_tstamp at;               // private
112 }
113
114 struct ev_watcher
115 {
116         mixin EV_WATCHER!(ev_watcher);
117 }
118
119 struct ev_watcher_list
120 {
121         mixin EV_WATCHER_LIST!(ev_watcher_list);
122 }
123
124 struct ev_watcher_time
125 {
126         mixin EV_WATCHER_TIME!(ev_watcher_time);
127 }
128
129 struct ev_io
130 {
131         mixin EV_WATCHER_LIST!(ev_io);
132         int fd;     // ro
133         int events; // ro
134 }
135
136 struct ev_timer
137 {
138         mixin EV_WATCHER_TIME!(ev_timer);
139         ev_tstamp repeat; // rw
140 }
141
142 version (EV_PERIODIC_ENABLE)
143 {
144         struct ev_periodic
145         {
146                 mixin EV_WATCHER_TIME!(ev_periodic);
147                 ev_tstamp offset;                     // rw
148                 ev_tstamp interval;                   // rw
149                 ev_tstamp function(ev_periodic *w,
150                                 ev_tstamp now) reschedule_cb; // rw
151         }
152 }
153
154 struct ev_signal
155 {
156         mixin EV_WATCHER_LIST!(ev_signal);
157         int signum; // ro
158 }
159
160 struct ev_child
161 {
162         mixin EV_WATCHER_LIST!(ev_child);
163         int flags;   // private
164         int pid;     // ro
165         int rpid;    // rw, holds the received pid
166         int rstatus; // rw, holds the exit status, use the
167         // macros from sys/wait.h
168 }
169
170 version (EV_STAT_ENABLE)
171 {
172
173         version (Windows) // alias _stati64 ev_statdata;
174         {
175                 pragma (msg, "ev_stat not supported in windows "
176                                 "because I don't know the "
177                                 "layout of _stati64");
178                 static assert(0);
179                 // Maybe this should work?
180                 //static import stat = std.c.windows.stat;
181                 //alias stat.struct_stat ev_statdata;
182         }
183         else // It should be POSIX
184         {
185                 static import stat = std.c.unix.unix;
186                 alias stat.struct_stat ev_statdata;
187         }
188
189         struct ev_stat
190         {
191                 mixin EV_WATCHER_LIST!(ev_stat);
192
193                 ev_timer timer;     // private
194                 ev_tstamp interval; // ro
195                 const char *path;   // ro
196                 ev_statdata prev;   // ro
197                 ev_statdata attr;   // ro
198                 int wd; // wd for inotify, fd for kqueue
199         }
200 }
201
202 version (EV_IDLE_ENABLE)
203 {
204         struct ev_idle
205         {
206                 mixin EV_WATCHER!(ev_idle);
207         }
208 }
209
210 struct ev_prepare
211 {
212         mixin EV_WATCHER!(ev_prepare);
213 }
214
215 struct ev_check
216 {
217         mixin EV_WATCHER!(ev_check);
218 }
219
220 version (EV_FORK_ENABLE)
221 {
222         struct ev_fork
223         {
224                 mixin EV_WATCHER!(ev_fork);
225         }
226 }
227
228 version (EV_EMBED_ENABLE)
229 {
230         struct ev_embed
231         {
232                 mixin EV_WATCHER!(ev_embed);
233                 ev_loop_t* other;     // ro
234                 ev_io io;             // private
235                 ev_prepare prepare;   // private
236                 ev_check check;       // unused
237                 ev_timer timer;       // unused
238                 ev_periodic periodic; // unused
239                 ev_idle idle;         // unused
240                 ev_fork fork;         // unused
241         }
242 }
243
244 int ev_version_major();
245 int ev_version_minor();
246
247 uint ev_supported_backends();
248 uint ev_recommended_backends();
249 uint ev_embeddable_backends();
250
251 ev_tstamp ev_time();
252 void ev_sleep(ev_tstamp delay); // sleep for a while
253
254 // Sets the allocation function to use, works like realloc.
255 // It is used to allocate and free memory.
256 // If it returns zero when memory needs to be allocated, the library
257 // might abort
258 // or take some potentially destructive action.
259 // The default is your system realloc function.
260 void ev_set_allocator(void* function(void* ptr, int size));
261
262 // set the callback function to call on a
263 // retryable syscall error
264 // (such as failed select, poll, epoll_wait)
265 void ev_set_syserr_cb(void function(char* msg));
266
267 extern ev_loop_t* ev_default_loop_ptr;
268
269 ev_loop_t* ev_default_loop_init(uint flags);
270
271 // create and destroy alternative loops that don't handle signals
272 ev_loop_t* ev_loop_new(uint flags);
273 void ev_loop_destroy(ev_loop_t*);
274 void ev_loop_fork(ev_loop_t*);
275
276 ev_tstamp ev_now(ev_loop_t*);
277 void ev_default_destroy();
278 void ev_default_fork();
279 uint ev_backend(ev_loop_t*);
280 uint ev_loop_count(ev_loop_t*);
281 void ev_loop(ev_loop_t*, int flags);
282 void ev_unloop(ev_loop_t*, int);
283 void ev_set_io_collect_interval(ev_loop_t*, ev_tstamp interval);
284 void ev_set_timeout_collect_interval(ev_loop_t*, ev_tstamp interval);
285 void ev_ref(ev_loop_t*);
286 void ev_unref(ev_loop_t*);
287 void ev_once(ev_loop_t*, int fd, int events, ev_tstamp timeout,
288                 void function(int revents, void* arg), void* arg);
289
290 void ev_feed_event(ev_loop_t*, void *w, int revents);
291 void ev_feed_fd_event(ev_loop_t*, int fd, int revents);
292 void ev_feed_signal_event (ev_loop_t*, int signum);
293 void ev_invoke(ev_loop_t*, void *w, int revents);
294 int  ev_clear_pending(ev_loop_t*, void *w);
295
296 void ev_io_start(ev_loop_t*, ev_io *w);
297 void ev_io_stop(ev_loop_t*, ev_io *w);
298
299 void ev_timer_start(ev_loop_t*, ev_timer *w);
300 void ev_timer_stop(ev_loop_t*, ev_timer *w);
301 void ev_timer_again(ev_loop_t*, ev_timer *w);
302
303 version (EV_PERIODIC_ENABLE)
304 {
305         void ev_periodic_start(ev_loop_t*, ev_periodic *w);
306         void ev_periodic_stop(ev_loop_t*, ev_periodic *w);
307         void ev_periodic_again(ev_loop_t*, ev_periodic *w);
308 }
309
310 void ev_signal_start(ev_loop_t*, ev_signal *w);
311 void ev_signal_stop(ev_loop_t*, ev_signal *w);
312
313 /* only supported in the default loop */
314 void ev_child_start(ev_loop_t*, ev_child *w);
315 void ev_child_stop(ev_loop_t*, ev_child *w);
316
317 version (EV_STAT_ENABLE)
318 {
319         void ev_stat_start(ev_loop_t*, ev_stat *w);
320         void ev_stat_stop(ev_loop_t*, ev_stat *w);
321         void ev_stat_stat(ev_loop_t*, ev_stat *w);
322 }
323
324 version (EV_IDLE_ENABLE)
325 {
326         void ev_idle_start(ev_loop_t*, ev_idle *w);
327         void ev_idle_stop(ev_loop_t*, ev_idle *w);
328 }
329
330 void ev_prepare_start(ev_loop_t*, ev_prepare *w);
331 void ev_prepare_stop(ev_loop_t*, ev_prepare *w);
332
333 void ev_check_start(ev_loop_t*, ev_check *w);
334 void ev_check_stop(ev_loop_t*, ev_check *w);
335
336 version (EV_FORK_ENABLE)
337 {
338         void ev_fork_start(ev_loop_t*, ev_fork *w);
339         void ev_fork_stop(ev_loop_t*, ev_fork *w);
340 }
341
342 version (EV_EMBED_ENABLE)
343 {
344         // only supported when loop to be embedded is in fact embeddable
345         void ev_embed_start(ev_loop_t*, ev_embed *w);
346         void ev_embed_stop(ev_loop_t*, ev_embed *w);
347         void ev_embed_sweep(ev_loop_t*, ev_embed *w);
348 }
349
350 bool ev_is_pending(TYPE)(TYPE* w)
351 {
352         return cast (bool) w.pending;
353 }
354
355 bool ev_is_active(TYPE)(TYPE* w)
356 {
357         return cast (bool) w.active;
358 }
359
360 int ev_priority(TYPE)(TYPE* w)
361 {
362         return cast (bool) w.priority;
363 }
364
365 void function(ev_loop_t*, TYPE*, int) ev_cb(TYPE)(TYPE* w)
366 {
367         return w.cb;
368 }
369
370 void ev_set_priority(TYPE)(TYPE* w, int pri)
371 {
372         w.priority = pri;
373 }
374
375 void ev_set_cb(TYPE)(TYPE* w,
376                 void function(ev_loop_t*, TYPE*, int) cb)
377 {
378         w.cb = cb;
379 }
380
381 void ev_init(TYPE)(TYPE* w,
382                 void function(ev_loop_t*, TYPE*, int) cb)
383 {
384         w.active = 0;
385         w.pending = 0;
386         w.priority = 0;
387         ev_set_cb(w, cb);
388 }
389
390 void ev_io_set(ev_io* w, int fd, int events)
391 {
392         w.fd = fd;
393         w.events = events | EV_IOFDSET;
394 }
395
396 void ev_timer_set(ev_timer* w, ev_tstamp after, ev_tstamp repeat)
397 {
398         w.at = after;
399         w.repeat = repeat;
400 }
401
402 void ev_periodic_set(ev_periodic* w, ev_tstamp ofs, ev_tstamp ival,
403                 ev_tstamp function(ev_periodic *w, ev_tstamp now) res)
404 {
405         w.offset = ofs;
406         w.interval = ival;
407         w.reschedule_cb = res;
408 }
409
410 void ev_signal_set(ev_signal* w, int signum)
411 {
412         w.signum = signum;
413 }
414
415 void ev_child_set(ev_child* w, int pid, int trace)
416 {
417         w.pid = pid;
418         w.flags = !!trace;
419 }
420
421 void ev_stat_set(ev_stat* w, char* path, ev_tstamp interval)
422 {
423         w.path = path;
424         w.interval = interval;
425         w.wd = -2;
426 }
427
428 void ev_idle_set(ev_idle* w)
429 {
430 }
431
432 void ev_prepare_set(ev_prepare* w)
433 {
434 }
435
436 void ev_check_set(ev_check* w)
437 {
438 }
439
440 void ev_embed_set(ev_embed* w, ev_loop_t* other)
441 {
442         w.other = other;
443 }
444
445 void ev_fork_set(ev_fork* w)
446 {
447 }
448
449 void ev_io_init(ev_io* w, void function(ev_loop_t*, ev_io*, int) cb, int fd,
450                 int events)
451 {
452         ev_init(w, cb);
453         ev_io_set(w, fd, events);
454 }
455
456 void ev_timer_init(ev_timer* w, void function(ev_loop_t*, ev_timer*, int) cb,
457                 ev_tstamp after, ev_tstamp repeat)
458 {
459         ev_init(w, cb);
460         ev_timer_set(w, after, repeat);
461 }
462
463 void ev_periodic_init(ev_periodic* w,
464                 void function(ev_loop_t*, ev_periodic*, int) cb,
465                 ev_tstamp ofs, ev_tstamp ival,
466                 ev_tstamp function(ev_periodic *w, ev_tstamp now) res)
467 {
468         ev_init(w, cb);
469         ev_periodic_set(w, ofs, ival, res);
470 }
471
472 void ev_signal_init(ev_signal* w, void function(ev_loop_t*, ev_signal*, int) cb,
473                 int signum)
474 {
475         ev_init(w, cb);
476         ev_signal_set(w, signum);
477 }
478
479 void ev_child_init(ev_child* w, void function(ev_loop_t*, ev_child*, int) cb,
480                 int pid, int trace)
481 {
482         ev_init(w, cb);
483         ev_child_set(w, pid, trace);
484 }
485
486 void ev_stat_init(ev_stat* w, void function(ev_loop_t*, ev_stat*, int) cb,
487                 char* path, ev_tstamp interval)
488 {
489         ev_init(w, cb);
490         ev_stat_set(w, path, interval);
491 }
492
493 void ev_idle_init(ev_idle* w, void function(ev_loop_t*, ev_idle*, int) cb)
494 {
495         ev_init(w, cb);
496         ev_idle_set(w);
497 }
498
499 void ev_prepare_init(ev_prepare* w,
500                 void function(ev_loop_t*, ev_prepare*, int) cb)
501 {
502         ev_init(w, cb);
503         ev_prepare_set(w);
504 }
505
506 void ev_check_init(ev_check* w, void function(ev_loop_t*, ev_check*, int) cb)
507 {
508         ev_init(w, cb);
509         ev_check_set(w);
510 }
511
512 void ev_embed_init(ev_embed* w, void function(ev_loop_t*, ev_embed*, int) cb,
513                 ev_loop_t* other)
514 {
515         ev_init(w, cb);
516         ev_embed_set(w, other);
517 }
518
519 void ev_fork_init(ev_fork* w, void function(ev_loop_t*, ev_fork*, int) cb)
520 {
521         ev_init(w, cb);
522         ev_fork_set(w);
523 }
524
525 ev_loop_t* ev_default_loop(uint flags = EVFLAG_AUTO)
526 {
527         if (!ev_default_loop_ptr)
528                 ev_default_loop_init(flags);
529         return ev_default_loop_ptr;
530 }
531
532 version (UnitTest):
533 extern (D):
534 import stdio = std.stdio;
535 import stdlib = std.c.stdlib;
536 import str = std.string;
537 import unix = std.c.unix.unix;
538 import proc = std.c.process;
539 enum { SIGINT = 2 }
540 const STAT_FILE = "/tmp/libev-stat-test-file";
541 const TEST_TEXT = "hello";
542 bool prepare_done = false;
543 bool check_done = false;
544 bool idle_done = false;
545 bool timer_done = false;
546 bool io_done = false;
547 bool stat_done = false;
548 bool child_done = false;
549 bool signal_done = false;
550 bool eio_done = false;
551 int child_pid = -1;
552 unittest
553 {
554         stdio.writefln("Unittesting...");
555         extern (C) static void cbprepare(ev_loop_t* loop, ev_prepare* w, int revents)
556         {
557                 stdio.writefln("ev_prepare");
558                 assert (!prepare_done);
559                 assert (!check_done);
560                 assert (!idle_done);
561                 assert (!timer_done);
562                 assert (!io_done);
563                 assert (!stat_done);
564                 assert (!child_done);
565                 assert (!signal_done);
566                 assert (!eio_done);
567                 prepare_done = true;
568                 ev_prepare_stop(loop, w);
569         }
570         extern (C) static void cbcheck(ev_loop_t* loop, ev_check* w, int revents)
571         {
572                 stdio.writefln("ev_check");
573                 assert (prepare_done);
574                 assert (!check_done);
575                 assert (!idle_done);
576                 assert (!timer_done);
577                 assert (!io_done);
578                 assert (!stat_done);
579                 assert (!child_done);
580                 assert (!signal_done);
581                 assert (!eio_done);
582                 check_done = true;
583                 ev_check_stop(loop, w);
584         }
585         extern (C) static void cbidle(ev_loop_t* loop, ev_idle* w, int revents)
586         {
587                 stdio.writefln("ev_idle");
588                 assert (prepare_done);
589                 assert (check_done);
590                 assert (!idle_done);
591                 assert (!timer_done);
592                 assert (!io_done);
593                 assert (!stat_done);
594                 assert (!child_done);
595                 assert (!signal_done);
596                 assert (!eio_done);
597                 idle_done = true;
598                 ev_idle_stop(loop, w);
599         }
600         extern (C) static void cbtimer(ev_loop_t* loop, ev_timer* w,
601                         int revents)
602         {
603                 stdio.writefln("ev_timer");
604                 assert (prepare_done);
605                 assert (check_done);
606                 assert (idle_done);
607                 assert (!timer_done);
608                 assert (!io_done);
609                 assert (!stat_done);
610                 assert (!child_done);
611                 assert (!signal_done);
612                 assert (!eio_done);
613                 timer_done = true;
614                 ev_timer_stop(loop, w);
615                 stdio.writefln("\tfiring ev_io");
616                 stdio.writefln("\t\topening pipe for writing...");
617                 int pipe_fd = *cast (int*) w.data;
618                 stdio.writefln("\t\twriting '%s' to pipe...", TEST_TEXT);
619                 int n = unix.write(pipe_fd, cast (void*) TEST_TEXT,
620                                 TEST_TEXT.length);
621                 assert (n == TEST_TEXT.length);
622         }
623         extern (C) static void cbio(ev_loop_t* loop, ev_io* w, int revents)
624         {
625                 stdio.writefln("ev_io");
626                 assert (prepare_done);
627                 assert (check_done);
628                 assert (idle_done);
629                 assert (timer_done);
630                 assert (!io_done);
631                 assert (!stat_done);
632                 assert (!child_done);
633                 assert (!signal_done);
634                 assert (!eio_done);
635                 io_done = true;
636                 ev_io_stop(loop, w);
637                 char[TEST_TEXT.length] buffer;
638                 stdio.writefln("\treading %d bytes from pipe...",
639                                 buffer.length);
640                 int n = unix.read(w.fd, cast (void*) buffer, buffer.length);
641                 assert (n == TEST_TEXT.length);
642                 assert (buffer.dup == TEST_TEXT.dup);
643                 stdio.writefln("\tread '%s'", buffer);
644                 stdio.writefln("\tfiring ev_stat");
645                 stdio.writefln("\t\topening file '%s'", STAT_FILE);
646                 int fd = unix.open(str.toStringz(STAT_FILE),
647                                 unix.O_WRONLY | unix.O_TRUNC | unix.O_CREAT);
648                 assert (fd != -1);
649                 stdio.writefln("\t\tfd: %d", fd);
650                 n = unix.write(fd, cast (void*) TEST_TEXT,
651                                 TEST_TEXT.length);
652                 assert (n == TEST_TEXT.length);
653                 unix.close(fd);
654         }
655         extern (C) static void cbstat(ev_loop_t* loop, ev_stat* w, int revents)
656         {
657                 stdio.writefln("ev_stat");
658                 assert (prepare_done);
659                 assert (check_done);
660                 assert (idle_done);
661                 assert (timer_done);
662                 assert (io_done);
663                 assert (!stat_done);
664                 assert (!child_done);
665                 assert (!signal_done);
666                 assert (!eio_done);
667                 stat_done = true;
668                 ev_stat_stop(loop, w);
669                 static void print_stat(ev_statdata* s)
670                 {
671                         stdio.writefln("\t\t\tinode: ", s.st_ino);
672                         stdio.writefln("\t\t\tmode: ", s.st_mode);
673                         stdio.writefln("\t\t\tlinks: ", s.st_nlink);
674                         stdio.writefln("\t\t\tuid: ", s.st_uid);
675                         stdio.writefln("\t\t\tgid: ", s.st_gid);
676                         stdio.writefln("\t\t\tsize: ", s.st_size);
677                         stdio.writefln("\t\t\tatime: ", s.st_atime);
678                         stdio.writefln("\t\t\tmtime: ", s.st_mtime);
679                         stdio.writefln("\t\t\tctime: ", s.st_ctime);
680                 }
681                 if (w.attr.st_nlink)
682                 {
683                         stdio.writefln("\tfile '%s' changed", str.toString(w.path));
684                         stdio.writefln("\t\tprevios state:");
685                         print_stat(&w.prev);
686                         stdio.writefln("\t\tcurrent state:");
687                         print_stat(&w.attr);
688                 }
689                 else
690                 {
691                         stdio.writefln("\tfile '%s' does not exist!",
692                                         str.toString(w.path));
693                         stdio.writefln("\t\tprevios state:");
694                         print_stat(&w.prev);
695                 }
696                 stdio.writefln("\tfiring ev_fork...");
697                 stdio.writefln("\t\tforking...");
698                 auto pid = unix.fork();
699                 assert (pid != -1);
700                 if (pid)
701                 {
702                         child_pid = pid;
703                         stdio.writefln("\t\tev_stat: in parent, child pid: ", pid);
704                 }
705                 else
706                 {
707                         stdio.writefln("\t\tev_stat: in child, calling "
708                                         "ev_default_fork...");
709                         ev_default_fork();
710                 }
711         }
712         extern (C) static void cbchild(ev_loop_t* loop, ev_child* w, int revents)
713         {
714                 stdio.writefln("ev_child");
715                 assert (prepare_done);
716                 assert (check_done);
717                 assert (idle_done);
718                 assert (timer_done);
719                 assert (io_done);
720                 assert (stat_done);
721                 assert (!child_done);
722                 assert (!signal_done);
723                 assert (!eio_done);
724                 child_done = true;
725                 ev_child_stop(loop, w);
726                 static ubyte WEXITSTATUS(int s)
727                 {
728                         return cast(ubyte)((s & 0xff00) >> 8);
729                 }
730                 static ubyte WTERMSIG(int s)
731                 {
732                         return cast(ubyte)(s & 0x7f);
733                 }
734                 static bool WIFEXITED(int s)
735                 {
736                         return WTERMSIG(s) == 0;
737                 }
738                 static bool WIFSIGNALED(int s)
739                 {
740                         return cast(byte)(((s & 0x7f) + 1) >> 1) > 0;
741                 }
742                 static bool WCOREDUMP(int s)
743                 {
744                         return cast(bool)(s & 0x80);
745                 }
746                 stdio.writefln("\tthe child with pid %d exited with status "
747                                 "%d", w.rpid, w.rstatus);
748                 assert (child_pid == w.rpid);
749                 if (WIFEXITED(w.rstatus))
750                         stdio.writefln("\tchild exited normally with code ",
751                                         WEXITSTATUS(w.rstatus));
752                 if (WIFSIGNALED(w.rstatus))
753                 {
754                         stdio.writefln("\tchild exited with signal ",
755                                         WTERMSIG(w.rstatus));
756                         if (WCOREDUMP(w.rstatus))
757                                 stdio.writefln("\tchild produced a core dump");
758                 }
759                 assert (WIFEXITED(w.rstatus) && WEXITSTATUS(w.rstatus) == 5);
760                 stdio.writefln("\tfiring ev_signal");
761                 stdio.writefln("\t\tsending signal 2 (SIGINT)");
762                 unix.kill(proc.getpid(), SIGINT);
763         }
764         extern (C) static void cbfork(ev_loop_t* loop, ev_fork* w, int revents)
765         {
766                 stdio.writefln("ev_fork");
767                 assert (prepare_done);
768                 assert (check_done);
769                 assert (idle_done);
770                 assert (timer_done);
771                 assert (io_done);
772                 assert (stat_done);
773                 assert (!child_done);
774                 assert (!signal_done);
775                 assert (!eio_done);
776                 ev_fork_stop(loop, w);
777                 stdio.writefln("\texiting the child program with return "
778                                 "code 5");
779                 stdlib.exit(5);
780         }
781         extern (C) static void cbsignal(ev_loop_t* loop, ev_signal* w,
782                         int revents)
783         {
784                 stdio.writefln("ev_signal");
785                 assert (prepare_done);
786                 assert (check_done);
787                 assert (idle_done);
788                 assert (timer_done);
789                 assert (io_done);
790                 assert (stat_done);
791                 assert (child_done);
792                 assert (!signal_done);
793                 assert (!eio_done);
794                 signal_done = true;
795                 ev_signal_stop(loop, w);
796                 stdio.writefln("\tfiring embeded ev_io...");
797                 stdio.writefln("\t\topening pipe for writing...");
798                 int pipe_fd = *cast(int*)w.data;
799                 stdio.writefln("\t\twriting '%s' to pipe...", TEST_TEXT);
800                 int n = unix.write(pipe_fd, cast(void*)TEST_TEXT,
801                                 TEST_TEXT.length);
802                 assert (n == TEST_TEXT.length);
803         }
804         extern (C) static void ecbio(ev_loop_t* loop, ev_io* w, int revents)
805         {
806                 stdio.writefln("embeded ev_io");
807                 assert (prepare_done);
808                 assert (check_done);
809                 assert (idle_done);
810                 assert (timer_done);
811                 assert (io_done);
812                 assert (stat_done);
813                 assert (child_done);
814                 assert (signal_done);
815                 assert (!eio_done);
816                 eio_done = true;
817                 //ev_io_stop(loop, w);
818                 char[TEST_TEXT.length] buffer;
819                 stdio.writefln("\treading %d bytes from pipe...",
820                                 buffer.length);
821                 int n = unix.read(w.fd, cast (void*) buffer, buffer.length);
822                 assert (n == TEST_TEXT.length);
823                 assert (buffer.dup == TEST_TEXT.dup);
824                 stdio.writefln("\tread '%s'", buffer);
825                 stdio.writefln("\tstoping the loop");
826                 ev_unloop(loop, EVUNLOOP_ONE);
827         }
828         extern (C) static void cbembed(ev_loop_t* loop, ev_embed* w, int revents)
829         {
830                 stdio.writefln("ev_embed");
831                 stdio.writefln("\tsweeping embeded loop...");
832                 ev_embed_sweep(w.other, w);
833                 //ev_embed_stop(loop, w);
834         }
835
836         auto loop = ev_default_loop(0);
837
838         ev_io wio;
839         ev_io ewio;
840         ev_timer wtimer;
841         ev_signal wsignal;
842         ev_child wchild;
843         ev_stat wstat;
844         ev_idle widle;
845         ev_prepare wprepare;
846         ev_check wcheck;
847         ev_fork wfork;
848         ev_embed wembed;
849
850         ev_loop_t* eloop = ev_embeddable_backends() & ev_recommended_backends()
851                 ? ev_loop_new(ev_embeddable_backends () &
852                                 ev_recommended_backends ())
853                 : null;
854
855         if (eloop)
856         {
857                 stdio.writefln("Initializing embeded loop");
858                 ev_embed_init(&wembed, &cbembed, eloop);
859                 ev_embed_start(loop, &wembed);
860         }
861         else
862         {
863                 stdio.writefln("No embeded loop, using the default");
864                 eloop = loop;
865         }
866
867         int[2] epipe;
868         {
869                 int ret = unix.pipe(epipe);
870                 assert (ret == 0);
871         }
872         ev_io_init(&ewio, &ecbio, epipe[0], EV_READ);
873         ev_io_start(eloop, &ewio);
874
875         int[2] pipe;
876         {
877                 int ret = unix.pipe(pipe);
878                 assert (ret == 0);
879         }
880
881         ev_io_init(&wio, &cbio, pipe[0], EV_READ);
882         ev_io_start(loop, &wio);
883
884         ev_timer_init(&wtimer, &cbtimer, 1.5, 0.);
885         wtimer.data = &pipe[1]; // write fd
886         ev_timer_start(loop, &wtimer);
887
888         ev_signal_init(&wsignal, &cbsignal, SIGINT);
889         wsignal.data = &epipe[1]; // write fd
890         ev_signal_start(loop, &wsignal);
891
892         ev_child_init(&wchild, &cbchild, 0 /* trace any PID */, 0 /* death */);
893         ev_child_start(loop, &wchild);
894
895         ev_stat_init(&wstat, &cbstat, str.toStringz(STAT_FILE), 0 /* auto */);
896         ev_stat_start(loop, &wstat);
897
898         ev_idle_init(&widle, &cbidle);
899         ev_idle_start(loop, &widle);
900
901         ev_prepare_init(&wprepare, &cbprepare);
902         ev_prepare_start(loop, &wprepare);
903
904         ev_check_init(&wcheck, &cbcheck);
905         ev_check_start(loop, &wcheck);
906
907         ev_fork_init(&wfork, &cbfork);
908         ev_fork_start(loop, &wfork);
909
910         ev_loop(loop, 0);
911
912         assert (prepare_done);
913         assert (check_done);
914         assert (idle_done);
915         assert (timer_done);
916         assert (io_done);
917         assert (stat_done);
918         assert (child_done);
919         assert (signal_done);
920         assert (eio_done);
921
922         stdio.writefln("Unittesting done!");
923
924 }
925