正在显示
1 个修改的文件
包含
111 行增加
和
0 行删除
src/server/spserver/spthread.cpp
0 → 100644
1 | + | ||
2 | +#include "spthread.hpp" | ||
3 | + | ||
4 | +#ifdef WIN32 | ||
5 | + | ||
6 | +int sp_thread_mutex_init( sp_thread_mutex_t * mutex, void * attr ) | ||
7 | +{ | ||
8 | + *mutex = CreateMutex( NULL, FALSE, NULL ); | ||
9 | + return NULL == * mutex ? GetLastError() : 0; | ||
10 | +} | ||
11 | + | ||
12 | +int sp_thread_mutex_destroy( sp_thread_mutex_t * mutex ) | ||
13 | +{ | ||
14 | + int ret = CloseHandle( *mutex ); | ||
15 | + | ||
16 | + return 0 == ret ? GetLastError() : 0; | ||
17 | +} | ||
18 | + | ||
19 | +int sp_thread_mutex_lock( sp_thread_mutex_t * mutex ) | ||
20 | +{ | ||
21 | + int ret = WaitForSingleObject( *mutex, INFINITE ); | ||
22 | + return WAIT_OBJECT_0 == ret ? 0 : GetLastError(); | ||
23 | +} | ||
24 | + | ||
25 | +int sp_thread_mutex_unlock( sp_thread_mutex_t * mutex ) | ||
26 | +{ | ||
27 | + int ret = ReleaseMutex( *mutex ); | ||
28 | + return 0 != ret ? 0 : GetLastError(); | ||
29 | +} | ||
30 | + | ||
31 | +int sp_thread_cond_init( sp_thread_cond_t * cond, void * attr ) | ||
32 | +{ | ||
33 | + *cond = CreateEvent( NULL, FALSE, FALSE, NULL ); | ||
34 | + return NULL == *cond ? GetLastError() : 0; | ||
35 | +} | ||
36 | + | ||
37 | +int sp_thread_cond_destroy( sp_thread_cond_t * cond ) | ||
38 | +{ | ||
39 | + int ret = CloseHandle( *cond ); | ||
40 | + return 0 == ret ? GetLastError() : 0; | ||
41 | +} | ||
42 | + | ||
43 | +/* | ||
44 | +Caller MUST be holding the mutex lock; the | ||
45 | +lock is released and the caller is blocked waiting | ||
46 | +on 'cond'. When 'cond' is signaled, the mutex | ||
47 | +is re-acquired before returning to the caller. | ||
48 | +*/ | ||
49 | +int sp_thread_cond_wait( sp_thread_cond_t * cond, sp_thread_mutex_t * mutex ) | ||
50 | +{ | ||
51 | + int ret = 0; | ||
52 | + | ||
53 | + sp_thread_mutex_unlock( mutex ); | ||
54 | + | ||
55 | + ret = WaitForSingleObject( *cond, INFINITE ); | ||
56 | + | ||
57 | + sp_thread_mutex_lock( mutex ); | ||
58 | + | ||
59 | + return WAIT_OBJECT_0 == ret ? 0 : GetLastError(); | ||
60 | +} | ||
61 | + | ||
62 | +int sp_thread_cond_signal( sp_thread_cond_t * cond ) | ||
63 | +{ | ||
64 | + int ret = SetEvent( *cond ); | ||
65 | + return 0 == ret ? GetLastError() : 0; | ||
66 | +} | ||
67 | + | ||
68 | +sp_thread_t sp_thread_self() | ||
69 | +{ | ||
70 | + return GetCurrentThreadId(); | ||
71 | +} | ||
72 | + | ||
73 | +int sp_thread_attr_init( sp_thread_attr_t * attr ) | ||
74 | +{ | ||
75 | + memset( attr, 0, sizeof( sp_thread_attr_t ) ); | ||
76 | + return 0; | ||
77 | +} | ||
78 | + | ||
79 | +int sp_thread_attr_destroy( sp_thread_attr_t * attr ) | ||
80 | +{ | ||
81 | + return 0; | ||
82 | +} | ||
83 | + | ||
84 | +int sp_thread_attr_setdetachstate( sp_thread_attr_t * attr, int detachstate ) | ||
85 | +{ | ||
86 | + attr->detachstate = detachstate; | ||
87 | + return 0; | ||
88 | +} | ||
89 | + | ||
90 | +int sp_thread_attr_setstacksize( sp_thread_attr_t * attr, size_t stacksize ) | ||
91 | +{ | ||
92 | + attr->stacksize = stacksize; | ||
93 | + return 0; | ||
94 | +} | ||
95 | + | ||
96 | +int sp_thread_create( sp_thread_t * thread, sp_thread_attr_t * attr, | ||
97 | + sp_thread_func_t myfunc, void * args ) | ||
98 | +{ | ||
99 | + // _beginthreadex returns 0 on an error | ||
100 | + HANDLE h = 0; | ||
101 | + | ||
102 | + if( NULL != attr ) { | ||
103 | + h = (HANDLE)_beginthreadex( NULL, attr->stacksize, myfunc, args, 0, thread ); | ||
104 | + } else { | ||
105 | + h = (HANDLE)_beginthreadex( NULL, 0, myfunc, args, 0, thread ); | ||
106 | + } | ||
107 | + | ||
108 | + return h > 0 ? 0 : GetLastError(); | ||
109 | +} | ||
110 | + | ||
111 | +#endif |
请
注册
或
登录
后发表评论