Skip to content

Commit 973e361

Browse files
committed
Make jl_codegen_lock a sleeping mutex instead of a spinning one
1 parent a6e97e0 commit 973e361

File tree

7 files changed

+28
-17
lines changed

7 files changed

+28
-17
lines changed

src/aotcompile.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
299299

300300
// compile all methods for the current world and type-inference world
301301

302-
JL_SPIN_LOCK(&jl_codegen_lock);
302+
JL_SLEEP_LOCK(&jl_codegen_lock);
303303
auto target_info = clone.withModuleDo([&](Module &M) {
304304
return std::make_pair(M.getDataLayout(), Triple(M.getTargetTriple()));
305305
});
@@ -351,7 +351,7 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
351351
// finally, make sure all referenced methods also get compiled or fixed up
352352
jl_compile_workqueue(emitted, *clone.getModuleUnlocked(), params, policy);
353353
}
354-
JL_SPIN_UNLOCK(&jl_codegen_lock); // Might GC
354+
JL_SLEEP_UNLOCK(&jl_codegen_lock); // Might GC
355355
JL_GC_POP();
356356

357357
// process the globals array, before jl_merge_module destroys them
@@ -2034,15 +2034,15 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
20342034
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
20352035
if (measure_compile_time_enabled)
20362036
compiler_start_time = jl_hrtime();
2037-
JL_SPIN_LOCK(&jl_codegen_lock);
2037+
JL_SLEEP_LOCK(&jl_codegen_lock);
20382038
auto target_info = m.withModuleDo([&](Module &M) {
20392039
return std::make_pair(M.getDataLayout(), Triple(M.getTargetTriple()));
20402040
});
20412041
jl_codegen_params_t output(*ctx, std::move(target_info.first), std::move(target_info.second));
20422042
output.world = world;
20432043
output.params = &params;
20442044
auto decls = jl_emit_code(m, mi, src, jlrettype, output);
2045-
JL_SPIN_UNLOCK(&jl_codegen_lock); // Might GC
2045+
JL_SLEEP_UNLOCK(&jl_codegen_lock); // Might GC
20462046

20472047
Function *F = NULL;
20482048
if (m) {

src/gf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,12 +3843,12 @@ JL_DLLEXPORT void jl_typeinf_timing_end(uint64_t start)
38433843

38443844
JL_DLLEXPORT void jl_typeinf_lock_begin(void)
38453845
{
3846-
JL_SPIN_LOCK(&jl_codegen_lock);
3846+
JL_SLEEP_LOCK(&jl_codegen_lock);
38473847
}
38483848

38493849
JL_DLLEXPORT void jl_typeinf_lock_end(void)
38503850
{
3851-
JL_SPIN_UNLOCK(&jl_codegen_lock);
3851+
JL_SLEEP_UNLOCK(&jl_codegen_lock);
38523852
}
38533853

38543854
#ifdef __cplusplus

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static void init_global_mutexes(void) {
725725
JL_SPIN_MUTEX_INIT(&precomp_statement_out_lock, "precomp_statement_out_lock");
726726
JL_SPIN_MUTEX_INIT(&newly_inferred_mutex, "newly_inferred_mutex");
727727
JL_SPIN_MUTEX_INIT(&global_roots_lock, "global_roots_lock");
728-
JL_SPIN_MUTEX_INIT(&jl_codegen_lock, "jl_codegen_lock");
728+
JL_SLEEP_MUTEX_INIT(&jl_codegen_lock, "jl_codegen_lock");
729729
JL_SPIN_MUTEX_INIT(&typecache_lock, "typecache_lock");
730730
}
731731

src/jitlayers.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
343343
backing = jl_create_ts_module("cextern", pparams ? pparams->tsctx : ctx, pparams ? pparams->imaging : imaging_default());
344344
into = &backing;
345345
}
346-
JL_SPIN_LOCK(&jl_codegen_lock);
346+
JL_SLEEP_LOCK(&jl_codegen_lock);
347347
auto target_info = into->withModuleDo([&](Module &M) {
348348
return std::make_pair(M.getDataLayout(), Triple(M.getTargetTriple()));
349349
});
@@ -366,7 +366,7 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
366366
if (success && llvmmod == NULL)
367367
jl_ExecutionEngine->addModule(std::move(*into));
368368
}
369-
JL_SPIN_UNLOCK(&jl_codegen_lock);
369+
JL_SLEEP_UNLOCK(&jl_codegen_lock);
370370
if (timed) {
371371
if (measure_compile_time_enabled) {
372372
auto end = jl_hrtime();
@@ -439,7 +439,7 @@ jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES
439439
// if we don't have any decls already, try to generate it now
440440
jl_code_info_t *src = NULL;
441441
JL_GC_PUSH1(&src);
442-
JL_SPIN_LOCK(&jl_codegen_lock); // also disables finalizers, to prevent any unexpected recursion
442+
JL_SLEEP_LOCK(&jl_codegen_lock); // also disables finalizers, to prevent any unexpected recursion
443443
jl_value_t *ci = jl_rettype_inferred(mi, world, world);
444444
jl_code_instance_t *codeinst = (ci == jl_nothing ? NULL : (jl_code_instance_t*)ci);
445445
if (codeinst) {
@@ -482,7 +482,7 @@ jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES
482482
else {
483483
codeinst = NULL;
484484
}
485-
JL_SPIN_UNLOCK(&jl_codegen_lock);
485+
JL_SLEEP_UNLOCK(&jl_codegen_lock);
486486
if (timed) {
487487
if (measure_compile_time_enabled) {
488488
uint64_t t_comp = jl_hrtime() - compiler_start_time;
@@ -511,7 +511,7 @@ void jl_generate_fptr_for_unspecialized_impl(jl_code_instance_t *unspec)
511511
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
512512
if (measure_compile_time_enabled)
513513
compiler_start_time = jl_hrtime();
514-
JL_SPIN_LOCK(&jl_codegen_lock);
514+
JL_SLEEP_LOCK(&jl_codegen_lock);
515515
if (jl_atomic_load_relaxed(&unspec->invoke) == NULL) {
516516
jl_code_info_t *src = NULL;
517517
JL_GC_PUSH1(&src);
@@ -538,7 +538,7 @@ void jl_generate_fptr_for_unspecialized_impl(jl_code_instance_t *unspec)
538538
jl_atomic_cmpswap(&unspec->invoke, &null, jl_fptr_interpret_call_addr);
539539
JL_GC_POP();
540540
}
541-
JL_SPIN_UNLOCK(&jl_codegen_lock); // Might GC
541+
JL_SLEEP_UNLOCK(&jl_codegen_lock); // Might GC
542542
if (timed) {
543543
if (measure_compile_time_enabled) {
544544
auto end = jl_hrtime();
@@ -573,7 +573,7 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
573573
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
574574
if (measure_compile_time_enabled)
575575
compiler_start_time = jl_hrtime();
576-
JL_SPIN_LOCK(&jl_codegen_lock); // also disables finalizers, to prevent any unexpected recursion
576+
JL_SLEEP_LOCK(&jl_codegen_lock); // also disables finalizers, to prevent any unexpected recursion
577577
specfptr = (uintptr_t)jl_atomic_load_relaxed(&codeinst->specptr.fptr);
578578
if (specfptr == 0) {
579579
jl_code_info_t *src = jl_type_infer(mi, world, 0);
@@ -597,7 +597,7 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
597597
}
598598
JL_GC_POP();
599599
}
600-
JL_SPIN_UNLOCK(&jl_codegen_lock);
600+
JL_SLEEP_UNLOCK(&jl_codegen_lock);
601601
if (timed) {
602602
if (measure_compile_time_enabled) {
603603
auto end = jl_hrtime();

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ JL_DLLEXPORT void jl_set_next_task(jl_task_t *task) JL_NOTSAFEPOINT;
13991399
// -- synchronization utilities -- //
14001400

14011401
extern jl_spin_mutex_t typecache_lock;
1402-
extern JL_DLLEXPORT jl_spin_mutex_t jl_codegen_lock;
1402+
extern JL_DLLEXPORT jl_sleep_mutex_t jl_codegen_lock;
14031403

14041404
#if defined(__APPLE__)
14051405
void jl_mach_gc_end(void);

src/julia_locks.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ static inline void jl_dyn_mutex_unlock_nogc(void *lock)
164164
#define JL_SPIN_LOCK_NOGC(m) jl_spin_mutex_lock_nogc(m)
165165
#define JL_SPIN_UNLOCK_NOGC(m) jl_spin_mutex_unlock_nogc(m)
166166

167+
#define JL_SLEEP_MUTEX_INIT(m, name) jl_sleep_mutex_init(m, name)
168+
#define JL_SLEEP_LOCK(m) jl_sleep_mutex_lock(m)
169+
#define JL_SLEEP_UNLOCK(m) jl_sleep_mutex_unlock(m)
170+
#define JL_SLEEP_LOCK_NOGC(m) jl_sleep_mutex_lock_nogc(m)
171+
#define JL_SLEEP_UNLOCK_NOGC(m) jl_sleep_mutex_unlock_nogc(m)
172+
167173
#ifdef __cplusplus
168174
}
169175
#endif

src/threading.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ static void jl_delete_thread(void *value) JL_NOTSAFEPOINT_ENTER
483483
//// the other threads time to fail and emit their failure message
484484
//__attribute__((destructor)) static void _waitthreaddeath(void) { sleep(1); }
485485

486-
JL_DLLEXPORT jl_spin_mutex_t jl_codegen_lock;
486+
JL_DLLEXPORT jl_sleep_mutex_t jl_codegen_lock;
487487
jl_spin_mutex_t typecache_lock;
488488

489489
JL_DLLEXPORT ssize_t jl_tls_offset = -1;
@@ -893,6 +893,11 @@ JL_DLLEXPORT void _jl_sleep_mutex_wait(jl_task_t *self, jl_sleep_mutex_t *lock,
893893
lock->count = 1;
894894
return;
895895
}
896+
if (jl_atomic_load_relaxed(&lock->owner) == self) {
897+
// we already own the lock, just increment the count
898+
lock->count++;
899+
return;
900+
}
896901
jl_profile_lock_start_wait(lock);
897902
//Do some retries?
898903
jl_atomic_fetch_add(&lock->waiters, 0b10);

0 commit comments

Comments
 (0)