Alex Bikfalvi
SimWorker Documentation
SimWorker.h
00001 #pragma once 00002 00003 #include "SimWorkItem.h" 00004 00005 #ifdef WIN32 00006 #elif POSIX 00007 #include <sched.h> 00008 #include <errno.h> 00009 #endif 00010 00011 class CSimWorker 00012 { 00013 public: 00014 enum EWorkerState 00015 { 00016 WORKER_STATE_STOPPED = 0, 00017 WORKER_STATE_STARTING = 1, 00018 WORKER_STATE_STARTED_IDLE = 2, 00019 WORKER_STATE_STARTED_BUSY = 3, 00020 WORKER_STATE_STOPPING = 4 00021 }; 00022 00023 private: 00024 unsigned int id; 00025 EWorkerState state; 00026 00027 unsigned int queueSize; 00028 unsigned int queueCount; 00029 unsigned int queuePtr; 00030 CSimWorkItem** queue; 00031 00032 #ifdef WIN32 00033 HANDLE threadHandle; // Thread handle 00034 DWORD threadId; // Thread id 00035 CRITICAL_SECTION threadMutex; // Thread mutex : used to synchronize shared variables with the worker thread 00036 00037 HANDLE stateHandle; // State event handle 00038 HANDLE workHandle; // Work event handle 00039 #elif POSIX 00040 pthread_t threadHandle; // Thread handle 00041 pthread_mutex_t threadMutex; // Thread mutex 00042 00043 pthread_cond_t stateCond; // State condition 00044 pthread_mutex_t stateMutex; // State mutex 00045 00046 pthread_cond_t workCond; // Work condition 00047 pthread_mutex_t workMutex; // Work mutex 00048 #endif 00049 00050 public: 00051 CSimWorker( 00052 unsigned int id, 00053 unsigned int queueSize 00054 ); 00055 ~CSimWorker(); 00056 00057 void Start(); 00058 void Stop(); 00059 00060 void Enqueue(CSimWorkItem* item); 00061 00062 private: 00063 00064 #ifdef WIN32 00065 static DWORD WINAPI Execute(__in LPVOID parameter); 00066 #elif POSIX 00067 static void* Execute(void* parameter); 00068 #endif 00069 };
Last updated: February 8, 2011