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.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.classdb;
24 import godot.reference;
25 /**
26 A unit of execution in a process.
27 
28 Can run methods on $(D GodotObject)s simultaneously. The use of synchronization via $(D Mutex) or $(D Semaphore) is advised if working with shared objects.
29 $(B Note:) Breakpoints won't break on code if it's running in a thread. This is a current limitation of the GDScript debugger.
30 */
31 @GodotBaseClass struct Thread
32 {
33 	package(godot) enum string _GODOT_internal_name = "_Thread";
34 public:
35 @nogc nothrow:
36 	union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; }
37 	alias _GODOT_base this;
38 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
39 	package(godot) __gshared bool _classBindingInitialized = false;
40 	package(godot) static struct GDNativeClassBinding
41 	{
42 		__gshared:
43 		@GodotName("get_id") GodotMethod!(String) getId;
44 		@GodotName("is_active") GodotMethod!(bool) isActive;
45 		@GodotName("start") GodotMethod!(GodotError, GodotObject, String, Variant, long) start;
46 		@GodotName("wait_to_finish") GodotMethod!(Variant) waitToFinish;
47 	}
48 	/// 
49 	pragma(inline, true) bool opEquals(in Thread other) const
50 	{ return _godot_object.ptr is other._godot_object.ptr; }
51 	/// 
52 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
53 	{ _godot_object.ptr = n; return null; }
54 	/// 
55 	pragma(inline, true) bool opEquals(typeof(null) n) const
56 	{ return _godot_object.ptr is n; }
57 	/// 
58 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
59 	mixin baseCasts;
60 	/// Construct a new instance of Thread.
61 	/// Note: use `memnew!Thread` instead.
62 	static Thread _new()
63 	{
64 		static godot_class_constructor constructor;
65 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_Thread");
66 		if(constructor is null) return typeof(this).init;
67 		return cast(Thread)(constructor());
68 	}
69 	@disable new(size_t s);
70 	/// 
71 	enum Priority : int
72 	{
73 		/**
74 		A thread running with lower priority than normally.
75 		*/
76 		priorityLow = 0,
77 		/**
78 		A thread with a standard priority.
79 		*/
80 		priorityNormal = 1,
81 		/**
82 		A thread running with higher priority than normally.
83 		*/
84 		priorityHigh = 2,
85 	}
86 	/// 
87 	enum Constants : int
88 	{
89 		priorityLow = 0,
90 		priorityNormal = 1,
91 		priorityHigh = 2,
92 	}
93 	/**
94 	Returns the current $(D Thread)'s ID, uniquely identifying it among all threads. If the $(D Thread) is not running this returns an empty string.
95 	*/
96 	String getId() const
97 	{
98 		checkClassBinding!(typeof(this))();
99 		return ptrcall!(String)(GDNativeClassBinding.getId, _godot_object);
100 	}
101 	/**
102 	Returns `true` if this $(D Thread) is currently active. An active $(D Thread) cannot start work on a new method but can be joined with $(D waitToFinish).
103 	*/
104 	bool isActive() const
105 	{
106 		checkClassBinding!(typeof(this))();
107 		return ptrcall!(bool)(GDNativeClassBinding.isActive, _godot_object);
108 	}
109 	/**
110 	Starts a new $(D Thread) that runs `method` on object `instance` with `userdata` passed as an argument. Even if no userdata is passed, `method` must accept one argument and it will be null. The `priority` of the $(D Thread) can be changed by passing a value from the $(D priority) enum.
111 	Returns $(D constant OK) on success, or $(D constant ERR_CANT_CREATE) on failure.
112 	*/
113 	GodotError start(VariantArg2)(GodotObject instance, in String method, in VariantArg2 userdata = Variant.nil, in long priority = 1)
114 	{
115 		checkClassBinding!(typeof(this))();
116 		return ptrcall!(GodotError)(GDNativeClassBinding.start, _godot_object, instance, method, userdata, priority);
117 	}
118 	/**
119 	Joins the $(D Thread) and waits for it to finish. Returns what the method called returned.
120 	*/
121 	Variant waitToFinish()
122 	{
123 		checkClassBinding!(typeof(this))();
124 		return ptrcall!(Variant)(GDNativeClassBinding.waitToFinish, _godot_object);
125 	}
126 }