3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "PosixThreads.h"
34 #include "ThreadWorker.h"
36 IThreadWorker *PosixThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
40 return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
42 return new BaseWorker(hooks);
46 void PosixThreader::DestroyWorker(IThreadWorker *pWorker)
51 void PosixThreader::ThreadSleep(unsigned int ms)
56 void PosixThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
58 max = ThreadPrio_Normal;
59 min = ThreadPrio_Normal;
62 IMutex *PosixThreader::MakeMutex()
64 pthread_mutex_t mutex;
66 if (pthread_mutex_init(&mutex, NULL) != 0)
69 PosixMutex *pMutex = new PosixMutex(mutex);
75 void PosixThreader::MakeThread(IThread *pThread)
77 ThreadParams defparams;
79 defparams.flags = Thread_AutoRelease;
80 defparams.prio = ThreadPrio_Normal;
82 MakeThread(pThread, &defparams);
85 IThreadHandle *PosixThreader::MakeThread(IThread *pThread, ThreadFlags flags)
87 ThreadParams defparams;
89 defparams.flags = flags;
90 defparams.prio = ThreadPrio_Normal;
92 return MakeThread(pThread, &defparams);
95 void *Posix_ThreadGate(void *param)
97 PosixThreader::ThreadHandle *pHandle =
98 reinterpret_cast<PosixThreader::ThreadHandle *>(param);
100 //Block this thread from being started initially.
101 pthread_mutex_lock(&pHandle->m_runlock);
102 //if we get here, we've obtained the lock and are allowed to run.
103 //unlock and continue.
104 pthread_mutex_unlock(&pHandle->m_runlock);
106 pHandle->m_run->RunThread(pHandle);
109 pthread_mutex_lock(&pHandle->m_statelock);
110 pHandle->m_state = Thread_Done;
111 pHandle->GetParams(¶ms);
112 pthread_mutex_unlock(&pHandle->m_statelock);
114 pHandle->m_run->OnTerminate(pHandle, false);
115 if (params.flags & Thread_AutoRelease)
121 ThreadParams g_defparams;
122 IThreadHandle *PosixThreader::MakeThread(IThread *pThread, const ThreadParams *params)
125 params = &g_defparams;
127 PosixThreader::ThreadHandle *pHandle =
128 new PosixThreader::ThreadHandle(this, pThread, params);
130 pthread_mutex_lock(&pHandle->m_runlock);
133 err = pthread_create(&pHandle->m_thread, NULL, Posix_ThreadGate, (void *)pHandle);
137 pthread_mutex_unlock(&pHandle->m_runlock);
142 //Don't bother setting priority...
144 if (!(pHandle->m_params.flags & Thread_CreateSuspended))
146 pHandle->m_state = Thread_Running;
147 err = pthread_mutex_unlock(&pHandle->m_runlock);
149 pHandle->m_state = Thread_Paused;
155 IEventSignal *PosixThreader::MakeEventSignal()
157 return new PosixEventSignal();
164 PosixThreader::PosixMutex::~PosixMutex()
166 pthread_mutex_destroy(&m_mutex);
169 bool PosixThreader::PosixMutex::TryLock()
171 int err = pthread_mutex_trylock(&m_mutex);
176 void PosixThreader::PosixMutex::Lock()
178 pthread_mutex_lock(&m_mutex);
181 void PosixThreader::PosixMutex::Unlock()
183 pthread_mutex_unlock(&m_mutex);
186 void PosixThreader::PosixMutex::DestroyThis()
195 PosixThreader::ThreadHandle::ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params) :
196 m_parent(parent), m_params(*params), m_run(run), m_state(Thread_Paused)
198 pthread_mutex_init(&m_runlock, NULL);
199 pthread_mutex_init(&m_statelock, NULL);
202 PosixThreader::ThreadHandle::~ThreadHandle()
204 pthread_mutex_destroy(&m_runlock);
205 pthread_mutex_destroy(&m_statelock);
208 bool PosixThreader::ThreadHandle::WaitForThread()
212 if (pthread_join(m_thread, &arg) != 0)
218 ThreadState PosixThreader::ThreadHandle::GetState()
222 pthread_mutex_lock(&m_statelock);
224 pthread_mutex_unlock(&m_statelock);
229 IThreadCreator *PosixThreader::ThreadHandle::Parent()
234 void PosixThreader::ThreadHandle::DestroyThis()
236 if (m_params.flags & Thread_AutoRelease)
242 void PosixThreader::ThreadHandle::GetParams(ThreadParams *ptparams)
247 *ptparams = m_params;
250 ThreadPriority PosixThreader::ThreadHandle::GetPriority()
252 return ThreadPrio_Normal;
255 bool PosixThreader::ThreadHandle::SetPriority(ThreadPriority prio)
257 return (prio == ThreadPrio_Normal);
260 bool PosixThreader::ThreadHandle::Unpause()
262 if (m_state != Thread_Paused)
265 m_state = Thread_Running;
267 if (pthread_mutex_unlock(&m_runlock) != 0)
269 m_state = Thread_Paused;
280 PosixThreader::PosixEventSignal::PosixEventSignal()
282 pthread_cond_init(&m_cond, NULL);
283 pthread_mutex_init(&m_mutex, NULL);
286 PosixThreader::PosixEventSignal::~PosixEventSignal()
288 pthread_cond_destroy(&m_cond);
289 pthread_mutex_destroy(&m_mutex);
292 void PosixThreader::PosixEventSignal::Wait()
294 pthread_mutex_lock(&m_mutex);
295 pthread_cond_wait(&m_cond, &m_mutex);
296 pthread_mutex_unlock(&m_mutex);
299 void PosixThreader::PosixEventSignal::Signal()
301 pthread_mutex_lock(&m_mutex);
302 pthread_cond_broadcast(&m_cond);
303 pthread_mutex_unlock(&m_mutex);
306 void PosixThreader::PosixEventSignal::DestroyThis()