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