1 /** 2 A unit of execution in a process. 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.thread; 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 unit of execution in a process. 26 27 Can run methods on $(D GodotObject)s simultaneously. The use of synchronization via $(D Mutex), $(D Semaphore) is advised if working with shared objects. 28 */ 29 @GodotBaseClass struct Thread 30 { 31 enum string _GODOT_internal_name = "_Thread"; 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("start") GodotMethod!(GodotError, GodotObject, String, Variant, long) start; 42 @GodotName("get_id") GodotMethod!(String) getId; 43 @GodotName("is_active") GodotMethod!(bool) isActive; 44 @GodotName("wait_to_finish") GodotMethod!(Variant) waitToFinish; 45 } 46 bool opEquals(in Thread other) const { return _godot_object.ptr is other._godot_object.ptr; } 47 Thread opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 48 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 49 mixin baseCasts; 50 static Thread _new() 51 { 52 static godot_class_constructor constructor; 53 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_Thread"); 54 if(constructor is null) return typeof(this).init; 55 return cast(Thread)(constructor()); 56 } 57 @disable new(size_t s); 58 /// 59 enum Priority : int 60 { 61 /** 62 63 */ 64 priorityLow = 0, 65 /** 66 67 */ 68 priorityNormal = 1, 69 /** 70 71 */ 72 priorityHigh = 2, 73 } 74 /// 75 enum Constants : int 76 { 77 priorityLow = 0, 78 priorityNormal = 1, 79 priorityHigh = 2, 80 } 81 /** 82 Starts a new `Thread` that runs "method" on object "instance" with "userdata" passed as an argument. The "priority" of the `Thread` can be changed by passing a PRIORITY_* enum. 83 Returns OK on success, or ERR_CANT_CREATE on failure. 84 */ 85 GodotError start(StringArg1, VariantArg2)(GodotObject instance, in StringArg1 method, in VariantArg2 userdata = Variant.nil, in long priority = 1) 86 { 87 checkClassBinding!(typeof(this))(); 88 return ptrcall!(GodotError)(_classBinding.start, _godot_object, instance, method, userdata, priority); 89 } 90 /** 91 Returns the current `Thread`s id, uniquely identifying it among all threads. 92 */ 93 String getId() const 94 { 95 checkClassBinding!(typeof(this))(); 96 return ptrcall!(String)(_classBinding.getId, _godot_object); 97 } 98 /** 99 Returns true if this `Thread` is currently active. An active `Thread` cannot start work on a new method but can be joined with $(D waitToFinish). 100 */ 101 bool isActive() const 102 { 103 checkClassBinding!(typeof(this))(); 104 return ptrcall!(bool)(_classBinding.isActive, _godot_object); 105 } 106 /** 107 Joins the `Thread` and waits for it to finish. Returns what the method called returned. 108 */ 109 Variant waitToFinish() 110 { 111 checkClassBinding!(typeof(this))(); 112 return ptrcall!(Variant)(_classBinding.waitToFinish, _godot_object); 113 } 114 }