thread.c
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <scot/thread.h>
#include <scot/exception.h>
int
thread_equal (THREAD_T a, THREAD_T b)
{
return (a == b);
}
void
thread_end (THREAD_T thread, unsigned int timeout)
{
int err;
err = CANCEL_THREAD (thread);
if (err != 0)
THROW (EXC (EXC_ERROR,
err,
"[EVENT_SOURCE_THREAD]cancel request failed"));
err = JOIN_THREAD (thread, timeout);
if (err == JOIN_TIMEOUT)
THROW (EXC (EXC_ERROR,
err,
"[EVENT_SOURCE_THREAD]timeout exceeded while waiting "
"for threads end"));
if (err == JOIN_ERROR)
THROW (EXC (EXC_ERROR,
err,
"[EVENT_SOURCE_THREAD]failure on waiting for threads end"));
}
THREAD_T
thread_new (T_PROC_RET (*t_proc)(void *), void* arg)
{
return _beginthreadex (NULL, 0, t_proc, arg, 0, NULL);
}
int
thread_join (THREAD_T thread, unsigned int timeout)
{
switch (WaitForSingleObject (thread, timeout * 1000))
{
case WAIT_OBJECT_0:
CloseHandle (thread);
return JOIN_OK;
case WAIT_TIMEOUT:
return JOIN_TIMEOUT;
}
}
int
thread_mutex_lock (THREAD_MUTEX_T * mutex)
{
switch (WaitForSingleObject (*mutex, INFINITE))
{
case WAIT_OBJECT_0:
return MUTEX_LOCK_OK;
default:
return MUTEX_LOCK_ERROR;
}
}
THREAD_MUTEX_T
thread_mutex_new (void)
{
return NULL;
}