ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV-Glib/Glib.xs
(Generate patch)

Comparing EV-Glib/Glib.xs (file contents):
Revision 1.1 by root, Sat Dec 8 02:49:42 2007 UTC vs.
Revision 1.3 by root, Sat Dec 8 03:17:40 2007 UTC

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
33static void 32static void
34timer_cb (ev_timer *w, int revents) 33timer_cb (EV_P_ ev_timer *w, int revents)
35{ 34{
36 /* nop */ 35 /* nop */
37} 36}
38 37
39static void 38static void
40io_cb (ev_io *w, int revents) 39io_cb (EV_P_ ev_io *w, int revents)
41{ 40{
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));
46
47 printf ("ctx rev %d %x:%x\n", w->fd, pfd->events, pfd->revents);//D
47} 48}
48 49
49static void 50static void
50cleanup (struct econtext *ctx)
51{
52 int i;
53
54 for (i = 0; i < ctx->nfd; ++i)
55 {
56 ev_ref ();
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 ();
65 ev_timer_stop (EV_A_ &ctx->tw);
66 }
67}
68
69static void
70prepare_cb (ev_prepare *w, int revents) 51prepare_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 (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 ();
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 } 114 }
123}
124
125static void
126check_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} 115}
135 116
136static struct econtext default_context; 117static struct econtext default_context;
137 118
138MODULE = EV::Glib PACKAGE = EV::Glib 119MODULE = EV::Glib PACKAGE = EV::Glib
150{ 131{
151 GMainContext *gc = get_gcontext (context); 132 GMainContext *gc = get_gcontext (context);
152 struct econtext *ctx = &default_context; 133 struct econtext *ctx = &default_context;
153 134
154 ctx->gc = g_main_context_ref (gc); 135 ctx->gc = g_main_context_ref (gc);
155 ctx->nfd = 0; 136 ctx->nfd = -1;
156 ctx->afd = 0; 137 ctx->afd = 0;
157 ctx->iow = 0; 138 ctx->iow = 0;
158 ctx->pfd = 0; 139 ctx->pfd = 0;
159 140
160 ev_prepare_init (&ctx->pw, prepare_cb); ev_prepare_start (EV_DEFAULT_ &ctx->pw); 141 ev_prepare_init (&ctx->pw, prepare_cb);
161 ev_check_init (&ctx->cw, check_cb); ev_check_start (EV_DEFAULT_ &ctx->cw); 142 ev_prepare_start (EV_DEFAULT_ &ctx->pw);
162 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);
163} 145}
164 OUTPUT: 146 OUTPUT:
165 RETVAL 147 RETVAL
166 148
167 149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines