Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle concurrent initialization of modules #3124

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions nativelib/src/main/resources/scala-native/module_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifdef SCALANATIVE_MULTITHREADING_ENABLED
#include "stdatomic.h"
#include "nativeThreadTLS.h"
#include "gc/shared/ScalaNativeGC.h"

#ifdef _WIN32
#include "windows.h"
#define YieldThread() SwitchToThread()
#else
#include "sched.h"
#define YieldThread() sched_yield()
#endif

#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h> // for nanosleep
#else
#include <unistd.h> // for usleep
#endif

// cross-platform sleep function
static void sleep_ms(int milliseconds) {
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
if (milliseconds >= 1000)
sleep(milliseconds / 1000);
usleep((milliseconds % 1000) * 1000);
#endif
}

typedef _Atomic(void **) ModuleRef;
typedef ModuleRef *ModuleSlot;
typedef void (*ModuleCtor)(ModuleRef);
typedef struct InitializationContext {
NativeThread thread;
ModuleRef instance;
} InitializationContext;

ModuleRef __scalanative_loadModule(ModuleSlot slot, void *classInfo,
size_t size, ModuleCtor ctor) {
ModuleRef module = atomic_load_explicit(slot, memory_order_acquire);

if (module == NULL) {
InitializationContext ctx = {};
void **expected = NULL;
if (atomic_compare_exchange_strong(slot, &expected, (void **)&ctx)) {
ModuleRef instance = scalanative_alloc(classInfo, size);
ctx.thread = currentNativeThread;
ctx.instance = instance;
ctor(instance);
atomic_store_explicit(slot, instance, memory_order_release);
return instance;
}
}

// Wait in loop until initialization is finished
if (*module != classInfo) {
int spin = 0;
while (*module != classInfo) {
InitializationContext *ctx = (InitializationContext *)module;
// Usage of module in it's constructor, return unitializied instance
// thread=null can happen only in the main thread initialization
if (ctx->thread == currentNativeThread || ctx->thread == NULL) {
return ctx->instance;
}
if (spin++ < 32)
YieldThread();
else
sleep_ms(1);
scalanative_gc_safepoint_poll();
module = atomic_load_explicit(slot, memory_order_acquire);
}
}
return module;
}
#endif
100 changes: 79 additions & 21 deletions tools/src/main/scala/scala/scalanative/codegen/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ object Generate {
buf += Defn.Var(Attrs.None, stackBottomName, Type.Ptr, Val.Null)

def genModuleAccessors(): Unit = {
val LoadModuleSig = Type.Function(
Seq(Type.Ptr, Type.Ptr, Type.Size, Type.Ptr),
Type.Ptr
)
val LoadModule =
Val.Global(extern("__scalanative_loadModule"), Type.Ptr)

val useSynchronizedAccessors = meta.config.multithreadingSupport
if (useSynchronizedAccessors) {
buf += Defn.Declare(
Attrs(isExtern = true),
LoadModule.name,
LoadModuleSig
)
}

meta.classes.foreach { cls =>
if (cls.isModule && cls.allocated) {
val name = cls.name
Expand All @@ -265,14 +281,10 @@ object Generate {
val alloc = Val.Local(fresh(), clsTy)

if (cls.isConstantModule) {
val moduleTyName =
name.member(Sig.Generated("type"))
val moduleTyVal =
Val.Global(moduleTyName, Type.Ptr)
val instanceName =
name.member(Sig.Generated("instance"))
val instanceVal =
Val.StructValue(Seq(moduleTyVal))
val moduleTyName = name.member(Sig.Generated("type"))
val moduleTyVal = Val.Global(moduleTyName, Type.Ptr)
val instanceName = name.member(Sig.Generated("instance"))
val instanceVal = Val.StructValue(Seq(moduleTyVal))
val instanceDefn = Defn.Const(
Attrs.None,
instanceName,
Expand All @@ -287,21 +299,28 @@ object Generate {

val loadName = name.member(Sig.Generated("load"))
val loadSig = Type.Function(Seq.empty, clsTy)
val loadDefn = Defn.Define(
Attrs(inlineHint = Attr.NoInline),
loadName,
loadSig,

val selectSlot = Op.Elem(
Type.Ptr,
Val.Global(moduleArrayName, Type.Ptr),
Seq(Val.Int(meta.moduleArray.index(cls)))
)

/* singlethreaded module load
* Uses simplified algorithm with lower overhead
* val instance = module[moduleId]
* if (instance != null) instance
* else {
* val instance = alloc
* module[moduleId] = instance
* moduleCtor(instance)
* instance
* }
*/
def loadSinglethreadImpl: Seq[Inst] = {
Seq(
Inst.Label(entry, Seq.empty),
Inst.Let(
slot.name,
Op.Elem(
Type.Ptr,
Val.Global(moduleArrayName, Type.Ptr),
Seq(Val.Int(meta.moduleArray.index(cls)))
),
Next.None
),
Inst.Let(slot.name, selectSlot, Next.None),
Inst.Let(self.name, Op.Load(clsTy, slot), Next.None),
Inst.Let(
cond.name,
Expand All @@ -317,6 +336,45 @@ object Generate {
Inst.Let(Op.Call(initSig, init, Seq(alloc)), Next.None),
Inst.Ret(alloc)
)
}

/* // Multithreading-safe module load
* val slot = modules.at(moduleId)
* return __scalanative_loadModule(slot, rtti, size, ctor)
*
* Underlying C function implements the main logic of module initialization and synchronization.
* Safety of safe multithreaded initialization comes with the increased complexity and overhead.
* For single-threaded usage we use the old implementation
*/
def loadMultithreadingSafeImpl: Seq[Inst] = {
val size = meta.layout(cls).size
val rtti = meta.rtti(cls).const

Seq(
Inst.Label(entry, Seq.empty),
Inst.Let(slot.name, selectSlot, Next.None),
Inst.Let(
self.name,
Op.Call(
LoadModuleSig,
LoadModule,
Seq(slot, rtti, Val.Size(size), init)
),
Next.None
),
Inst.Ret(self)
)
}

val loadDefn = Defn.Define(
Attrs(inlineHint =
if (useSynchronizedAccessors) Attr.MayInline
else Attr.NoInline
),
loadName,
loadSig,
if (useSynchronizedAccessors) loadMultithreadingSafeImpl
else loadSinglethreadImpl
)

buf += loadDefn
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package scala.scalanative.runtime

import java.lang.Runtime

import org.junit.Test
import org.junit.Assert._
import scala.scalanative.junit.utils.AssumesHelper

object TestModule {
val slowInitField = {
assertNotNull(this)
assertNotNull(this.getClass())
Thread.sleep(1000)
42
}
}

class ModuleInitializationTest {
@Test def initializeFromMultipleThreads(): Unit = {
AssumesHelper.assumeMultithreadingIsEnabled()

val latch = new java.util.concurrent.CountDownLatch(1)
val threads = Seq.fill(4) {
new Thread(() => {
latch.await()
assertEquals(42, TestModule.slowInitField)
})
}
threads.foreach(_.start())
Thread.sleep(100)
latch.countDown()
threads.foreach(_.join())
assertEquals(42, TestModule.slowInitField)
}
}