Fix to to too two times
[oweals/minetest.git] / src / jthread / win32 / jthread.cpp
1 /*
2
3     This file is a part of the JThread package, which contains some object-
4     oriented thread wrappers for different thread implementations.
5
6     Copyright (c) 2000-2006  Jori Liesenborgs (jori.liesenborgs@gmail.com)
7
8     Permission is hereby granted, free of charge, to any person obtaining a
9     copy of this software and associated documentation files (the "Software"),
10     to deal in the Software without restriction, including without limitation
11     the rights to use, copy, modify, merge, publish, distribute, sublicense,
12     and/or sell copies of the Software, and to permit persons to whom the
13     Software is furnished to do so, subject to the following conditions:
14
15     The above copyright notice and this permission notice shall be included in
16     all copies or substantial portions of the Software.
17
18     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24     DEALINGS IN THE SOFTWARE.
25
26 */
27
28 #include "jthread/jthread.h"
29 #include <assert.h>
30 #define UNUSED(expr) do { (void)(expr); } while (0)
31 #ifndef _WIN32_WCE
32         #include <process.h>
33 #endif // _WIN32_WCE
34
35 JThread::JThread()
36 {
37         retval = NULL;
38         requeststop = false;
39         running = false;
40 }
41
42 JThread::~JThread()
43 {
44         Kill();
45 }
46
47 void JThread::Wait() {
48         if (running)
49         {
50                 WaitForSingleObject(threadhandle, INFINITE);
51         }
52 }
53
54 int JThread::Start()
55 {
56         if (running)
57         {
58                 return ERR_JTHREAD_ALREADYRUNNING;
59         }
60         requeststop = false;
61
62         continuemutex.Lock();
63 #ifndef _WIN32_WCE
64         threadhandle = (HANDLE)_beginthreadex(NULL,0,TheThread,this,0,&threadid);
65 #else
66         threadhandle = CreateThread(NULL,0,TheThread,this,0,&threadid);
67 #endif // _WIN32_WCE
68         if (threadhandle == NULL)
69         {
70                 continuemutex.Unlock();
71                 return ERR_JTHREAD_CANTSTARTTHREAD;
72         }
73
74         /* Wait until 'running' is set */
75         while (!running)
76         {
77                 Sleep(1);
78         }
79
80         continuemutex.Unlock();
81
82         continuemutex2.Lock();
83         continuemutex2.Unlock();
84
85         return 0;
86 }
87
88 int JThread::Kill()
89 {
90         if (!running)
91         {
92                 return ERR_JTHREAD_NOTRUNNING;
93         }
94         TerminateThread(threadhandle,0);
95         CloseHandle(threadhandle);
96         running = false;
97         return 0;
98 }
99
100 void *JThread::GetReturnValue()
101 {
102         void *val;
103
104         if (running) {
105                 val = NULL;
106         } else {
107                 val = retval;
108         }
109         return val;
110 }
111
112 bool JThread::IsSameThread()
113 {
114         return GetCurrentThreadId() == threadid;
115 }
116
117 #ifndef _WIN32_WCE
118 UINT __stdcall JThread::TheThread(void *param)
119 #else
120 DWORD WINAPI JThread::TheThread(void *param)
121 #endif // _WIN32_WCE
122 {
123         JThread *jthread;
124         void *ret;
125
126         jthread = (JThread *)param;
127
128         jthread->continuemutex2.Lock();
129         jthread->running = true;
130
131         jthread->continuemutex.Lock();
132         jthread->continuemutex.Unlock();
133
134         ret = jthread->Thread();
135
136         jthread->running = false;
137         jthread->retval = ret;
138         CloseHandle(jthread->threadhandle);
139         return 0;
140 }
141
142 void JThread::ThreadStarted()
143 {
144         continuemutex2.Unlock();
145 }
146