3 * ===============================================================
4 * SourceMod Threader API
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ===============================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "PosixThreads.h"
26 #include "ThreadWorker.h"
28 IThreadWorker *PosixThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
32 return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
34 return new BaseWorker(hooks);
38 void PosixThreader::DestroyWorker(IThreadWorker *pWorker)
43 void PosixThreader::ThreadSleep(unsigned int ms)
48 void PosixThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
50 max = ThreadPrio_Normal;
51 min = ThreadPrio_Normal;
54 IMutex *PosixThreader::MakeMutex()
56 pthread_mutex_t mutex;
58 if (pthread_mutex_init(&mutex, NULL) != 0)
61 PosixMutex *pMutex = new PosixMutex(mutex);
67 void PosixThreader::MakeThread(IThread *pThread)
69 ThreadParams defparams;
71 defparams.flags = Thread_AutoRelease;
72 defparams.prio = ThreadPrio_Normal;
74 MakeThread(pThread, &defparams);
77 IThreadHandle *PosixThreader::MakeThread(IThread *pThread, ThreadFlags flags)
79 ThreadParams defparams;
81 defparams.flags = flags;
82 defparams.prio = ThreadPrio_Normal;
84 return MakeThread(pThread, &defparams);
87 void *Posix_ThreadGate(void *param)
89 PosixThreader::ThreadHandle *pHandle =
90 reinterpret_cast<PosixThreader::ThreadHandle *>(param);
92 //Block this thread from being started initially.
93 pthread_mutex_lock(&pHandle->m_runlock);
94 //if we get here, we've obtained the lock and are allowed to run.
95 //unlock and continue.
96 pthread_mutex_unlock(&pHandle->m_runlock);
98 pHandle->m_run->RunThread(pHandle);
101 pthread_mutex_lock(&pHandle->m_statelock);
102 pHandle->m_state = Thread_Done;
103 pHandle->GetParams(¶ms);
104 pthread_mutex_unlock(&pHandle->m_statelock);
106 pHandle->m_run->OnTerminate(pHandle, false);
107 if (params.flags & Thread_AutoRelease)
113 ThreadParams g_defparams;
114 IThreadHandle *PosixThreader::MakeThread(IThread *pThread, const ThreadParams *params)
117 params = &g_defparams;
119 PosixThreader::ThreadHandle *pHandle =
120 new PosixThreader::ThreadHandle(this, pThread, params);
122 pthread_mutex_lock(&pHandle->m_runlock);
125 err = pthread_create(&pHandle->m_thread, NULL, Posix_ThreadGate, (void *)pHandle);
129 pthread_mutex_unlock(&pHandle->m_runlock);
134 //Don't bother setting priority...
136 if (!(pHandle->m_params.flags & Thread_CreateSuspended))
138 pHandle->m_state = Thread_Running;
139 err = pthread_mutex_unlock(&pHandle->m_runlock);
141 pHandle->m_state = Thread_Paused;
147 IEventSignal *PosixThreader::MakeEventSignal()
149 return new PosixEventSignal();
156 PosixThreader::PosixMutex::~PosixMutex()
158 pthread_mutex_destroy(&m_mutex);
161 bool PosixThreader::PosixMutex::TryLock()
163 int err = pthread_mutex_trylock(&m_mutex);
168 void PosixThreader::PosixMutex::Lock()
170 pthread_mutex_lock(&m_mutex);
173 void PosixThreader::PosixMutex::Unlock()
175 pthread_mutex_unlock(&m_mutex);
178 void PosixThreader::PosixMutex::DestroyThis()
187 PosixThreader::ThreadHandle::ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params) :
188 m_parent(parent), m_params(*params), m_run(run), m_state(Thread_Paused)
190 pthread_mutex_init(&m_runlock, NULL);
191 pthread_mutex_init(&m_statelock, NULL);
194 PosixThreader::ThreadHandle::~ThreadHandle()
196 pthread_mutex_destroy(&m_runlock);
197 pthread_mutex_destroy(&m_statelock);
200 bool PosixThreader::ThreadHandle::WaitForThread()
204 if (pthread_join(m_thread, &arg) != 0)
210 ThreadState PosixThreader::ThreadHandle::GetState()
214 pthread_mutex_lock(&m_statelock);
216 pthread_mutex_unlock(&m_statelock);
221 IThreadCreator *PosixThreader::ThreadHandle::Parent()
226 void PosixThreader::ThreadHandle::DestroyThis()
228 if (m_params.flags & Thread_AutoRelease)
234 void PosixThreader::ThreadHandle::GetParams(ThreadParams *ptparams)
239 *ptparams = m_params;
242 ThreadPriority PosixThreader::ThreadHandle::GetPriority()
244 return ThreadPrio_Normal;
247 bool PosixThreader::ThreadHandle::SetPriority(ThreadPriority prio)
249 return (prio == ThreadPrio_Normal);
252 bool PosixThreader::ThreadHandle::Unpause()
254 if (m_state != Thread_Paused)
257 m_state = Thread_Running;
259 if (pthread_mutex_unlock(&m_runlock) != 0)
261 m_state = Thread_Paused;
272 PosixThreader::PosixEventSignal::PosixEventSignal()
274 pthread_cond_init(&m_cond, NULL);
275 pthread_mutex_init(&m_mutex, NULL);
278 PosixThreader::PosixEventSignal::~PosixEventSignal()
280 pthread_cond_destroy(&m_cond);
281 pthread_mutex_destroy(&m_mutex);
284 void PosixThreader::PosixEventSignal::Wait()
286 pthread_mutex_lock(&m_mutex);
287 pthread_cond_wait(&m_cond, &m_mutex);
288 pthread_mutex_unlock(&m_mutex);
291 void PosixThreader::PosixEventSignal::Signal()
293 pthread_mutex_lock(&m_mutex);
294 pthread_cond_broadcast(&m_cond);
295 pthread_mutex_unlock(&m_mutex);
298 void PosixThreader::PosixEventSignal::DestroyThis()