equal
deleted
inserted
replaced
17 |
17 |
18 #include <pthread.h> |
18 #include <pthread.h> |
19 #include <semaphore.h> |
19 #include <semaphore.h> |
20 #include <stdio.h> |
20 #include <stdio.h> |
21 #include <stdlib.h> |
21 #include <stdlib.h> |
22 #include <assert.h> |
|
23 #include <errno.h> |
22 #include <errno.h> |
24 |
23 |
25 #include "xaplatform.h" |
24 #include "xaplatform.h" |
26 |
25 |
27 /** MACROS **/ |
26 /** MACROS **/ |
43 #endif |
42 #endif |
44 |
43 |
45 XAresult XAImpl_CreateMutex(XAImplMutexHandle *mtx) |
44 XAresult XAImpl_CreateMutex(XAImplMutexHandle *mtx) |
46 { |
45 { |
47 XA_MTX *pMtx = (XA_MTX *) malloc(sizeof(XA_MTX)); |
46 XA_MTX *pMtx = (XA_MTX *) malloc(sizeof(XA_MTX)); |
48 assert(mtx); |
|
49 if (pMtx) |
47 if (pMtx) |
50 { |
48 { |
51 pthread_mutexattr_t *pAttr = NULL; |
49 pthread_mutexattr_t *pAttr = NULL; |
52 #ifdef _MUTEXERRORSUPPORT |
50 #ifdef _MUTEXERRORSUPPORT |
53 pthread_mutexattr_t attr; |
51 pthread_mutexattr_t attr; |
73 } |
71 } |
74 |
72 |
75 XAresult XAImpl_LockMutex(XAImplMutexHandle mtx) |
73 XAresult XAImpl_LockMutex(XAImplMutexHandle mtx) |
76 { |
74 { |
77 XA_MTX *pMtx = (XA_MTX*) mtx; |
75 XA_MTX *pMtx = (XA_MTX*) mtx; |
78 assert(pMtx); |
|
79 #ifdef _MUTEXERRORSUPPORT |
76 #ifdef _MUTEXERRORSUPPORT |
80 if(pthread_mutex_lock(pMtx)) |
77 if(pthread_mutex_lock(pMtx)) |
81 { |
78 { |
82 return XA_RESULT_PRECONDITIONS_VIOLATED; |
79 return XA_RESULT_PRECONDITIONS_VIOLATED; |
83 } |
80 } |
94 XAresult XAImpl_TryLockMutex(XAImplMutexHandle mtx) |
91 XAresult XAImpl_TryLockMutex(XAImplMutexHandle mtx) |
95 { |
92 { |
96 XA_MTX *pMtx = (XA_MTX*) mtx; |
93 XA_MTX *pMtx = (XA_MTX*) mtx; |
97 XAint32 mutexRet; |
94 XAint32 mutexRet; |
98 XAresult ret = XA_RESULT_SUCCESS; |
95 XAresult ret = XA_RESULT_SUCCESS; |
99 assert(pMtx); |
|
100 |
96 |
101 #ifdef _MUTEXERRORSUPPORT |
97 #ifdef _MUTEXERRORSUPPORT |
102 mutexRet = pthread_ mutex_trylock(pMtx); |
98 mutexRet = pthread_ mutex_trylock(pMtx); |
103 switch (mutexRet) |
99 switch (mutexRet) |
104 { |
100 { |
139 } |
135 } |
140 |
136 |
141 XAresult XAImpl_UnlockMutex(XAImplMutexHandle mtx) |
137 XAresult XAImpl_UnlockMutex(XAImplMutexHandle mtx) |
142 { |
138 { |
143 XA_MTX *pMtx = (XA_MTX*) mtx; |
139 XA_MTX *pMtx = (XA_MTX*) mtx; |
144 assert(pMtx); |
|
145 #ifdef _MUTEXERRORSUPPORT |
140 #ifdef _MUTEXERRORSUPPORT |
146 if(pthread_mutex_lock(pMtx)) |
141 if(pthread_mutex_lock(pMtx)) |
147 { |
142 { |
148 return XA_RESULT_PRECONDITIONS_VIOLATED; |
143 return XA_RESULT_PRECONDITIONS_VIOLATED; |
149 } |
144 } |
178 **********************************************************************/ |
173 **********************************************************************/ |
179 |
174 |
180 XAresult XAImpl_CreateSemaphore(XAImplSemHandle *sem) |
175 XAresult XAImpl_CreateSemaphore(XAImplSemHandle *sem) |
181 { |
176 { |
182 sem_t *pSem = (sem_t*) malloc(sizeof(sem_t)); |
177 sem_t *pSem = (sem_t*) malloc(sizeof(sem_t)); |
183 assert(sem); |
|
184 if (pSem) |
178 if (pSem) |
185 { |
179 { |
186 if (sem_init(pSem, 0, 0)) |
180 if (sem_init(pSem, 0, 0)) |
187 { |
181 { |
188 free(pSem); |
182 free(pSem); |
199 } |
193 } |
200 |
194 |
201 XAresult XAImpl_WaitSemaphore(XAImplSemHandle sem) |
195 XAresult XAImpl_WaitSemaphore(XAImplSemHandle sem) |
202 { |
196 { |
203 sem_t* pSem = (sem_t*) sem; |
197 sem_t* pSem = (sem_t*) sem; |
204 assert(pSem); |
|
205 sem_wait(pSem); |
198 sem_wait(pSem); |
206 return XA_RESULT_SUCCESS; |
199 return XA_RESULT_SUCCESS; |
207 } |
200 } |
208 |
201 |
209 XAresult XAImpl_PostSemaphore(XAImplSemHandle sem) |
202 XAresult XAImpl_PostSemaphore(XAImplSemHandle sem) |
210 { |
203 { |
211 sem_t *pSem = (sem_t*) sem; |
204 sem_t *pSem = (sem_t*) sem; |
212 assert(pSem); |
|
213 sem_post(pSem); |
205 sem_post(pSem); |
214 return XA_RESULT_SUCCESS; |
206 return XA_RESULT_SUCCESS; |
215 } |
207 } |
216 |
208 |
217 void XAImpl_DeleteSemaphore(XAImplSemHandle sem) |
209 void XAImpl_DeleteSemaphore(XAImplSemHandle sem) |
229 **********************************************************************/ |
221 **********************************************************************/ |
230 |
222 |
231 XAresult XAImpl_CreateThreadHandle(XAImplThreadHandle *thd) |
223 XAresult XAImpl_CreateThreadHandle(XAImplThreadHandle *thd) |
232 { |
224 { |
233 pthread_t *pThd = (pthread_t*) malloc(sizeof(pthread_t)); |
225 pthread_t *pThd = (pthread_t*) malloc(sizeof(pthread_t)); |
234 assert(thd); |
|
235 if (!pThd) |
226 if (!pThd) |
236 { |
227 { |
237 return XA_RESULT_MEMORY_FAILURE; |
228 return XA_RESULT_MEMORY_FAILURE; |
238 } |
229 } |
239 *thd = (XAImplThreadHandle) pThd; |
230 *thd = (XAImplThreadHandle) pThd; |
242 |
233 |
243 XAresult XAImpl_StartThread(XAImplThreadHandle thd, void *thdattrib, |
234 XAresult XAImpl_StartThread(XAImplThreadHandle thd, void *thdattrib, |
244 XAImplThreadFunction thdfunc, void* thdfuncargs) |
235 XAImplThreadFunction thdfunc, void* thdfuncargs) |
245 { |
236 { |
246 pthread_t *pThd = (pthread_t*) thd; |
237 pthread_t *pThd = (pthread_t*) thd; |
247 assert(thd); |
|
248 if (pthread_create(pThd, thdattrib, thdfunc, thdfuncargs)) |
238 if (pthread_create(pThd, thdattrib, thdfunc, thdfuncargs)) |
249 { |
239 { |
250 return XA_RESULT_INTERNAL_ERROR; |
240 return XA_RESULT_INTERNAL_ERROR; |
251 } |
241 } |
252 return XA_RESULT_SUCCESS; |
242 return XA_RESULT_SUCCESS; |