1 /** 2 A synchronization Mutex. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.mutex; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.reference; 24 /** 25 A synchronization Mutex. 26 27 Element used to synchronize multiple $(D Thread)s. Basically a binary $(D Semaphore). Guarantees that only one thread can ever acquire this lock at a time. Can be used to protect a critical section. Be careful to avoid deadlocks. 28 */ 29 @GodotBaseClass struct Mutex 30 { 31 enum string _GODOT_internal_name = "_Mutex"; 32 public: 33 @nogc nothrow: 34 union { godot_object _godot_object; Reference _GODOT_base; } 35 alias _GODOT_base this; 36 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 37 package(godot) __gshared bool _classBindingInitialized = false; 38 package(godot) static struct _classBinding 39 { 40 __gshared: 41 @GodotName("lock") GodotMethod!(void) lock; 42 @GodotName("try_lock") GodotMethod!(GodotError) tryLock; 43 @GodotName("unlock") GodotMethod!(void) unlock; 44 } 45 bool opEquals(in Mutex other) const { return _godot_object.ptr is other._godot_object.ptr; } 46 Mutex opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 47 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 48 mixin baseCasts; 49 static Mutex _new() 50 { 51 static godot_class_constructor constructor; 52 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_Mutex"); 53 if(constructor is null) return typeof(this).init; 54 return cast(Mutex)(constructor()); 55 } 56 @disable new(size_t s); 57 /** 58 Lock this `Mutex`, blocks until it is unlocked by the current owner. 59 */ 60 void lock() 61 { 62 checkClassBinding!(typeof(this))(); 63 ptrcall!(void)(_classBinding.lock, _godot_object); 64 } 65 /** 66 Try locking this `Mutex`, does not block. Returns $(D OK) on success, $(D ERR_BUSY) otherwise. 67 */ 68 GodotError tryLock() 69 { 70 checkClassBinding!(typeof(this))(); 71 return ptrcall!(GodotError)(_classBinding.tryLock, _godot_object); 72 } 73 /** 74 Unlock this `Mutex`, leaving it to other threads. 75 */ 76 void unlock() 77 { 78 checkClassBinding!(typeof(this))(); 79 ptrcall!(void)(_classBinding.unlock, _godot_object); 80 } 81 }