-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Thread struct to separate file (might be changed to class), Impl…
…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
Showing
8 changed files
with
155 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.