1 #define _MTHREADIFY_PTHREADS
2 #include <minix/mthread.h>
7 * The following works under the hypothesis that we have only green threads,
8 * which implies that we have no preemption, unless explicit yield or possible
9 * calls done to mthread functions.
11 * This has impact on the fact we do not maintain a table of currently being
12 * initialized mutexes or condition variables, to prevent double initialization
13 * and/or TOCTU problems. TOCTU could appear between the test against the
14 * initializer value, and the actual initialization, which could lead to double
15 * initialization of the same mutex AND get two threads at the same time in the
16 * critical section as they both hold a (different) mutex.
20 /*===========================================================================*
21 * pthread_mutex_init *
22 *===========================================================================*/
23 int pthread_mutex_init(pthread_mutex_t
*mutex
, pthread_mutexattr_t
*mattr
)
25 return mthread_mutex_init(mutex
, mattr
);
28 /*===========================================================================*
29 * pthread_mutex_destroy *
30 *===========================================================================*/
31 int pthread_mutex_destroy(pthread_mutex_t
*mutex
)
33 if (PTHREAD_MUTEX_INITIALIZER
== *mutex
) {
38 return mthread_mutex_destroy(mutex
);
41 /*===========================================================================*
42 * pthread_mutex_lock *
43 *===========================================================================*/
44 int pthread_mutex_lock(pthread_mutex_t
*mutex
)
46 if (PTHREAD_MUTEX_INITIALIZER
== *mutex
) {
47 mthread_mutex_init(mutex
, NULL
);
50 return mthread_mutex_lock(mutex
);
53 /*===========================================================================*
54 * pthread_mutex_trylock *
55 *===========================================================================*/
56 int pthread_mutex_trylock(pthread_mutex_t
*mutex
)
58 if (PTHREAD_MUTEX_INITIALIZER
== *mutex
) {
59 mthread_mutex_init(mutex
, NULL
);
62 return pthread_mutex_trylock(mutex
);
65 /*===========================================================================*
66 * pthread_mutex_unlock *
67 *===========================================================================*/
68 int pthread_mutex_unlock(pthread_mutex_t
*mutex
)
70 if (PTHREAD_MUTEX_INITIALIZER
== *mutex
) {
71 mthread_mutex_init(mutex
, NULL
);
74 return mthread_mutex_unlock(mutex
);
77 /*===========================================================================*
79 *===========================================================================*/
80 int pthread_cond_init(pthread_cond_t
*cond
, pthread_condattr_t
*cattr
)
82 return mthread_cond_init(cond
, cattr
);
85 /*===========================================================================*
86 * pthread_cond_broadcast *
87 *===========================================================================*/
88 int pthread_cond_broadcast(pthread_cond_t
*cond
)
90 if (PTHREAD_COND_INITIALIZER
== *cond
) {
91 mthread_cond_init(cond
, NULL
);
94 return mthread_cond_broadcast(cond
);
97 /*===========================================================================*
98 * pthread_cond_destroy *
99 *===========================================================================*/
100 int pthread_cond_destroy(pthread_cond_t
*cond
)
102 if (PTHREAD_COND_INITIALIZER
== *cond
) {
107 return mthread_cond_destroy(cond
);
110 /*===========================================================================*
111 * pthread_cond_signal *
112 *===========================================================================*/
113 int pthread_cond_signal(pthread_cond_t
*cond
)
115 if (PTHREAD_COND_INITIALIZER
== *cond
) {
116 mthread_cond_init(cond
, NULL
);
119 return mthread_cond_signal(cond
);
122 /*===========================================================================*
123 * pthread_cond_wait *
124 *===========================================================================*/
125 int pthread_cond_wait(pthread_cond_t
*cond
, pthread_mutex_t
*mutex
)
127 if (PTHREAD_COND_INITIALIZER
== *cond
) {
128 mthread_cond_init(cond
, NULL
);
131 return mthread_cond_wait(cond
, mutex
);
134 /*===========================================================================*
135 * pthread_rwlock_init *
136 *===========================================================================*/
137 int pthread_rwlock_init(pthread_rwlock_t
*rwlock
, pthread_rwlockattr_t
*UNUSED(attr
))
139 return mthread_rwlock_init(rwlock
);
142 #if !defined(__weak_alias)
143 #error __weak_alias is required to compile the pthread compat library