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 PerlInterpreter* old_context = PERL_GET_CONTEXT;
48 PL_sharedsv_space = perl_alloc();
49 perl_construct(PL_sharedsv_space);
50 PERL_SET_CONTEXT(old_context);
51 MUTEX_INIT(&PL_sharedsv_space_mutex);
55 =for apidoc sharedsv_new
57 Allocates a new shared sv struct, you must yourself create the SV/AV/HV.
62 Perl_sharedsv_new(pTHX)
65 New(2555,ssv,1,shared_sv);
66 MUTEX_INIT(&ssv->mutex);
67 COND_INIT(&ssv->cond);
68 COND_INIT(&ssv->user_cond);
76 =for apidoc sharedsv_find
78 Tries to find if a given SV has a shared backend, either by
79 looking at magic, or by checking if it is tied again threads::shared.
85 Perl_sharedsv_find(pTHX_ SV* sv)
87 /* does all it can to find a shared_sv struct, returns NULL otherwise */
88 shared_sv* ssv = NULL;
93 =for apidoc sharedsv_lock
95 Recursive locks on a sharedsv.
96 Locks are dynamicly scoped at the level of the first lock.
100 Perl_sharedsv_lock(pTHX_ shared_sv* ssv)
104 MUTEX_LOCK(&ssv->mutex);
105 if(ssv->owner && ssv->owner == my_perl) {
107 MUTEX_UNLOCK(&ssv->mutex);
111 COND_WAIT(&ssv->cond,&ssv->mutex);
113 ssv->owner = my_perl;
115 SAVEDESTRUCTOR_X(Perl_sharedsv_unlock_scope,ssv);
116 MUTEX_UNLOCK(&ssv->mutex);
120 =for apidoc sharedsv_unlock
122 Recursively unlocks a shared sv.
128 Perl_sharedsv_unlock(pTHX_ shared_sv* ssv)
130 MUTEX_LOCK(&ssv->mutex);
131 if(ssv->owner != my_perl) {
132 Perl_croak(aTHX_ "panic: Perl_sharedsv_unlock unlocking mutex that we don't own");
133 MUTEX_UNLOCK(&ssv->mutex);
137 if(--ssv->locks == 0) {
139 COND_SIGNAL(&ssv->cond);
141 MUTEX_UNLOCK(&ssv->mutex);
145 Perl_sharedsv_unlock_scope(pTHX_ shared_sv* ssv)
147 MUTEX_LOCK(&ssv->mutex);
148 if(ssv->owner != my_perl) {
149 MUTEX_UNLOCK(&ssv->mutex);
154 COND_SIGNAL(&ssv->cond);
155 MUTEX_UNLOCK(&ssv->mutex);
159 =for apidoc sharedsv_thrcnt_inc
161 Increments the threadcount of a sharedsv.
165 Perl_sharedsv_thrcnt_inc(pTHX_ shared_sv* ssv)
168 SvREFCNT_inc(ssv->sv);
169 SHAREDSvRELEASE(ssv);
173 =for apidoc sharedsv_thrcnt_dec
175 Decrements the threadcount of a shared sv. When a threads frontend is freed
176 this function should be called.
182 Perl_sharedsv_thrcnt_dec(pTHX_ shared_sv* ssv)
186 sv = SHAREDSvGET(ssv);
187 if (SvREFCNT(sv) == 1) {
188 switch (SvTYPE(sv)) {
191 Perl_sharedsv_thrcnt_dec(aTHX_ INT2PTR(shared_sv *, SvIV(SvRV(sv))));
194 SV **src_ary = AvARRAY((AV *)sv);
195 SSize_t items = AvFILLp((AV *)sv) + 1;
197 while (items-- > 0) {
199 Perl_sharedsv_thrcnt_dec(aTHX_ INT2PTR(shared_sv *, SvIV(*src_ary++)));
205 (void)hv_iterinit((HV *)sv);
206 while ((entry = hv_iternext((HV *)sv)))
207 Perl_sharedsv_thrcnt_dec(
208 aTHX_ INT2PTR(shared_sv *, SvIV(hv_iterval((HV *)sv, entry)))
215 SHAREDSvRELEASE(ssv);
218 #endif /* USE_ITHREADS */