Skip to content

Commit a6e97e0

Browse files
committed
Add sleeping lock implementation
1 parent bdcf5ca commit a6e97e0

23 files changed

+387
-139
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_LOCK(&jl_codegen_lock);
302+
JL_SPIN_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_UNLOCK(&jl_codegen_lock); // Might GC
354+
JL_SPIN_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_LOCK(&jl_codegen_lock);
2037+
JL_SPIN_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_UNLOCK(&jl_codegen_lock); // Might GC
2045+
JL_SPIN_UNLOCK(&jl_codegen_lock); // Might GC
20462046

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

src/codegen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,18 +2783,18 @@ static jl_value_t *jl_ensure_rooted(jl_codectx_t &ctx, jl_value_t *val)
27832783
jl_method_t *m = ctx.linfo->def.method;
27842784
if (jl_is_method(m)) {
27852785
// the method might have a root for this already; use it if so
2786-
JL_LOCK(&m->writelock);
2786+
JL_SPIN_LOCK(&m->writelock);
27872787
if (m->roots) {
27882788
size_t i, len = jl_array_dim0(m->roots);
27892789
for (i = 0; i < len; i++) {
27902790
jl_value_t *mval = jl_array_ptr_ref(m->roots, i);
27912791
if (mval == val || jl_egal(mval, val)) {
2792-
JL_UNLOCK(&m->writelock);
2792+
JL_SPIN_UNLOCK(&m->writelock);
27932793
return mval;
27942794
}
27952795
}
27962796
}
2797-
JL_UNLOCK(&m->writelock);
2797+
JL_SPIN_UNLOCK(&m->writelock);
27982798
}
27992799
return jl_as_global_root(val);
28002800
}

src/datatype.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ JL_DLLEXPORT jl_methtable_t *jl_new_method_table(jl_sym_t *name, jl_module_t *mo
5353
jl_atomic_store_relaxed(&mt->cache, jl_nothing);
5454
jl_atomic_store_relaxed(&mt->max_args, 0);
5555
mt->backedges = NULL;
56-
JL_MUTEX_INIT(&mt->writelock, "methodtable->writelock");
56+
JL_SPIN_MUTEX_INIT(&mt->writelock, "methodtable->writelock");
5757
mt->offs = 0;
5858
mt->frozen = 0;
5959
return mt;
@@ -1517,12 +1517,12 @@ JL_DLLEXPORT jl_value_t *jl_new_struct_uninit(jl_datatype_t *type)
15171517

15181518
JL_DLLEXPORT void jl_lock_value(jl_value_t *v) JL_NOTSAFEPOINT
15191519
{
1520-
JL_LOCK_NOGC((jl_spin_mutex_t*)v);
1520+
JL_SPIN_LOCK_NOGC((jl_spin_mutex_t*)v);
15211521
}
15221522

15231523
JL_DLLEXPORT void jl_unlock_value(jl_value_t *v) JL_NOTSAFEPOINT
15241524
{
1525-
JL_UNLOCK_NOGC((jl_spin_mutex_t*)v);
1525+
JL_SPIN_UNLOCK_NOGC((jl_spin_mutex_t*)v);
15261526
}
15271527

15281528
JL_DLLEXPORT int jl_field_index(jl_datatype_t *t, jl_sym_t *fld, int err)

src/gc-heap-snapshot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ JL_DLLEXPORT void jl_gc_take_heap_snapshot(ios_t *stream, char all_one)
135135
HeapSnapshot snapshot;
136136
_add_internal_root(&snapshot);
137137

138-
jl_spin_mutex_lock(&heapsnapshot_lock);
138+
JL_SPIN_LOCK(&heapsnapshot_lock);
139139

140140
// Enable snapshotting
141141
g_snapshot = &snapshot;
@@ -148,7 +148,7 @@ JL_DLLEXPORT void jl_gc_take_heap_snapshot(ios_t *stream, char all_one)
148148
gc_heap_snapshot_enabled = false;
149149
g_snapshot = nullptr;
150150

151-
jl_spin_mutex_unlock(&heapsnapshot_lock);
151+
JL_SPIN_UNLOCK(&heapsnapshot_lock);
152152

153153
// When we return, the snapshot is full
154154
// Dump the snapshot

src/gc.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ static void jl_gc_run_finalizers_in_list(jl_task_t *ct, arraylist_t *list) JL_NO
495495
jl_gc_push_arraylist(ct, list);
496496
void **items = list->items;
497497
size_t len = list->len;
498-
JL_UNLOCK_NOGC(&finalizers_lock);
498+
JL_SPIN_UNLOCK_NOGC(&finalizers_lock);
499499
// run finalizers in reverse order they were added, so lower-level finalizers run last
500500
for (size_t i = len-4; i >= 2; i -= 2)
501501
run_finalizer(ct, items[i], items[i + 1]);
@@ -524,9 +524,9 @@ static void run_finalizers(jl_task_t *ct)
524524
// will flush it.
525525
if (to_finalize.len == 0)
526526
return;
527-
JL_LOCK_NOGC(&finalizers_lock);
527+
JL_SPIN_LOCK_NOGC(&finalizers_lock);
528528
if (to_finalize.len == 0) {
529-
JL_UNLOCK_NOGC(&finalizers_lock);
529+
JL_SPIN_UNLOCK_NOGC(&finalizers_lock);
530530
return;
531531
}
532532
arraylist_t copied_list;
@@ -661,14 +661,14 @@ void jl_gc_add_finalizer_(jl_ptls_t ptls, void *v, void *f) JL_NOTSAFEPOINT
661661
// between the acquire and the release of the length.
662662
size_t oldlen = jl_atomic_load_acquire((_Atomic(size_t)*)&a->len);
663663
if (__unlikely(oldlen + 2 > a->max)) {
664-
JL_LOCK_NOGC(&finalizers_lock);
664+
JL_SPIN_LOCK_NOGC(&finalizers_lock);
665665
// `a->len` might have been modified.
666666
// Another possibility is to always grow the array to `oldlen + 2` but
667667
// it's simpler this way and uses slightly less memory =)
668668
oldlen = a->len;
669669
arraylist_grow(a, 2);
670670
a->len = oldlen;
671-
JL_UNLOCK_NOGC(&finalizers_lock);
671+
JL_SPIN_UNLOCK_NOGC(&finalizers_lock);
672672
}
673673
void **items = a->items;
674674
items[oldlen] = v;
@@ -700,7 +700,7 @@ JL_DLLEXPORT void jl_gc_add_finalizer_th(jl_ptls_t ptls, jl_value_t *v, jl_funct
700700

701701
JL_DLLEXPORT void jl_finalize_th(jl_task_t *ct, jl_value_t *o)
702702
{
703-
JL_LOCK_NOGC(&finalizers_lock);
703+
JL_SPIN_LOCK_NOGC(&finalizers_lock);
704704
// Copy the finalizers into a temporary list so that code in the finalizer
705705
// won't change the list as we loop through them.
706706
// This list is also used as the GC frame when we are running the finalizers
@@ -725,7 +725,7 @@ JL_DLLEXPORT void jl_finalize_th(jl_task_t *ct, jl_value_t *o)
725725
jl_gc_run_finalizers_in_list(ct, &copied_list);
726726
}
727727
else {
728-
JL_UNLOCK_NOGC(&finalizers_lock);
728+
JL_SPIN_UNLOCK_NOGC(&finalizers_lock);
729729
}
730730
arraylist_free(&copied_list);
731731
}
@@ -3297,7 +3297,7 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
32973297
gc_cblist_pre_gc, (collection));
32983298

32993299
if (!jl_atomic_load_relaxed(&jl_gc_disable_counter)) {
3300-
JL_LOCK_NOGC(&finalizers_lock); // all the other threads are stopped, so this does not make sense, right? otherwise, failing that, this seems like plausibly a deadlock
3300+
JL_SPIN_LOCK_NOGC(&finalizers_lock); // all the other threads are stopped, so this does not make sense, right? otherwise, failing that, this seems like plausibly a deadlock
33013301
#ifndef __clang_gcanalyzer__
33023302
if (_jl_gc_collect(ptls, collection)) {
33033303
// recollect
@@ -3306,7 +3306,7 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection)
33063306
assert(!ret);
33073307
}
33083308
#endif
3309-
JL_UNLOCK_NOGC(&finalizers_lock);
3309+
JL_SPIN_UNLOCK_NOGC(&finalizers_lock);
33103310
}
33113311

33123312
gc_n_threads = 0;
@@ -3399,8 +3399,8 @@ void jl_init_thread_heap(jl_ptls_t ptls)
33993399
void jl_gc_init(void)
34003400
{
34013401

3402-
JL_MUTEX_INIT(&heapsnapshot_lock, "heapsnapshot_lock");
3403-
JL_MUTEX_INIT(&finalizers_lock, "finalizers_lock");
3402+
JL_SPIN_MUTEX_INIT(&heapsnapshot_lock, "heapsnapshot_lock");
3403+
JL_SPIN_MUTEX_INIT(&finalizers_lock, "finalizers_lock");
34043404
uv_mutex_init(&gc_cache_lock);
34053405
uv_mutex_init(&gc_perm_lock);
34063406

0 commit comments

Comments
 (0)