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

Comparing Coro-Multicore/Multicore.xs (file contents):
Revision 1.8 by root, Wed Jul 1 22:39:07 2015 UTC vs.
Revision 1.21 by root, Sun Aug 26 15:30:55 2018 UTC

1/* most win32 perls are beyond fixing, requiring dTHX */
2/* even for ISO-C functions such as malloc. avoid! avoid! avoid! */
3/* and fail to define numerous symbols, but still overrwide them */
4/* with non-working versions (e.g. setjmp). */
5#ifdef _WIN32
6/*# define PERL_CORE 1 fixes some, breaks others */
7#else
1#define PERL_NO_GET_CONTEXT 8# define PERL_NO_GET_CONTEXT
9#endif
2 10
3#include "EXTERN.h" 11#include "EXTERN.h"
4#include "perl.h" 12#include "perl.h"
5#include "XSUB.h" 13#include "XSUB.h"
6 14
10#include "perlmulticore.h" 18#include "perlmulticore.h"
11#include "schmorp.h" 19#include "schmorp.h"
12#include "xthread.h" 20#include "xthread.h"
13 21
14#ifdef _WIN32 22#ifdef _WIN32
15 typedef char sigset_t; 23 #ifndef sigset_t
16 #define pthread_sigmask(mode,new,old) 24 #define sigset_t int
25 #endif
17#endif 26#endif
18 27
19#ifndef SvREFCNT_dec_NN 28#ifndef SvREFCNT_dec_NN
20 #define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) 29 #define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv)
21#endif 30#endif
22 31
23#ifndef SvREFCNT_inc_NN 32#ifndef SvREFCNT_inc_NN
24 #define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) 33 #define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
25#endif 34#endif
26 35
27static pthread_key_t current_key; 36#define RECURSION_CHECK 0
37
38static X_TLS_DECLARE(current_key);
39#if RECURSION_CHECK
40static X_TLS_DECLARE(check_key);
41#endif
42
28 43
29static s_epipe ep; 44static s_epipe ep;
30static void *perl_thx; 45static void *perl_thx;
31static sigset_t cursigset, fullsigset; 46static sigset_t cursigset, fullsigset;
32 47
36/* assigned to a thread for each release/acquire */ 51/* assigned to a thread for each release/acquire */
37struct tctx 52struct tctx
38{ 53{
39 void *coro; 54 void *coro;
40 int wait_f; 55 int wait_f;
41 xcond_t wait_c; 56 xcond_t acquire_c;
57 int jeret;
42}; 58};
43 59
44static struct tctx *tctx_free; 60static struct tctx *tctx_free;
45 61
62static struct tctx *
63tctx_get (void)
64{
65 struct tctx *ctx;
66
67 if (!tctx_free)
68 {
69 ctx = malloc (sizeof (*tctx_free));
70 X_COND_CREATE (ctx->acquire_c);
71 }
72 else
73 {
74 ctx = tctx_free;
75 tctx_free = tctx_free->coro;
76 }
77
78 return ctx;
79}
80
81static void
82tctx_put (struct tctx *ctx)
83{
84 ctx->coro = tctx_free;
85 tctx_free = ctx;
86}
87
88/* a stack of tctxs */
89struct tctxs
90{
91 struct tctx **ctxs;
92 int cur, max;
93};
94
95static struct tctx *
96tctxs_get (struct tctxs *ctxs)
97{
98 return ctxs->ctxs[--ctxs->cur];
99}
100
101static void
102tctxs_put (struct tctxs *ctxs, struct tctx *ctx)
103{
104 if (ctxs->cur >= ctxs->max)
105 {
106 ctxs->max = ctxs->max ? ctxs->max * 2 : 16;
107 ctxs->ctxs = realloc (ctxs->ctxs, ctxs->max * sizeof (ctxs->ctxs[0]));
108 }
109
110 ctxs->ctxs[ctxs->cur++] = ctx;
111}
112
113static xmutex_t release_m = X_MUTEX_INIT;
114static xcond_t release_c = X_COND_INIT;
115static struct tctxs releasers;
46static int idle; 116static int idle;
47static int min_idle = 1; 117static int min_idle = 1;
118static int curthreads, max_threads = 1; /* protected by release_m */
48 119
49static xmutex_t perl_m = X_MUTEX_INIT;
50static xcond_t perl_c = X_COND_INIT;
51static struct tctx *perl_f;
52
53static xmutex_t wait_m = X_MUTEX_INIT; 120static xmutex_t acquire_m = X_MUTEX_INIT;
54
55static int wakeup_f;
56static struct tctx **waiters; 121static struct tctxs acquirers;
57static int waiters_count, waiters_max;
58
59static struct tctx *
60tctx_get (void)
61{
62 struct tctx *ctx;
63
64 if (!tctx_free)
65 {
66 ctx = malloc (sizeof (*tctx_free));
67 X_COND_CREATE (ctx->wait_c);
68 }
69 else
70 {
71 ctx = tctx_free;
72 tctx_free = tctx_free->coro;
73 }
74
75 return ctx;
76}
77
78static void
79tctx_put (struct tctx *ctx)
80{
81 ctx->coro = tctx_free;
82 tctx_free = ctx;
83}
84 122
85X_THREAD_PROC(thread_proc) 123X_THREAD_PROC(thread_proc)
86{ 124{
87 PERL_SET_CONTEXT (perl_thx); 125 PERL_SET_CONTEXT (perl_thx);
88 126
89 { 127 {
90 dTHX; /* inefficient, we already have perl_thx, but I see no better way */ 128 dTHXa (perl_thx);
129 dJMPENV;
91 struct tctx *ctx; 130 struct tctx *ctx;
131 int catchret;
92 132
93 X_LOCK (perl_m); 133 X_LOCK (release_m);
94 134
95 for (;;) 135 for (;;)
96 { 136 {
97 while (!perl_f) 137 while (!releasers.cur)
98 if (idle <= min_idle || 1) 138 if (idle <= min_idle || 1)
99 X_COND_WAIT (perl_c, perl_m); 139 X_COND_WAIT (release_c, release_m);
100 else 140 else
101 { 141 {
102 struct timespec ts = { time (0) + idle - min_idle, 0 }; 142 struct timespec ts = { time (0) + idle - min_idle, 0 };
103 143
104 if (X_COND_TIMEDWAIT (perl_c, perl_m, ts) == ETIMEDOUT) 144 if (X_COND_TIMEDWAIT (release_c, release_m, ts) == ETIMEDOUT)
105 if (idle > min_idle && !perl_f) 145 if (idle > min_idle && !releasers.cur)
106 break; 146 break;
107 } 147 }
108 148
109 ctx = perl_f; 149 ctx = tctxs_get (&releasers);
110 perl_f = 0;
111 --idle; 150 --idle;
112 X_UNLOCK (perl_m); 151 X_UNLOCK (release_m);
113 152
114 if (!ctx) /* timed out? */ 153 if (!ctx) /* timed out? */
115 break; 154 break;
116 155
117 pthread_sigmask (SIG_SETMASK, &cursigset, 0); 156 pthread_sigmask (SIG_SETMASK, &cursigset, 0);
157 JMPENV_PUSH (ctx->jeret);
118 158
159 if (!ctx->jeret)
119 while (ctx->coro) 160 while (ctx->coro)
120 CORO_SCHEDULE; 161 CORO_SCHEDULE;
121 162
163 JMPENV_POP;
122 pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset); 164 pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset);
123 165
124 X_LOCK (wait_m); 166 X_LOCK (acquire_m);
125 ctx->wait_f = 1; 167 ctx->wait_f = 1;
126 X_COND_SIGNAL (ctx->wait_c); 168 X_COND_SIGNAL (ctx->acquire_c);
127 X_UNLOCK (wait_m); 169 X_UNLOCK (acquire_m);
128 170
129 X_LOCK (perl_m); 171 X_LOCK (release_m);
130 ++idle; 172 ++idle;
131 } 173 }
132 } 174 }
133} 175}
134 176
135static void 177static void
136start_thread (void) 178start_thread (void)
137{ 179{
138 xthread_t tid; 180 xthread_t tid;
139 181
182 if (curthreads >= max_threads && 0)
183 return;
184
185 ++curthreads;
140 ++idle; 186 ++idle;
141 xthread_create (&tid, thread_proc, 0); 187 xthread_create (&tid, thread_proc, 0);
142} 188}
143 189
144static void 190static void
145pmapi_release (void) 191pmapi_release (void)
146{ 192{
193 #if RECURSION_CHECK
194 if (X_TLS_GET (check_key))
195 croak ("perlinterp_release () called without valid perl context");
196
197 X_TLS_SET (check_key, &check_key);
198 #endif
199
147 if (!(thread_enable ? thread_enable & 1 : global_enable)) 200 if (! ((thread_enable ? thread_enable : global_enable) & 1))
148 { 201 {
149 pthread_setspecific (current_key, 0); 202 X_TLS_SET (current_key, 0);
150 return; 203 return;
151 } 204 }
152 205
153 struct tctx *ctx = tctx_get (); 206 struct tctx *ctx = tctx_get ();
154 ctx->coro = SvREFCNT_inc_NN (CORO_CURRENT); 207 ctx->coro = SvREFCNT_inc_simple_NN (CORO_CURRENT);
155 ctx->wait_f = 0; 208 ctx->wait_f = 0;
156 209
157 pthread_setspecific (current_key, ctx); 210 X_TLS_SET (current_key, ctx);
158 pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset); 211 pthread_sigmask (SIG_SETMASK, &fullsigset, &cursigset);
159 212
160 X_LOCK (perl_m); 213 X_LOCK (release_m);
161 214
162 if (idle <= min_idle) 215 if (idle <= min_idle)
163 start_thread (); 216 start_thread ();
164 217
165 perl_f = ctx; 218 tctxs_put (&releasers, ctx);
166 X_COND_SIGNAL (perl_c); 219 X_COND_SIGNAL (release_c);
167 220
221 while (!idle && releasers.cur)
222 {
223 X_UNLOCK (release_m);
224 X_LOCK (release_m);
225 }
226
168 X_UNLOCK (perl_m); 227 X_UNLOCK (release_m);
169} 228}
170 229
171static void 230static void
172pmapi_acquire (void) 231pmapi_acquire (void)
173{ 232{
174 struct tctx *ctx = pthread_getspecific (current_key); 233 int jeret;
234 struct tctx *ctx = X_TLS_GET (current_key);
235
236 #if RECURSION_CHECK
237 if (X_TLS_GET (check_key) != &check_key)
238 croak ("perlinterp_acquire () called with valid perl context");
239
240 X_TLS_SET (check_key, 0);
241 #endif
175 242
176 if (!ctx) 243 if (!ctx)
177 return; 244 return;
178 245
179 X_LOCK (wait_m); 246 X_LOCK (acquire_m);
180 247
181 if (waiters_count >= waiters_max) 248 tctxs_put (&acquirers, ctx);
182 {
183 waiters_max = waiters_max ? waiters_max * 2 : 16;
184 waiters = realloc (waiters, waiters_max * sizeof (*waiters));
185 }
186
187 waiters [waiters_count++] = ctx;
188 249
189 s_epipe_signal (&ep); 250 s_epipe_signal (&ep);
190 while (!ctx->wait_f) 251 while (!ctx->wait_f)
191 X_COND_WAIT (ctx->wait_c, wait_m); 252 X_COND_WAIT (ctx->acquire_c, acquire_m);
192 X_UNLOCK (wait_m); 253 X_UNLOCK (acquire_m);
193 254
255 jeret = ctx->jeret;
194 tctx_put (ctx); 256 tctx_put (ctx);
195 pthread_sigmask (SIG_SETMASK, &cursigset, 0); 257 pthread_sigmask (SIG_SETMASK, &cursigset, 0);
258
259 if (jeret)
260 {
261 dTHX;
262 JMPENV_JUMP (jeret);
263 }
196} 264}
197 265
198static void 266static void
199set_thread_enable (pTHX_ void *arg) 267set_thread_enable (pTHX_ void *arg)
200{ 268{
205 273
206PROTOTYPES: DISABLE 274PROTOTYPES: DISABLE
207 275
208BOOT: 276BOOT:
209{ 277{
210 #ifndef _WIN32 278#ifndef _WIN32
211 sigfillset (&fullsigset); 279 sigfillset (&fullsigset);
212 #endif 280#endif
213 281
214 pthread_key_create (&current_key, 0); 282 X_TLS_INIT (current_key);
283#if RECURSION_CHECK
284 X_TLS_INIT (check_key);
285#endif
215 286
216 if (s_epipe_new (&ep)) 287 if (s_epipe_new (&ep))
217 croak ("Coro::Multicore: unable to initialise event pipe.\n"); 288 croak ("Coro::Multicore: unable to initialise event pipe.\n");
218 289
219 perl_thx = PERL_GET_CONTEXT; 290 perl_thx = PERL_GET_CONTEXT;
220 291
221 I_CORO_API ("Coro::Multicore"); 292 I_CORO_API ("Coro::Multicore");
222 293
223 X_LOCK (perl_m); 294 X_LOCK (release_m);
224 while (idle < min_idle) 295 while (idle < min_idle)
225 start_thread (); 296 start_thread ();
226 start_thread ();//D
227 X_UNLOCK (perl_m); 297 X_UNLOCK (release_m);
228 298
229 /* not perfectly efficient to do it this way, but it's simple */ 299 /* not perfectly efficient to do it this way, but it is simple */
230 perl_multicore_init (); 300 perl_multicore_init (); /* calls release */
231 perl_multicore_api->pmapi_release = pmapi_release; 301 perl_multicore_api->pmapi_release = pmapi_release;
232 perl_multicore_api->pmapi_acquire = pmapi_acquire; 302 perl_multicore_api->pmapi_acquire = pmapi_acquire;
233} 303}
234 304
235bool 305bool
256 ENTER; /* see Guard.xs */ 326 ENTER; /* see Guard.xs */
257 327
258U32 328U32
259min_idle_threads (U32 min = NO_INIT) 329min_idle_threads (U32 min = NO_INIT)
260 CODE: 330 CODE:
261 X_LOCK (wait_m); 331 X_LOCK (acquire_m);
262 RETVAL = min_idle; 332 RETVAL = min_idle;
263 if (items) 333 if (items)
264 min_idle = min; 334 min_idle = min;
265 X_UNLOCK (wait_m); 335 X_UNLOCK (acquire_m);
266 OUTPUT: 336 OUTPUT:
267 RETVAL 337 RETVAL
268 338
269 339
270int 340int
276 346
277void 347void
278poll (...) 348poll (...)
279 CODE: 349 CODE:
280 s_epipe_drain (&ep); 350 s_epipe_drain (&ep);
281 X_LOCK (wait_m); 351 X_LOCK (acquire_m);
282 while (waiters_count) 352 while (acquirers.cur)
283 { 353 {
284 struct tctx *ctx = waiters [--waiters_count]; 354 struct tctx *ctx = tctxs_get (&acquirers);
285 CORO_READY ((SV *)ctx->coro); 355 CORO_READY ((SV *)ctx->coro);
286 SvREFCNT_dec_NN ((SV *)ctx->coro); 356 SvREFCNT_dec_NN ((SV *)ctx->coro);
287 ctx->coro = 0; 357 ctx->coro = 0;
288 } 358 }
289 X_UNLOCK (wait_m); 359 X_UNLOCK (acquire_m);
290 360
291void 361void
292sleep (NV seconds) 362sleep (NV seconds)
293 CODE: 363 CODE:
294 perlinterp_release (); 364 perlinterp_release ();
295 usleep (seconds * 1e6); 365 {
366 int nsec = seconds;
367 if (nsec) sleep (nsec);
368 nsec = (seconds - nsec) * 1e9;
369 if (nsec) usleep (nsec);
370 }
296 perlinterp_acquire (); 371 perlinterp_acquire ();
297 372

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines