summaryrefslogtreecommitdiff
path: root/op.h
diff options
context:
space:
mode:
authorYves Orton <demerphq@gmail.com>2022-10-28 21:14:23 +0200
committerYves Orton <demerphq@gmail.com>2022-11-01 11:57:31 +0100
commit6760f691a95ab3a37fd59212795de2b1a7cf7888 (patch)
treecb90dfaef26bcfd6f84b048e20f6267ec3ded521 /op.h
parente6421d31ff3b18d931e0da3152437c6f8d3f24a9 (diff)
downloadperl-6760f691a95ab3a37fd59212795de2b1a7cf7888.tar.gz
cop.h - add support for refcounted filenames in cops under threads
We have a weird bifurcation of the cop logic around threads. With threads we use a char * cop_file member, without it we use a GV * and replace cop_file with cop_filegv. The GV * code refcounts filenames and more or less efficiently shares the filename amongst many opcodes. However under threads we were simplify copying the filenames into each opcode. This is because in theory opcodes created in one thread can be destroyed in another. I say in theory because as far as I know the core code does not actually do this. But we have tests that you can construct a perl, clone it, and then destroy the original, and have the copy work just fine, this means that opcodes constructed in the main thread will be destroyed in the cloned thread. This in turn means that you can't put SV derived structures into the op-tree under threads. Which is why we can not use the GV * stategy under threads. As such this code adds a new struct/type RCPV, which is a refcounted string using shared memory. This is implemented in such a way that code that previously used a char * can continue to do so, as the refcounting data is located a specific offset before the char * pointer itself. This also allows the len data to embedded "into" the PV, which allows us to expose macros to acces the length of what is in theory a null terminated string. struct rcpv { UV refcount; STRLEN len; char pv[1]; }; typedef struct rcpv RCPV; The struct is sized appropriately on creation in rcpv_new() so that the pv member contains the full string plus a null byte. It then returns a pointer to the pv member of the struct. Thus the refcount and length and embedded at a predictable offset in front of the char *, which means we do not have to change any types for members using this. We provide three operations: rcpv_new(), rcpv_copy() and rcpv_free(), which roughly correspond with newSVpv(), SvREFCNT_inc(), SvREFCNT_dec(), and a handful of macros as well. We also expose SAVERCPVFREE which is similar to SAVEGENERICSV but operates on pv's constructed with rcpv_new(). Currently I have not restricted use of this logic to threaded perls. We simply do not use it in unthreaded perls, but I see no reason we couldn't normalize the code to use this in both cases, except possibly that actually the GV case is more efficient. Note that rcpv_new() does NOT use a hash table to dedup strings. Two calls to rcpv_new() with the same arguments will produce two distinct pointers with their own refcount data. Refcounting the cop_file data was Tony Cook's idea.
Diffstat (limited to 'op.h')
-rw-r--r--op.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/op.h b/op.h
index 12f0c5ce2d..5beaeb6af3 100644
--- a/op.h
+++ b/op.h
@@ -619,6 +619,7 @@ typedef enum {
* The same mutex is used to protect the refcounts of the reg_trie_data
* and reg_ac_data structures, which are shared between duplicated
* regexes.
+ * The same mutex is used to protect the refcounts for RCPV objects.
*/
#ifdef USE_ITHREADS
@@ -1169,6 +1170,7 @@ struct op_argcheck_aux {
#define MI_INIT_WORKAROUND_PACK "Module::Install::DSL"
+
/*
* ex: set ts=8 sts=4 sw=4 et:
*/