32 lines
805 B
C
32 lines
805 B
C
|
//
|
||
|
// Created by nicol on 2018-03-18.
|
||
|
//
|
||
|
|
||
|
#include "thread.h"
|
||
|
|
||
|
#ifdef _WIN32
|
||
|
thread_t thread_create(thread_start_t thread_start, thread_arg_t thread_arg) {
|
||
|
thread_t thread = malloc(sizeof(struct st_thread));
|
||
|
thread->handle = CreateThread(NULL, 0, thread_start, thread_arg, CREATE_SUSPENDED, &thread->id);
|
||
|
if (thread->handle == NULL) {
|
||
|
free(thread);
|
||
|
return NULL;
|
||
|
}
|
||
|
return thread;
|
||
|
}
|
||
|
|
||
|
int thread_start(thread_t thread) {
|
||
|
if (ResumeThread(thread->handle) == -1) return -1;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int thread_wait(thread_t thread) {
|
||
|
if (WaitForSingleObject(thread->handle, INFINITE) != WAIT_OBJECT_0) return -1;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int thread_destroy(thread_t thread) {
|
||
|
if (thread_wait(thread) != 0) SuspendThread(thread->handle);
|
||
|
CloseHandle(thread->handle);
|
||
|
}
|
||
|
#endif
|