spthread.hpp
2.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef __spthread_hpp__
#define __spthread_hpp__
#ifndef WIN32
/// pthread
#include <pthread.h>
#include <unistd.h>
typedef void * sp_thread_result_t;
typedef pthread_mutex_t sp_thread_mutex_t;
typedef pthread_cond_t sp_thread_cond_t;
typedef pthread_t sp_thread_t;
typedef pthread_attr_t sp_thread_attr_t;
#define sp_thread_mutex_init pthread_mutex_init
#define sp_thread_mutex_destroy pthread_mutex_destroy
#define sp_thread_mutex_lock pthread_mutex_lock
#define sp_thread_mutex_unlock pthread_mutex_unlock
#define sp_thread_cond_init pthread_cond_init
#define sp_thread_cond_destroy pthread_cond_destroy
#define sp_thread_cond_wait pthread_cond_wait
#define sp_thread_cond_signal pthread_cond_signal
#define sp_thread_attr_init pthread_attr_init
#define sp_thread_attr_destroy pthread_attr_destroy
#define sp_thread_attr_setdetachstate pthread_attr_setdetachstate
#define SP_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
#define sp_thread_attr_setstacksize pthread_attr_setstacksize
#define sp_thread_self pthread_self
#define sp_thread_create pthread_create
#define SP_THREAD_CALL
typedef sp_thread_result_t ( * sp_thread_func_t )( void * args );
#ifndef sp_sleep
#define sp_sleep(x) sleep(x)
#endif
#else ///////////////////////////////////////////////////////////////////////
// win32 thread
#include <winsock2.h>
#include <process.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned sp_thread_t;
typedef unsigned sp_thread_result_t;
#define SP_THREAD_CALL __stdcall
typedef sp_thread_result_t ( __stdcall * sp_thread_func_t )( void * args );
typedef HANDLE sp_thread_mutex_t;
typedef HANDLE sp_thread_cond_t;
//typedef DWORD sp_thread_attr_t;
typedef struct tagsp_thread_attr {
int stacksize;
int detachstate;
} sp_thread_attr_t;
#define SP_THREAD_CREATE_DETACHED 1
#ifndef sp_sleep
#define sp_sleep(x) Sleep(1000*x)
#endif
int sp_thread_mutex_init( sp_thread_mutex_t * mutex, void * attr );
int sp_thread_mutex_destroy( sp_thread_mutex_t * mutex );
int sp_thread_mutex_lock( sp_thread_mutex_t * mutex );
int sp_thread_mutex_unlock( sp_thread_mutex_t * mutex );
int sp_thread_cond_init( sp_thread_cond_t * cond, void * attr );
int sp_thread_cond_destroy( sp_thread_cond_t * cond );
int sp_thread_cond_wait( sp_thread_cond_t * cond, sp_thread_mutex_t * mutex );
int sp_thread_cond_signal( sp_thread_cond_t * cond );
int sp_thread_attr_init( sp_thread_attr_t * attr );
int sp_thread_attr_destroy( sp_thread_attr_t * attr );
int sp_thread_attr_setdetachstate( sp_thread_attr_t * attr, int detachstate );
int sp_thread_attr_setstacksize( sp_thread_attr_t * attr, size_t stacksize );
int sp_thread_create( sp_thread_t * thread, sp_thread_attr_t * attr,
sp_thread_func_t myfunc, void * args );
sp_thread_t sp_thread_self();
#ifdef __cplusplus
}
#endif
#endif
#endif