Skip to content

Commit

Permalink
Move Thread struct to separate file (might be changed to class), Impl…
Browse files Browse the repository at this point in the history
…ement Runable (abstract class for Thread functions), some cleanup
  • Loading branch information
NTX authored and NTX committed May 12, 2012
1 parent 8f3549b commit 98f4b84
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 123 deletions.
19 changes: 9 additions & 10 deletions Application/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "Application.h"

void* CallFreezeDetector(void* obj)
void* CallRunnable(void* obj)
{
((FreezeDetector*)obj)->_process(NULL);
reinterpret_cast<Runnable*>(obj)->Run();
return NULL;
}

void FreezeDetector::Run()
void Runnable::Execute()
{
_detectorThread = Thread::CreateThread(CallFreezeDetector, this);
_thread = Thread::CreateThread(CallRunnable, this);
}

void* FreezeDetector::_process(void*)
void FreezeDetector::Run()
{
_active = true;
_detectorThread->status = THREAD_ACTIVE;
_thread->status = THREAD_ACTIVE;
while (!_exit)
{
if (_maxDiffTime < _diff)
Expand All @@ -29,9 +29,8 @@ void* FreezeDetector::_process(void*)
}
usleep(_maxDiffTime - _diff);
}
_detectorThread->status = THREAD_EXIT;
_thread->status = THREAD_EXIT;
_active = false;
return NULL;
}

Application::Application(int argc, char* argv[], const char *conf):
Expand Down Expand Up @@ -500,7 +499,7 @@ uint32 Application::Update()

int ProcessQueue = threadMgr->CreateThread("Queue", &CallProcessQueue, handler);
if (BoolConfigs[CONFIG_BOOL_ENABLE_FREEZE_DETECTOR])
freezeDetector->Run();
freezeDetector->Execute();
if (ProcessQueue == -1)
{
sLog->outError("[Recv Thread] Executing Packet Handling Queue Failed Error(%d): %s", errno, strerror(errno));
Expand Down Expand Up @@ -616,9 +615,9 @@ uint32 Application::Update()

libLoader->close();
socketMgr->CloseAllSockets();
freezeDetector->Exit();
close(ServerSocket);
threadMgr->JoinThead(ProcessQueue, NULL);
threadMgr->JoinThead(ProcessQueue, NULL);
sLog->outControl("[Main Process] Exiting Application");
return 1;
}
32 changes: 24 additions & 8 deletions Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,45 @@ extern SimpleLog* sLog;

void* CallFreezeDetector(void* obj);

class FreezeDetector
class Runnable
{
friend void* CallFreezeDetector(void* obj);
friend void* CallRunnable(void* obj);

public:
Runnable(): _thread(NULL) { }

virtual void Run() { }

void Kill() { if (_thread) _thread->Kill(); }
void Suspend() { if (_thread) _thread->Suspend(); }
void Terminate() { if (_thread) _thread->Terminate(); }
void Continue() { if (_thread) _thread->Continue(); }

void Execute();

int GetStatus() const { if (_thread) return _thread->status; return 0; }
Thread* GetThread() { return _thread; }

protected:
Thread* _thread;
};

class FreezeDetector : public Runnable
{
public:
FreezeDetector(uint64 maxDiffTime, uint64& diff):
_maxDiffTime(maxDiffTime), _diff(diff), _exit(false), _pause(false), _active(false), _detectorThread(NULL) { }
_maxDiffTime(maxDiffTime), _diff(diff), _exit(false), _pause(false), _active(false) { }

void Run();
void Exit() { _exit = true; }
void Pause() { _pause = true; }
void Continue() { if (_detectorThread) _detectorThread->Continue(); }

int GetStatus() const { if (_detectorThread) return _detectorThread->status; return 0; }

private:
uint64 _maxDiffTime;
uint64& _diff;
bool _exit;
bool _pause;
bool _active;
void* _process(void*);
Thread* _detectorThread;
};


Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ set (STAT_CORE
Signal/SignalHandler.cpp
Sockets/Socket.cpp
Sockets/SocketMgr.cpp
Threads/Thread.cpp
Threads/ThreadMgr.cpp
ConfigMgr/ConfigMgr.cpp
Handling/Handler.cpp
Expand Down
2 changes: 1 addition & 1 deletion Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ typedef int int32 ;
typedef short int16 ;
typedef char int8 ;

typedef void (*handleFunc)(char*, int);// Used for opcode Handlers
typedef void* (*CalledFunction)(void*);

#define IN_KILOBYTES 1024
#define IN_MEGABYTES IN_KILOBYTES * IN_KILOBYTES
Expand Down
63 changes: 63 additions & 0 deletions Threads/Thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Thread.h"

Thread* Thread::CreateThread(CalledFunction func, void *args)
{
Thread* n = new Thread();
if (pthread_create(&n->thread, NULL, func, args) == 0)
{
n->status = THREAD_ACTIVE;
n->function = func;
n->args = args;
return n ;
}
return NULL ;
}

int Thread::Kill() const
{
return pthread_kill(thread, SIGKILL);
}

int Thread::Terminate() const
{
return pthread_kill(thread, SIGTERM);
}

int Thread::Interrupt() const
{
return pthread_kill(thread, SIGINT);
}

int Thread::Continue() const
{
return pthread_kill(thread, SIGCONT);
}

int Thread::SendSignal(int sig)
{
return pthread_kill(thread, sig);
}

void Thread::Suspend()
{
sigset_t suspendSig;
int sig = SIGCONT;
sigemptyset(&suspendSig);
sigaddset(&suspendSig, SIGCONT);
sigaddset(&suspendSig, SIGINT);
sigaddset(&suspendSig, SIGTERM);
status = THREAD_SUSPENDED;
sigwait(&suspendSig, &sig);
status = THREAD_ACTIVE;
}

void Thread::SuspendThisThread()
{
sigset_t suspendSig;
int sig = SIGCONT;
sigemptyset(&suspendSig);
sigaddset(&suspendSig, SIGCONT);
sigaddset(&suspendSig, SIGINT);
sigaddset(&suspendSig, SIGTERM);
sigwait(&suspendSig, &sig);
}
48 changes: 48 additions & 0 deletions Threads/Thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <pthread.h>
#include <signal.h>

#include "Defs.h"


#ifndef __Thread
#define __Thread

enum ThreadStatus
{
THREAD_STATUS_BEGIN = 1,
THREAD_ACTIVE,
THREAD_SUSPENDED,
THREAD_EXIT,
THREAD_STATUS_END
};

struct Thread
{
Thread(): id(0), status(0), attributes(0) { }
pthread_t thread;
uint32 id;
int status;
uint32 attributes;
CalledFunction function;
void* args;

static Thread* CreateThread(void* (*func)(void*), void* args);
operator pthread_t() { return thread; }
operator int() { return id; }

int Kill() const;
int Terminate() const;
int Interrupt() const;
int Continue() const;
int SendSignal(int sig);

void Suspend();
static void SuspendThisThread();
};

inline pthread_t GetThisThread()
{
return pthread_self();
}

#endif // __Thread
25 changes: 5 additions & 20 deletions Threads/ThreadMgr.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
#include "ThreadMgr.h"


Thread* Thread::CreateThread(void * (*func)(void *), void *args)
{
Thread* n = new Thread();
if (pthread_create(&n->thread, NULL, func, args) == 0)
{
n->status = THREAD_ACTIVE;
n->function = func;
n->args = args;
return n ;
}
return NULL ;
}



int ThreadMgr::CreateThread(void * (*func)(void *), void *args)
int ThreadMgr::CreateThread(CalledFunction func, void *args)
{
Thread n;
if (pthread_create(&n.thread, NULL, func, args) == 0)
Expand All @@ -29,7 +13,7 @@ int ThreadMgr::CreateThread(void * (*func)(void *), void *args)
return -1 ;
}

int ThreadMgr::CreateThread(string ThreadName, void * (*func)(void *), void *args)
int ThreadMgr::CreateThread(string ThreadName, CalledFunction func, void *args)
{
if (pThreadNames.find(ThreadName) != pThreadNames.end())
return -1;
Expand Down Expand Up @@ -57,15 +41,15 @@ bool ThreadMgr::CancelThread(string ThreadName)
return CancelThread(GetThreadIdByName(ThreadName));
}

bool ThreadMgr::JoinThead(uint32 threadId,void** retval)
bool ThreadMgr::JoinThead(uint32 threadId, void** retval)
{
if (threadId < pThreads.size())
if (pthread_join(getThread(threadId), retval) == 0)
return true;
return false;
}

bool ThreadMgr::JoinThead(string ThreadName,void** retval)
bool ThreadMgr::JoinThead(string ThreadName, void** retval)
{
return JoinThead(GetThreadIdByName(ThreadName), retval);
}
Expand Down Expand Up @@ -176,6 +160,7 @@ void ThreadMgr::SetThreadStatus(uint32 ThreadId, int Status)
/* */
/* Mutexes */
/* */

int ThreadMgr::CreateMutex(std::string MutexName, const pthread_mutexattr_t * Attr)
{
if (pMutexes.find(MutexName) != pMutexes.end())
Expand Down
Loading

0 comments on commit 98f4b84

Please sign in to comment.