This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #37919] Blessed scalar refs with threads::shared
[perl5.git] / ext / threads / shared / shared.xs
CommitLineData
afe38520 1/* shared.xs
68795e93 2 *
afe38520 3 * Copyright (c) 2001-2002, Larry Wall
68795e93
NIS
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
21312124
NIS
8 * "Hand any two wizards a piece of rope and they would instinctively pull in
9 * opposite directions."
10 * --Sourcery
11 *
12 * Contributed by Arthur Bergman arthur@contiller.se
13 * pulled in the (an)other direction by Nick Ing-Simmons nick@ing-simmons.net
14 */
68795e93
NIS
15
16#define PERL_NO_GET_CONTEXT
b050c948
AB
17#include "EXTERN.h"
18#include "perl.h"
19#include "XSUB.h"
20
73e09c8f
JH
21#ifdef USE_ITHREADS
22
21312124
NIS
23#define SHAREDSvPTR(a) ((a)->sv)
24
25/*
26 * The shared things need an intepreter to live in ...
27 */
28PerlInterpreter *PL_sharedsv_space; /* The shared sv space */
29/* To access shared space we fake aTHX in this scope and thread's context */
057e91b3
DM
30
31/* bug #24255: we include ENTER+SAVETMPS/FREETMPS+LEAVE with
32 * SHARED_CONTEXT/CALLER_CONTEXT macros, so that any mortals etc created
1011f542 33 * while in the shared interpreter context don't languish */
057e91b3
DM
34
35#define SHARED_CONTEXT \
36 STMT_START { \
37 PERL_SET_CONTEXT((aTHX = PL_sharedsv_space)); \
38 ENTER; \
39 SAVETMPS; \
40 } STMT_END
21312124
NIS
41
42/* So we need a way to switch back to the caller's context... */
43/* So we declare _another_ copy of the aTHX variable ... */
44#define dTHXc PerlInterpreter *caller_perl = aTHX
057e91b3 45
21312124 46/* and use it to switch back */
057e91b3
DM
47#define CALLER_CONTEXT \
48 STMT_START { \
49 FREETMPS; \
50 LEAVE; \
51 PERL_SET_CONTEXT((aTHX = caller_perl)); \
52 } STMT_END
21312124
NIS
53
54/*
55 * Only one thread at a time is allowed to mess with shared space.
56 */
a446a88f 57
6d56dc1c
NIS
58typedef struct
59{
60 perl_mutex mutex;
6d56dc1c
NIS
61 PerlInterpreter *owner;
62 I32 locks;
6b85e4fe
NIS
63 perl_cond cond;
64#ifdef DEBUG_LOCKS
65 char * file;
66 int line;
67#endif
6d56dc1c
NIS
68} recursive_lock_t;
69
70recursive_lock_t PL_sharedsv_lock; /* Mutex protecting the shared sv space */
71
72void
73recursive_lock_init(pTHX_ recursive_lock_t *lock)
74{
75 Zero(lock,1,recursive_lock_t);
76 MUTEX_INIT(&lock->mutex);
77 COND_INIT(&lock->cond);
78}
79
a39edb3a 80void
579f9913
AB
81recursive_lock_destroy(pTHX_ recursive_lock_t *lock)
82{
83 MUTEX_DESTROY(&lock->mutex);
84 COND_DESTROY(&lock->cond);
85}
86
6d56dc1c
NIS
87void
88recursive_lock_release(pTHX_ recursive_lock_t *lock)
89{
90 MUTEX_LOCK(&lock->mutex);
91 if (lock->owner != aTHX) {
92 MUTEX_UNLOCK(&lock->mutex);
93 }
94 else {
95 if (--lock->locks == 0) {
96 lock->owner = NULL;
97 COND_SIGNAL(&lock->cond);
98 }
99 }
100 MUTEX_UNLOCK(&lock->mutex);
101}
a446a88f 102
6d56dc1c 103void
6b85e4fe 104recursive_lock_acquire(pTHX_ recursive_lock_t *lock,char *file,int line)
6d56dc1c
NIS
105{
106 assert(aTHX);
107 MUTEX_LOCK(&lock->mutex);
108 if (lock->owner == aTHX) {
109 lock->locks++;
110 }
111 else {
6b85e4fe
NIS
112 while (lock->owner) {
113#ifdef DEBUG_LOCKS
114 Perl_warn(aTHX_ " %p waiting - owned by %p %s:%d\n",
115 aTHX, lock->owner, lock->file, lock->line);
116#endif
6d56dc1c 117 COND_WAIT(&lock->cond,&lock->mutex);
6b85e4fe 118 }
6d56dc1c
NIS
119 lock->locks = 1;
120 lock->owner = aTHX;
6b85e4fe
NIS
121#ifdef DEBUG_LOCKS
122 lock->file = file;
123 lock->line = line;
124#endif
6d56dc1c
NIS
125 }
126 MUTEX_UNLOCK(&lock->mutex);
6b85e4fe 127 SAVEDESTRUCTOR_X(recursive_lock_release,lock);
6d56dc1c
NIS
128}
129
130#define ENTER_LOCK STMT_START { \
131 ENTER; \
6b85e4fe 132 recursive_lock_acquire(aTHX_ &PL_sharedsv_lock, __FILE__, __LINE__); \
a446a88f 133 } STMT_END
21312124 134
6d56dc1c
NIS
135#define LEAVE_LOCK LEAVE
136
21312124
NIS
137
138/* A common idiom is to acquire access and switch in ... */
139#define SHARED_EDIT STMT_START { \
6d56dc1c 140 ENTER_LOCK; \
21312124
NIS
141 SHARED_CONTEXT; \
142 } STMT_END
143
144/* then switch out and release access. */
145#define SHARED_RELEASE STMT_START { \
146 CALLER_CONTEXT; \
6d56dc1c 147 LEAVE_LOCK; \
21312124 148 } STMT_END
85e0a142 149
21312124
NIS
150
151/*
152
153 Shared SV
154
155 Shared SV is a structure for keeping the backend storage
156 of shared svs.
157
158 Shared-ness really only needs the SV * - the rest is for locks.
159 (Which suggests further space optimization ... )
160
161*/
68795e93
NIS
162
163typedef struct {
21312124 164 SV *sv; /* The actual SV - in shared space */
6d56dc1c 165 recursive_lock_t lock;
68795e93 166 perl_cond user_cond; /* For user-level conditions */
68795e93
NIS
167} shared_sv;
168
21312124
NIS
169/* The SV in shared-space has a back-pointer to the shared_sv
170 struct associated with it PERL_MAGIC_ext.
68795e93 171
21312124
NIS
172 The vtable used has just one entry - when the SV goes away
173 we free the memory for the above.
68795e93 174
21312124 175 */
68795e93 176
21312124
NIS
177int
178sharedsv_shared_mg_free(pTHX_ SV *sv, MAGIC *mg)
179{
180 shared_sv *shared = (shared_sv *) mg->mg_ptr;
6b85e4fe 181 assert( aTHX == PL_sharedsv_space );
21312124 182 if (shared) {
cab6ddb1 183 recursive_lock_destroy(aTHX_ &shared->lock);
579f9913 184 COND_DESTROY(&shared->user_cond);
21312124
NIS
185 PerlMemShared_free(shared);
186 mg->mg_ptr = NULL;
187 }
188 return 0;
189}
190
21312124
NIS
191MGVTBL sharedsv_shared_vtbl = {
192 0, /* get */
193 0, /* set */
194 0, /* len */
195 0, /* clear */
196 sharedsv_shared_mg_free, /* free */
197 0, /* copy */
198 0, /* dup */
199};
200
201/* Access to shared things is heavily based on MAGIC - in mg.h/mg.c/sv.c sense */
202
203/* In any thread that has access to a shared thing there is a "proxy"
204 for it in its own space which has 'MAGIC' associated which accesses
205 the shared thing.
206 */
207
208MGVTBL sharedsv_scalar_vtbl; /* scalars have this vtable */
209MGVTBL sharedsv_array_vtbl; /* hashes and arrays have this - like 'tie' */
210MGVTBL sharedsv_elem_vtbl; /* elements of hashes and arrays have this
211 _AS WELL AS_ the scalar magic */
212
213/* The sharedsv_elem_vtbl associates the element with the array/hash and
214 the sharedsv_scalar_vtbl associates it with the value
215 */
216
6b85e4fe
NIS
217
218/* Accessor to convert threads::shared::tie objects back shared_sv * */
219shared_sv *
220SV_to_sharedsv(pTHX_ SV *sv)
221{
222 shared_sv *shared = 0;
223 if (SvROK(sv))
224 {
225 shared = INT2PTR(shared_sv *, SvIV(SvRV(sv)));
226 }
227 return shared;
228}
229
21312124
NIS
230=for apidoc sharedsv_find
231
6b85e4fe 232Given a private side SV tries to find if the SV has a shared backend,
21312124
NIS
233by looking for the magic.
234
235=cut
236
237shared_sv *
238Perl_sharedsv_find(pTHX_ SV *sv)
239{
240 MAGIC *mg;
a446a88f
NIS
241 if (SvTYPE(sv) >= SVt_PVMG) {
242 switch(SvTYPE(sv)) {
243 case SVt_PVAV:
244 case SVt_PVHV:
245 if ((mg = mg_find(sv, PERL_MAGIC_tied))
246 && mg->mg_virtual == &sharedsv_array_vtbl) {
21312124
NIS
247 return (shared_sv *) mg->mg_ptr;
248 }
249 break;
a446a88f 250 default:
6b85e4fe
NIS
251 /* This should work for elements as well as they
252 * have scalar magic as well as their element magic
253 */
a446a88f
NIS
254 if ((mg = mg_find(sv, PERL_MAGIC_shared_scalar))
255 && mg->mg_virtual == &sharedsv_scalar_vtbl) {
21312124 256 return (shared_sv *) mg->mg_ptr;
6b85e4fe 257 }
a446a88f 258 break;
21312124
NIS
259 }
260 }
6b85e4fe
NIS
261 /* Just for tidyness of API also handle tie objects */
262 if (SvROK(sv) && sv_derived_from(sv, "threads::shared::tie")) {
263 return SV_to_sharedsv(aTHX_ sv);
264 }
21312124
NIS
265 return NULL;
266}
68795e93
NIS
267
268/*
21312124
NIS
269 * Almost all the pain is in this routine.
270 *
271 */
68795e93 272
21312124
NIS
273shared_sv *
274Perl_sharedsv_associate(pTHX_ SV **psv, SV *ssv, shared_sv *data)
275{
21312124 276 dTHXc;
a446a88f 277 MAGIC *mg = 0;
6b85e4fe 278 SV *sv = (psv) ? *psv : Nullsv;
a446a88f
NIS
279
280 /* If we are asked for an private ops we need a thread */
281 assert ( aTHX != PL_sharedsv_space );
282
283 /* To avoid need for recursive locks require caller to hold lock */
6d56dc1c 284 assert ( PL_sharedsv_lock.owner == aTHX );
6b85e4fe
NIS
285
286 /* First try and get existing global data structure */
68795e93 287
21312124 288 /* Try shared SV as 1st choice */
a446a88f 289 if (!data && ssv && SvTYPE(ssv) >= SVt_PVMG) {
436c6dd3 290 if( (mg = mg_find(ssv, PERL_MAGIC_ext)) ){
21312124
NIS
291 data = (shared_sv *) mg->mg_ptr;
292 }
293 }
6b85e4fe
NIS
294
295 /* Next see if private SV is associated with something */
296 if (!data && sv) {
297 data = Perl_sharedsv_find(aTHX_ sv);
21312124 298 }
6b85e4fe 299
21312124
NIS
300 /* If neither of those then create a new one */
301 if (!data) {
6b85e4fe 302 SHARED_CONTEXT;
b0cd0593 303 if (!ssv) {
6b85e4fe 304 ssv = newSV(0);
b0cd0593
AB
305 SvREFCNT(ssv) = 0;
306 }
8f77bfdb 307 data = (shared_sv *) PerlMemShared_malloc(sizeof(shared_sv));
21312124 308 Zero(data,1,shared_sv);
6b85e4fe
NIS
309 SHAREDSvPTR(data) = ssv;
310 /* Tag shared side SV with data pointer */
311 sv_magicext(ssv, ssv, PERL_MAGIC_ext, &sharedsv_shared_vtbl,
312 (char *)data, 0);
6d56dc1c 313 recursive_lock_init(aTHX_ &data->lock);
21312124 314 COND_INIT(&data->user_cond);
6b85e4fe 315 CALLER_CONTEXT;
21312124 316 }
68795e93 317
21312124
NIS
318 if (!ssv)
319 ssv = SHAREDSvPTR(data);
6b85e4fe
NIS
320 if (!SHAREDSvPTR(data))
321 SHAREDSvPTR(data) = ssv;
322
323 /* If we know type upgrade shared side SV */
324 if (sv && SvTYPE(ssv) < SvTYPE(sv)) {
21312124 325 SHARED_CONTEXT;
21312124 326 sv_upgrade(ssv, SvTYPE(*psv));
aa49c2f8
DM
327 if (SvTYPE(ssv) == SVt_PVAV) /* #24061 */
328 AvREAL_on(ssv);
21312124
NIS
329 CALLER_CONTEXT;
330 }
68795e93 331
21312124 332 /* Now if requested allocate private SV */
6b85e4fe
NIS
333 if (psv && !sv) {
334 *psv = sv = newSV(0);
21312124
NIS
335 }
336
337 /* Finally if private SV exists check and add magic */
6b85e4fe 338 if (sv) {
a446a88f 339 MAGIC *mg = 0;
6b85e4fe
NIS
340 if (SvTYPE(sv) < SvTYPE(ssv)) {
341 sv_upgrade(sv, SvTYPE(ssv));
342 }
21312124
NIS
343 switch(SvTYPE(sv)) {
344 case SVt_PVAV:
345 case SVt_PVHV:
346 if (!(mg = mg_find(sv, PERL_MAGIC_tied))
6b85e4fe
NIS
347 || mg->mg_virtual != &sharedsv_array_vtbl
348 || (shared_sv *) mg->mg_ptr != data) {
a446a88f
NIS
349 SV *obj = newSV(0);
350 sv_setref_iv(obj, "threads::shared::tie",PTR2IV(data));
6b85e4fe 351 if (mg) {
21312124 352 sv_unmagic(sv, PERL_MAGIC_tied);
6b85e4fe 353 }
a446a88f 354 mg = sv_magicext(sv, obj, PERL_MAGIC_tied, &sharedsv_array_vtbl,
21312124
NIS
355 (char *) data, 0);
356 mg->mg_flags |= (MGf_COPY|MGf_DUP);
6b85e4fe 357 SvREFCNT_inc(ssv);
a446a88f 358 SvREFCNT_dec(obj);
5c360ac5
AB
359 if(SvOBJECT(ssv)) {
360 STRLEN len;
361 char* stash_ptr = SvPV((SV*) SvSTASH(ssv), len);
362 HV* stash = gv_stashpvn(stash_ptr, len, TRUE);
363 SvOBJECT_on(sv);
b162af07 364 SvSTASH_set(sv, (HV*)SvREFCNT_inc(stash));
5c360ac5 365 }
21312124
NIS
366 }
367 break;
368
369 default:
6b85e4fe
NIS
370 if ((SvTYPE(sv) < SVt_PVMG)
371 || !(mg = mg_find(sv, PERL_MAGIC_shared_scalar))
372 || mg->mg_virtual != &sharedsv_scalar_vtbl
373 || (shared_sv *) mg->mg_ptr != data) {
374 if (mg) {
21312124 375 sv_unmagic(sv, PERL_MAGIC_shared_scalar);
6b85e4fe 376 }
21312124
NIS
377 mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar,
378 &sharedsv_scalar_vtbl, (char *)data, 0);
379 mg->mg_flags |= (MGf_COPY|MGf_DUP);
6b85e4fe 380 SvREFCNT_inc(ssv);
402174ad
JH
381 if(SvOBJECT(ssv)) {
382 STRLEN len;
383 char* stash_ptr = SvPV((SV*) SvSTASH(ssv), len);
384 HV* stash = gv_stashpvn(stash_ptr, len, TRUE);
385 SvOBJECT_on(sv);
386 SvSTASH_set(sv, (HV*)SvREFCNT_inc(stash));
387 }
21312124
NIS
388 }
389 break;
390 }
6d56dc1c 391 assert ( Perl_sharedsv_find(aTHX_ *psv) == data );
21312124 392 }
21312124
NIS
393 return data;
394}
68795e93
NIS
395
396void
21312124 397Perl_sharedsv_free(pTHX_ shared_sv *shared)
68795e93 398{
21312124
NIS
399 if (shared) {
400 dTHXc;
401 SHARED_EDIT;
402 SvREFCNT_dec(SHAREDSvPTR(shared));
403 SHARED_RELEASE;
404 }
68795e93
NIS
405}
406
21312124
NIS
407void
408Perl_sharedsv_share(pTHX_ SV *sv)
409{
410 switch(SvTYPE(sv)) {
411 case SVt_PVGV:
412 Perl_croak(aTHX_ "Cannot share globs yet");
413 break;
414
415 case SVt_PVCV:
416 Perl_croak(aTHX_ "Cannot share subs yet");
417 break;
85e0a142 418
21312124 419 default:
6d56dc1c 420 ENTER_LOCK;
21312124 421 Perl_sharedsv_associate(aTHX_ &sv, 0, 0);
6d56dc1c 422 LEAVE_LOCK;
a446a88f
NIS
423 SvSETMAGIC(sv);
424 break;
21312124
NIS
425 }
426}
68795e93 427
a0e036c1
MP
428#if defined(WIN32) || defined(OS2)
429# define ABS2RELMILLI(abs) \
430 do { \
2666606c 431 abs -= (double)time(NULL); \
a0e036c1
MP
432 if (abs > 0) { abs *= 1000; } \
433 else { abs = 0; } \
434 } while (0)
435#endif /* WIN32 || OS2 */
436
437bool
438Perl_sharedsv_cond_timedwait(perl_cond *cond, perl_mutex *mut, double abs)
439{
440#if defined(NETWARE) || defined(FAKE_THREADS) || defined(I_MACH_CTHREADS)
441 Perl_croak_nocontext("cond_timedwait not supported on this platform");
442#else
443# ifdef WIN32
444 int got_it = 0;
445
446 ABS2RELMILLI(abs);
447
448 cond->waiters++;
449 MUTEX_UNLOCK(mut);
450 /* See comments in win32/win32thread.h COND_WAIT vis-a-vis race */
028f8eaa 451 switch (WaitForSingleObject(cond->sem, (DWORD)abs)) {
a0e036c1
MP
452 case WAIT_OBJECT_0: got_it = 1; break;
453 case WAIT_TIMEOUT: break;
454 default:
455 /* WAIT_FAILED? WAIT_ABANDONED? others? */
456 Perl_croak_nocontext("panic: cond_timedwait (%ld)",GetLastError());
457 break;
458 }
459 MUTEX_LOCK(mut);
2666606c 460 cond->waiters--;
a0e036c1
MP
461 return got_it;
462# else
463# ifdef OS2
464 int rc, got_it = 0;
465 STRLEN n_a;
466
467 ABS2RELMILLI(abs);
468
469 if ((rc = DosResetEventSem(*cond,&n_a)) && (rc != ERROR_ALREADY_RESET))
470 Perl_rc = rc, croak_with_os2error("panic: cond_timedwait-reset");
471 MUTEX_UNLOCK(mut);
472 if (CheckOSError(DosWaitEventSem(*cond,abs))
473 && (rc != ERROR_INTERRUPT))
474 croak_with_os2error("panic: cond_timedwait");
475 if (rc == ERROR_INTERRUPT) errno = EINTR;
476 MUTEX_LOCK(mut);
477 return got_it;
478# else /* hope you're I_PTHREAD! */
479 struct timespec ts;
480 int got_it = 0;
481
482 ts.tv_sec = (long)abs;
483 abs -= (NV)ts.tv_sec;
484 ts.tv_nsec = (long)(abs * 1000000000.0);
485
486 switch (pthread_cond_timedwait(cond, mut, &ts)) {
487 case 0: got_it = 1; break;
488 case ETIMEDOUT: break;
489 default:
490 Perl_croak_nocontext("panic: cond_timedwait");
491 break;
492 }
493 return got_it;
494# endif /* OS2 */
495# endif /* WIN32 */
496#endif /* NETWARE || FAKE_THREADS || I_MACH_CTHREADS */
497}
498
21312124 499/* MAGIC (in mg.h sense) hooks */
68795e93 500
21312124
NIS
501int
502sharedsv_scalar_mg_get(pTHX_ SV *sv, MAGIC *mg)
68795e93 503{
21312124 504 shared_sv *shared = (shared_sv *) mg->mg_ptr;
6b85e4fe 505 assert(shared);
21312124 506
6d56dc1c 507 ENTER_LOCK;
21312124
NIS
508 if (SHAREDSvPTR(shared)) {
509 if (SvROK(SHAREDSvPTR(shared))) {
a446a88f
NIS
510 SV *obj = Nullsv;
511 Perl_sharedsv_associate(aTHX_ &obj, SvRV(SHAREDSvPTR(shared)), NULL);
512 sv_setsv_nomg(sv, &PL_sv_undef);
b162af07 513 SvRV_set(sv, obj);
a446a88f 514 SvROK_on(sv);
5c360ac5 515
21312124
NIS
516 }
517 else {
a446a88f 518 sv_setsv_nomg(sv, SHAREDSvPTR(shared));
21312124
NIS
519 }
520 }
6d56dc1c 521 LEAVE_LOCK;
21312124
NIS
522 return 0;
523}
524
6b85e4fe
NIS
525void
526sharedsv_scalar_store(pTHX_ SV *sv, shared_sv *shared)
21312124
NIS
527{
528 dTHXc;
21312124 529 bool allowed = TRUE;
21312124
NIS
530 if (SvROK(sv)) {
531 shared_sv* target = Perl_sharedsv_find(aTHX_ SvRV(sv));
532 if (target) {
a446a88f
NIS
533 SV *tmp;
534 SHARED_CONTEXT;
535 tmp = newRV(SHAREDSvPTR(target));
536 sv_setsv_nomg(SHAREDSvPTR(shared), tmp);
21312124 537 SvREFCNT_dec(tmp);
5c360ac5 538 if(SvOBJECT(SvRV(sv))) {
bfcb3514 539 SV* fake_stash = newSVpv(HvNAME_get(SvSTASH(SvRV(sv))),0);
5c360ac5 540 SvOBJECT_on(SHAREDSvPTR(target));
b162af07 541 SvSTASH_set(SHAREDSvPTR(target), (HV*)fake_stash);
5c360ac5 542 }
a446a88f 543 CALLER_CONTEXT;
21312124
NIS
544 }
545 else {
546 allowed = FALSE;
547 }
548 }
549 else {
5c360ac5 550 SvTEMP_off(sv);
a446a88f
NIS
551 SHARED_CONTEXT;
552 sv_setsv_nomg(SHAREDSvPTR(shared), sv);
5c360ac5 553 if(SvOBJECT(sv)) {
bfcb3514 554 SV* fake_stash = newSVpv(HvNAME_get(SvSTASH(sv)),0);
5c360ac5 555 SvOBJECT_on(SHAREDSvPTR(shared));
b162af07 556 SvSTASH_set(SHAREDSvPTR(shared), (HV*)fake_stash);
5c360ac5 557 }
a446a88f 558 CALLER_CONTEXT;
21312124 559 }
21312124
NIS
560 if (!allowed) {
561 Perl_croak(aTHX_ "Invalid value for shared scalar");
562 }
6b85e4fe
NIS
563}
564
565int
566sharedsv_scalar_mg_set(pTHX_ SV *sv, MAGIC *mg)
567{
568 shared_sv *shared;
569 ENTER_LOCK;
570 /* We call associate to potentially upgrade shared side SV */
571 shared = Perl_sharedsv_associate(aTHX_ &sv, Nullsv, (shared_sv *) mg->mg_ptr);
572 assert(shared);
573 sharedsv_scalar_store(aTHX_ sv, shared);
574 LEAVE_LOCK;
21312124 575 return 0;
68795e93
NIS
576}
577
21312124
NIS
578int
579sharedsv_scalar_mg_free(pTHX_ SV *sv, MAGIC *mg)
580{
a446a88f 581 shared_sv *shared = (shared_sv *) mg->mg_ptr;
eb31b78e 582#if 0
a446a88f 583 assert (SvREFCNT(SHAREDSvPTR(shared)) < 1000);
eb31b78e 584#endif
a446a88f
NIS
585 Perl_sharedsv_free(aTHX_ shared);
586 return 0;
587}
588
589int
590sharedsv_scalar_mg_clear(pTHX_ SV *sv, MAGIC *mg)
591{
21312124
NIS
592 return 0;
593}
68795e93
NIS
594
595/*
21312124
NIS
596 * Called during cloning of new threads
597 */
598int
599sharedsv_scalar_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
600{
601 shared_sv *shared = (shared_sv *) mg->mg_ptr;
602 if (shared) {
603 SvREFCNT_inc(SHAREDSvPTR(shared));
604 }
605 return 0;
606}
68795e93 607
21312124
NIS
608MGVTBL sharedsv_scalar_vtbl = {
609 sharedsv_scalar_mg_get, /* get */
610 sharedsv_scalar_mg_set, /* set */
611 0, /* len */
a446a88f 612 sharedsv_scalar_mg_clear, /* clear */
21312124
NIS
613 sharedsv_scalar_mg_free, /* free */
614 0, /* copy */
615 sharedsv_scalar_mg_dup /* dup */
616};
68795e93 617
21312124 618/* Now the arrays/hashes stuff */
21312124
NIS
619int
620sharedsv_elem_mg_FETCH(pTHX_ SV *sv, MAGIC *mg)
68795e93 621{
21312124 622 dTHXc;
6b85e4fe 623 shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
21312124
NIS
624 shared_sv *target = Perl_sharedsv_find(aTHX_ sv);
625 SV** svp;
626
a446a88f
NIS
627 assert ( shared );
628 assert ( SHAREDSvPTR(shared) );
629
6b85e4fe 630 ENTER_LOCK;
21312124 631 if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
a446a88f 632 assert ( mg->mg_ptr == 0 );
6b85e4fe 633 SHARED_CONTEXT;
a446a88f 634 svp = av_fetch((AV*) SHAREDSvPTR(shared), mg->mg_len, 0);
21312124
NIS
635 }
636 else {
6b85e4fe
NIS
637 char *key = mg->mg_ptr;
638 STRLEN len = mg->mg_len;
a446a88f 639 assert ( mg->mg_ptr != 0 );
6b85e4fe
NIS
640 if (mg->mg_len == HEf_SVKEY) {
641 key = SvPV((SV *) mg->mg_ptr, len);
642 }
643 SHARED_CONTEXT;
644 svp = hv_fetch((HV*) SHAREDSvPTR(shared), key, len, 0);
21312124 645 }
6b85e4fe 646 CALLER_CONTEXT;
21312124 647 if (svp) {
6b85e4fe 648 /* Exists in the array */
abdac9fa
DM
649 if (SvROK(*svp)) {
650 SV *obj = Nullsv;
651 Perl_sharedsv_associate(aTHX_ &obj, SvRV(*svp), NULL);
652 sv_setsv_nomg(sv, &PL_sv_undef);
b162af07 653 SvRV_set(sv, obj);
abdac9fa
DM
654 SvROK_on(sv);
655 SvSETMAGIC(sv);
656 }
657 else {
658 target = Perl_sharedsv_associate(aTHX_ &sv, *svp, target);
659 sv_setsv(sv, *svp);
660 }
21312124 661 }
6b85e4fe
NIS
662 else {
663 /* Not in the array */
664 sv_setsv(sv, &PL_sv_undef);
68795e93 665 }
6b85e4fe 666 LEAVE_LOCK;
21312124 667 return 0;
68795e93
NIS
668}
669
21312124
NIS
670int
671sharedsv_elem_mg_STORE(pTHX_ SV *sv, MAGIC *mg)
672{
673 dTHXc;
6b85e4fe 674 shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
a446a88f 675 shared_sv *target;
6b85e4fe 676 SV **svp;
21312124
NIS
677 /* Theory - SV itself is magically shared - and we have ordered the
678 magic such that by the time we get here it has been stored
679 to its shared counterpart
680 */
6d56dc1c
NIS
681 ENTER_LOCK;
682 assert(shared);
683 assert(SHAREDSvPTR(shared));
21312124 684 if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
6b85e4fe
NIS
685 assert ( mg->mg_ptr == 0 );
686 SHARED_CONTEXT;
687 svp = av_fetch((AV*) SHAREDSvPTR(shared), mg->mg_len, 1);
21312124
NIS
688 }
689 else {
6b85e4fe
NIS
690 char *key = mg->mg_ptr;
691 STRLEN len = mg->mg_len;
692 assert ( mg->mg_ptr != 0 );
693 if (mg->mg_len == HEf_SVKEY)
694 key = SvPV((SV *) mg->mg_ptr, len);
695 SHARED_CONTEXT;
696 svp = hv_fetch((HV*) SHAREDSvPTR(shared), key, len, 1);
21312124 697 }
6b85e4fe
NIS
698 CALLER_CONTEXT;
699 target = Perl_sharedsv_associate(aTHX_ &sv, *svp, 0);
700 sharedsv_scalar_store(aTHX_ sv, target);
701 LEAVE_LOCK;
21312124
NIS
702 return 0;
703}
68795e93 704
21312124
NIS
705int
706sharedsv_elem_mg_DELETE(pTHX_ SV *sv, MAGIC *mg)
68795e93 707{
21312124 708 dTHXc;
057e91b3 709 MAGIC *shmg;
6b85e4fe 710 shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
6b85e4fe
NIS
711 ENTER_LOCK;
712 sharedsv_elem_mg_FETCH(aTHX_ sv, mg);
057e91b3
DM
713 if ((shmg = mg_find(sv, PERL_MAGIC_shared_scalar)))
714 sharedsv_scalar_mg_get(aTHX_ sv, shmg);
21312124 715 if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
6b85e4fe
NIS
716 SHARED_CONTEXT;
717 av_delete((AV*) SHAREDSvPTR(shared), mg->mg_len, G_DISCARD);
68795e93 718 }
21312124 719 else {
6b85e4fe
NIS
720 char *key = mg->mg_ptr;
721 STRLEN len = mg->mg_len;
722 assert ( mg->mg_ptr != 0 );
723 if (mg->mg_len == HEf_SVKEY)
724 key = SvPV((SV *) mg->mg_ptr, len);
725 SHARED_CONTEXT;
726 hv_delete((HV*) SHAREDSvPTR(shared), key, len, G_DISCARD);
21312124 727 }
6b85e4fe
NIS
728 CALLER_CONTEXT;
729 LEAVE_LOCK;
21312124
NIS
730 return 0;
731}
732
21312124
NIS
733int
734sharedsv_elem_mg_free(pTHX_ SV *sv, MAGIC *mg)
735{
6b85e4fe 736 Perl_sharedsv_free(aTHX_ SV_to_sharedsv(aTHX_ mg->mg_obj));
21312124
NIS
737 return 0;
738}
739
740int
741sharedsv_elem_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
742{
6b85e4fe 743 shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
21312124
NIS
744 SvREFCNT_inc(SHAREDSvPTR(shared));
745 mg->mg_flags |= MGf_DUP;
746 return 0;
747}
748
749MGVTBL sharedsv_elem_vtbl = {
750 sharedsv_elem_mg_FETCH, /* get */
751 sharedsv_elem_mg_STORE, /* set */
752 0, /* len */
753 sharedsv_elem_mg_DELETE, /* clear */
754 sharedsv_elem_mg_free, /* free */
755 0, /* copy */
756 sharedsv_elem_mg_dup /* dup */
757};
758
759U32
760sharedsv_array_mg_FETCHSIZE(pTHX_ SV *sv, MAGIC *mg)
761{
762 dTHXc;
763 shared_sv *shared = (shared_sv *) mg->mg_ptr;
764 U32 val;
765 SHARED_EDIT;
766 if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
767 val = av_len((AV*) SHAREDSvPTR(shared));
768 }
769 else {
770 /* not actually defined by tie API but ... */
771 val = HvKEYS((HV*) SHAREDSvPTR(shared));
772 }
773 SHARED_RELEASE;
774 return val;
775}
776
777int
778sharedsv_array_mg_CLEAR(pTHX_ SV *sv, MAGIC *mg)
779{
780 dTHXc;
781 shared_sv *shared = (shared_sv *) mg->mg_ptr;
782 SHARED_EDIT;
783 if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
784 av_clear((AV*) SHAREDSvPTR(shared));
785 }
786 else {
787 hv_clear((HV*) SHAREDSvPTR(shared));
788 }
789 SHARED_RELEASE;
790 return 0;
791}
792
793int
794sharedsv_array_mg_free(pTHX_ SV *sv, MAGIC *mg)
795{
796 Perl_sharedsv_free(aTHX_ (shared_sv *) mg->mg_ptr);
797 return 0;
68795e93
NIS
798}
799
800/*
21312124
NIS
801 * This is called when perl is about to access an element of
802 * the array -
803 */
804int
805sharedsv_array_mg_copy(pTHX_ SV *sv, MAGIC* mg,
806 SV *nsv, const char *name, int namlen)
807{
808 shared_sv *shared = (shared_sv *) mg->mg_ptr;
809 MAGIC *nmg = sv_magicext(nsv,mg->mg_obj,
810 toLOWER(mg->mg_type),&sharedsv_elem_vtbl,
811 name, namlen);
b747d46a 812 ENTER_LOCK;
a446a88f 813 SvREFCNT_inc(SHAREDSvPTR(shared));
b747d46a 814 LEAVE_LOCK;
21312124 815 nmg->mg_flags |= MGf_DUP;
21312124
NIS
816 return 1;
817}
818
819int
820sharedsv_array_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
821{
822 shared_sv *shared = (shared_sv *) mg->mg_ptr;
823 SvREFCNT_inc(SHAREDSvPTR(shared));
824 mg->mg_flags |= MGf_DUP;
825 return 0;
826}
827
828MGVTBL sharedsv_array_vtbl = {
829 0, /* get */
830 0, /* set */
831 sharedsv_array_mg_FETCHSIZE, /* len */
832 sharedsv_array_mg_CLEAR, /* clear */
833 sharedsv_array_mg_free, /* free */
834 sharedsv_array_mg_copy, /* copy */
835 sharedsv_array_mg_dup /* dup */
836};
837
838=for apidoc sharedsv_unlock
68795e93
NIS
839
840Recursively unlocks a shared sv.
841
21312124 842=cut
68795e93
NIS
843
844void
845Perl_sharedsv_unlock(pTHX_ shared_sv* ssv)
846{
6d56dc1c 847 recursive_lock_release(aTHX_ &ssv->lock);
68795e93
NIS
848}
849
21312124 850=for apidoc sharedsv_lock
68795e93 851
21312124
NIS
852Recursive locks on a sharedsv.
853Locks are dynamically scoped at the level of the first lock.
68795e93 854
21312124 855=cut
68795e93
NIS
856
857void
21312124 858Perl_sharedsv_lock(pTHX_ shared_sv* ssv)
68795e93 859{
21312124
NIS
860 if (!ssv)
861 return;
6b85e4fe 862 recursive_lock_acquire(aTHX_ &ssv->lock, __FILE__, __LINE__);
68795e93
NIS
863}
864
afe38520
DM
865/* handles calls from lock() builtin via PL_lockhook */
866
21312124
NIS
867void
868Perl_sharedsv_locksv(pTHX_ SV *sv)
869{
afe38520
DM
870 shared_sv* shared;
871
872 if(SvROK(sv))
873 sv = SvRV(sv);
38875929 874 shared = Perl_sharedsv_find(aTHX_ sv);
afe38520
DM
875 if(!shared)
876 croak("lock can only be used on shared values");
877 Perl_sharedsv_lock(aTHX_ shared);
b050c948
AB
878}
879
21312124 880=head1 Shared SV Functions
b050c948 881
21312124 882=for apidoc sharedsv_init
b050c948 883
21312124 884Saves a space for keeping SVs wider than an interpreter,
b050c948 885
21312124
NIS
886=cut
887
888void
889Perl_sharedsv_init(pTHX)
890{
891 dTHXc;
892 /* This pair leaves us in shared context ... */
893 PL_sharedsv_space = perl_alloc();
894 perl_construct(PL_sharedsv_space);
895 CALLER_CONTEXT;
6d56dc1c 896 recursive_lock_init(aTHX_ &PL_sharedsv_lock);
21312124
NIS
897 PL_lockhook = &Perl_sharedsv_locksv;
898 PL_sharehook = &Perl_sharedsv_share;
b050c948
AB
899}
900
73e09c8f
JH
901#endif /* USE_ITHREADS */
902
21312124 903MODULE = threads::shared PACKAGE = threads::shared::tie
b050c948 904
21312124 905PROTOTYPES: DISABLE
b050c948 906
73e09c8f 907#ifdef USE_ITHREADS
6b85e4fe 908
21312124
NIS
909void
910PUSH(shared_sv *shared, ...)
911CODE:
912 dTHXc;
913 int i;
21312124
NIS
914 for(i = 1; i < items; i++) {
915 SV* tmp = newSVsv(ST(i));
a446a88f 916 shared_sv *target;
6d56dc1c 917 ENTER_LOCK;
a446a88f 918 target = Perl_sharedsv_associate(aTHX_ &tmp, Nullsv, 0);
6b85e4fe 919 sharedsv_scalar_store(aTHX_ tmp, target);
21312124
NIS
920 SHARED_CONTEXT;
921 av_push((AV*) SHAREDSvPTR(shared), SHAREDSvPTR(target));
b0cd0593 922 SvREFCNT_inc(SHAREDSvPTR(target));
a446a88f 923 SHARED_RELEASE;
21312124
NIS
924 SvREFCNT_dec(tmp);
925 }
b050c948 926
21312124
NIS
927void
928UNSHIFT(shared_sv *shared, ...)
929CODE:
930 dTHXc;
931 int i;
6d56dc1c 932 ENTER_LOCK;
21312124
NIS
933 SHARED_CONTEXT;
934 av_unshift((AV*)SHAREDSvPTR(shared), items - 1);
935 CALLER_CONTEXT;
936 for(i = 1; i < items; i++) {
937 SV* tmp = newSVsv(ST(i));
938 shared_sv *target = Perl_sharedsv_associate(aTHX_ &tmp, Nullsv, 0);
6b85e4fe 939 sharedsv_scalar_store(aTHX_ tmp, target);
21312124
NIS
940 SHARED_CONTEXT;
941 av_store((AV*) SHAREDSvPTR(shared), i - 1, SHAREDSvPTR(target));
b0cd0593 942 SvREFCNT_inc(SHAREDSvPTR(target));
21312124
NIS
943 CALLER_CONTEXT;
944 SvREFCNT_dec(tmp);
945 }
6d56dc1c 946 LEAVE_LOCK;
b050c948 947
21312124
NIS
948void
949POP(shared_sv *shared)
950CODE:
951 dTHXc;
952 SV* sv;
6d56dc1c 953 ENTER_LOCK;
21312124
NIS
954 SHARED_CONTEXT;
955 sv = av_pop((AV*)SHAREDSvPTR(shared));
956 CALLER_CONTEXT;
9b018978 957 ST(0) = sv_newmortal();
21312124 958 Perl_sharedsv_associate(aTHX_ &ST(0), sv, 0);
9b018978 959 SvREFCNT_dec(sv);
6d56dc1c 960 LEAVE_LOCK;
21312124 961 XSRETURN(1);
b050c948 962
21312124
NIS
963void
964SHIFT(shared_sv *shared)
965CODE:
966 dTHXc;
967 SV* sv;
6d56dc1c 968 ENTER_LOCK;
21312124
NIS
969 SHARED_CONTEXT;
970 sv = av_shift((AV*)SHAREDSvPTR(shared));
971 CALLER_CONTEXT;
9b018978 972 ST(0) = sv_newmortal();
21312124 973 Perl_sharedsv_associate(aTHX_ &ST(0), sv, 0);
9b018978 974 SvREFCNT_dec(sv);
6d56dc1c 975 LEAVE_LOCK;
21312124 976 XSRETURN(1);
b050c948 977
21312124
NIS
978void
979EXTEND(shared_sv *shared, IV count)
980CODE:
981 dTHXc;
982 SHARED_EDIT;
983 av_extend((AV*)SHAREDSvPTR(shared), count);
984 SHARED_RELEASE;
b050c948 985
21312124 986void
6b85e4fe
NIS
987STORESIZE(shared_sv *shared,IV count)
988CODE:
989 dTHXc;
990 SHARED_EDIT;
991 av_fill((AV*) SHAREDSvPTR(shared), count);
992 SHARED_RELEASE;
993
994
995
996
997void
21312124
NIS
998EXISTS(shared_sv *shared, SV *index)
999CODE:
1000 dTHXc;
1001 bool exists;
21312124 1002 if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
680c9d89 1003 SHARED_EDIT;
21312124
NIS
1004 exists = av_exists((AV*) SHAREDSvPTR(shared), SvIV(index));
1005 }
1006 else {
6b85e4fe
NIS
1007 STRLEN len;
1008 char *key = SvPV(index,len);
680c9d89 1009 SHARED_EDIT;
6b85e4fe 1010 exists = hv_exists((HV*) SHAREDSvPTR(shared), key, len);
21312124
NIS
1011 }
1012 SHARED_RELEASE;
1013 ST(0) = (exists) ? &PL_sv_yes : &PL_sv_no;
1014 XSRETURN(1);
b050c948
AB
1015
1016
1017void
21312124
NIS
1018FIRSTKEY(shared_sv *shared)
1019CODE:
1020 dTHXc;
1021 char* key = NULL;
1022 I32 len = 0;
1023 HE* entry;
6d56dc1c 1024 ENTER_LOCK;
21312124
NIS
1025 SHARED_CONTEXT;
1026 hv_iterinit((HV*) SHAREDSvPTR(shared));
1027 entry = hv_iternext((HV*) SHAREDSvPTR(shared));
1028 if (entry) {
1029 key = hv_iterkey(entry,&len);
1030 CALLER_CONTEXT;
1031 ST(0) = sv_2mortal(newSVpv(key, len));
1032 } else {
1033 CALLER_CONTEXT;
1034 ST(0) = &PL_sv_undef;
1035 }
6d56dc1c 1036 LEAVE_LOCK;
21312124 1037 XSRETURN(1);
b050c948 1038
866fba46 1039void
21312124
NIS
1040NEXTKEY(shared_sv *shared, SV *oldkey)
1041CODE:
1042 dTHXc;
1043 char* key = NULL;
1044 I32 len = 0;
1045 HE* entry;
6d56dc1c 1046 ENTER_LOCK;
21312124
NIS
1047 SHARED_CONTEXT;
1048 entry = hv_iternext((HV*) SHAREDSvPTR(shared));
6b85e4fe 1049 if (entry) {
21312124
NIS
1050 key = hv_iterkey(entry,&len);
1051 CALLER_CONTEXT;
1052 ST(0) = sv_2mortal(newSVpv(key, len));
1053 } else {
1054 CALLER_CONTEXT;
1055 ST(0) = &PL_sv_undef;
1056 }
6d56dc1c 1057 LEAVE_LOCK;
21312124
NIS
1058 XSRETURN(1);
1059
1060MODULE = threads::shared PACKAGE = threads::shared
1061
1062PROTOTYPES: ENABLE
866fba46 1063
68795e93 1064void
9c4972d9
NIS
1065_id(SV *ref)
1066 PROTOTYPE: \[$@%]
1067CODE:
1068 shared_sv *shared;
afe38520 1069 ref = SvRV(ref);
9c4972d9
NIS
1070 if(SvROK(ref))
1071 ref = SvRV(ref);
436c6dd3 1072 if( (shared = Perl_sharedsv_find(aTHX_ ref)) ){
9c4972d9
NIS
1073 ST(0) = sv_2mortal(newSViv(PTR2IV(shared)));
1074 XSRETURN(1);
1075 }
1076 XSRETURN_UNDEF;
1077
1078
1079void
6b85e4fe 1080_refcnt(SV *ref)
a446a88f
NIS
1081 PROTOTYPE: \[$@%]
1082CODE:
1083 shared_sv *shared;
afe38520 1084 ref = SvRV(ref);
a446a88f
NIS
1085 if(SvROK(ref))
1086 ref = SvRV(ref);
436c6dd3 1087 if( (shared = Perl_sharedsv_find(aTHX_ ref)) ){
a446a88f
NIS
1088 if (SHAREDSvPTR(shared)) {
1089 ST(0) = sv_2mortal(newSViv(SvREFCNT(SHAREDSvPTR(shared))));
1090 XSRETURN(1);
1091 }
1092 else {
436c6dd3 1093 Perl_warn(aTHX_ "%" SVf " s=%p has no shared SV",ST(0),shared);
a446a88f
NIS
1094 }
1095 }
1096 else {
436c6dd3 1097 Perl_warn(aTHX_ "%" SVf " is not shared",ST(0));
a446a88f
NIS
1098 }
1099 XSRETURN_UNDEF;
1100
caf25f3b 1101SV*
a446a88f
NIS
1102share(SV *ref)
1103 PROTOTYPE: \[$@%]
1104 CODE:
56fcff86
AB
1105 if(!SvROK(ref))
1106 Perl_croak(aTHX_ "Argument to share needs to be passed as ref");
afe38520 1107 ref = SvRV(ref);
a446a88f
NIS
1108 if(SvROK(ref))
1109 ref = SvRV(ref);
38875929 1110 Perl_sharedsv_share(aTHX_ ref);
caf25f3b
AB
1111 RETVAL = newRV(ref);
1112 OUTPUT:
1113 RETVAL
a446a88f
NIS
1114
1115void
21312124 1116lock_enabled(SV *ref)
ce127893 1117 PROTOTYPE: \[$@%]
6f942b98
AB
1118 CODE:
1119 shared_sv* shared;
56fcff86
AB
1120 if(!SvROK(ref))
1121 Perl_croak(aTHX_ "Argument to lock needs to be passed as ref");
afe38520 1122 ref = SvRV(ref);
6f942b98
AB
1123 if(SvROK(ref))
1124 ref = SvRV(ref);
38875929 1125 shared = Perl_sharedsv_find(aTHX_ ref);
21312124
NIS
1126 if(!shared)
1127 croak("lock can only be used on shared values");
1128 Perl_sharedsv_lock(aTHX_ shared);
6f942b98
AB
1129
1130void
a0e036c1
MP
1131cond_wait_enabled(SV *ref_cond, SV *ref_lock = 0)
1132 PROTOTYPE: \[$@%];\[$@%]
1133 PREINIT:
6f942b98 1134 shared_sv* shared;
a0e036c1 1135 perl_cond* user_condition;
6f942b98 1136 int locks;
a0e036c1
MP
1137 int same = 0;
1138
1139 CODE:
1140 if (!ref_lock || ref_lock == ref_cond) same = 1;
1141
1142 if(!SvROK(ref_cond))
56fcff86 1143 Perl_croak(aTHX_ "Argument to cond_wait needs to be passed as ref");
a0e036c1
MP
1144 ref_cond = SvRV(ref_cond);
1145 if(SvROK(ref_cond))
1146 ref_cond = SvRV(ref_cond);
1147 shared = Perl_sharedsv_find(aTHX_ ref_cond);
6f942b98
AB
1148 if(!shared)
1149 croak("cond_wait can only be used on shared values");
a0e036c1
MP
1150
1151 user_condition = &shared->user_cond;
1152 if (! same) {
1153 if (!SvROK(ref_lock))
1154 Perl_croak(aTHX_ "cond_wait lock needs to be passed as ref");
1155 ref_lock = SvRV(ref_lock);
1156 if (SvROK(ref_lock)) ref_lock = SvRV(ref_lock);
1157 shared = Perl_sharedsv_find(aTHX_ ref_lock);
1158 if (!shared)
1159 croak("cond_wait lock must be a shared value");
1160 }
6d56dc1c 1161 if(shared->lock.owner != aTHX)
6f942b98 1162 croak("You need a lock before you can cond_wait");
6d56dc1c
NIS
1163 /* Stealing the members of the lock object worries me - NI-S */
1164 MUTEX_LOCK(&shared->lock.mutex);
1165 shared->lock.owner = NULL;
39f33d92
AB
1166 locks = shared->lock.locks;
1167 shared->lock.locks = 0;
89661126
AB
1168
1169 /* since we are releasing the lock here we need to tell other
1170 people that is ok to go ahead and use it */
1171 COND_SIGNAL(&shared->lock.cond);
a0e036c1 1172 COND_WAIT(user_condition, &shared->lock.mutex);
89661126 1173 while(shared->lock.owner != NULL) {
a0e036c1
MP
1174 /* OK -- must reacquire the lock */
1175 COND_WAIT(&shared->lock.cond, &shared->lock.mutex);
1176 }
1177 shared->lock.owner = aTHX;
1178 shared->lock.locks = locks;
1179 MUTEX_UNLOCK(&shared->lock.mutex);
1180
1181int
1182cond_timedwait_enabled(SV *ref_cond, double abs, SV *ref_lock = 0)
1183 PROTOTYPE: \[$@%]$;\[$@%]
1184 PREINIT:
1185 shared_sv* shared;
1186 perl_cond* user_condition;
1187 int locks;
1188 int same = 0;
1189
1190 CODE:
1191 if (!ref_lock || ref_cond == ref_lock) same = 1;
1192
1193 if(!SvROK(ref_cond))
1194 Perl_croak(aTHX_ "Argument to cond_timedwait needs to be passed as ref");
1195 ref_cond = SvRV(ref_cond);
1196 if(SvROK(ref_cond))
1197 ref_cond = SvRV(ref_cond);
1198 shared = Perl_sharedsv_find(aTHX_ ref_cond);
1199 if(!shared)
1200 croak("cond_timedwait can only be used on shared values");
1201
1202 user_condition = &shared->user_cond;
1203 if (! same) {
1204 if (!SvROK(ref_lock))
1205 Perl_croak(aTHX_ "cond_timedwait lock needs to be passed as ref");
1206 ref_lock = SvRV(ref_lock);
1207 if (SvROK(ref_lock)) ref_lock = SvRV(ref_lock);
1208 shared = Perl_sharedsv_find(aTHX_ ref_lock);
1209 if (!shared)
1210 croak("cond_timedwait lock must be a shared value");
1211 }
1212 if(shared->lock.owner != aTHX)
1213 croak("You need a lock before you can cond_wait");
1214
1215 MUTEX_LOCK(&shared->lock.mutex);
1216 shared->lock.owner = NULL;
1217 locks = shared->lock.locks;
1218 shared->lock.locks = 0;
1219 /* since we are releasing the lock here we need to tell other
1220 people that is ok to go ahead and use it */
1221 COND_SIGNAL(&shared->lock.cond);
1222 RETVAL = Perl_sharedsv_cond_timedwait(user_condition, &shared->lock.mutex, abs);
1223 while (shared->lock.owner != NULL) {
1224 /* OK -- must reacquire the lock... */
1225 COND_WAIT(&shared->lock.cond, &shared->lock.mutex);
1226 }
6d56dc1c
NIS
1227 shared->lock.owner = aTHX;
1228 shared->lock.locks = locks;
1229 MUTEX_UNLOCK(&shared->lock.mutex);
6f942b98 1230
a0e036c1
MP
1231 if (RETVAL == 0)
1232 XSRETURN_UNDEF;
1233 OUTPUT:
1234 RETVAL
1235
21312124
NIS
1236void
1237cond_signal_enabled(SV *ref)
ce127893 1238 PROTOTYPE: \[$@%]
6f942b98
AB
1239 CODE:
1240 shared_sv* shared;
56fcff86
AB
1241 if(!SvROK(ref))
1242 Perl_croak(aTHX_ "Argument to cond_signal needs to be passed as ref");
afe38520 1243 ref = SvRV(ref);
6f942b98
AB
1244 if(SvROK(ref))
1245 ref = SvRV(ref);
1246 shared = Perl_sharedsv_find(aTHX_ ref);
10532ef0
DM
1247 if(!shared)
1248 croak("cond_signal can only be used on shared values");
38875929
DM
1249 if (ckWARN(WARN_THREADS) && shared->lock.owner != aTHX)
1250 Perl_warner(aTHX_ packWARN(WARN_THREADS),
1251 "cond_signal() called on unlocked variable");
6f942b98
AB
1252 COND_SIGNAL(&shared->user_cond);
1253
21312124
NIS
1254void
1255cond_broadcast_enabled(SV *ref)
ce127893 1256 PROTOTYPE: \[$@%]
6f942b98
AB
1257 CODE:
1258 shared_sv* shared;
56fcff86
AB
1259 if(!SvROK(ref))
1260 Perl_croak(aTHX_ "Argument to cond_broadcast needs to be passed as ref");
afe38520 1261 ref = SvRV(ref);
6f942b98
AB
1262 if(SvROK(ref))
1263 ref = SvRV(ref);
1264 shared = Perl_sharedsv_find(aTHX_ ref);
1265 if(!shared)
1266 croak("cond_broadcast can only be used on shared values");
38875929
DM
1267 if (ckWARN(WARN_THREADS) && shared->lock.owner != aTHX)
1268 Perl_warner(aTHX_ packWARN(WARN_THREADS),
1269 "cond_broadcast() called on unlocked variable");
6f942b98 1270 COND_BROADCAST(&shared->user_cond);
b050c948 1271
5c360ac5
AB
1272
1273SV*
1274bless(SV* ref, ...);
1275 PROTOTYPE: $;$
1276 CODE:
1277 {
1278 HV* stash;
1279 shared_sv* shared;
1280 if (items == 1)
1281 stash = CopSTASH(PL_curcop);
1282 else {
1283 SV* ssv = ST(1);
1284 STRLEN len;
1285 char *ptr;
1286
1287 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
1288 Perl_croak(aTHX_ "Attempt to bless into a reference");
1289 ptr = SvPV(ssv,len);
1290 if (ckWARN(WARN_MISC) && len == 0)
1291 Perl_warner(aTHX_ packWARN(WARN_MISC),
1292 "Explicit blessing to '' (assuming package main)");
1293 stash = gv_stashpvn(ptr, len, TRUE);
1294 }
1295 SvREFCNT_inc(ref);
1296 (void)sv_bless(ref, stash);
1297 RETVAL = ref;
1298 shared = Perl_sharedsv_find(aTHX_ ref);
1299 if(shared) {
1300 dTHXc;
1301 ENTER_LOCK;
1302 SHARED_CONTEXT;
1303 {
bfcb3514 1304 SV* fake_stash = newSVpv(HvNAME_get(stash),0);
5c360ac5
AB
1305 (void)sv_bless(SHAREDSvPTR(shared),(HV*)fake_stash);
1306 }
1307 CALLER_CONTEXT;
1308 LEAVE_LOCK;
1309 }
1310 }
1311 OUTPUT:
1312 RETVAL
1313
73e09c8f
JH
1314#endif /* USE_ITHREADS */
1315
68795e93
NIS
1316BOOT:
1317{
73e09c8f 1318#ifdef USE_ITHREADS
68795e93 1319 Perl_sharedsv_init(aTHX);
73e09c8f 1320#endif /* USE_ITHREADS */
68795e93 1321}
73e09c8f
JH
1322
1323
1324