… | |
… | |
29 | |
29 | |
30 | GMainContext *gc; |
30 | GMainContext *gc; |
31 | }; |
31 | }; |
32 | |
32 | |
33 | static void |
33 | static void |
34 | timer_cb (ev_timer *w, int revents) |
34 | timer_cb (EV_P_ ev_timer *w, int revents) |
35 | { |
35 | { |
36 | /* nop */ |
36 | /* nop */ |
37 | } |
37 | } |
38 | |
38 | |
39 | static void |
39 | static void |
40 | io_cb (ev_io *w, int revents) |
40 | io_cb (EV_P_ ev_io *w, int revents) |
41 | { |
41 | { |
42 | GPollFD *pfd = (GPollFD *)w->data; |
42 | GPollFD *pfd = (GPollFD *)w->data; |
43 | |
43 | |
44 | pfd->revents |= pfd->events & |
44 | pfd->revents |= pfd->events & |
45 | ((revents & EV_READ ? G_IO_IN : 0) |
45 | ((revents & EV_READ ? G_IO_IN : 0) |
46 | | (revents & EV_READ ? G_IO_OUT : 0)); |
46 | | (revents & EV_READ ? G_IO_OUT : 0)); |
47 | } |
47 | } |
48 | |
48 | |
49 | static void |
49 | static void |
50 | cleanup (struct econtext *ctx) |
50 | check_cb (EV_P_ ev_check *w, int revents) |
51 | { |
51 | { |
52 | int i; |
52 | struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, cw)); |
53 | |
53 | |
54 | for (i = 0; i < ctx->nfd; ++i) |
54 | if (ctx->nfd >= 0) |
55 | { |
55 | { |
56 | ev_ref (); |
56 | int i; |
57 | ev_io_stop (EV_A_ ctx->iow + i); |
|
|
58 | } |
|
|
59 | |
57 | |
60 | ctx->nfd = 0; |
58 | for (i = 0; i < ctx->nfd; ++i) |
|
|
59 | ev_io_stop (EV_A_ ctx->iow + i); |
61 | |
60 | |
62 | if (ev_is_active (&ctx->tw)) |
61 | if (ev_is_active (&ctx->tw)) |
63 | { |
|
|
64 | ev_ref (); |
|
|
65 | ev_timer_stop (EV_A_ &ctx->tw); |
62 | ev_timer_stop (EV_A_ &ctx->tw); |
|
|
63 | |
|
|
64 | g_main_context_check (ctx->gc, ctx->maxpri, ctx->pfd, ctx->nfd); |
|
|
65 | |
|
|
66 | ctx->nfd = -1; |
66 | } |
67 | } |
67 | } |
68 | } |
68 | |
69 | |
69 | static void |
70 | static void |
70 | prepare_cb (ev_prepare *w, int revents) |
71 | prepare_cb (EV_P_ ev_prepare *w, int revents) |
71 | { |
72 | { |
72 | struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, pw)); |
73 | struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, pw)); |
73 | gint timeout; |
74 | gint timeout; |
74 | int n; |
75 | int i; |
75 | |
76 | |
76 | cleanup (ctx); |
77 | check_cb (EV_A_ &ctx->cw, 0); |
|
|
78 | |
|
|
79 | g_main_context_dispatch (ctx->gc); |
77 | |
80 | |
78 | g_main_context_prepare (ctx->gc, &ctx->maxpri); |
81 | g_main_context_prepare (ctx->gc, &ctx->maxpri); |
79 | |
82 | |
80 | while (ctx->afd < (ctx->nfd = g_main_context_query ( |
83 | while (ctx->afd < (ctx->nfd = g_main_context_query ( |
81 | ctx->gc, |
84 | ctx->gc, |
… | |
… | |
94 | |
97 | |
95 | ctx->pfd = malloc (ctx->afd * sizeof (GPollFD)); |
98 | ctx->pfd = malloc (ctx->afd * sizeof (GPollFD)); |
96 | ctx->iow = malloc (ctx->afd * sizeof (ev_io)); |
99 | ctx->iow = malloc (ctx->afd * sizeof (ev_io)); |
97 | } |
100 | } |
98 | |
101 | |
99 | for (n = 0; n < ctx->nfd; ++n) |
102 | for (i = 0; i < ctx->nfd; ++i) |
100 | { |
103 | { |
101 | GPollFD *pfd = ctx->pfd + n; |
104 | GPollFD *pfd = ctx->pfd + i; |
102 | ev_io *iow = ctx->iow + n; |
105 | ev_io *iow = ctx->iow + i; |
103 | |
106 | |
104 | pfd->revents = 0; |
107 | pfd->revents = 0; |
105 | |
108 | |
106 | ev_io_init ( |
109 | ev_io_init ( |
107 | iow, |
110 | iow, |
… | |
… | |
109 | pfd->fd, |
112 | pfd->fd, |
110 | (pfd->events & G_IO_IN ? EV_READ : 0) |
113 | (pfd->events & G_IO_IN ? EV_READ : 0) |
111 | | (pfd->events & G_IO_OUT ? EV_WRITE : 0) |
114 | | (pfd->events & G_IO_OUT ? EV_WRITE : 0) |
112 | ); |
115 | ); |
113 | iow->data = (void *)pfd; |
116 | iow->data = (void *)pfd; |
|
|
117 | ev_set_priority (&ctx->cw, ev_priority (w) + 1); |
114 | ev_io_start (EV_A_ iow); |
118 | ev_io_start (EV_A_ iow); |
115 | ev_unref (); |
|
|
116 | } |
119 | } |
117 | |
120 | |
118 | if (timeout >= 0) |
121 | if (timeout >= 0) |
119 | { |
122 | { |
120 | ev_timer_set (&ctx->tw, timeout * 1e-3, 0.); |
123 | ev_timer_set (&ctx->tw, timeout * 1e-3, 0.); |
121 | ev_timer_start (EV_A_ &ctx->tw); |
124 | ev_timer_start (EV_A_ &ctx->tw); |
122 | } |
125 | } |
123 | } |
|
|
124 | |
|
|
125 | static void |
|
|
126 | check_cb (ev_check *w, int revents) |
|
|
127 | { |
|
|
128 | struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, cw)); |
|
|
129 | |
|
|
130 | cleanup (ctx); |
|
|
131 | |
|
|
132 | g_main_context_check (ctx->gc, ctx->maxpri, ctx->pfd, ctx->nfd); |
|
|
133 | g_main_context_dispatch (ctx->gc); |
|
|
134 | } |
126 | } |
135 | |
127 | |
136 | static struct econtext default_context; |
128 | static struct econtext default_context; |
137 | |
129 | |
138 | MODULE = EV::Glib PACKAGE = EV::Glib |
130 | MODULE = EV::Glib PACKAGE = EV::Glib |
… | |
… | |
150 | { |
142 | { |
151 | GMainContext *gc = get_gcontext (context); |
143 | GMainContext *gc = get_gcontext (context); |
152 | struct econtext *ctx = &default_context; |
144 | struct econtext *ctx = &default_context; |
153 | |
145 | |
154 | ctx->gc = g_main_context_ref (gc); |
146 | ctx->gc = g_main_context_ref (gc); |
155 | ctx->nfd = 0; |
147 | ctx->nfd = -1; |
156 | ctx->afd = 0; |
148 | ctx->afd = 0; |
157 | ctx->iow = 0; |
149 | ctx->iow = 0; |
158 | ctx->pfd = 0; |
150 | ctx->pfd = 0; |
159 | |
151 | |
160 | ev_prepare_init (&ctx->pw, prepare_cb); ev_prepare_start (EV_DEFAULT_ &ctx->pw); |
152 | ev_prepare_init (&ctx->pw, prepare_cb); |
161 | ev_check_init (&ctx->cw, check_cb); ev_check_start (EV_DEFAULT_ &ctx->cw); |
153 | ev_prepare_start (EV_DEFAULT_ &ctx->pw); |
162 | ev_init (&ctx->tw, timer_cb); ev_set_priority (&ctx->tw, EV_MINPRI); |
154 | |
|
|
155 | ev_check_init (&ctx->cw, check_cb); |
|
|
156 | ev_set_priority (&ctx->cw, EV_MAXPRI - 1); |
|
|
157 | ev_check_start (EV_DEFAULT_ &ctx->cw); |
|
|
158 | |
|
|
159 | ev_init (&ctx->tw, timer_cb); |
|
|
160 | ev_set_priority (&ctx->tw, EV_MINPRI); |
163 | } |
161 | } |
164 | OUTPUT: |
162 | OUTPUT: |
165 | RETVAL |
163 | RETVAL |
166 | |
164 | |
167 | |
165 | |