1 | /**
|
---|
2 | * cf_thread.c
|
---|
3 | */
|
---|
4 | #include "cf_thread.h"
|
---|
5 | #include "cf_local.h"
|
---|
6 |
|
---|
7 | #include <stdlib.h>
|
---|
8 |
|
---|
9 | #ifdef _WIN32
|
---|
10 | # include <windows.h>
|
---|
11 | # include <process.h>
|
---|
12 | # define THREAD_TYPE HANDLE
|
---|
13 | # define MUTEX_TYPE HANDLE
|
---|
14 | #else // #ifdef _WIN32
|
---|
15 | # include <pthread.h>
|
---|
16 | # define THREAD_TYPE pthread_t
|
---|
17 | # define MUTEX_TYPE pthread_mutex_t
|
---|
18 | #endif // #ifdef _WIN32
|
---|
19 |
|
---|
20 | #define CHECK_INVALID_THREAD(__h) \
|
---|
21 | if (__h == NULL) \
|
---|
22 | return CF_ERROR_THREAD_INVALID_ARGS
|
---|
23 |
|
---|
24 | #define CHECK_INVALID_MUTEX(__h) \
|
---|
25 | if (__h == NULL) \
|
---|
26 | return CF_ERROR_MUTEX_INVALID_ARGS
|
---|
27 |
|
---|
28 | int
|
---|
29 | CF_CLOSE_CTX (void * ctx)
|
---|
30 | {
|
---|
31 | #ifdef _WIN32
|
---|
32 | CloseHandle (ctx);
|
---|
33 | #else
|
---|
34 | free (ctx);
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | return CF_OK;
|
---|
38 | }
|
---|
39 |
|
---|
40 | int
|
---|
41 | CF_Thread_Create (CF_Thread * threadID,
|
---|
42 | CF_THREAD_RETURN callback,
|
---|
43 | void * arg)
|
---|
44 | {
|
---|
45 | int result = 0;
|
---|
46 |
|
---|
47 | TRY
|
---|
48 | {
|
---|
49 | #ifdef _WIN32
|
---|
50 | *threadID = (THREAD_TYPE) _beginthreadex (0, 0, callback, arg, 0, 0);
|
---|
51 | if (*threadID == NULL)
|
---|
52 | result = -1;
|
---|
53 | #else
|
---|
54 | *threadID = (THREAD_TYPE *) calloc (sizeof (THREAD_TYPE), 1);
|
---|
55 | if (*threadID == NULL)
|
---|
56 | {
|
---|
57 | result = -2;
|
---|
58 | TRY_BREAK;
|
---|
59 | }
|
---|
60 |
|
---|
61 | result = pthread_create ((THREAD_TYPE *) *threadID, NULL, callback, arg);
|
---|
62 | #endif
|
---|
63 | }
|
---|
64 | CATCH_IF (result < 0)
|
---|
65 | {
|
---|
66 | return CF_ERROR_THREAD_CREATE;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return CF_OK;
|
---|
70 | }
|
---|
71 |
|
---|
72 | int
|
---|
73 | CF_Thread_Release (CF_Thread * threadID)
|
---|
74 | {
|
---|
75 | CHECK_INVALID_THREAD (*threadID);
|
---|
76 |
|
---|
77 | CF_CLOSE_CTX (*threadID);
|
---|
78 |
|
---|
79 | return CF_OK;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int
|
---|
83 | CF_Thread_Join (CF_Thread * threadID)
|
---|
84 | {
|
---|
85 | CHECK_INVALID_THREAD (*threadID);
|
---|
86 |
|
---|
87 | #ifdef _WIN32
|
---|
88 | WaitForSingleObject ((THREAD_TYPE) *threaID, INFINITE);
|
---|
89 | #else
|
---|
90 | int status;
|
---|
91 | pthread_join (*((THREAD_TYPE *) *threadID), (void *)&status);
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | return CF_OK;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int
|
---|
98 | CF_Mutex_Create (CF_Mutex * mutex)
|
---|
99 | {
|
---|
100 | int result = 0;
|
---|
101 |
|
---|
102 | TRY
|
---|
103 | {
|
---|
104 | #ifdef _WIN32
|
---|
105 | *mutex = (MUTEX_TYPE) CreateMutexA (NULL, FALSE, NULL);
|
---|
106 | if (*mutex == NULL)
|
---|
107 | {
|
---|
108 | result = -1;
|
---|
109 | TRY_BREAK;
|
---|
110 | }
|
---|
111 | #else
|
---|
112 | *mutex = (MUTEX_TYPE *) calloc (sizeof (MUTEX_TYPE), 1);
|
---|
113 | if (*mutex == NULL)
|
---|
114 | {
|
---|
115 | result = -2;
|
---|
116 | TRY_BREAK;
|
---|
117 | }
|
---|
118 |
|
---|
119 | result = pthread_mutex_init (*mutex, NULL);
|
---|
120 | #endif
|
---|
121 | }
|
---|
122 | CATCH_IF (result < 0)
|
---|
123 | {
|
---|
124 | return CF_ERROR_MUTEX_CREATE;
|
---|
125 | }
|
---|
126 |
|
---|
127 | return CF_OK;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int
|
---|
131 | CF_Mutex_Destory (CF_Mutex * mutex)
|
---|
132 | {
|
---|
133 | CHECK_INVALID_MUTEX (*mutex);
|
---|
134 |
|
---|
135 | CF_CLOSE_CTX (*mutex);
|
---|
136 |
|
---|
137 | return CF_OK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | int
|
---|
141 | CF_Mutex_Lock (CF_Mutex * mutex)
|
---|
142 | {
|
---|
143 | CHECK_INVALID_MUTEX (*mutex);
|
---|
144 |
|
---|
145 | #ifdef _WIN32
|
---|
146 | WaitForSingleObject ((MUTEX_TYPE) *mutex, INFINITE);
|
---|
147 | #else
|
---|
148 | pthread_mutex_lock ((MUTEX_TYPE *) *mutex);
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | return CF_OK;
|
---|
152 | }
|
---|
153 |
|
---|
154 | int
|
---|
155 | CF_Mutex_Unlock (CF_Mutex * mutex)
|
---|
156 | {
|
---|
157 | CHECK_INVALID_MUTEX (*mutex);
|
---|
158 |
|
---|
159 | #ifdef _WIN32
|
---|
160 | ReleaseMutex (*mutex);
|
---|
161 | #else
|
---|
162 | pthread_mutex_unlock ((MUTEX_TYPE *) *mutex);
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | return CF_OK;
|
---|
166 | }
|
---|