summaryrefslogtreecommitdiff
path: root/sv.h
diff options
context:
space:
mode:
authorMalcolm Beattie <mbeattie@sable.ox.ac.uk>1998-02-27 18:06:41 +0000
committerMalcolm Beattie <mbeattie@sable.ox.ac.uk>1998-02-27 18:06:41 +0000
commitdce16143c60882f40eb787063b9483c00bb82139 (patch)
treedcb3c30695829781cc03b666fe1d2ae5eeda975c /sv.h
parentd22150111e6fba142fe332ab0f2c6233df82013c (diff)
downloadperl-dce16143c60882f40eb787063b9483c00bb82139.tar.gz
Make refcounts atomic for threading (dependent on appropriate
arch-dependent and compiler-dependent definitions in atomic.h or else falls back to a global mutex to protect refcounts). p4raw-id: //depot/perl@598
Diffstat (limited to 'sv.h')
-rw-r--r--sv.h37
1 files changed, 35 insertions, 2 deletions
diff --git a/sv.h b/sv.h
index 5993a8de9b..bf3454765d 100644
--- a/sv.h
+++ b/sv.h
@@ -72,13 +72,46 @@ struct io {
#define SvFLAGS(sv) (sv)->sv_flags
#define SvREFCNT(sv) (sv)->sv_refcnt
+#ifdef USE_THREADS
+
+# ifndef EMULATE_ATOMIC_REFCOUNTS
+# include "atomic.h"
+# endif
+
+# ifdef EMULATE_ATOMIC_REFCOUNTS
+# define ATOMIC_INC(count) STMT_START { \
+ MUTEX_LOCK(&svref_mutex); \
+ ++count; \
+ MUTEX_UNLOCK(&svref_mutex); \
+ } STMT_END
+# define ATOMIC_DEC_AND_TEST(res,count) \
+ MUTEX_LOCK(&svref_mutex); \
+ res = (--count == 0); \
+ MUTEX_UNLOCK(&svref_mutex); \
+ } STMT_END
+# else
+# define ATOMIC_INC(count) atomic_inc(&count)
+# define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
+# endif /* EMULATE_ATOMIC_REFCOUNTS */
+#else
+# define ATOMIC_INC(count) (++count)
+# define ATOMIC_DEC_AND_TEST(res, count) (res = --count)
+#endif /* USE_THREADS */
+
#ifdef __GNUC__
-# define SvREFCNT_inc(sv) ({SV* nsv=(SV*)(sv); if(nsv) ++SvREFCNT(nsv); nsv;})
+# define SvREFCNT_inc(sv) \
+ ({ \
+ SV *nsv = (SV*)(sv); \
+ if (nsv) \
+ ATOMIC_INC(SvREFCNT(nsv)); \
+ nsv; \
+ })
#else
# if defined(CRIPPLED_CC) || defined(USE_THREADS)
# define SvREFCNT_inc(sv) sv_newref((SV*)sv)
# else
-# define SvREFCNT_inc(sv) ((Sv=(SV*)(sv)), (Sv && ++SvREFCNT(Sv)), (SV*)Sv)
+# define SvREFCNT_inc(sv) \
+ ((Sv=(SV*)(sv)), (Sv && ATOMIC_INC(SvREFCNT(Sv))), (SV*)Sv)
# endif
#endif