1 /*************************************************************************/
2 /* thread_windows.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "thread_windows.h"
32
33 #if defined(WINDOWS_ENABLED) && !defined(UWP_ENABLED)
34
35 #include "core/os/memory.h"
36
get_id() const37 Thread::ID ThreadWindows::get_id() const {
38
39 return id;
40 }
41
create_thread_windows()42 Thread *ThreadWindows::create_thread_windows() {
43
44 return memnew(ThreadWindows);
45 }
46
thread_callback(LPVOID userdata)47 DWORD ThreadWindows::thread_callback(LPVOID userdata) {
48
49 ThreadWindows *t = reinterpret_cast<ThreadWindows *>(userdata);
50
51 ScriptServer::thread_enter(); //scripts may need to attach a stack
52
53 t->id = (ID)GetCurrentThreadId(); // must implement
54 t->callback(t->user);
55 SetEvent(t->handle);
56
57 ScriptServer::thread_exit();
58
59 return 0;
60 }
61
create_func_windows(ThreadCreateCallback p_callback,void * p_user,const Settings &)62 Thread *ThreadWindows::create_func_windows(ThreadCreateCallback p_callback, void *p_user, const Settings &) {
63
64 ThreadWindows *tr = memnew(ThreadWindows);
65 tr->callback = p_callback;
66 tr->user = p_user;
67 tr->handle = CreateEvent(NULL, TRUE, FALSE, NULL);
68
69 QueueUserWorkItem(thread_callback, tr, WT_EXECUTELONGFUNCTION);
70
71 return tr;
72 }
get_thread_id_func_windows()73 Thread::ID ThreadWindows::get_thread_id_func_windows() {
74
75 return (ID)GetCurrentThreadId(); //must implement
76 }
wait_to_finish_func_windows(Thread * p_thread)77 void ThreadWindows::wait_to_finish_func_windows(Thread *p_thread) {
78
79 ThreadWindows *tp = static_cast<ThreadWindows *>(p_thread);
80 ERR_FAIL_COND(!tp);
81 WaitForSingleObject(tp->handle, INFINITE);
82 CloseHandle(tp->handle);
83 //`memdelete(tp);
84 }
85
make_default()86 void ThreadWindows::make_default() {
87
88 create_func = create_func_windows;
89 get_thread_id_func = get_thread_id_func_windows;
90 wait_to_finish_func = wait_to_finish_func_windows;
91 }
92
ThreadWindows()93 ThreadWindows::ThreadWindows() :
94 handle(NULL) {
95 }
96
~ThreadWindows()97 ThreadWindows::~ThreadWindows() {
98 }
99
100 #endif
101