]> git.lizzy.rs Git - minetest.git/blob - src/jthread/win32/jthread.cpp
Generic NodeMetadata text input
[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.h"
29
30 #ifndef _WIN32_WCE
31         #include <process.h>
32 #endif // _WIN32_WCE
33
34 JThread::JThread()
35 {
36         retval = NULL;
37         mutexinit = false;
38         running = false;
39 }
40
41 JThread::~JThread()
42 {
43         Kill();
44 }
45
46 int JThread::Start()
47 {
48         if (!mutexinit)
49         {
50                 if (!runningmutex.IsInitialized())
51                 {
52                         if (runningmutex.Init() < 0)
53                                 return ERR_JTHREAD_CANTINITMUTEX;
54                 }
55                 if (!continuemutex.IsInitialized())
56                 {
57                         if (continuemutex.Init() < 0)
58                                 return ERR_JTHREAD_CANTINITMUTEX;
59                 }
60                 if (!continuemutex2.IsInitialized())
61                 {
62                         if (continuemutex2.Init() < 0)
63                                 return ERR_JTHREAD_CANTINITMUTEX;
64                 }               mutexinit = true;
65         }
66         
67         runningmutex.Lock();
68         if (running)
69         {
70                 runningmutex.Unlock();
71                 return ERR_JTHREAD_ALREADYRUNNING;
72         }
73         runningmutex.Unlock();
74         
75         continuemutex.Lock();
76 #ifndef _WIN32_WCE
77         threadhandle = (HANDLE)_beginthreadex(NULL,0,TheThread,this,0,&threadid);
78 #else
79         threadhandle = CreateThread(NULL,0,TheThread,this,0,&threadid);
80 #endif // _WIN32_WCE
81         if (threadhandle == NULL)
82         {
83                 continuemutex.Unlock();
84                 return ERR_JTHREAD_CANTSTARTTHREAD;
85         }
86         
87         /* Wait until 'running' is set */
88
89         runningmutex.Lock();                    
90         while (!running)
91         {
92                 runningmutex.Unlock();
93                 Sleep(1);
94                 runningmutex.Lock();
95         }
96         runningmutex.Unlock();
97         
98         continuemutex.Unlock();
99         
100         continuemutex2.Lock();
101         continuemutex2.Unlock();
102                 
103         return 0;
104 }
105
106 int JThread::Kill()
107 {
108         runningmutex.Lock();                    
109         if (!running)
110         {
111                 runningmutex.Unlock();
112                 return ERR_JTHREAD_NOTRUNNING;
113         }
114         TerminateThread(threadhandle,0);
115         CloseHandle(threadhandle);
116         running = false;
117         runningmutex.Unlock();
118         return 0;
119 }
120
121 bool JThread::IsRunning()
122 {
123         bool r;
124         
125         runningmutex.Lock();                    
126         r = running;
127         runningmutex.Unlock();
128         return r;
129 }
130
131 void *JThread::GetReturnValue()
132 {
133         void *val;
134         
135         runningmutex.Lock();
136         if (running)
137                 val = NULL;
138         else
139                 val = retval;
140         runningmutex.Unlock();
141         return val;
142 }
143
144 #ifndef _WIN32_WCE
145 UINT __stdcall JThread::TheThread(void *param)
146 #else
147 DWORD WINAPI JThread::TheThread(void *param)
148 #endif // _WIN32_WCE
149 {
150         JThread *jthread;
151         void *ret;
152
153         jthread = (JThread *)param;
154         
155         jthread->continuemutex2.Lock();
156         jthread->runningmutex.Lock();
157         jthread->running = true;
158         jthread->runningmutex.Unlock();
159         
160         jthread->continuemutex.Lock();
161         jthread->continuemutex.Unlock();
162         
163         ret = jthread->Thread();
164         
165         jthread->runningmutex.Lock();
166         jthread->running = false;
167         jthread->retval = ret;
168         CloseHandle(jthread->threadhandle);
169         jthread->runningmutex.Unlock();
170         return 0;               
171 }
172
173 void JThread::ThreadStarted()
174 {
175         continuemutex2.Unlock();
176 }
177