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
23 PerlInterpreter* sharedsv_space;
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 sharedsv_space = PERL_GET_CONTEXT;
51 =for apidoc sharedsv_new
53 Allocates a new shared sv struct, you must yourself create the SV/AV/HV.
58 Perl_sharedsv_new(pTHX)
61 New(2555,ssv,1,shared_sv);
62 MUTEX_INIT(&ssv->mutex);
63 COND_INIT(&ssv->cond);
70 =for apidoc sharedsv_find
72 Tries to find if a given SV has a shared backend, either by
73 looking at magic, or by checking if it is tied again threads::shared.
79 Perl_sharedsv_find(pTHX_ SV* sv)
81 /* does all it can to find a shared_sv struct, returns NULL otherwise */
82 shared_sv* ssv = NULL;
87 =for apidoc sharedsv_lock
89 Recursive locks on a sharedsv.
90 Locks are dynamicly scoped at the level of the first lock.
94 Perl_sharedsv_lock(pTHX_ shared_sv* ssv)
98 if(ssv->owner && ssv->owner == my_perl) {
102 MUTEX_LOCK(&ssv->mutex);
104 ssv->owner = my_perl;
106 SAVEDESTRUCTOR_X(Perl_sharedsv_unlock_scope,ssv);
110 =for apidoc sharedsv_unlock
112 Recursively unlocks a shared sv.
118 Perl_sharedsv_unlock(pTHX_ shared_sv* ssv)
120 if(ssv->owner != my_perl)
123 if(--ssv->locks == 0) {
125 MUTEX_UNLOCK(&ssv->mutex);
130 Perl_sharedsv_unlock_scope(pTHX_ shared_sv* ssv)
132 if(ssv->owner != my_perl)
136 MUTEX_UNLOCK(&ssv->mutex);
140 =for apidoc sharedsv_thrcnt_inc
142 Increments the threadcount of a sharedsv.
146 Perl_sharedsv_thrcnt_inc(pTHX_ shared_sv* ssv)
149 SvREFCNT_inc(ssv->sv);
154 =for apidoc sharedsv_thrcnt_dec
156 Decrements the threadcount of a shared sv. When a threads frontend is freed
157 this function should be called.
163 Perl_sharedsv_thrcnt_dec(pTHX_ shared_sv* ssv)
168 sv = SHAREDSvGET(ssv);
169 if (SvREFCNT(sv) == 1) {
170 switch (SvTYPE(sv)) {
173 Perl_sharedsv_thrcnt_dec(aTHX_ (shared_sv *)SvIV(SvRV(sv)));
176 SV **src_ary = AvARRAY((AV *)sv);
177 SSize_t items = AvFILLp((AV *)sv) + 1;
179 while (items-- > 0) {
181 Perl_sharedsv_thrcnt_dec(aTHX_ (shared_sv *)SvIV(*src_ary++));
187 (void)hv_iterinit((HV *)sv);
188 while ((entry = hv_iternext((HV *)sv)))
189 Perl_sharedsv_thrcnt_dec(
190 aTHX_ (shared_sv *)SvIV(hv_iterval((HV *)sv, entry))
197 SHAREDSvRELEASE(ssv);