]> pd.if.org Git - pdclib/blob - opt/pthreads/mtx_init.c
90c67e62109b0549b2a5f1ee5000c367e66d23ad
[pdclib] / opt / pthreads / mtx_init.c
1 #ifndef REGTEST
2 #include <threads.h>
3 #include <pthread.h>
4
5 int mtx_init(mtx_t *mtx, int type)
6 {
7     if(type == mtx_plain || type == mtx_timed) {
8         if(pthread_mutex_init(mtx, NULL) == 0)
9             return thrd_success;
10         else
11             return thrd_error;
12
13     } else if (type == mtx_recursive || type == (mtx_recursive | mtx_timed)) {
14         int rc = thrd_error;
15         pthread_mutexattr_t attr;
16
17         if(pthread_mutexattr_init(&attr))
18             goto cleanup1;
19
20         if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
21             goto cleanup2;
22
23         if(pthread_mutex_init(mtx, &attr) == 0)
24             rc = thrd_success;
25
26     cleanup2:
27         pthread_mutexattr_destroy(&attr);
28     cleanup1:
29         return rc;
30     } else {
31         return thrd_error;
32     }
33 }
34 #endif
35
36 #ifdef TEST
37 #include <_PDCLIB_test.h>
38
39 int main( void )
40 {
41     return TEST_RESULTS;
42 }
43
44 #endif