1 /** 2 Native Semaphore 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.semaphore; 12 import numem; 13 14 /** 15 Native implementation of a semaphore. 16 */ 17 export 18 abstract 19 class NativeSemaphore : NuObject { 20 public: 21 @nogc: 22 23 /** 24 Creates a new native semaphore using a linked backend. 25 26 Returns: 27 A new $(D NativeSemaphore) or $(D null) if the 28 platform does not support mutexes. 29 */ 30 static NativeSemaphore create(uint count) => _nu_semaphore_new(count); 31 32 /** 33 Signals the semaphore. 34 35 Note: 36 Control is not transferred to the waiter. 37 */ 38 abstract void signal(); 39 40 /** 41 Suspends the thread until the semaphore is signaled, 42 or the timeout is reached. 43 44 Params: 45 timeout = Timeout in miliseconds to block the 46 calling thread before giving up. 47 48 Returns: 49 $(D true) if the semaphore was signaled in time, 50 $(D false) otherwise. 51 */ 52 abstract bool await(ulong timeout = 0); 53 54 /** 55 Checks if the semaphore is signalled then 56 awaits on it if is. 57 58 Returns: 59 $(D true) if the semaphore was signalled, 60 $(D false) otherwise. 61 */ 62 abstract bool tryAwait(); 63 } 64 65 // 66 // FOR IMPLEMENTORS 67 // 68 69 private extern(C): 70 import core.attribute : weak; 71 72 version(DigitalMars) version = WeakIsBroken; 73 version(linux) version = WeakIsBroken; 74 version(WeakIsBroken) { 75 76 // Needed on Linux because we can't specify load order. 77 extern(C) extern NativeSemaphore _nu_semaphore_new(uint count) @nogc nothrow; 78 } else { 79 80 /* 81 Function which creates a new native semaphore. 82 */ 83 NativeSemaphore _nu_semaphore_new(uint count) @weak @nogc nothrow { return null; } 84 }