3 * Copyright (c) 2001, Larry Wall
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.
11 * Contributed by Arthur Bergman arthur@contiller.se
13 * "Hand any two wizards a piece of rope and they would instinctively pull in
14 * opposite directions."
20 #define PERL_IN_SHAREDSV_C
30 Shared SV is a structure for keeping the backend storage
36 =for apidoc sharedsv_init
38 Saves a space for keeping SVs wider than an interpreter,
39 currently only stores a pointer to the first interpreter.
45 Perl_sharedsv_init(pTHX)
47 PL_sharedsv_space = PERL_GET_CONTEXT;
48 MUTEX_INIT(&PL_sharedsv_space_mutex);
52 =for apidoc sharedsv_new
54 Allocates a new shared sv struct, you must yourself create the SV/AV/HV.
59 Perl_sharedsv_new(pTHX)
62 New(2555,ssv,1,shared_sv);
63 MUTEX_INIT(&ssv->mutex);
64 COND_INIT(&ssv->cond);
65 COND_INIT(&ssv->user_cond);
73 =for apidoc sharedsv_find
75 Tries to find if a given SV has a shared backend, either by
76 looking at magic, or by checking if it is tied again threads::shared.
82 Perl_sharedsv_find(pTHX_ SV* sv)
84 /* does all it can to find a shared_sv struct, returns NULL otherwise */
85 shared_sv* ssv = NULL;
90 =for apidoc sharedsv_lock
92 Recursive locks on a sharedsv.
93 Locks are dynamicly scoped at the level of the first lock.
97 Perl_sharedsv_lock(pTHX_ shared_sv* ssv)
101 MUTEX_LOCK(&ssv->mutex);
102 if(ssv->owner && ssv->owner == my_perl) {
104 MUTEX_UNLOCK(&ssv->mutex);
108 COND_WAIT(&ssv->cond,&ssv->mutex);
110 ssv->owner = my_perl;
112 SAVEDESTRUCTOR_X(Perl_sharedsv_unlock_scope,ssv);
113 MUTEX_UNLOCK(&ssv->mutex);
117 =for apidoc sharedsv_unlock
119 Recursively unlocks a shared sv.
125 Perl_sharedsv_unlock(pTHX_ shared_sv* ssv)
127 MUTEX_LOCK(&ssv->mutex);
128 if(ssv->owner != my_perl) {
129 Perl_croak(aTHX_ "panic: Perl_sharedsv_unlock unlocking mutex that we don't own");
130 MUTEX_UNLOCK(&ssv->mutex);
134 if(--ssv->locks == 0) {
136 COND_SIGNAL(&ssv->cond);
138 MUTEX_UNLOCK(&ssv->mutex);
142 Perl_sharedsv_unlock_scope(pTHX_ shared_sv* ssv)
144 MUTEX_LOCK(&ssv->mutex);
145 if(ssv->owner != my_perl) {
146 MUTEX_UNLOCK(&ssv->mutex);
151 COND_SIGNAL(&ssv->cond);
152 MUTEX_UNLOCK(&ssv->mutex);
156 =for apidoc sharedsv_thrcnt_inc
158 Increments the threadcount of a sharedsv.
162 Perl_sharedsv_thrcnt_inc(pTHX_ shared_sv* ssv)
165 SvREFCNT_inc(ssv->sv);
166 SHAREDSvRELEASE(ssv);
170 =for apidoc sharedsv_thrcnt_dec
172 Decrements the threadcount of a shared sv. When a threads frontend is freed
173 this function should be called.
179 Perl_sharedsv_thrcnt_dec(pTHX_ shared_sv* ssv)
183 sv = SHAREDSvGET(ssv);
184 if (SvREFCNT(sv) == 1) {
185 switch (SvTYPE(sv)) {
188 Perl_sharedsv_thrcnt_dec(aTHX_ INT2PTR(shared_sv *, SvIV(SvRV(sv))));
191 SV **src_ary = AvARRAY((AV *)sv);
192 SSize_t items = AvFILLp((AV *)sv) + 1;
194 while (items-- > 0) {
196 Perl_sharedsv_thrcnt_dec(aTHX_ INT2PTR(shared_sv *, SvIV(*src_ary++)));
202 (void)hv_iterinit((HV *)sv);
203 while ((entry = hv_iternext((HV *)sv)))
204 Perl_sharedsv_thrcnt_dec(
205 aTHX_ INT2PTR(shared_sv *, SvIV(hv_iterval((HV *)sv, entry)))
212 SHAREDSvRELEASE(ssv);
215 #endif /* USE_ITHREADS */