]> git.lizzy.rs Git - dragonfireclient.git/blob - src/jthread/pthread/jthread.cpp
CMake working on Linux (not on windows)
[dragonfireclient.git] / src / jthread / pthread / 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.h"
29 #include <sys/time.h>
30 #include <time.h>
31 #include <stdlib.h>
32
33 JThread::JThread()
34 {
35         retval = NULL;
36         mutexinit = false;
37         running = false;
38 }
39
40 JThread::~JThread()
41 {
42         Kill();
43 }
44
45 int JThread::Start()
46 {
47         int status;
48
49         if (!mutexinit)
50         {
51                 if (!runningmutex.IsInitialized())
52                 {
53                         if (runningmutex.Init() < 0)
54                                 return ERR_JTHREAD_CANTINITMUTEX;
55                 }
56                 if (!continuemutex.IsInitialized())
57                 {
58                         if (continuemutex.Init() < 0)
59                                 return ERR_JTHREAD_CANTINITMUTEX;
60                 }
61                 if (!continuemutex2.IsInitialized())
62                 {
63                         if (continuemutex2.Init() < 0)
64                                 return ERR_JTHREAD_CANTINITMUTEX;
65                 }
66                 mutexinit = true;
67         }
68         
69         runningmutex.Lock();
70         if (running)
71         {
72                 runningmutex.Unlock();
73                 return ERR_JTHREAD_ALREADYRUNNING;
74         }
75         runningmutex.Unlock();
76         
77         pthread_attr_t attr;
78         pthread_attr_init(&attr);
79         pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
80         
81         continuemutex.Lock();
82         status = pthread_create(&threadid,&attr,TheThread,this);        
83         pthread_attr_destroy(&attr);
84         if (status != 0)
85         {
86                 continuemutex.Unlock();
87                 return ERR_JTHREAD_CANTSTARTTHREAD;
88         }
89         
90         /* Wait until 'running' is set */
91         
92         runningmutex.Lock();                    
93         while (!running)
94         {
95                 runningmutex.Unlock();
96                 
97                 struct timespec req,rem;
98
99                 req.tv_sec = 0;
100                 req.tv_nsec = 1000000;
101                 nanosleep(&req,&rem);
102
103                 runningmutex.Lock();
104         }
105         runningmutex.Unlock();
106         
107         continuemutex.Unlock();
108         
109         continuemutex2.Lock();
110         continuemutex2.Unlock();
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         pthread_cancel(threadid);
123         running = false;
124         runningmutex.Unlock();
125         return 0;
126 }
127
128 bool JThread::IsRunning()
129 {
130         bool r;
131         
132         runningmutex.Lock();                    
133         r = running;
134         runningmutex.Unlock();
135         return r;
136 }
137
138 void *JThread::GetReturnValue()
139 {
140         void *val;
141         
142         runningmutex.Lock();
143         if (running)
144                 val = NULL;
145         else
146                 val = retval;
147         runningmutex.Unlock();
148         return val;
149 }
150
151 void *JThread::TheThread(void *param)
152 {
153         JThread *jthread;
154         void *ret;
155         
156         jthread = (JThread *)param;
157         
158         jthread->continuemutex2.Lock();
159         jthread->runningmutex.Lock();
160         jthread->running = true;
161         jthread->runningmutex.Unlock();
162         
163         jthread->continuemutex.Lock();
164         jthread->continuemutex.Unlock();
165         
166         ret = jthread->Thread();
167
168         jthread->runningmutex.Lock();
169         jthread->running = false;
170         jthread->retval = ret;
171         jthread->runningmutex.Unlock();
172
173         return NULL;
174 }
175
176 void JThread::ThreadStarted()
177 {
178         continuemutex2.Unlock();
179 }
180