3 This file is a part of the JThread package, which contains some object-
4 oriented thread wrappers for different thread implementations.
6 Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
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:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
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.
28 #include "jthread/jthread.h"
47 void JThread::Stop() {
50 runningmutex.Unlock();
57 if (!runningmutex.IsInitialized())
59 if (runningmutex.Init() < 0)
60 return ERR_JTHREAD_CANTINITMUTEX;
62 if (!continuemutex.IsInitialized())
64 if (continuemutex.Init() < 0)
65 return ERR_JTHREAD_CANTINITMUTEX;
67 if (!continuemutex2.IsInitialized())
69 if (continuemutex2.Init() < 0)
70 return ERR_JTHREAD_CANTINITMUTEX;
77 runningmutex.Unlock();
78 return ERR_JTHREAD_ALREADYRUNNING;
81 runningmutex.Unlock();
85 threadhandle = (HANDLE)_beginthreadex(NULL,0,TheThread,this,0,&threadid);
87 threadhandle = CreateThread(NULL,0,TheThread,this,0,&threadid);
89 if (threadhandle == NULL)
91 continuemutex.Unlock();
92 return ERR_JTHREAD_CANTSTARTTHREAD;
95 /* Wait until 'running' is set */
100 runningmutex.Unlock();
104 runningmutex.Unlock();
106 continuemutex.Unlock();
108 continuemutex2.Lock();
109 continuemutex2.Unlock();
119 runningmutex.Unlock();
120 return ERR_JTHREAD_NOTRUNNING;
122 TerminateThread(threadhandle,0);
123 CloseHandle(threadhandle);
125 runningmutex.Unlock();
129 bool JThread::IsRunning()
135 runningmutex.Unlock();
139 bool JThread::StopRequested() {
144 runningmutex.Unlock();
148 void *JThread::GetReturnValue()
157 runningmutex.Unlock();
161 bool JThread::IsSameThread()
163 return GetCurrentThreadId() == threadid;
167 UINT __stdcall JThread::TheThread(void *param)
169 DWORD WINAPI JThread::TheThread(void *param)
175 jthread = (JThread *)param;
177 jthread->continuemutex2.Lock();
178 jthread->runningmutex.Lock();
179 jthread->running = true;
180 jthread->runningmutex.Unlock();
182 jthread->continuemutex.Lock();
183 jthread->continuemutex.Unlock();
185 ret = jthread->Thread();
187 jthread->runningmutex.Lock();
188 jthread->running = false;
189 jthread->retval = ret;
190 CloseHandle(jthread->threadhandle);
191 jthread->runningmutex.Unlock();
195 void JThread::ThreadStarted()
197 continuemutex2.Unlock();