… | |
… | |
35 | |
35 | |
36 | #include <EXTERN.h> |
36 | #include <EXTERN.h> |
37 | #include <perl.h> |
37 | #include <perl.h> |
38 | #include <XSUB.h> |
38 | #include <XSUB.h> |
39 | |
39 | |
40 | #include <EventAPI.h> |
40 | #include <EVAPI.h> |
41 | #include <CoroAPI.h> |
41 | #include <CoroAPI.h> |
42 | |
42 | |
43 | #include "util.h" |
43 | #include "util.h" |
44 | #include "keyword.h" |
44 | #include "keyword.h" |
45 | #include "dynbuf.h" |
45 | #include "dynbuf.h" |
… | |
… | |
489 | |
489 | |
490 | static void wait_for_tick (); |
490 | static void wait_for_tick (); |
491 | static void wait_for_tick_begin (); |
491 | static void wait_for_tick_begin (); |
492 | }; |
492 | }; |
493 | |
493 | |
494 | struct watcher_base |
494 | struct evapi |
495 | { |
495 | { |
496 | static struct EventAPI *GEventAPI; |
496 | static struct EVAPI *GEVAPI; |
497 | }; |
497 | }; |
498 | |
498 | |
499 | template<class base> |
|
|
500 | struct watcher : watcher_base |
|
|
501 | { |
|
|
502 | base *pe; |
|
|
503 | |
|
|
504 | void start (bool repeat = false) { GEventAPI->start ((pe_watcher *)pe, repeat); } |
|
|
505 | void stop (bool cancel_events = false) { GEventAPI->stop ((pe_watcher *)pe, cancel_events); } |
|
|
506 | void now () { GEventAPI->now ((pe_watcher *)pe); } |
|
|
507 | void suspend () { GEventAPI->suspend ((pe_watcher *)pe); } |
|
|
508 | void resume () { GEventAPI->resume ((pe_watcher *)pe); } |
|
|
509 | |
|
|
510 | void prio (int new_prio) { ((pe_watcher *)pe)->prio = new_prio; } |
|
|
511 | |
|
|
512 | ~watcher () |
|
|
513 | { |
|
|
514 | cancel (); |
|
|
515 | } |
|
|
516 | |
|
|
517 | private: |
|
|
518 | void cancel () { GEventAPI->cancel ((pe_watcher *)pe); } // private |
|
|
519 | }; |
|
|
520 | |
|
|
521 | struct iw : watcher<pe_idle>, callback<void (iw &)> |
|
|
522 | { |
|
|
523 | template<class O, class M> |
|
|
524 | iw (O object, M method) |
|
|
525 | : callback<void (iw &)> (object, method) |
|
|
526 | { |
|
|
527 | alloc (); |
|
|
528 | } |
|
|
529 | |
|
|
530 | private: |
|
|
531 | void alloc (); |
|
|
532 | }; |
|
|
533 | |
|
|
534 | struct iow : watcher<pe_io>, callback<void (iow &, int)> |
499 | struct iow : ev_io, evapi, callback<void (iow &, int)> |
535 | { |
500 | { |
|
|
501 | static void thunk (struct ev_io *w_, int revents) |
|
|
502 | { |
|
|
503 | iow &w = *static_cast<iow *>(w_); |
|
|
504 | |
|
|
505 | w (w, revents); |
|
|
506 | } |
|
|
507 | |
536 | template<class O, class M> |
508 | template<class O, class M> |
537 | iow (O object, M method) |
509 | iow (O object, M method) |
538 | : callback<void (iow &, int)> (object, method) |
510 | : callback<void (iow &, int)> (object, method) |
539 | { |
511 | { |
540 | alloc (); |
512 | ev_init ((ev_io *)this, thunk); |
541 | } |
513 | } |
542 | |
514 | |
543 | void fd (int fd); |
515 | void set (int fd, int events) |
544 | int poll (); |
516 | { |
|
|
517 | ev_io_set ((ev_io *)this, fd, events); |
|
|
518 | } |
|
|
519 | |
|
|
520 | int poll () const { return events; } |
|
|
521 | |
545 | void poll (int events); |
522 | void poll (int events); |
546 | |
523 | |
547 | private: |
524 | void start () |
548 | void alloc (); |
525 | { |
|
|
526 | ev_io_start ((ev_io *)this); |
|
|
527 | } |
|
|
528 | |
|
|
529 | void stop () |
|
|
530 | { |
|
|
531 | ev_io_stop ((ev_io *)this); |
|
|
532 | } |
|
|
533 | |
|
|
534 | ~iow () |
|
|
535 | { |
|
|
536 | stop (); |
|
|
537 | } |
549 | }; |
538 | }; |
550 | |
539 | |
551 | #endif |
540 | #endif |
552 | |
541 | |