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