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