thread.h
3.14 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
69
70
71
72
73
74
75
76
77
78
79
/*
* used for threadsafety within exception
* actually only pthread is supported
*/
#ifndef THREAD_H
#define THREAD_H
#include <stdio.h>
#include <windows.h>
#define JOIN_OK 0x00
#define JOIN_TIMEOUT 0x01
#define JOIN_ERROR 0x02
/*
* i guess it is possible to write mutex code that uses cond
* elements to timeout on lock, but right now i have no clear
* idea of how to do this. The following thoughts i had:
* - create a struct that holds a pthread_mutex_t and a pthread_cond_t
* for every mutex.
* - lock mutexes only by calling pthread_cond_timedwait
* And here starts the problem...to do a cond_timedwait i need to ensure
* that the mutex is already locked...well and then a question i have not
* clearyfied, if a thread ends, will all locks on mutexes be removed?
* I guess so, but i did not test it. And an even more important question,
* is it possible to write cleanup-code that guarantees that a
* pthread_cond_signal is called for every mutex the thread holds...
* well, we will end up with the requirement that the programmer has to
* call a release mutex function before ending a thread and if the programmer
* forgets that the behaviour of out code is unspecified
* Right now i will not support timeouts on mutex-locks and to be consistent
* it is not supported on win32 either, also it would be much easier to
* implement there.
*/
#define MUTEX_LOCK_OK 0x00
#define MUTEX_LOCK_ERROR 0x02
#define THREAD_T HANDLE
#define THREAD_ID_T DWORD
#define THREAD_ID GetCurrentThreadId ()
#define SELF_THREAD GetCurrentThread ()
#define NEW_THREAD thread_new
#define CANCEL_THREAD(thread) (! TerminateThread ((thread), -1))
#define EXIT_THREAD(retval) _endthreadex ((DWORD) (retval))
#define JOIN_THREAD thread_join
#define THREAD_CANCEL_ENABLE
#define THREAD_CANCEL_DISABLE
#define THREAD_CANCEL_ASYNC
#define THREAD_CANCEL_DEFER
#define T_PROC_RET DWORD WINAPI
#define THREAD_MUTEX_T HANDLE
#define NEW_THREAD_MUTEX CreateMutex (NULL, FALSE, NULL)
#define LOCK_THREAD_MUTEX thread_mutex_lock
#define UNLOCK_THREAD_MUTEX(mutex) (! ReleaseMutex ((mutex)))
#define THREAD_COND_T CONDITION_VARIABLE
#define THREAD_COND_CS_T CRITICAL_SECTION
#define GET_THREAD_COND(c)
#define GET_THREAD_COND_CS(c)
#define THREAD_COND_ENTER_CS(cs) EnterCriticalSection ((cs))
#define THREAD_COND_LEAVE_CS(cs) LeaveCriticalSection ((cs))
#define SIGNAL_THREAD_COND(cond) WakeConditionVariable ((cond))
#define BCAST_THREAD_COND(cond) WakeAllConditionVariable ((cond))
#define WAIT_THREAD_COND(cond, cs, t) \
SleepConditionVariableCS((cond), (cs), t)
#define THREAD_ATEXIT_BEGIN
#define THREAD_ATEXIT_END
typedef T_PROC_RET (*scot_thread_entry_fptr) (void *);
THREAD_T thread_new (T_PROC_RET (*)(void *), void*);
int thread_join (THREAD_T, unsigned int);
THREAD_MUTEX_T thread_mutex_new (void);
int thread_mutex_lock (THREAD_MUTEX_T *);
#endif /* THREAD_H */