WARNS level 4 cleanup.
[dragonfly.git] / lib / libthread_xu / thread / thr_mutex.c
1 /*
2  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/lib/libpthread/thread/thr_mutex.c,v 1.46 2004/10/31 05:03:50 green Exp $
33  * $DragonFly: src/lib/libthread_xu/thread/thr_mutex.c,v 1.11 2006/04/06 13:03:09 davidxu Exp $
34  */
35
36 #include "namespace.h"
37 #include <machine/tls.h>
38
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/queue.h>
43 #include <pthread.h>
44 #include "un-namespace.h"
45
46 #include "thr_private.h"
47
48 #if defined(_PTHREADS_INVARIANTS)
49 #define MUTEX_INIT_LINK(m)              do {            \
50         (m)->m_qe.tqe_prev = NULL;                      \
51         (m)->m_qe.tqe_next = NULL;                      \
52 } while (0)
53 #define MUTEX_ASSERT_IS_OWNED(m)        do {            \
54         if ((m)->m_qe.tqe_prev == NULL)                 \
55                 PANIC("mutex is not on list");          \
56 } while (0)
57 #define MUTEX_ASSERT_NOT_OWNED(m)       do {            \
58         if (((m)->m_qe.tqe_prev != NULL) ||             \
59             ((m)->m_qe.tqe_next != NULL))               \
60                 PANIC("mutex is on list");              \
61 } while (0)
62 #define THR_ASSERT_NOT_IN_SYNCQ(thr)    do {            \
63         THR_ASSERT(((thr)->sflags & THR_FLAGS_IN_SYNCQ) == 0, \
64             "thread in syncq when it shouldn't be.");   \
65 } while (0);
66 #else
67 #define MUTEX_INIT_LINK(m)
68 #define MUTEX_ASSERT_IS_OWNED(m)
69 #define MUTEX_ASSERT_NOT_OWNED(m)
70 #define THR_ASSERT_NOT_IN_SYNCQ(thr)
71 #endif
72
73 #define THR_IN_MUTEXQ(thr)      (((thr)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
74 #define MUTEX_DESTROY(m) do {           \
75         free(m);                        \
76 } while (0)
77
78 umtx_t  _mutex_static_lock;
79
80 /*
81  * Prototypes
82  */
83 static int      mutex_self_trylock(pthread_mutex_t);
84 static int      mutex_self_lock(pthread_mutex_t,
85                         const struct timespec *abstime);
86 static int      mutex_unlock_common(pthread_mutex_t *, int);
87
88 int __pthread_mutex_init(pthread_mutex_t *mutex,
89         const pthread_mutexattr_t *mutex_attr);
90 int __pthread_mutex_trylock(pthread_mutex_t *mutex);
91 int __pthread_mutex_lock(pthread_mutex_t *mutex);
92 int __pthread_mutex_timedlock(pthread_mutex_t *mutex,
93         const struct timespec *abs_timeout);
94
95 static int
96 mutex_init(pthread_mutex_t *mutex,
97     const pthread_mutexattr_t *mutex_attr, int private)
98 {
99         const struct pthread_mutex_attr *attr;
100         struct pthread_mutex *pmutex;
101
102         if (mutex_attr == NULL) {
103                 attr = &_pthread_mutexattr_default;
104         } else {
105                 attr = *mutex_attr;
106                 if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK ||
107                     attr->m_type >= MUTEX_TYPE_MAX)
108                         return (EINVAL);
109                 if (attr->m_protocol < PTHREAD_PRIO_NONE ||
110                     attr->m_protocol > PTHREAD_PRIO_PROTECT)
111                         return (EINVAL);
112         }
113
114         if ((pmutex = (pthread_mutex_t)
115                 malloc(sizeof(struct pthread_mutex))) == NULL)
116                 return (ENOMEM);
117
118         _thr_umtx_init(&pmutex->m_lock);
119         pmutex->m_type = attr->m_type;
120         pmutex->m_protocol = attr->m_protocol;
121         TAILQ_INIT(&pmutex->m_queue);
122         pmutex->m_owner = NULL;
123         pmutex->m_flags = attr->m_flags | MUTEX_FLAGS_INITED;
124         if (private)
125                 pmutex->m_flags |= MUTEX_FLAGS_PRIVATE;
126         pmutex->m_count = 0;
127         pmutex->m_refcount = 0;
128         if (attr->m_protocol == PTHREAD_PRIO_PROTECT)
129                 pmutex->m_prio = attr->m_ceiling;
130         else
131                 pmutex->m_prio = -1;
132         pmutex->m_saved_prio = 0;
133         MUTEX_INIT_LINK(pmutex);
134         *mutex = pmutex;
135         return (0);
136 }
137
138 static int
139 init_static(struct pthread *thread, pthread_mutex_t *mutex)
140 {
141         int ret;
142
143         THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
144
145         if (*mutex == NULL)
146                 ret = mutex_init(mutex, NULL, 0);
147         else
148                 ret = 0;
149
150         THR_LOCK_RELEASE(thread, &_mutex_static_lock);
151
152         return (ret);
153 }
154
155 static int
156 init_static_private(struct pthread *thread, pthread_mutex_t *mutex)
157 {
158         int ret;
159
160         THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
161
162         if (*mutex == NULL)
163                 ret = mutex_init(mutex, NULL, 1);
164         else
165                 ret = 0;
166
167         THR_LOCK_RELEASE(thread, &_mutex_static_lock);
168
169         return (ret);
170 }
171
172 int
173 _pthread_mutex_init(pthread_mutex_t *mutex,
174     const pthread_mutexattr_t *mutex_attr)
175 {
176         return mutex_init(mutex, mutex_attr, 1);
177 }
178
179 int
180 __pthread_mutex_init(pthread_mutex_t *mutex,
181     const pthread_mutexattr_t *mutex_attr)
182 {
183         return mutex_init(mutex, mutex_attr, 0);
184 }
185
186 int
187 _mutex_reinit(pthread_mutex_t *mutex)
188 {
189         _thr_umtx_init(&(*mutex)->m_lock);
190         TAILQ_INIT(&(*mutex)->m_queue);
191         MUTEX_INIT_LINK(*mutex);
192         (*mutex)->m_owner = NULL;
193         (*mutex)->m_count = 0;
194         (*mutex)->m_refcount = 0;
195         (*mutex)->m_prio = 0;
196         (*mutex)->m_saved_prio = 0;
197         return (0);
198 }
199
200 void
201 _mutex_fork(struct pthread *curthread)
202 {
203         struct pthread_mutex *m;
204
205         TAILQ_FOREACH(m, &curthread->mutexq, m_qe)
206                 m->m_lock = UMTX_LOCKED;
207 }
208
209 int
210 _pthread_mutex_destroy(pthread_mutex_t *mutex)
211 {
212         struct pthread *curthread = tls_get_curthread();
213         pthread_mutex_t m;
214         int ret = 0;
215
216         if (mutex == NULL || *mutex == NULL)
217                 ret = EINVAL;
218         else {
219                 /*
220                  * Try to lock the mutex structure, we only need to
221                  * try once, if failed, the mutex is in used.
222                  */
223                 ret = THR_UMTX_TRYLOCK(curthread, &(*mutex)->m_lock);
224                 if (ret)
225                         return (ret);
226
227                 /*
228                  * Check mutex other fields to see if this mutex is
229                  * in use. Mostly for prority mutex types, or there
230                  * are condition variables referencing it.
231                  */
232                 if (((*mutex)->m_owner != NULL) ||
233                     (TAILQ_FIRST(&(*mutex)->m_queue) != NULL) ||
234                     ((*mutex)->m_refcount != 0)) {
235                         THR_UMTX_UNLOCK(curthread, &(*mutex)->m_lock);
236                         ret = EBUSY;
237                 } else {
238                         /*
239                          * Save a pointer to the mutex so it can be free'd
240                          * and set the caller's pointer to NULL:
241                          */
242                         m = *mutex;
243                         *mutex = NULL;
244
245                         /* Unlock the mutex structure: */
246                         _thr_umtx_unlock(&m->m_lock, curthread->tid);
247
248                         /*
249                          * Free the memory allocated for the mutex
250                          * structure:
251                          */
252                         MUTEX_ASSERT_NOT_OWNED(m);
253                         MUTEX_DESTROY(m);
254                 }
255         }
256
257         /* Return the completion status: */
258         return (ret);
259 }
260
261 static int
262 mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
263 {
264         int ret = 0;
265
266         THR_ASSERT((mutex != NULL) && (*mutex != NULL),
267             "Uninitialized mutex in mutex_trylock_common");
268
269         ret = THR_UMTX_TRYLOCK(curthread, &(*mutex)->m_lock);
270         if (ret == 0) {
271                 (*mutex)->m_owner = curthread;
272                 /* Add to the list of owned mutexes: */
273                 MUTEX_ASSERT_NOT_OWNED(*mutex);
274                 TAILQ_INSERT_TAIL(&curthread->mutexq,
275                     (*mutex), m_qe);
276         } else if ((*mutex)->m_owner == curthread) {
277                 ret = mutex_self_trylock(*mutex);
278         } /* else {} */
279
280         return (ret);
281 }
282
283 int
284 __pthread_mutex_trylock(pthread_mutex_t *mutex)
285 {
286         struct pthread *curthread = tls_get_curthread();
287         int ret = 0;
288
289         /*
290          * If the mutex is statically initialized, perform the dynamic
291          * initialization:
292          */
293         if ((*mutex != NULL) ||
294             ((ret = init_static(curthread, mutex)) == 0))
295                 ret = mutex_trylock_common(curthread, mutex);
296
297         return (ret);
298 }
299
300 int
301 _pthread_mutex_trylock(pthread_mutex_t *mutex)
302 {
303         struct pthread  *curthread = tls_get_curthread();
304         int     ret = 0;
305
306         /*
307          * If the mutex is statically initialized, perform the dynamic
308          * initialization marking the mutex private (delete safe):
309          */
310         if ((*mutex != NULL) ||
311             ((ret = init_static_private(curthread, mutex)) == 0))
312                 ret = mutex_trylock_common(curthread, mutex);
313
314         return (ret);
315 }
316
317 static int
318 mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m,
319         const struct timespec * abstime)
320 {
321         struct  timespec ts, ts2;
322         int     ret = 0;
323
324         THR_ASSERT((m != NULL) && (*m != NULL),
325             "Uninitialized mutex in mutex_lock_common");
326
327         if (abstime != NULL && (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
328             abstime->tv_nsec >= 1000000000))
329                 return (EINVAL);
330
331         ret = THR_UMTX_TRYLOCK(curthread, &(*m)->m_lock);
332         if (ret == 0) {
333                 (*m)->m_owner = curthread;
334                 /* Add to the list of owned mutexes: */
335                 MUTEX_ASSERT_NOT_OWNED(*m);
336                 TAILQ_INSERT_TAIL(&curthread->mutexq,
337                     (*m), m_qe);
338         } else if ((*m)->m_owner == curthread) {
339                 ret = mutex_self_lock(*m, abstime);
340         } else {
341                 if (abstime == NULL) {
342                         THR_UMTX_LOCK(curthread, &(*m)->m_lock);
343                         ret = 0;
344                 } else {
345                         clock_gettime(CLOCK_REALTIME, &ts);
346                         TIMESPEC_SUB(&ts2, abstime, &ts);
347                         ret = THR_UMTX_TIMEDLOCK(curthread,
348                                 &(*m)->m_lock, &ts2);
349                         /*
350                          * Timed out wait is not restarted if
351                          * it was interrupted, not worth to do it.
352                          */
353                         if (ret == EINTR)
354                                 ret = ETIMEDOUT;
355                 }
356                 if (ret == 0) {
357                         (*m)->m_owner = curthread;
358                         /* Add to the list of owned mutexes: */
359                         MUTEX_ASSERT_NOT_OWNED(*m);
360                         TAILQ_INSERT_TAIL(&curthread->mutexq,
361                             (*m), m_qe);
362                 }
363         }
364         return (ret);
365 }
366
367 int
368 __pthread_mutex_lock(pthread_mutex_t *m)
369 {
370         struct pthread *curthread;
371         int     ret = 0;
372
373         _thr_check_init();
374
375         curthread = tls_get_curthread();
376
377         /*
378          * If the mutex is statically initialized, perform the dynamic
379          * initialization:
380          */
381         if ((*m != NULL) || ((ret = init_static(curthread, m)) == 0))
382                 ret = mutex_lock_common(curthread, m, NULL);
383
384         return (ret);
385 }
386
387 int
388 _pthread_mutex_lock(pthread_mutex_t *m)
389 {
390         struct pthread *curthread;
391         int     ret = 0;
392
393         _thr_check_init();
394
395         curthread = tls_get_curthread();
396
397         /*
398          * If the mutex is statically initialized, perform the dynamic
399          * initialization marking it private (delete safe):
400          */
401         if ((*m != NULL) ||
402             ((ret = init_static_private(curthread, m)) == 0))
403                 ret = mutex_lock_common(curthread, m, NULL);
404
405         return (ret);
406 }
407
408 int
409 __pthread_mutex_timedlock(pthread_mutex_t *m,
410         const struct timespec *abs_timeout)
411 {
412         struct pthread *curthread;
413         int     ret = 0;
414
415         _thr_check_init();
416
417         curthread = tls_get_curthread();
418
419         /*
420          * If the mutex is statically initialized, perform the dynamic
421          * initialization:
422          */
423         if ((*m != NULL) || ((ret = init_static(curthread, m)) == 0))
424                 ret = mutex_lock_common(curthread, m, abs_timeout);
425
426         return (ret);
427 }
428
429 int
430 _pthread_mutex_timedlock(pthread_mutex_t *m,
431         const struct timespec *abs_timeout)
432 {
433         struct pthread *curthread;
434         int     ret = 0;
435
436         _thr_check_init();
437
438         curthread = tls_get_curthread();
439
440         /*
441          * If the mutex is statically initialized, perform the dynamic
442          * initialization marking it private (delete safe):
443          */
444         if ((*m != NULL) ||
445             ((ret = init_static_private(curthread, m)) == 0))
446                 ret = mutex_lock_common(curthread, m, abs_timeout);
447
448         return (ret);
449 }
450
451 int
452 _pthread_mutex_unlock(pthread_mutex_t *m)
453 {
454         return (mutex_unlock_common(m, /* add reference */ 0));
455 }
456
457 int
458 _mutex_cv_unlock(pthread_mutex_t *m)
459 {
460         return (mutex_unlock_common(m, /* add reference */ 1));
461 }
462
463 int
464 _mutex_cv_lock(pthread_mutex_t *m)
465 {
466         int     ret;
467
468         if ((ret = _pthread_mutex_lock(m)) == 0)
469                 (*m)->m_refcount--;
470         return (ret);
471 }
472
473 static int
474 mutex_self_trylock(pthread_mutex_t m)
475 {
476         int     ret;
477
478         switch (m->m_type) {
479         /* case PTHREAD_MUTEX_DEFAULT: */
480         case PTHREAD_MUTEX_ERRORCHECK:
481         case PTHREAD_MUTEX_NORMAL:
482                 ret = EBUSY; 
483                 break;
484
485         case PTHREAD_MUTEX_RECURSIVE:
486                 /* Increment the lock count: */
487                 if (m->m_count + 1 > 0) {
488                         m->m_count++;
489                         ret = 0;
490                 } else
491                         ret = EAGAIN;
492                 break;
493
494         default:
495                 /* Trap invalid mutex types; */
496                 ret = EINVAL;
497         }
498
499         return (ret);
500 }
501
502 static int
503 mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
504 {
505         struct timespec ts1, ts2;
506         int ret;
507
508         switch (m->m_type) {
509         /* case PTHREAD_MUTEX_DEFAULT: */
510         case PTHREAD_MUTEX_ERRORCHECK:
511                 if (abstime) {
512                         clock_gettime(CLOCK_REALTIME, &ts1);
513                         TIMESPEC_SUB(&ts2, abstime, &ts1);
514                         __sys_nanosleep(&ts2, NULL);
515                         ret = ETIMEDOUT;
516                 } else {
517                         /*
518                          * POSIX specifies that mutexes should return
519                          * EDEADLK if a recursive lock is detected.
520                          */
521                         ret = EDEADLK; 
522                 }
523                 break;
524
525         case PTHREAD_MUTEX_NORMAL:
526                 /*
527                  * What SS2 define as a 'normal' mutex.  Intentionally
528                  * deadlock on attempts to get a lock you already own.
529                  */
530                 ret = 0;
531                 if (abstime) {
532                         clock_gettime(CLOCK_REALTIME, &ts1);
533                         TIMESPEC_SUB(&ts2, abstime, &ts1);
534                         __sys_nanosleep(&ts2, NULL);
535                         ret = ETIMEDOUT;
536                 } else {
537                         ts1.tv_sec = 30;
538                         ts1.tv_nsec = 0;
539                         for (;;)
540                                 __sys_nanosleep(&ts1, NULL);
541                 }
542                 break;
543
544         case PTHREAD_MUTEX_RECURSIVE:
545                 /* Increment the lock count: */
546                 if (m->m_count + 1 > 0) {
547                         m->m_count++;
548                         ret = 0;
549                 } else
550                         ret = EAGAIN;
551                 break;
552
553         default:
554                 /* Trap invalid mutex types; */
555                 ret = EINVAL;
556         }
557
558         return (ret);
559 }
560
561 static int
562 mutex_unlock_common(pthread_mutex_t *m, int add_reference)
563 {
564         struct pthread *curthread = tls_get_curthread();
565         int ret = 0;
566
567         if (m == NULL || *m == NULL)
568                 ret = EINVAL;
569         else {
570                 /*
571                  * Check if the running thread is not the owner of the
572                  * mutex:
573                  */
574                 if (__predict_false((*m)->m_owner != curthread)) {
575                         ret = EPERM;
576                 } else if (__predict_false(
577                           (*m)->m_type == PTHREAD_MUTEX_RECURSIVE &&
578                           (*m)->m_count > 0)) {
579                         /* Decrement the count: */
580                         (*m)->m_count--;
581                         if (add_reference)
582                                 (*m)->m_refcount++;
583                 } else {
584                         /*
585                          * Clear the count in case this is a recursive
586                          * mutex.
587                          */
588                         (*m)->m_count = 0;
589                         (*m)->m_owner = NULL;
590                         /* Remove the mutex from the threads queue. */
591                         MUTEX_ASSERT_IS_OWNED(*m);
592                         TAILQ_REMOVE(&curthread->mutexq, (*m), m_qe);
593                         MUTEX_INIT_LINK(*m);
594                         if (add_reference)
595                                 (*m)->m_refcount++;
596                         /*
597                          * Hand off the mutex to the next waiting
598                          * thread.
599                          */
600                         _thr_umtx_unlock(&(*m)->m_lock, curthread->tid);
601                 }
602         }
603         return (ret);
604 }
605
606 void
607 _mutex_unlock_private(pthread_t pthread)
608 {
609         struct pthread_mutex    *m, *m_next;
610
611         for (m = TAILQ_FIRST(&pthread->mutexq); m != NULL; m = m_next) {
612                 m_next = TAILQ_NEXT(m, m_qe);
613                 if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0)
614                         _pthread_mutex_unlock(&m);
615         }
616 }
617
618 __strong_reference(__pthread_mutex_init, pthread_mutex_init);
619 __strong_reference(__pthread_mutex_lock, pthread_mutex_lock);
620 __strong_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
621 __strong_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
622
623 /* Single underscore versions provided for libc internal usage: */
624 /* No difference between libc and application usage of these: */
625 __strong_reference(_pthread_mutex_destroy, pthread_mutex_destroy);
626 __strong_reference(_pthread_mutex_unlock, pthread_mutex_unlock);