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.
50 if (!runningmutex.IsInitialized())
52 if (runningmutex.Init() < 0)
53 return ERR_JTHREAD_CANTINITMUTEX;
55 if (!continuemutex.IsInitialized())
57 if (continuemutex.Init() < 0)
58 return ERR_JTHREAD_CANTINITMUTEX;
60 if (!continuemutex2.IsInitialized())
62 if (continuemutex2.Init() < 0)
63 return ERR_JTHREAD_CANTINITMUTEX;
70 runningmutex.Unlock();
71 return ERR_JTHREAD_ALREADYRUNNING;
73 runningmutex.Unlock();
77 threadhandle = (HANDLE)_beginthreadex(NULL,0,TheThread,this,0,&threadid);
79 threadhandle = CreateThread(NULL,0,TheThread,this,0,&threadid);
81 if (threadhandle == NULL)
83 continuemutex.Unlock();
84 return ERR_JTHREAD_CANTSTARTTHREAD;
87 /* Wait until 'running' is set */
92 runningmutex.Unlock();
96 runningmutex.Unlock();
98 continuemutex.Unlock();
100 continuemutex2.Lock();
101 continuemutex2.Unlock();
111 runningmutex.Unlock();
112 return ERR_JTHREAD_NOTRUNNING;
114 TerminateThread(threadhandle,0);
115 CloseHandle(threadhandle);
117 runningmutex.Unlock();
121 bool JThread::IsRunning()
127 runningmutex.Unlock();
131 void *JThread::GetReturnValue()
140 runningmutex.Unlock();
145 UINT __stdcall JThread::TheThread(void *param)
147 DWORD WINAPI JThread::TheThread(void *param)
153 jthread = (JThread *)param;
155 jthread->continuemutex2.Lock();
156 jthread->runningmutex.Lock();
157 jthread->running = true;
158 jthread->runningmutex.Unlock();
160 jthread->continuemutex.Lock();
161 jthread->continuemutex.Unlock();
163 ret = jthread->Thread();
165 jthread->runningmutex.Lock();
166 jthread->running = false;
167 jthread->retval = ret;
168 CloseHandle(jthread->threadhandle);
169 jthread->runningmutex.Unlock();
173 void JThread::ThreadStarted()
175 continuemutex2.Unlock();