|
| 1 | +/* ////////////////////////////////////////////////////////////////////////////////////// |
| 2 | + * includes |
| 3 | + */ |
| 4 | +#include "../demo.h" |
| 5 | + |
| 6 | +/* ////////////////////////////////////////////////////////////////////////////////////// |
| 7 | + * private implementation |
| 8 | + */ |
| 9 | +static tb_void_t tb_demo_coroutine_watch(tb_cpointer_t priv) |
| 10 | +{ |
| 11 | + tb_char_t const* path = (tb_char_t const*)priv; |
| 12 | + tb_fwatcher_ref_t fwatcher = tb_fwatcher_init(); |
| 13 | + if (fwatcher) |
| 14 | + { |
| 15 | + tb_trace_i("watching %s", path); |
| 16 | + if (tb_fwatcher_add(fwatcher, path)) |
| 17 | + { |
| 18 | + tb_bool_t eof = tb_false; |
| 19 | + tb_long_t count = 0; |
| 20 | + tb_fwatcher_event_t events[64]; |
| 21 | + while (!eof && (count = tb_fwatcher_wait(fwatcher, events, tb_arrayn(events), -1)) >= 0) |
| 22 | + { |
| 23 | + for (tb_size_t i = 0; i < count && !eof; i++) |
| 24 | + { |
| 25 | + tb_fwatcher_event_t const* event = &events[i]; |
| 26 | + tb_char_t const* status = event->event == TB_FWATCHER_EVENT_CREATE? "created" : |
| 27 | + (event->event == TB_FWATCHER_EVENT_MODIFY? "modified" : "deleted"); |
| 28 | + tb_trace_i("watch: %s %s", event->filepath, status); |
| 29 | + if (tb_strstr(event->filepath, "eof")) |
| 30 | + eof = tb_true; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + tb_fwatcher_exit(fwatcher); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +static tb_void_t tb_demo_coroutine_sleep(tb_cpointer_t priv) |
| 39 | +{ |
| 40 | + while (1) |
| 41 | + { |
| 42 | + tb_trace_i("sleep .."); |
| 43 | + tb_sleep(1); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/* ////////////////////////////////////////////////////////////////////////////////////// |
| 48 | + * main |
| 49 | + */ |
| 50 | +tb_int_t tb_demo_coroutine_fwatcher_main(tb_int_t argc, tb_char_t** argv) |
| 51 | +{ |
| 52 | + tb_co_scheduler_ref_t scheduler = tb_co_scheduler_init(); |
| 53 | + if (scheduler) |
| 54 | + { |
| 55 | + // start coroutines |
| 56 | + tb_coroutine_start(scheduler, tb_demo_coroutine_watch, argv[1], 1024 * 1024); |
| 57 | + tb_coroutine_start(scheduler, tb_demo_coroutine_sleep, tb_null, 0); |
| 58 | + |
| 59 | + // do loop |
| 60 | + tb_co_scheduler_loop(scheduler, tb_true); |
| 61 | + tb_co_scheduler_exit(scheduler); |
| 62 | + } |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
0 commit comments