1 /**
2     Native Mutex Abstraction
3 
4     Copyright:
5         Copyright © 2023-2025, Kitsunebi Games
6         Copyright © 2023-2025, Inochi2D Project
7     
8     License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9     Authors:   Luna Nielsen
10 */
11 module nulib.threading.internal.mutex;
12 import numem;
13 
14 /**
15     Native implementation of a mutually exclusive lock.
16 */
17 export
18 abstract
19 class NativeMutex : NuObject {
20 public:
21 @nogc:
22 
23     /**
24         Creates a new native mutex using a linked backend.
25 
26         Returns:
27             A new $(D NativeMutex) or $(D null) if the
28             platform does not support mutexes.
29     */
30     static NativeMutex create() {
31         return _nu_mutex_new();
32     }
33 
34     /**
35         Increments the internal lock counter.
36     */
37     abstract void lock() nothrow @trusted;
38 
39     /**
40         Tries to increment the internal lock counter.
41 
42         Returns:
43             $(D true) if the mutex was locked,
44             $(D false) otherwise.
45     */
46     abstract bool tryLock() nothrow @trusted;
47 
48     /**
49         Decrements the internal lock counter,
50         If the counter reaches 0, the lock is released.
51     */
52     abstract void unlock() nothrow @trusted;
53 }
54 
55 //
56 //          FOR IMPLEMENTORS
57 //
58 
59 private extern(C):
60 import core.attribute : weak;
61 
62 version(DigitalMars) version = WeakIsBroken;
63 version(linux) version = WeakIsBroken;
64 version(WeakIsBroken) {
65 
66     // Needed on Linux because we can't specify load order.
67     extern(C) extern NativeMutex _nu_mutex_new() @nogc @trusted nothrow;
68 } else {
69     
70     /*
71         Backend function used to create a new mutex object.
72     */
73     NativeMutex _nu_mutex_new() @weak @nogc nothrow { return null; }
74 }