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_Thread_Local_Close (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_Function callback,
|
---|
43 | void * arg)
|
---|
44 | {
|
---|
45 | int result = 0;
|
---|
46 |
|
---|
47 | #ifdef _WIN32
|
---|
48 | *threadID = (THREAD_TYPE) _beginthreadex (0, 0, callback, arg, 0, 0);
|
---|
49 | if (*threadID == NULL)
|
---|
50 | return CF_ERROR_THREAD_CREATE;
|
---|
51 | #else
|
---|
52 | *threadID = (THREAD_TYPE *) calloc (sizeof (THREAD_TYPE), 1);
|
---|
53 | if (*threadID == NULL)
|
---|
54 | return CF_ERROR_THREAD_CREATE;
|
---|
55 |
|
---|
56 | result = pthread_create ((THREAD_TYPE *) *threadID, NULL, callback, arg);
|
---|
57 | if (result < 0)
|
---|
58 | return CF_ERROR_THREAD_CREATE;
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | return CF_OK;
|
---|
62 | }
|
---|
63 |
|
---|
64 | int
|
---|
65 | CF_Thread_Release (CF_Thread * threadID)
|
---|
66 | {
|
---|
67 | CHECK_INVALID_THREAD (*threadID);
|
---|
68 |
|
---|
69 | CF_Thread_Local_Close (*threadID);
|
---|
70 |
|
---|
71 | return CF_OK;
|
---|
72 | }
|
---|
73 |
|
---|
74 | int
|
---|
75 | CF_Thread_Join (CF_Thread * threadID)
|
---|
76 | {
|
---|
77 | CHECK_INVALID_THREAD (*threadID);
|
---|
78 |
|
---|
79 | #ifdef _WIN32
|
---|
80 | WaitForSingleObject ((THREAD_TYPE) *threadID, INFINITE);
|
---|
81 | #else
|
---|
82 | char status[16] = {0x00,};
|
---|
83 | pthread_join (*((THREAD_TYPE *) *threadID), (void **)status);
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | return CF_OK;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int
|
---|
90 | CF_Mutex_Create (CF_Mutex * mutex)
|
---|
91 | {
|
---|
92 | int result = 0;
|
---|
93 |
|
---|
94 | #ifdef _WIN32
|
---|
95 | *mutex = (MUTEX_TYPE) CreateMutexA (NULL, FALSE, NULL);
|
---|
96 | if (*mutex == NULL)
|
---|
97 | return CF_ERROR_MUTEX_CREATE;
|
---|
98 | #else
|
---|
99 | *mutex = (MUTEX_TYPE *) calloc (sizeof (MUTEX_TYPE), 1);
|
---|
100 | if (*mutex == NULL)
|
---|
101 | return CF_ERROR_MUTEX_CREATE;
|
---|
102 |
|
---|
103 | result = pthread_mutex_init (*mutex, NULL);
|
---|
104 | if (result < 0)
|
---|
105 | return CF_ERROR_MUTEX_CREATE;
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | return CF_OK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int
|
---|
112 | CF_Mutex_Destory (CF_Mutex * mutex)
|
---|
113 | {
|
---|
114 | CHECK_INVALID_MUTEX (*mutex);
|
---|
115 |
|
---|
116 | CF_Thread_Local_Close (*mutex);
|
---|
117 |
|
---|
118 | return CF_OK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | int
|
---|
122 | CF_Mutex_Lock (CF_Mutex * mutex)
|
---|
123 | {
|
---|
124 | CHECK_INVALID_MUTEX (*mutex);
|
---|
125 |
|
---|
126 | #ifdef _WIN32
|
---|
127 | WaitForSingleObject ((MUTEX_TYPE) *mutex, INFINITE);
|
---|
128 | #else
|
---|
129 | pthread_mutex_lock ((MUTEX_TYPE *) *mutex);
|
---|
130 | #endif
|
---|
131 |
|
---|
132 | return CF_OK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int
|
---|
136 | CF_Mutex_Unlock (CF_Mutex * mutex)
|
---|
137 | {
|
---|
138 | CHECK_INVALID_MUTEX (*mutex);
|
---|
139 |
|
---|
140 | #ifdef _WIN32
|
---|
141 | ReleaseMutex (*mutex);
|
---|
142 | #else
|
---|
143 | pthread_mutex_unlock ((MUTEX_TYPE *) *mutex);
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | return CF_OK;
|
---|
147 | }
|
---|