Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
[SAOC Milestone 1, Task 2]
Browse files Browse the repository at this point in the history
+ Fix usage of shared in core.sync.mutex.
+ LWDR helper class now initialises global mutex
+ rt.monitor_ now hidden behind LWDR_Sync
+ Fix a few typos that prevented compilation

Tests:
Tests on STM32F407 has found the implementation working.
  • Loading branch information
hmmdyl committed Oct 15, 2021
1 parent 7559b09 commit f6a31d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
41 changes: 24 additions & 17 deletions source/core/sync/mutex.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,49 @@ class Mutex : Object.Monitor
private void* mutexHandle;

/// Constructor implementation
private mixin template Ctor()
{
mutexHandle = rtosbackend_mutexInit;
proxy.userMonitor = this;
this.__monitor = cast(void*)proxy;
}
private enum Ctor = q{
};

/// Initialise a new mutex object
this() @trusted nothrow @nogc
{
mixin(Ctor);
init();
}

/// ditto
shared this() @trusted nothrow @nogc
{
mixin(Ctor);
init();
}

/// Create mutex and assign it to the object's monitor.
private mixin template ObjCtor()
private void init(this Q)() @trusted nothrow @nogc
if(is(Q == Mutex) || is(Q == shared Mutex))
{
static if(is(Q == shared Mutex))
mutexHandle = cast(shared void*)rtosbackend_mutexInit;
else
mutexHandle = rtosbackend_mutexInit;

proxy.userMonitor = this;
this.__monitor = cast(void*)&proxy;
}

/// Create mutex and assign it to the object's monitor.
private enum ObjCtor = q{
assert(obj !is null);
assert(obj.__monitor is null);
this();
obj.__monitor = cast(void*)proxy;
}
obj.__monitor = cast(void*)&proxy;
};

/// Create mutex and assign it to the object's monitor.
this(Object o) @trusted nothrow @nogc
this(Object obj) @trusted nothrow @nogc
{
mixin(ObjCtor);
}

/// ditto
shared this(Object o) @trusted nothrow @nogc
shared this(Object obj) @trusted nothrow @nogc
{
mixin(ObjCtor);
}
Expand All @@ -84,7 +91,7 @@ class Mutex : Object.Monitor
final void lock_nothrow(this Q)() nothrow @trusted @nogc
if(is(Q == Mutex) || is(Q == shared Mutex))
{
rtosbackend_mutexLock(mutexHandle);
rtosbackend_mutexLock(cast(void*)mutexHandle);
}

/**
Expand All @@ -101,7 +108,7 @@ class Mutex : Object.Monitor
/// ditto
final void unlock_nothrow(this Q)() nothrow @trusted @nogc
{
rtosbackend_mutexUnlock(mutexHandle);
rtosbackend_mutexUnlock(cast(void*)mutexHandle);
}

/**
Expand All @@ -122,6 +129,6 @@ class Mutex : Object.Monitor
/// ditto
final bool tryLock_nothrow(this Q)() @trusted nothrow @nogc
{
return rtosbackend_mutexTryLock(void*) == 1;
return rtosbackend_mutexTryLock(cast(void*)mutexHandle) == 1;
}
}
10 changes: 10 additions & 0 deletions source/lwdr/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ static final class LWDR
/// Start the runtime. Must be called once per process and before any runtime functionality is used!
static void startRuntime() @trusted nothrow
{
version(LWDR_Sync)
{
import rt.monitor_;
__lwdr_monitor_init;
}
version(LWDR_ModuleCtors)
{
import rt.moduleinfo;
Expand All @@ -93,6 +98,11 @@ static final class LWDR
{
__lwdr_deinitLifetimeDelegate();
}
version(LWDR_Sync)
{
import rt.monitor_;
__lwdr_monitor_deinit;
}
}

version(LWDR_TLS)
Expand Down
16 changes: 9 additions & 7 deletions source/rt/monitor_.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module rt.monitor_;
version(LDC)
pragma(LDC_no_moduleinfo);

version(LWDR_Sync):

import rtoslink;
import lwdr.tracking;

Expand Down Expand Up @@ -76,7 +78,7 @@ private
{
__gshared void* globalMutex;

@property ref shared(Monitor)* monitor(return Object o) pure nothrow @nogc
@property ref shared(Monitor*) monitor(return Object o) pure nothrow @nogc
{ return *cast(shared Monitor**)&o.__monitor; }

shared(Monitor)* getMonitor(Object o) pure @nogc
Expand All @@ -101,26 +103,26 @@ private
rtosbackend_mutexLock(globalMutex);
if(getMonitor(o) is null)
{
m.referenceCount = 1;
setMonitor(o, cast(shared)m);
monitor.referenceCount = 1;
setMonitor(o, cast(shared)monitor);
success = true;
}
rtosbackend_mutexUnlock(globalMutex);

if(success)
return cast(shared(Monitor)*)m;
return cast(shared(Monitor)*)monitor;
else
{
deleteMonitor(m);
deleteMonitor(monitor);
return getMonitor(o);
}
}

/// Deletes mutex and monitor
void deleteMonitor(Monitor* m) @nogc nothrow
{
rtosbackend_mutexDestroy(monitor.mutex);
lwdrInternal_free(monitor);
rtosbackend_mutexDestroy(m.mutex);
lwdrInternal_free(m);
}
}

Expand Down

0 comments on commit f6a31d8

Please sign in to comment.