@@ -210,9 +210,11 @@ extern "C" JL_DLLEXPORT char *jl_format_filename(const char *output_pattern)
210
210
}
211
211
212
212
213
+ static jl_mutex_t trampoline_lock; // for accesses to the cache and freelist
214
+
213
215
static void *trampoline_freelist;
214
216
215
- static void *trampoline_alloc ()
217
+ static void *trampoline_alloc () // lock taken by caller
216
218
{
217
219
const int sz = 64 ; // oversized for most platforms. todo: use precise value?
218
220
if (!trampoline_freelist) {
@@ -245,7 +247,7 @@ static void *trampoline_alloc()
245
247
return tramp;
246
248
}
247
249
248
- static void trampoline_free (void *tramp)
250
+ static void trampoline_free (void *tramp) // lock taken by caller
249
251
{
250
252
*(void **)tramp = trampoline_freelist;
251
253
trampoline_freelist = tramp;
@@ -260,17 +262,18 @@ static void trampoline_deleter(void **f)
260
262
f[0 ] = NULL ;
261
263
f[2 ] = NULL ;
262
264
f[3 ] = NULL ;
265
+ JL_LOCK_NOGC (&trampoline_lock);
263
266
if (tramp)
264
267
trampoline_free (tramp);
265
268
if (fobj && cache)
266
269
ptrhash_remove ((htable_t *)cache, fobj);
267
270
if (nval)
268
271
free (nval);
272
+ JL_UNLOCK_NOGC (&trampoline_lock);
269
273
}
270
274
271
275
// Use of `cache` is not clobbered in JL_TRY
272
276
JL_GCC_IGNORE_START (" -Wclobbered" )
273
- // TODO: need a thread lock around the cache access parts of this function
274
277
extern "C" JL_DLLEXPORT
275
278
jl_value_t *jl_get_cfunction_trampoline(
276
279
// dynamic inputs:
@@ -284,6 +287,7 @@ jl_value_t *jl_get_cfunction_trampoline(
284
287
jl_value_t **vals)
285
288
{
286
289
// lookup (fobj, vals) in cache
290
+ JL_LOCK_NOGC (&trampoline_lock);
287
291
if (!cache->table )
288
292
htable_new (cache, 1 );
289
293
if (fill != jl_emptysvec) {
@@ -295,6 +299,7 @@ jl_value_t *jl_get_cfunction_trampoline(
295
299
}
296
300
}
297
301
void *tramp = ptrhash_get (cache, (void *)fobj);
302
+ JL_UNLOCK_NOGC (&trampoline_lock);
298
303
if (tramp != HT_NOTFOUND) {
299
304
assert ((jl_datatype_t *)jl_typeof (tramp) == result_type);
300
305
return (jl_value_t *)tramp;
@@ -347,10 +352,12 @@ jl_value_t *jl_get_cfunction_trampoline(
347
352
free (nval);
348
353
jl_rethrow ();
349
354
}
355
+ JL_LOCK_NOGC (&trampoline_lock);
350
356
tramp = trampoline_alloc ();
351
357
((void **)result)[0 ] = tramp;
352
358
tramp = init_trampoline (tramp, nval);
353
359
ptrhash_put (cache, (void *)fobj, result);
360
+ JL_UNLOCK_NOGC (&trampoline_lock);
354
361
return result;
355
362
}
356
363
JL_GCC_IGNORE_STOP
0 commit comments