core/thread/PosixThreads.h
author David Anderson <dvander@alliedmods.net>
Wed Mar 12 03:33:52 2008 +0000 (2008-03-12)
changeset 1925 61d7a244b641
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
reworked antiflood. it now has some logic in core to take care of loading order nastiness, and to fully prevent trigger spamming
dvander@1084
     1
/**
dvander@1084
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@1084
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@1084
    28
 *
dvander@1084
    29
 * Version: $Id$
dvander@1084
    30
 */
dvander@1084
    31
dvander@1084
    32
#ifndef _INCLUDE_POSIXTHREADS_H_
dvander@1084
    33
#define _INCLUDE_POSIXTHREADS_H_
dvander@1084
    34
dvander@1084
    35
#include <pthread.h>
dvander@1084
    36
#include "IThreader.h"
dvander@1084
    37
dvander@1084
    38
using namespace SourceMod;
dvander@1084
    39
dvander@1084
    40
void *Posix_ThreadGate(void *param);
dvander@1084
    41
dvander@1084
    42
class PosixThreader : public IThreader
dvander@1084
    43
{
dvander@1084
    44
public:
dvander@1084
    45
	class ThreadHandle : public IThreadHandle
dvander@1084
    46
	{
dvander@1084
    47
		friend class PosixThreader;
dvander@1084
    48
		friend void *Posix_ThreadGate(void *param);
dvander@1084
    49
	public:
dvander@1084
    50
		ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
dvander@1084
    51
		virtual ~ThreadHandle();
dvander@1084
    52
	public:
dvander@1084
    53
		virtual bool WaitForThread();
dvander@1084
    54
		virtual void DestroyThis();
dvander@1084
    55
		virtual IThreadCreator *Parent();
dvander@1084
    56
		virtual void GetParams(ThreadParams *ptparams);
dvander@1084
    57
		virtual ThreadPriority GetPriority();
dvander@1084
    58
		virtual bool SetPriority(ThreadPriority prio);
dvander@1084
    59
		virtual ThreadState GetState();
dvander@1084
    60
		virtual bool Unpause();
dvander@1084
    61
	protected:
dvander@1084
    62
		IThreader *m_parent;		//Parent handle
dvander@1084
    63
		pthread_t m_thread;			//Windows HANDLE	
dvander@1084
    64
		ThreadParams m_params;		//Current Parameters
dvander@1084
    65
		IThread *m_run;	//Runnable context
dvander@1084
    66
		pthread_mutex_t m_statelock;
dvander@1084
    67
		pthread_mutex_t m_runlock;
dvander@1084
    68
		ThreadState m_state;		//internal state
dvander@1084
    69
	};
dvander@1084
    70
	class PosixMutex : public IMutex
dvander@1084
    71
	{
dvander@1084
    72
	public:
dvander@1084
    73
		PosixMutex(pthread_mutex_t m) : m_mutex(m)
dvander@1084
    74
		{
dvander@1084
    75
		};
dvander@1084
    76
		virtual ~PosixMutex();
dvander@1084
    77
	public:
dvander@1084
    78
		virtual bool TryLock();
dvander@1084
    79
		virtual void Lock();
dvander@1084
    80
		virtual void Unlock();
dvander@1084
    81
		virtual void DestroyThis();
dvander@1084
    82
	protected:
dvander@1084
    83
		pthread_mutex_t m_mutex;
dvander@1084
    84
	};
dvander@1084
    85
	class PosixEventSignal : public IEventSignal
dvander@1084
    86
	{
dvander@1084
    87
	public:
dvander@1084
    88
		PosixEventSignal();
dvander@1084
    89
		virtual ~PosixEventSignal();
dvander@1084
    90
	public:
dvander@1084
    91
		virtual void Wait();
dvander@1084
    92
		virtual void Signal();
dvander@1084
    93
		virtual void DestroyThis();
dvander@1084
    94
	protected:
dvander@1084
    95
		pthread_cond_t m_cond;
dvander@1084
    96
		pthread_mutex_t m_mutex;
dvander@1084
    97
	};
dvander@1084
    98
public:
dvander@1084
    99
	IMutex *MakeMutex();
dvander@1084
   100
	void MakeThread(IThread *pThread);
dvander@1084
   101
	IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
dvander@1084
   102
	IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
dvander@1084
   103
	void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
dvander@1084
   104
	void ThreadSleep(unsigned int ms);
dvander@1084
   105
	IEventSignal *MakeEventSignal();
dvander@1090
   106
	IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
dvander@1084
   107
	void DestroyWorker(IThreadWorker *pWorker);
dvander@1084
   108
};
dvander@1084
   109
dvander@1084
   110
#if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
dvander@1084
   111
#define SM_MAIN_THREADER PosixThreader;
dvander@1084
   112
typedef class PosixThreader MainThreader;
dvander@1084
   113
#endif
dvander@1084
   114
dvander@1084
   115
#endif //_INCLUDE_POSIXTHREADS_H_