* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
- */
-
-/*
- *
* "Hand any two wizards a piece of rope and they would instinctively pull in
* opposite directions."
* --Sourcery
#include "perl.h"
#include "XSUB.h"
+#ifdef USE_ITHREADS
+
#define SHAREDSvPTR(a) ((a)->sv)
/*
/*
* Only one thread at a time is allowed to mess with shared space.
*/
-perl_mutex PL_sharedsv_space_mutex; /* Mutex protecting the shared sv space */
-PerlInterpreter *PL_shared_owner; /* For locking assertions */
-#define SHARED_LOCK STMT_START { \
- MUTEX_LOCK(&PL_sharedsv_space_mutex); \
- PL_shared_owner = aTHX; \
- } STMT_END
+typedef struct
+{
+ perl_mutex mutex;
+ PerlInterpreter *owner;
+ I32 locks;
+ perl_cond cond;
+#ifdef DEBUG_LOCKS
+ char * file;
+ int line;
+#endif
+} recursive_lock_t;
+
+recursive_lock_t PL_sharedsv_lock; /* Mutex protecting the shared sv space */
+
+void
+recursive_lock_init(pTHX_ recursive_lock_t *lock)
+{
+ Zero(lock,1,recursive_lock_t);
+ MUTEX_INIT(&lock->mutex);
+ COND_INIT(&lock->cond);
+}
+
+void
+recursive_lock_release(pTHX_ recursive_lock_t *lock)
+{
+ MUTEX_LOCK(&lock->mutex);
+ if (lock->owner != aTHX) {
+ MUTEX_UNLOCK(&lock->mutex);
+ }
+ else {
+ if (--lock->locks == 0) {
+ lock->owner = NULL;
+ COND_SIGNAL(&lock->cond);
+ }
+ }
+ MUTEX_UNLOCK(&lock->mutex);
+}
+
+void
+recursive_lock_acquire(pTHX_ recursive_lock_t *lock,char *file,int line)
+{
+ assert(aTHX);
+ MUTEX_LOCK(&lock->mutex);
+ if (lock->owner == aTHX) {
+ lock->locks++;
+ }
+ else {
+ while (lock->owner) {
+#ifdef DEBUG_LOCKS
+ Perl_warn(aTHX_ " %p waiting - owned by %p %s:%d\n",
+ aTHX, lock->owner, lock->file, lock->line);
+#endif
+ COND_WAIT(&lock->cond,&lock->mutex);
+ }
+ lock->locks = 1;
+ lock->owner = aTHX;
+#ifdef DEBUG_LOCKS
+ lock->file = file;
+ lock->line = line;
+#endif
+ }
+ MUTEX_UNLOCK(&lock->mutex);
+ SAVEDESTRUCTOR_X(recursive_lock_release,lock);
+}
-#define SHARED_UNLOCK STMT_START { \
- PL_shared_owner = NULL; \
- MUTEX_UNLOCK(&PL_sharedsv_space_mutex); \
+#define ENTER_LOCK STMT_START { \
+ ENTER; \
+ recursive_lock_acquire(aTHX_ &PL_sharedsv_lock, __FILE__, __LINE__); \
} STMT_END
+#define LEAVE_LOCK LEAVE
+
/* A common idiom is to acquire access and switch in ... */
#define SHARED_EDIT STMT_START { \
- SHARED_LOCK; \
+ ENTER_LOCK; \
SHARED_CONTEXT; \
} STMT_END
/* then switch out and release access. */
#define SHARED_RELEASE STMT_START { \
CALLER_CONTEXT; \
- SHARED_UNLOCK; \
+ LEAVE_LOCK; \
} STMT_END
-
+
/*
typedef struct {
SV *sv; /* The actual SV - in shared space */
- perl_mutex mutex; /* Our mutex */
- perl_cond cond; /* Our condition variable */
+ recursive_lock_t lock;
perl_cond user_cond; /* For user-level conditions */
- IV locks; /* Number of locks held */
- PerlInterpreter *owner; /* Who owns the lock? */
} shared_sv;
/* The SV in shared-space has a back-pointer to the shared_sv
sharedsv_shared_mg_free(pTHX_ SV *sv, MAGIC *mg)
{
shared_sv *shared = (shared_sv *) mg->mg_ptr;
+ assert( aTHX == PL_sharedsv_space );
if (shared) {
- PerlIO_debug(__FUNCTION__ "Free %p\n",shared);
PerlMemShared_free(shared);
mg->mg_ptr = NULL;
}
return 0;
}
-
MGVTBL sharedsv_shared_vtbl = {
0, /* get */
0, /* set */
the sharedsv_scalar_vtbl associates it with the value
*/
+
+/* Accessor to convert threads::shared::tie objects back shared_sv * */
+shared_sv *
+SV_to_sharedsv(pTHX_ SV *sv)
+{
+ shared_sv *shared = 0;
+ if (SvROK(sv))
+ {
+ shared = INT2PTR(shared_sv *, SvIV(SvRV(sv)));
+ }
+ return shared;
+}
+
=for apidoc sharedsv_find
-Given a private side SV tries to find if a given SV has a shared backend,
+Given a private side SV tries to find if the SV has a shared backend,
by looking for the magic.
=cut
}
break;
default:
+ /* This should work for elements as well as they
+ * have scalar magic as well as their element magic
+ */
if ((mg = mg_find(sv, PERL_MAGIC_shared_scalar))
&& mg->mg_virtual == &sharedsv_scalar_vtbl) {
return (shared_sv *) mg->mg_ptr;
- }
+ }
break;
}
}
+ /* Just for tidyness of API also handle tie objects */
+ if (SvROK(sv) && sv_derived_from(sv, "threads::shared::tie")) {
+ return SV_to_sharedsv(aTHX_ sv);
+ }
return NULL;
}
shared_sv *
Perl_sharedsv_associate(pTHX_ SV **psv, SV *ssv, shared_sv *data)
{
- /* First try and get global data structure */
dTHXc;
MAGIC *mg = 0;
- SV *sv;
+ SV *sv = (psv) ? *psv : Nullsv;
/* If we are asked for an private ops we need a thread */
assert ( aTHX != PL_sharedsv_space );
/* To avoid need for recursive locks require caller to hold lock */
- if ( PL_shared_owner != aTHX )
- abort();
- assert ( PL_shared_owner == aTHX );
+ assert ( PL_sharedsv_lock.owner == aTHX );
+
+ /* First try and get existing global data structure */
/* Try shared SV as 1st choice */
if (!data && ssv && SvTYPE(ssv) >= SVt_PVMG) {
data = (shared_sv *) mg->mg_ptr;
}
}
- /* Next try private SV */
- if (!data && psv && *psv) {
- data = Perl_sharedsv_find(aTHX,*psv);
+
+ /* Next see if private SV is associated with something */
+ if (!data && sv) {
+ data = Perl_sharedsv_find(aTHX_ sv);
}
+
/* If neither of those then create a new one */
if (!data) {
+ SHARED_CONTEXT;
+ if (!ssv)
+ ssv = newSV(0);
data = PerlMemShared_malloc(sizeof(shared_sv));
Zero(data,1,shared_sv);
- MUTEX_INIT(&data->mutex);
- COND_INIT(&data->cond);
+ SHAREDSvPTR(data) = ssv;
+ /* Tag shared side SV with data pointer */
+ sv_magicext(ssv, ssv, PERL_MAGIC_ext, &sharedsv_shared_vtbl,
+ (char *)data, 0);
+ recursive_lock_init(aTHX_ &data->lock);
COND_INIT(&data->user_cond);
- data->owner = 0;
- data->locks = 0;
+ CALLER_CONTEXT;
}
if (!ssv)
ssv = SHAREDSvPTR(data);
-
- /* If we know type allocate shared side SV */
- if (psv && *psv && !ssv) {
+ if (!SHAREDSvPTR(data))
+ SHAREDSvPTR(data) = ssv;
+
+ /* If we know type upgrade shared side SV */
+ if (sv && SvTYPE(ssv) < SvTYPE(sv)) {
SHARED_CONTEXT;
- ssv = newSV(0);
sv_upgrade(ssv, SvTYPE(*psv));
- /* Tag shared side SV with data pointer */
- sv_magicext(ssv, ssv, PERL_MAGIC_ext, &sharedsv_shared_vtbl,
- (char *)data, 0);
CALLER_CONTEXT;
}
- if (!SHAREDSvPTR(data))
- SHAREDSvPTR(data) = ssv;
-
/* Now if requested allocate private SV */
- if (psv && !*psv && ssv) {
- sv = newSV(0);
- sv_upgrade(sv, SvTYPE(SHAREDSvPTR(data)));
- *psv = sv;
+ if (psv && !sv) {
+ *psv = sv = newSV(0);
}
/* Finally if private SV exists check and add magic */
- if (psv && (sv = *psv)) {
+ if (sv) {
MAGIC *mg = 0;
+ if (SvTYPE(sv) < SvTYPE(ssv)) {
+ sv_upgrade(sv, SvTYPE(ssv));
+ }
switch(SvTYPE(sv)) {
case SVt_PVAV:
case SVt_PVHV:
if (!(mg = mg_find(sv, PERL_MAGIC_tied))
- || mg->mg_virtual != &sharedsv_array_vtbl) {
+ || mg->mg_virtual != &sharedsv_array_vtbl
+ || (shared_sv *) mg->mg_ptr != data) {
SV *obj = newSV(0);
sv_setref_iv(obj, "threads::shared::tie",PTR2IV(data));
- if (mg)
+ if (mg) {
sv_unmagic(sv, PERL_MAGIC_tied);
+ }
mg = sv_magicext(sv, obj, PERL_MAGIC_tied, &sharedsv_array_vtbl,
(char *) data, 0);
mg->mg_flags |= (MGf_COPY|MGf_DUP);
- SvREFCNT_inc(SHAREDSvPTR(data));
- PerlIO_debug(__FUNCTION__ " %p %d\n",data,SvREFCNT(SHAREDSvPTR(data)));
+ SvREFCNT_inc(ssv);
SvREFCNT_dec(obj);
}
break;
default:
- if (SvTYPE(sv) < SVt_PVMG || !(mg = mg_find(sv, PERL_MAGIC_shared_scalar)) ||
- mg->mg_virtual != &sharedsv_scalar_vtbl) {
- if (mg)
+ if ((SvTYPE(sv) < SVt_PVMG)
+ || !(mg = mg_find(sv, PERL_MAGIC_shared_scalar))
+ || mg->mg_virtual != &sharedsv_scalar_vtbl
+ || (shared_sv *) mg->mg_ptr != data) {
+ if (mg) {
sv_unmagic(sv, PERL_MAGIC_shared_scalar);
+ }
mg = sv_magicext(sv, Nullsv, PERL_MAGIC_shared_scalar,
&sharedsv_scalar_vtbl, (char *)data, 0);
mg->mg_flags |= (MGf_COPY|MGf_DUP);
- SvREFCNT_inc(SHAREDSvPTR(data));
- PerlIO_debug(__FUNCTION__ " %p %d\n",data,SvREFCNT(SHAREDSvPTR(data)));
+ SvREFCNT_inc(ssv);
}
break;
}
+ assert ( Perl_sharedsv_find(aTHX_ *psv) == data );
}
return data;
}
case SVt_PVCV:
Perl_croak(aTHX_ "Cannot share subs yet");
break;
-
+
default:
- SHARED_LOCK;
+ ENTER_LOCK;
Perl_sharedsv_associate(aTHX_ &sv, 0, 0);
- SHARED_UNLOCK;
+ LEAVE_LOCK;
SvSETMAGIC(sv);
break;
}
sharedsv_scalar_mg_get(pTHX_ SV *sv, MAGIC *mg)
{
shared_sv *shared = (shared_sv *) mg->mg_ptr;
+ assert(shared);
- SHARED_LOCK;
+ ENTER_LOCK;
if (SHAREDSvPTR(shared)) {
if (SvROK(SHAREDSvPTR(shared))) {
SV *obj = Nullsv;
sv_setsv_nomg(sv, SHAREDSvPTR(shared));
}
}
- SHARED_UNLOCK;
+ LEAVE_LOCK;
return 0;
}
-int
-sharedsv_scalar_mg_set(pTHX_ SV *sv, MAGIC *mg)
+void
+sharedsv_scalar_store(pTHX_ SV *sv, shared_sv *shared)
{
dTHXc;
- shared_sv *shared;
bool allowed = TRUE;
- SHARED_LOCK;
- shared = Perl_sharedsv_associate(aTHX_ &sv, Nullsv, (shared_sv *) mg->mg_ptr);
-
if (SvROK(sv)) {
shared_sv* target = Perl_sharedsv_find(aTHX_ SvRV(sv));
if (target) {
}
}
else {
+ SvTEMP_off(sv);
SHARED_CONTEXT;
sv_setsv_nomg(SHAREDSvPTR(shared), sv);
CALLER_CONTEXT;
}
- SHARED_RELEASE;
-
if (!allowed) {
Perl_croak(aTHX_ "Invalid value for shared scalar");
}
+}
+
+int
+sharedsv_scalar_mg_set(pTHX_ SV *sv, MAGIC *mg)
+{
+ shared_sv *shared;
+ ENTER_LOCK;
+ /* We call associate to potentially upgrade shared side SV */
+ shared = Perl_sharedsv_associate(aTHX_ &sv, Nullsv, (shared_sv *) mg->mg_ptr);
+ assert(shared);
+ sharedsv_scalar_store(aTHX_ sv, shared);
+ LEAVE_LOCK;
return 0;
}
sharedsv_scalar_mg_free(pTHX_ SV *sv, MAGIC *mg)
{
shared_sv *shared = (shared_sv *) mg->mg_ptr;
- PerlIO_debug(__FUNCTION__ " %p %d\n",shared,SvREFCNT(SHAREDSvPTR(shared))-1);
+#if 0
assert (SvREFCNT(SHAREDSvPTR(shared)) < 1000);
+#endif
Perl_sharedsv_free(aTHX_ shared);
return 0;
}
int
sharedsv_scalar_mg_clear(pTHX_ SV *sv, MAGIC *mg)
{
- shared_sv *shared = (shared_sv *) mg->mg_ptr;
- PerlIO_debug(__FUNCTION__ " %p %d\n",shared,SvREFCNT(SHAREDSvPTR(shared)));
return 0;
}
if (shared) {
SvREFCNT_inc(SHAREDSvPTR(shared));
}
- PerlIO_debug(__FUNCTION__ " %p %d\n",shared,SvREFCNT(SHAREDSvPTR(shared)));
return 0;
}
};
/* Now the arrays/hashes stuff */
-
int
sharedsv_elem_mg_FETCH(pTHX_ SV *sv, MAGIC *mg)
{
dTHXc;
- shared_sv *shared = Perl_sharedsv_find(aTHX_ mg->mg_obj);
+ shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
shared_sv *target = Perl_sharedsv_find(aTHX_ sv);
SV** svp;
assert ( shared );
assert ( SHAREDSvPTR(shared) );
- SHARED_EDIT;
+ ENTER_LOCK;
+
if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
assert ( mg->mg_ptr == 0 );
+ SHARED_CONTEXT;
svp = av_fetch((AV*) SHAREDSvPTR(shared), mg->mg_len, 0);
}
else {
+ char *key = mg->mg_ptr;
+ STRLEN len = mg->mg_len;
assert ( mg->mg_ptr != 0 );
- svp = hv_fetch((HV*) SHAREDSvPTR(shared), mg->mg_ptr, mg->mg_len, 0);
+ if (mg->mg_len == HEf_SVKEY) {
+ key = SvPV((SV *) mg->mg_ptr, len);
+ }
+ SHARED_CONTEXT;
+ svp = hv_fetch((HV*) SHAREDSvPTR(shared), key, len, 0);
}
-
+ CALLER_CONTEXT;
if (svp) {
- if (target) {
- if (SHAREDSvPTR(target) != *svp) {
- if (SHAREDSvPTR(target)) {
- PerlIO_debug(__FUNCTION__ " %p %d\n",shared,SvREFCNT(SHAREDSvPTR(shared)));
- SvREFCNT_dec(SHAREDSvPTR(target));
- }
- SHAREDSvPTR(target) = SvREFCNT_inc(*svp);
- }
- }
- else {
- CALLER_CONTEXT;
- Perl_sharedsv_associate(aTHX_ &sv, *svp, 0);
- SHARED_CONTEXT;
- }
+ /* Exists in the array */
+ target = Perl_sharedsv_associate(aTHX_ &sv, *svp, target);
+ sv_setsv(sv, *svp);
}
- else if (target) {
- if (SHAREDSvPTR(target)) {
- SvREFCNT_dec(SHAREDSvPTR(target));
- }
- SHAREDSvPTR(target) = Nullsv;
+ else {
+ /* Not in the array */
+ sv_setsv(sv, &PL_sv_undef);
}
- SHARED_RELEASE;
+ LEAVE_LOCK;
return 0;
}
sharedsv_elem_mg_STORE(pTHX_ SV *sv, MAGIC *mg)
{
dTHXc;
- shared_sv *shared = Perl_sharedsv_find(aTHX_ mg->mg_obj);
+ shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
shared_sv *target;
- SV *val;
+ SV **svp;
/* Theory - SV itself is magically shared - and we have ordered the
magic such that by the time we get here it has been stored
to its shared counterpart
*/
- SHARED_LOCK;
- target = Perl_sharedsv_associate(aTHX_ &sv, Nullsv, 0);
- SHARED_CONTEXT;
- val = SHAREDSvPTR(target);
+ ENTER_LOCK;
+ assert(shared);
+ assert(SHAREDSvPTR(shared));
if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
- av_store((AV*) SHAREDSvPTR(shared), mg->mg_len, SvREFCNT_inc(val));
+ assert ( mg->mg_ptr == 0 );
+ SHARED_CONTEXT;
+ svp = av_fetch((AV*) SHAREDSvPTR(shared), mg->mg_len, 1);
}
else {
- hv_store((HV*) SHAREDSvPTR(shared), mg->mg_ptr, mg->mg_len,
- SvREFCNT_inc(val), 0);
+ char *key = mg->mg_ptr;
+ STRLEN len = mg->mg_len;
+ assert ( mg->mg_ptr != 0 );
+ if (mg->mg_len == HEf_SVKEY)
+ key = SvPV((SV *) mg->mg_ptr, len);
+ SHARED_CONTEXT;
+ svp = hv_fetch((HV*) SHAREDSvPTR(shared), key, len, 1);
}
- SHARED_RELEASE;
+ CALLER_CONTEXT;
+ target = Perl_sharedsv_associate(aTHX_ &sv, *svp, 0);
+ sharedsv_scalar_store(aTHX_ sv, target);
+ LEAVE_LOCK;
return 0;
}
sharedsv_elem_mg_DELETE(pTHX_ SV *sv, MAGIC *mg)
{
dTHXc;
- shared_sv *shared = Perl_sharedsv_find(aTHX_ mg->mg_obj);
- SV* ssv;
- SHARED_EDIT;
+ shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
+ ENTER_LOCK;
+ sharedsv_elem_mg_FETCH(aTHX_ sv, mg);
if (SvTYPE(SHAREDSvPTR(shared)) == SVt_PVAV) {
- ssv = av_delete((AV*) SHAREDSvPTR(shared), mg->mg_len, 0);
+ SHARED_CONTEXT;
+ av_delete((AV*) SHAREDSvPTR(shared), mg->mg_len, G_DISCARD);
}
else {
- ssv = hv_delete((HV*) SHAREDSvPTR(shared), mg->mg_ptr, mg->mg_len, 0);
+ char *key = mg->mg_ptr;
+ STRLEN len = mg->mg_len;
+ assert ( mg->mg_ptr != 0 );
+ if (mg->mg_len == HEf_SVKEY)
+ key = SvPV((SV *) mg->mg_ptr, len);
+ SHARED_CONTEXT;
+ hv_delete((HV*) SHAREDSvPTR(shared), key, len, G_DISCARD);
}
- SHARED_RELEASE;
- /* It is no longer in the array - so remove that magic */
- sv_unmagic(sv, PERL_MAGIC_tiedelem);
- Perl_sharedsv_associate(aTHX_ &sv, ssv, 0);
+ CALLER_CONTEXT;
+ LEAVE_LOCK;
return 0;
}
-
int
sharedsv_elem_mg_free(pTHX_ SV *sv, MAGIC *mg)
{
- Perl_sharedsv_free(aTHX_ Perl_sharedsv_find(aTHX_ mg->mg_obj));
+ Perl_sharedsv_free(aTHX_ SV_to_sharedsv(aTHX_ mg->mg_obj));
return 0;
}
int
sharedsv_elem_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
{
- shared_sv *shared = Perl_sharedsv_find(aTHX_ mg->mg_obj);
+ shared_sv *shared = SV_to_sharedsv(aTHX_ mg->mg_obj);
SvREFCNT_inc(SHAREDSvPTR(shared));
- PerlIO_debug(__FUNCTION__ " %p %d\n",shared,SvREFCNT(SHAREDSvPTR(shared)));
mg->mg_flags |= MGf_DUP;
return 0;
}
name, namlen);
SvREFCNT_inc(SHAREDSvPTR(shared));
nmg->mg_flags |= MGf_DUP;
-#if 0
- /* Maybe do this to associate shared value immediately ? */
- sharedsv_elem_FIND(aTHX_ nsv, nmg);
-#endif
return 1;
}
{
shared_sv *shared = (shared_sv *) mg->mg_ptr;
SvREFCNT_inc(SHAREDSvPTR(shared));
- PerlIO_debug(__FUNCTION__ " %p %d\n",shared,SvREFCNT(SHAREDSvPTR(shared)));
mg->mg_flags |= MGf_DUP;
return 0;
}
void
Perl_sharedsv_unlock(pTHX_ shared_sv* ssv)
{
- MUTEX_LOCK(&ssv->mutex);
- if (ssv->owner != aTHX) {
- Perl_croak(aTHX_ "panic: Perl_sharedsv_unlock unlocking mutex that we don't own");
- MUTEX_UNLOCK(&ssv->mutex);
- return;
- }
-
- if (--ssv->locks == 0) {
- ssv->owner = NULL;
- COND_SIGNAL(&ssv->cond);
- }
- MUTEX_UNLOCK(&ssv->mutex);
- }
-
-void
-Perl_sharedsv_unlock_scope(pTHX_ shared_sv* ssv)
-{
- MUTEX_LOCK(&ssv->mutex);
- if (ssv->owner != aTHX) {
- MUTEX_UNLOCK(&ssv->mutex);
- return;
- }
- ssv->locks = 0;
- ssv->owner = NULL;
- COND_SIGNAL(&ssv->cond);
- MUTEX_UNLOCK(&ssv->mutex);
+ recursive_lock_release(aTHX_ &ssv->lock);
}
=for apidoc sharedsv_lock
{
if (!ssv)
return;
- MUTEX_LOCK(&ssv->mutex);
- if (ssv->owner && ssv->owner == aTHX) {
- ssv->locks++;
- MUTEX_UNLOCK(&ssv->mutex);
- return;
- }
- while (ssv->owner)
- COND_WAIT(&ssv->cond,&ssv->mutex);
- ssv->locks++;
- ssv->owner = aTHX;
- if (ssv->locks == 1)
- SAVEDESTRUCTOR_X(Perl_sharedsv_unlock_scope,ssv);
- MUTEX_UNLOCK(&ssv->mutex);
+ recursive_lock_acquire(aTHX_ &ssv->lock, __FILE__, __LINE__);
}
void
PL_sharedsv_space = perl_alloc();
perl_construct(PL_sharedsv_space);
CALLER_CONTEXT;
- MUTEX_INIT(&PL_sharedsv_space_mutex);
+ recursive_lock_init(aTHX_ &PL_sharedsv_lock);
PL_lockhook = &Perl_sharedsv_locksv;
PL_sharehook = &Perl_sharedsv_share;
}
-/* Accessor to convert threads::shared::tie objects back shared_sv * */
-shared_sv *
-SV_to_sharedsv(pTHX_ SV *sv)
-{
- shared_sv *shared = 0;
- if (SvROK(sv))
- {
- shared = INT2PTR(shared_sv *, SvIV(SvRV(sv)));
- }
- return shared;
-}
+#endif /* USE_ITHREADS */
MODULE = threads::shared PACKAGE = threads::shared::tie
PROTOTYPES: DISABLE
+#ifdef USE_ITHREADS
+
void
PUSH(shared_sv *shared, ...)
CODE:
for(i = 1; i < items; i++) {
SV* tmp = newSVsv(ST(i));
shared_sv *target;
- SHARED_LOCK;
+ ENTER_LOCK;
target = Perl_sharedsv_associate(aTHX_ &tmp, Nullsv, 0);
+ sharedsv_scalar_store(aTHX_ tmp, target);
SHARED_CONTEXT;
av_push((AV*) SHAREDSvPTR(shared), SHAREDSvPTR(target));
SHARED_RELEASE;
CODE:
dTHXc;
int i;
- SHARED_LOCK;
+ ENTER_LOCK;
SHARED_CONTEXT;
av_unshift((AV*)SHAREDSvPTR(shared), items - 1);
CALLER_CONTEXT;
for(i = 1; i < items; i++) {
SV* tmp = newSVsv(ST(i));
shared_sv *target = Perl_sharedsv_associate(aTHX_ &tmp, Nullsv, 0);
+ sharedsv_scalar_store(aTHX_ tmp, target);
SHARED_CONTEXT;
av_store((AV*) SHAREDSvPTR(shared), i - 1, SHAREDSvPTR(target));
CALLER_CONTEXT;
SvREFCNT_dec(tmp);
}
- SHARED_UNLOCK;
+ LEAVE_LOCK;
void
POP(shared_sv *shared)
CODE:
dTHXc;
SV* sv;
- SHARED_LOCK;
+ ENTER_LOCK;
SHARED_CONTEXT;
sv = av_pop((AV*)SHAREDSvPTR(shared));
CALLER_CONTEXT;
ST(0) = Nullsv;
Perl_sharedsv_associate(aTHX_ &ST(0), sv, 0);
- SHARED_UNLOCK;
+ LEAVE_LOCK;
XSRETURN(1);
void
CODE:
dTHXc;
SV* sv;
- SHARED_LOCK;
+ ENTER_LOCK;
SHARED_CONTEXT;
sv = av_shift((AV*)SHAREDSvPTR(shared));
CALLER_CONTEXT;
ST(0) = Nullsv;
Perl_sharedsv_associate(aTHX_ &ST(0), sv, 0);
- SHARED_UNLOCK;
+ LEAVE_LOCK;
XSRETURN(1);
void
SHARED_RELEASE;
void
+STORESIZE(shared_sv *shared,IV count)
+CODE:
+ dTHXc;
+ SHARED_EDIT;
+ av_fill((AV*) SHAREDSvPTR(shared), count);
+ SHARED_RELEASE;
+
+
+
+
+void
EXISTS(shared_sv *shared, SV *index)
CODE:
dTHXc;
exists = av_exists((AV*) SHAREDSvPTR(shared), SvIV(index));
}
else {
- exists = hv_exists_ent((HV*) SHAREDSvPTR(shared), index, 0);
+ STRLEN len;
+ char *key = SvPV(index,len);
+ exists = hv_exists((HV*) SHAREDSvPTR(shared), key, len);
}
SHARED_RELEASE;
ST(0) = (exists) ? &PL_sv_yes : &PL_sv_no;
XSRETURN(1);
-void
-STORESIZE(shared_sv *shared,IV count)
-CODE:
- dTHXc;
- SHARED_EDIT;
- av_fill((AV*) SHAREDSvPTR(shared), count);
- SHARED_RELEASE;
void
FIRSTKEY(shared_sv *shared)
char* key = NULL;
I32 len = 0;
HE* entry;
- SHARED_LOCK;
+ ENTER_LOCK;
SHARED_CONTEXT;
hv_iterinit((HV*) SHAREDSvPTR(shared));
entry = hv_iternext((HV*) SHAREDSvPTR(shared));
CALLER_CONTEXT;
ST(0) = &PL_sv_undef;
}
- SHARED_UNLOCK;
+ LEAVE_LOCK;
XSRETURN(1);
void
char* key = NULL;
I32 len = 0;
HE* entry;
- SHARED_LOCK;
+ ENTER_LOCK;
SHARED_CONTEXT;
entry = hv_iternext((HV*) SHAREDSvPTR(shared));
- if(entry) {
+ if (entry) {
key = hv_iterkey(entry,&len);
CALLER_CONTEXT;
ST(0) = sv_2mortal(newSVpv(key, len));
CALLER_CONTEXT;
ST(0) = &PL_sv_undef;
}
- SHARED_UNLOCK;
+ LEAVE_LOCK;
XSRETURN(1);
MODULE = threads::shared PACKAGE = threads::shared
PROTOTYPES: ENABLE
void
-_thrcnt(SV *ref)
+_id(SV *ref)
+ PROTOTYPE: \[$@%]
+CODE:
+ shared_sv *shared;
+ if(SvROK(ref))
+ ref = SvRV(ref);
+ if (shared = Perl_sharedsv_find(aTHX_ ref)) {
+ ST(0) = sv_2mortal(newSViv(PTR2IV(shared)));
+ XSRETURN(1);
+ }
+ XSRETURN_UNDEF;
+
+
+void
+_refcnt(SV *ref)
PROTOTYPE: \[$@%]
CODE:
shared_sv *shared;
shared = Perl_sharedsv_find(aTHX_ ref);
if(!shared)
croak("cond_wait can only be used on shared values");
- if(shared->owner != aTHX)
+ if(shared->lock.owner != aTHX)
croak("You need a lock before you can cond_wait");
- MUTEX_LOCK(&shared->mutex);
- shared->owner = NULL;
- locks = shared->locks = 0;
- COND_WAIT(&shared->user_cond, &shared->mutex);
- shared->owner = aTHX;
- shared->locks = locks;
- MUTEX_UNLOCK(&shared->mutex);
+ /* Stealing the members of the lock object worries me - NI-S */
+ MUTEX_LOCK(&shared->lock.mutex);
+ shared->lock.owner = NULL;
+ locks = shared->lock.locks = 0;
+ COND_WAIT(&shared->user_cond, &shared->lock.mutex);
+ shared->lock.owner = aTHX;
+ shared->lock.locks = locks;
+ MUTEX_UNLOCK(&shared->lock.mutex);
void
cond_signal_enabled(SV *ref)
croak("cond_broadcast can only be used on shared values");
COND_BROADCAST(&shared->user_cond);
+#endif /* USE_ITHREADS */
+
BOOT:
{
+#ifdef USE_ITHREADS
Perl_sharedsv_init(aTHX);
+#endif /* USE_ITHREADS */
}
+
+
+