| 1 |
/* |
| 2 |
* threadlocal.c - thread locals |
| 3 |
* |
| 4 |
* Copyright (c) 2022-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#define LIBGAUCHE_BODY |
| 35 |
#include "gauche.h" |
| 36 |
#include "gauche/priv/configP.h" |
| 37 |
#include "gauche/priv/vmP.h" |
| 38 |
|
| 39 |
/* Thread local storage are kept in each VM, and each thread local object |
| 40 |
* has index of it. Thread locals are used in the core so we define |
| 41 |
* it in src/ instead of ext/threads/. |
| 42 |
* |
| 43 |
* We have inheritable and non-inheritable thread locals. Inheritable |
| 44 |
* thread locals are initialized with the value of the thread that calls |
| 45 |
* make-thread. Non-inheritable ones are initialized by the init-value |
| 46 |
* given to the thread local constructor. |
| 47 |
* |
| 48 |
* TODO: We now need to allocate tl slots to every thread. |
| 49 |
* We may be able to use a tree instead of a flat vector so that we |
| 50 |
* can avoid allocation of leaf nodes until they are accessed. |
| 51 |
*/ |
| 52 |
|
| 53 |
#define THREAD_LOCAL_INIT_SIZE 64 |
| 54 |
#define THREAD_LOCAL_GROW 16 |
| 55 |
|
| 56 |
/* Global table to track free slots of the thread local table. |
| 57 |
* tl_freelists[].freelist array has a list of free slots, terminated by -1. |
| 58 |
*/ |
| 59 |
typedef struct tl_freelist_entry { |
| 60 |
ScmSize next; |
| 61 |
u_long generation; |
| 62 |
} tl_freelist_entry; |
| 63 |
|
| 64 |
struct tl_freelist { |
| 65 |
ScmSize size; |
| 66 |
ScmSize head; |
| 67 |
tl_freelist_entry *freelist; |
| 68 |
} tl_freelists[2]; |
| 69 |
|
| 70 |
static ScmInternalMutex tl_mutex = SCM_INTERNAL_MUTEX_INITIALIZER; |
| 71 |
|
| 72 |
static void tl_print(ScmObj obj, ScmPort *out, ScmWriteContext *ctx); |
| 73 |
|
| 74 |
SCM_DEFINE_BUILTIN_CLASS(Scm_ThreadLocalClass, |
| 75 |
tl_print, NULL, NULL, NULL, |
| 76 |
SCM_CLASS_OBJECT_CPL); |
| 77 |
|
| 78 |
static void tl_print(ScmObj obj, |
| 79 |
ScmPort *out, |
| 80 |
ScmWriteContext *ctx SCM_UNUSED) |
| 81 |
{ |
| 82 |
Scm_Printf(out, "#<%A %S @%p>", |
| 83 |
Scm_ShortClassName(Scm_ClassOf(obj)), |
| 84 |
SCM_THREAD_LOCAL(obj)->name, |
| 85 |
obj); |
| 86 |
} |
| 87 |
|
| 88 |
static void init_tl_freelist(void) |
| 89 |
{ |
| 90 |
for (int i=0; i<2; i++) { |
| 91 |
tl_freelists[i].freelist = |
| 92 |
SCM_NEW_ATOMIC_ARRAY(tl_freelist_entry, THREAD_LOCAL_INIT_SIZE); |
| 93 |
for (ScmSize j=0; j<THREAD_LOCAL_INIT_SIZE; j++) { |
| 94 |
tl_freelists[i].freelist[j].next = j+1; |
| 95 |
tl_freelists[i].freelist[j].generation = 0; |
| 96 |
} |
| 97 |
tl_freelists[i].size = THREAD_LOCAL_INIT_SIZE; |
| 98 |
tl_freelists[i].head = 0; |
| 99 |
tl_freelists[i].freelist[THREAD_LOCAL_INIT_SIZE-1].next = -1; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static ScmSize allocate_tl_slot(int kind, u_long *generation /*out*/) |
| 104 |
{ |
| 105 |
ScmSize r = -1; |
| 106 |
u_long gen; |
| 107 |
SCM_INTERNAL_MUTEX_LOCK(tl_mutex); |
| 108 |
if (tl_freelists[kind].head >= 0) { |
| 109 |
r = tl_freelists[kind].head; |
| 110 |
gen = tl_freelists[kind].freelist[r].generation; |
| 111 |
tl_freelists[kind].head = tl_freelists[kind].freelist[r].next; |
| 112 |
tl_freelists[kind].freelist[r].next = -1; |
| 113 |
} else { |
| 114 |
ScmSize oldsize = tl_freelists[kind].size; |
| 115 |
ScmSize newsize = oldsize + THREAD_LOCAL_GROW; |
| 116 |
tl_freelist_entry *newlist = |
| 117 |
SCM_NEW_ATOMIC_ARRAY(tl_freelist_entry, newsize); |
| 118 |
memcpy(tl_freelists[kind].freelist, newlist, oldsize); |
| 119 |
for (ScmSize i=oldsize; i < newsize; i++) { |
| 120 |
newlist[i].next = i+1; |
| 121 |
newlist[i].generation = 0; |
| 122 |
} |
| 123 |
newlist[oldsize].next = -1; |
| 124 |
newlist[newsize-1].next = -1; |
| 125 |
tl_freelists[kind].freelist = newlist; |
| 126 |
tl_freelists[kind].size = newsize; |
| 127 |
tl_freelists[kind].head = oldsize+1; |
| 128 |
r = oldsize; |
| 129 |
gen = 0; |
| 130 |
} |
| 131 |
SCM_INTERNAL_MUTEX_UNLOCK(tl_mutex); |
| 132 |
*generation = gen; |
| 133 |
return r; |
| 134 |
} |
| 135 |
|
| 136 |
static void free_tl_slot(int kind, ScmSize slot) |
| 137 |
{ |
| 138 |
SCM_INTERNAL_MUTEX_LOCK(tl_mutex); |
| 139 |
tl_freelists[kind].freelist[slot].next = tl_freelists[kind].head; |
| 140 |
tl_freelists[kind].freelist[slot].generation++; |
| 141 |
tl_freelists[kind].head = slot; |
| 142 |
SCM_INTERNAL_MUTEX_UNLOCK(tl_mutex); |
| 143 |
} |
| 144 |
|
| 145 |
/* Init table. For primordial thread, base == NULL. For non-primordial |
| 146 |
* thread, base is the current thread (this must be called from the |
| 147 |
* creator thread). |
| 148 |
*/ |
| 149 |
static void fill_tl_vector(ScmVMThreadLocalVector *dst, |
| 150 |
const ScmVMThreadLocalVector *base) |
| 151 |
{ |
| 152 |
if (base) { |
| 153 |
/* NB: In this case, the caller is the owner thread of BASE, |
| 154 |
so we don't need to worry about base->parameters being |
| 155 |
modified during copying. */ |
| 156 |
dst->vector = SCM_NEW_ARRAY(ScmVMThreadLocalSlot, base->size); |
| 157 |
dst->size = base->size; |
| 158 |
for (ScmSize i=0; i<dst->size; i++) { |
| 159 |
dst->vector[i] = base->vector[i]; |
| 160 |
} |
| 161 |
} else { |
| 162 |
dst->vector = SCM_NEW_ARRAY(ScmVMThreadLocalSlot, THREAD_LOCAL_INIT_SIZE); |
| 163 |
dst->size = THREAD_LOCAL_INIT_SIZE; |
| 164 |
for (ScmSize i=0; i<dst->size; i++) { |
| 165 |
dst->vector[i].value = SCM_UNBOUND; |
| 166 |
dst->vector[i].generation = 0; /* doesn't matter */ |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
ScmVMThreadLocalTable *Scm__MakeVMThreadLocalTable(ScmVM *base) |
| 172 |
{ |
| 173 |
ScmVMThreadLocalTable *t = SCM_NEW(ScmVMThreadLocalTable); |
| 174 |
if (base) { |
| 175 |
fill_tl_vector(&t->vs[SCM_THREAD_LOCAL_VECTOR_INHERITABLE], |
| 176 |
&base->threadLocals->vs[SCM_THREAD_LOCAL_VECTOR_INHERITABLE]); |
| 177 |
fill_tl_vector(&t->vs[SCM_THREAD_LOCAL_VECTOR_NONINHERITABLE], NULL); |
| 178 |
} else { |
| 179 |
fill_tl_vector(&t->vs[SCM_THREAD_LOCAL_VECTOR_INHERITABLE], NULL); |
| 180 |
fill_tl_vector(&t->vs[SCM_THREAD_LOCAL_VECTOR_NONINHERITABLE], NULL); |
| 181 |
} |
| 182 |
return t; |
| 183 |
} |
| 184 |
|
| 185 |
static int tl_kind(const ScmThreadLocal *tl) |
| 186 |
{ |
| 187 |
if (tl->flags & SCM_THREAD_LOCAL_INHERITABLE) |
| 188 |
return SCM_THREAD_LOCAL_VECTOR_INHERITABLE; |
| 189 |
else |
| 190 |
return SCM_THREAD_LOCAL_VECTOR_NONINHERITABLE; |
| 191 |
} |
| 192 |
|
| 193 |
static void ensure_tl_slot(ScmVMThreadLocalTable *t, ScmSize index, int kind) |
| 194 |
{ |
| 195 |
ScmVMThreadLocalVector *p = &t->vs[kind]; |
| 196 |
if (index >= p->size) { |
| 197 |
ScmSize newsiz = |
| 198 |
((index+THREAD_LOCAL_GROW)/THREAD_LOCAL_GROW)*THREAD_LOCAL_GROW; |
| 199 |
ScmVMThreadLocalSlot *newvec = SCM_NEW_ARRAY(ScmVMThreadLocalSlot, newsiz); |
| 200 |
|
| 201 |
ScmSize i; |
| 202 |
for (i=0; i < p->size; i++) { |
| 203 |
newvec[i] = p->vector[i]; |
| 204 |
p->vector[i].value = SCM_FALSE; /*be friendly to GC*/ |
| 205 |
} |
| 206 |
for (; i < newsiz; i++) { |
| 207 |
newvec[i].value = SCM_UNBOUND; |
| 208 |
newvec[i].generation = 0; /* doesn't matter */ |
| 209 |
} |
| 210 |
p->vector = newvec; |
| 211 |
p->size = newsiz; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/* |
| 216 |
* Create a thread local |
| 217 |
*/ |
| 218 |
static void tl_finalize(ScmObj obj, void *data SCM_UNUSED) |
| 219 |
{ |
| 220 |
ScmThreadLocal *tl = SCM_THREAD_LOCAL(obj); |
| 221 |
if (tl->index >= 0) { |
| 222 |
int kind = ((tl->flags & SCM_THREAD_LOCAL_INHERITABLE) |
| 223 |
? SCM_THREAD_LOCAL_VECTOR_INHERITABLE |
| 224 |
: SCM_THREAD_LOCAL_VECTOR_NONINHERITABLE); |
| 225 |
free_tl_slot(kind, tl->index); |
| 226 |
tl->index = -1; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
ScmThreadLocal *Scm_MakeThreadLocal(ScmObj name, |
| 231 |
ScmObj initval, |
| 232 |
u_long flags) |
| 233 |
{ |
| 234 |
int kind = ((flags & SCM_THREAD_LOCAL_INHERITABLE) |
| 235 |
? SCM_THREAD_LOCAL_VECTOR_INHERITABLE |
| 236 |
: SCM_THREAD_LOCAL_VECTOR_NONINHERITABLE); |
| 237 |
u_long generation; |
| 238 |
ScmSize index = allocate_tl_slot(kind, &generation); |
| 239 |
ensure_tl_slot(Scm_VM()->threadLocals, index, kind); |
| 240 |
|
| 241 |
ScmThreadLocal *tl = SCM_NEW(ScmThreadLocal); |
| 242 |
SCM_SET_CLASS(tl, SCM_CLASS_THREAD_LOCAL); |
| 243 |
tl->name = name; |
| 244 |
tl->index = index; |
| 245 |
tl->initialValue = initval; |
| 246 |
tl->generation = generation; |
| 247 |
tl->flags = flags; |
| 248 |
Scm_RegisterFinalizer(SCM_OBJ(tl), tl_finalize, NULL); |
| 249 |
return tl; |
| 250 |
} |
| 251 |
|
| 252 |
/* |
| 253 |
* Accessor & modifier |
| 254 |
*/ |
| 255 |
|
| 256 |
ScmObj Scm_ThreadLocalRef(ScmVM *vm, const ScmThreadLocal *tl) |
| 257 |
{ |
| 258 |
int kind = tl_kind(tl); |
| 259 |
ScmVMThreadLocalVector *t = &vm->threadLocals->vs[kind]; |
| 260 |
|
| 261 |
ScmObj result; |
| 262 |
if (tl->index >= t->size) { |
| 263 |
result = tl->initialValue; |
| 264 |
} else { |
| 265 |
result = t->vector[tl->index].value; |
| 266 |
if (SCM_UNBOUNDP(result) |
| 267 |
|| tl->generation != t->vector[tl->index].generation) { |
| 268 |
result = t->vector[tl->index].value = tl->initialValue; |
| 269 |
t->vector[tl->index].generation = tl->generation; |
| 270 |
} |
| 271 |
} |
| 272 |
return result; |
| 273 |
} |
| 274 |
|
| 275 |
ScmObj Scm_ThreadLocalSet(ScmVM *vm, const ScmThreadLocal *tl, |
| 276 |
ScmObj val) |
| 277 |
{ |
| 278 |
ScmObj oldval = SCM_UNBOUND; |
| 279 |
int kind = tl_kind(tl); |
| 280 |
ScmVMThreadLocalVector *t = &vm->threadLocals->vs[kind]; |
| 281 |
|
| 282 |
if (tl->index >= t->size) { |
| 283 |
ensure_tl_slot(vm->threadLocals, tl->index, tl_kind(tl)); |
| 284 |
} else if (tl->generation == t->vector[tl->index].generation) { |
| 285 |
oldval = t->vector[tl->index].value; |
| 286 |
} |
| 287 |
if (SCM_UNBOUNDP(oldval)) { |
| 288 |
oldval = tl->initialValue; |
| 289 |
} |
| 290 |
|
| 291 |
t->vector[tl->index].value = val; |
| 292 |
t->vector[tl->index].generation = tl->generation; |
| 293 |
|
| 294 |
return oldval; |
| 295 |
} |
| 296 |
|
| 297 |
void Scm__InitThreadLocal(void) |
| 298 |
{ |
| 299 |
init_tl_freelist(); |
| 300 |
SCM_INTERNAL_MUTEX_INIT(tl_mutex); |
| 301 |
/* We don't initialize Scm_ThraedLocalClass yet, since class |
| 302 |
stuff is not initialized yet. The class is initialized in |
| 303 |
class.c. */ |
| 304 |
} |