3 * ================================================================
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 * version 3.0, as published by the Free Software Foundation.
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, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #ifndef _INCLUDE_POSIXTHREADS_H_
34 #define _INCLUDE_POSIXTHREADS_H_
37 #include "IThreader.h"
39 using namespace SourceMod;
41 void *Posix_ThreadGate(void *param);
43 class PosixThreader : public IThreader
46 class ThreadHandle : public IThreadHandle
48 friend class PosixThreader;
49 friend void *Posix_ThreadGate(void *param);
51 ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
52 virtual ~ThreadHandle();
54 virtual bool WaitForThread();
55 virtual void DestroyThis();
56 virtual IThreadCreator *Parent();
57 virtual void GetParams(ThreadParams *ptparams);
58 virtual ThreadPriority GetPriority();
59 virtual bool SetPriority(ThreadPriority prio);
60 virtual ThreadState GetState();
61 virtual bool Unpause();
63 IThreader *m_parent; //Parent handle
64 pthread_t m_thread; //Windows HANDLE
65 ThreadParams m_params; //Current Parameters
66 IThread *m_run; //Runnable context
67 pthread_mutex_t m_statelock;
68 pthread_mutex_t m_runlock;
69 ThreadState m_state; //internal state
71 class PosixMutex : public IMutex
74 PosixMutex(pthread_mutex_t m) : m_mutex(m)
77 virtual ~PosixMutex();
79 virtual bool TryLock();
81 virtual void Unlock();
82 virtual void DestroyThis();
84 pthread_mutex_t m_mutex;
86 class PosixEventSignal : public IEventSignal
90 virtual ~PosixEventSignal();
93 virtual void Signal();
94 virtual void DestroyThis();
96 pthread_cond_t m_cond;
97 pthread_mutex_t m_mutex;
101 void MakeThread(IThread *pThread);
102 IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
103 IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
104 void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
105 void ThreadSleep(unsigned int ms);
106 IEventSignal *MakeEventSignal();
107 IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
108 void DestroyWorker(IThreadWorker *pWorker);
111 #if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
112 #define SM_MAIN_THREADER PosixThreader;
113 typedef class PosixThreader MainThreader;
116 #endif //_INCLUDE_POSIXTHREADS_H_