xref: /libuv/docs/code/queue-cancel/main.c (revision d59d6e6f)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 
5 #include <uv.h>
6 
7 #define FIB_UNTIL 25
8 uv_loop_t *loop;
9 uv_work_t fib_reqs[FIB_UNTIL];
10 
fib_(long t)11 long fib_(long t) {
12     if (t == 0 || t == 1)
13         return 1;
14     else
15         return fib_(t-1) + fib_(t-2);
16 }
17 
fib(uv_work_t * req)18 void fib(uv_work_t *req) {
19     int n = *(int *) req->data;
20     if (random() % 2)
21         sleep(1);
22     else
23         sleep(3);
24     long fib = fib_(n);
25     fprintf(stderr, "%dth fibonacci is %lu\n", n, fib);
26 }
27 
after_fib(uv_work_t * req,int status)28 void after_fib(uv_work_t *req, int status) {
29     if (status == UV_ECANCELED)
30         fprintf(stderr, "Calculation of %d cancelled.\n", *(int *) req->data);
31 }
32 
signal_handler(uv_signal_t * req,int signum)33 void signal_handler(uv_signal_t *req, int signum)
34 {
35     printf("Signal received!\n");
36     int i;
37     for (i = 0; i < FIB_UNTIL; i++) {
38         uv_cancel((uv_req_t*) &fib_reqs[i]);
39     }
40     uv_signal_stop(req);
41 }
42 
main()43 int main() {
44     loop = uv_default_loop();
45 
46     int data[FIB_UNTIL];
47     int i;
48     for (i = 0; i < FIB_UNTIL; i++) {
49         data[i] = i;
50         fib_reqs[i].data = (void *) &data[i];
51         uv_queue_work(loop, &fib_reqs[i], fib, after_fib);
52     }
53 
54     uv_signal_t sig;
55     uv_signal_init(loop, &sig);
56     uv_signal_start(&sig, signal_handler, SIGINT);
57 
58     return uv_run(loop, UV_RUN_DEFAULT);
59 }
60