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