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.
51 if (!runningmutex.IsInitialized())
53 if (runningmutex.Init() < 0)
54 return ERR_JTHREAD_CANTINITMUTEX;
56 if (!continuemutex.IsInitialized())
58 if (continuemutex.Init() < 0)
59 return ERR_JTHREAD_CANTINITMUTEX;
61 if (!continuemutex2.IsInitialized())
63 if (continuemutex2.Init() < 0)
64 return ERR_JTHREAD_CANTINITMUTEX;
72 runningmutex.Unlock();
73 return ERR_JTHREAD_ALREADYRUNNING;
75 runningmutex.Unlock();
78 pthread_attr_init(&attr);
79 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
82 status = pthread_create(&threadid,&attr,TheThread,this);
83 pthread_attr_destroy(&attr);
86 continuemutex.Unlock();
87 return ERR_JTHREAD_CANTSTARTTHREAD;
90 /* Wait until 'running' is set */
95 runningmutex.Unlock();
97 struct timespec req,rem;
100 req.tv_nsec = 1000000;
101 nanosleep(&req,&rem);
105 runningmutex.Unlock();
107 continuemutex.Unlock();
109 continuemutex2.Lock();
110 continuemutex2.Unlock();
119 runningmutex.Unlock();
120 return ERR_JTHREAD_NOTRUNNING;
122 pthread_cancel(threadid);
124 runningmutex.Unlock();
128 bool JThread::IsRunning()
134 runningmutex.Unlock();
138 void *JThread::GetReturnValue()
147 runningmutex.Unlock();
151 void *JThread::TheThread(void *param)
156 jthread = (JThread *)param;
158 jthread->continuemutex2.Lock();
159 jthread->runningmutex.Lock();
160 jthread->running = true;
161 jthread->runningmutex.Unlock();
163 jthread->continuemutex.Lock();
164 jthread->continuemutex.Unlock();
166 ret = jthread->Thread();
168 jthread->runningmutex.Lock();
169 jthread->running = false;
170 jthread->retval = ret;
171 jthread->runningmutex.Unlock();
176 void JThread::ThreadStarted()
178 continuemutex2.Unlock();