You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
#include "thread/Thread.h" |
|
|
|
#include <pthread.h> |
|
#include <stdlib.h> |
|
#include <stdio.h> |
|
#include <signal.h> |
|
|
|
typedef struct ThreadPthreadImpl |
|
{ |
|
pthread_t thread; |
|
} ThreadPthreadImpl; |
|
|
|
|
|
Thread* createThread(ThreadFn threadFn, void* arg) |
|
{ |
|
ThreadPthreadImpl* pthreadImpl = NULL; |
|
Thread* thread = calloc(1, sizeof(Thread)); |
|
thread->impl = calloc(1, sizeof(ThreadPthreadImpl)); |
|
pthreadImpl = (ThreadPthreadImpl*)thread->impl; |
|
if (pthread_create(&(pthreadImpl->thread), NULL, threadFn, arg) != 0) { |
|
printf("Thread: Failed to create thread.\n"); |
|
} |
|
return thread; |
|
} |
|
|
|
void joinThread(Thread* thread) |
|
{ |
|
ThreadPthreadImpl* pthreadImpl = (ThreadPthreadImpl*)thread->impl; |
|
pthread_join(pthreadImpl->thread, NULL); |
|
} |
|
|
|
void killThread(Thread* thread) |
|
{ |
|
ThreadPthreadImpl* pthreadImpl = (ThreadPthreadImpl*)thread->impl; |
|
pthread_kill(pthreadImpl->thread, SIGUSR1); // thread needs to handle or SIG_IGN this |
|
} |
|
|
|
void freeThread(Thread** thread) |
|
{ |
|
free((ThreadPthreadImpl*)((*thread)->impl)); |
|
free(*thread); |
|
*thread = NULL; |
|
}
|
|
|