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"
34 #define UNUSED(expr) do { (void)(expr); } while (0)
49 void JThread::Wait() {
52 int pthread_join_retval = pthread_join(threadid,&status);
53 assert(pthread_join_retval == 0);
54 UNUSED(pthread_join_retval);
65 return ERR_JTHREAD_ALREADYRUNNING;
70 pthread_attr_init(&attr);
71 //pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
74 status = pthread_create(&threadid,&attr,TheThread,this);
75 pthread_attr_destroy(&attr);
78 continuemutex.Unlock();
79 return ERR_JTHREAD_CANTSTARTTHREAD;
82 /* Wait until 'running' is set */
86 struct timespec req,rem;
89 req.tv_nsec = 1000000;
94 continuemutex.Unlock();
96 continuemutex2.Lock();
97 continuemutex2.Unlock();
107 int pthread_join_retval = pthread_join(threadid,&status);
108 assert(pthread_join_retval == 0);
109 UNUSED(pthread_join_retval);
112 return ERR_JTHREAD_NOTRUNNING;
115 pthread_kill(threadid, SIGKILL);
117 pthread_cancel(threadid);
120 int pthread_join_retval = pthread_join(threadid,&status);
121 assert(pthread_join_retval == 0);
122 UNUSED(pthread_join_retval);
129 void *JThread::GetReturnValue()
142 bool JThread::IsSameThread()
144 return pthread_equal(pthread_self(), threadid);
147 void *JThread::TheThread(void *param)
152 jthread = (JThread *)param;
154 jthread->continuemutex2.Lock();
155 jthread->running = true;
157 jthread->continuemutex.Lock();
158 jthread->continuemutex.Unlock();
160 ret = jthread->Thread();
162 jthread->running = false;
167 void JThread::ThreadStarted()
169 continuemutex2.Unlock();