]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthr/thread/thr_mutex.c
Merge ACPICA 20100915.
[FreeBSD/FreeBSD.git] / lib / libthr / thread / thr_mutex.c
1 /*
2  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3  * Copyright (c) 2006 David Xu <davidxu@freebsd.org>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by John Birrell.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35
36 #include "namespace.h"
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <pthread.h>
43 #include <pthread_np.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 (__predict_false((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 (__predict_false((m)->m_qe.tqe_prev != NULL ||       \
59             (m)->m_qe.tqe_next != NULL))        \
60                 PANIC("mutex is on list");              \
61 } while (0)
62 #else
63 #define MUTEX_INIT_LINK(m)
64 #define MUTEX_ASSERT_IS_OWNED(m)
65 #define MUTEX_ASSERT_NOT_OWNED(m)
66 #endif
67
68 /*
69  * For adaptive mutexes, how many times to spin doing trylock2
70  * before entering the kernel to block
71  */
72 #define MUTEX_ADAPTIVE_SPINS    2000
73
74 /*
75  * Prototypes
76  */
77 int     __pthread_mutex_init(pthread_mutex_t *mutex,
78                 const pthread_mutexattr_t *mutex_attr);
79 int     __pthread_mutex_trylock(pthread_mutex_t *mutex);
80 int     __pthread_mutex_lock(pthread_mutex_t *mutex);
81 int     __pthread_mutex_timedlock(pthread_mutex_t *mutex,
82                 const struct timespec *abstime);
83 int     _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
84                 void *(calloc_cb)(size_t, size_t));
85 int     _pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count);
86 int     _pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count);
87 int     __pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count);
88 int     _pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count);
89 int     _pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count);
90 int     __pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count);
91
92 static int      mutex_self_trylock(pthread_mutex_t);
93 static int      mutex_self_lock(pthread_mutex_t,
94                                 const struct timespec *abstime);
95 static int      mutex_unlock_common(pthread_mutex_t *);
96 static int      mutex_lock_sleep(struct pthread *, pthread_mutex_t,
97                                 const struct timespec *);
98
99 __weak_reference(__pthread_mutex_init, pthread_mutex_init);
100 __strong_reference(__pthread_mutex_init, _pthread_mutex_init);
101 __weak_reference(__pthread_mutex_lock, pthread_mutex_lock);
102 __strong_reference(__pthread_mutex_lock, _pthread_mutex_lock);
103 __weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
104 __strong_reference(__pthread_mutex_timedlock, _pthread_mutex_timedlock);
105 __weak_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
106 __strong_reference(__pthread_mutex_trylock, _pthread_mutex_trylock);
107
108 /* Single underscore versions provided for libc internal usage: */
109 /* No difference between libc and application usage of these: */
110 __weak_reference(_pthread_mutex_destroy, pthread_mutex_destroy);
111 __weak_reference(_pthread_mutex_unlock, pthread_mutex_unlock);
112
113 __weak_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling);
114 __weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling);
115
116 __weak_reference(__pthread_mutex_setspinloops_np, pthread_mutex_setspinloops_np);
117 __strong_reference(__pthread_mutex_setspinloops_np, _pthread_mutex_setspinloops_np);
118 __weak_reference(_pthread_mutex_getspinloops_np, pthread_mutex_getspinloops_np);
119
120 __weak_reference(__pthread_mutex_setyieldloops_np, pthread_mutex_setyieldloops_np);
121 __strong_reference(__pthread_mutex_setyieldloops_np, _pthread_mutex_setyieldloops_np);
122 __weak_reference(_pthread_mutex_getyieldloops_np, pthread_mutex_getyieldloops_np);
123 __weak_reference(_pthread_mutex_isowned_np, pthread_mutex_isowned_np);
124
125 static int
126 mutex_init(pthread_mutex_t *mutex,
127     const pthread_mutexattr_t *mutex_attr,
128     void *(calloc_cb)(size_t, size_t))
129 {
130         const struct pthread_mutex_attr *attr;
131         struct pthread_mutex *pmutex;
132
133         if (mutex_attr == NULL) {
134                 attr = &_pthread_mutexattr_default;
135         } else {
136                 attr = *mutex_attr;
137                 if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK ||
138                     attr->m_type >= PTHREAD_MUTEX_TYPE_MAX)
139                         return (EINVAL);
140                 if (attr->m_protocol < PTHREAD_PRIO_NONE ||
141                     attr->m_protocol > PTHREAD_PRIO_PROTECT)
142                         return (EINVAL);
143         }
144         if ((pmutex = (pthread_mutex_t)
145                 calloc_cb(1, sizeof(struct pthread_mutex))) == NULL)
146                 return (ENOMEM);
147
148         pmutex->m_type = attr->m_type;
149         pmutex->m_owner = NULL;
150         pmutex->m_count = 0;
151         pmutex->m_refcount = 0;
152         pmutex->m_spinloops = 0;
153         pmutex->m_yieldloops = 0;
154         MUTEX_INIT_LINK(pmutex);
155         switch(attr->m_protocol) {
156         case PTHREAD_PRIO_INHERIT:
157                 pmutex->m_lock.m_owner = UMUTEX_UNOWNED;
158                 pmutex->m_lock.m_flags = UMUTEX_PRIO_INHERIT;
159                 break;
160         case PTHREAD_PRIO_PROTECT:
161                 pmutex->m_lock.m_owner = UMUTEX_CONTESTED;
162                 pmutex->m_lock.m_flags = UMUTEX_PRIO_PROTECT;
163                 pmutex->m_lock.m_ceilings[0] = attr->m_ceiling;
164                 break;
165         case PTHREAD_PRIO_NONE:
166                 pmutex->m_lock.m_owner = UMUTEX_UNOWNED;
167                 pmutex->m_lock.m_flags = 0;
168         }
169
170         if (pmutex->m_type == PTHREAD_MUTEX_ADAPTIVE_NP) {
171                 pmutex->m_spinloops =
172                     _thr_spinloops ? _thr_spinloops: MUTEX_ADAPTIVE_SPINS;
173                 pmutex->m_yieldloops = _thr_yieldloops;
174         }
175
176         *mutex = pmutex;
177         return (0);
178 }
179
180 static int
181 init_static(struct pthread *thread, pthread_mutex_t *mutex)
182 {
183         int ret;
184
185         THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
186
187         if (*mutex == NULL)
188                 ret = mutex_init(mutex, NULL, calloc);
189         else
190                 ret = 0;
191
192         THR_LOCK_RELEASE(thread, &_mutex_static_lock);
193
194         return (ret);
195 }
196
197 static void
198 set_inherited_priority(struct pthread *curthread, struct pthread_mutex *m)
199 {
200         struct pthread_mutex *m2;
201
202         m2 = TAILQ_LAST(&curthread->pp_mutexq, mutex_queue);
203         if (m2 != NULL)
204                 m->m_lock.m_ceilings[1] = m2->m_lock.m_ceilings[0];
205         else
206                 m->m_lock.m_ceilings[1] = -1;
207 }
208
209 int
210 __pthread_mutex_init(pthread_mutex_t *mutex,
211     const pthread_mutexattr_t *mutex_attr)
212 {
213         return mutex_init(mutex, mutex_attr, calloc);
214 }
215
216 /* This function is used internally by malloc. */
217 int
218 _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
219     void *(calloc_cb)(size_t, size_t))
220 {
221         static const struct pthread_mutex_attr attr = {
222                 .m_type = PTHREAD_MUTEX_NORMAL,
223                 .m_protocol = PTHREAD_PRIO_NONE,
224                 .m_ceiling = 0
225         };
226         static const struct pthread_mutex_attr *pattr = &attr;
227         int ret;
228
229         ret = mutex_init(mutex, (pthread_mutexattr_t *)&pattr, calloc_cb);
230         if (ret == 0)
231                 (*mutex)->m_private = 1;
232         return (ret);
233 }
234
235 void
236 _mutex_fork(struct pthread *curthread)
237 {
238         struct pthread_mutex *m;
239
240         /*
241          * Fix mutex ownership for child process.
242          * note that process shared mutex should not
243          * be inherited because owner is forking thread
244          * which is in parent process, they should be
245          * removed from the owned mutex list, current,
246          * process shared mutex is not supported, so I
247          * am not worried.
248          */
249
250         TAILQ_FOREACH(m, &curthread->mutexq, m_qe)
251                 m->m_lock.m_owner = TID(curthread);
252         TAILQ_FOREACH(m, &curthread->pp_mutexq, m_qe)
253                 m->m_lock.m_owner = TID(curthread) | UMUTEX_CONTESTED;
254 }
255
256 int
257 _pthread_mutex_destroy(pthread_mutex_t *mutex)
258 {
259         struct pthread *curthread = _get_curthread();
260         pthread_mutex_t m;
261         uint32_t id;
262         int ret = 0;
263
264         if (__predict_false(*mutex == NULL))
265                 ret = EINVAL;
266         else {
267                 id = TID(curthread);
268
269                 /*
270                  * Try to lock the mutex structure, we only need to
271                  * try once, if failed, the mutex is in used.
272                  */
273                 ret = _thr_umutex_trylock(&(*mutex)->m_lock, id);
274                 if (ret)
275                         return (ret);
276                 m  = *mutex;
277                 /*
278                  * Check mutex other fields to see if this mutex is
279                  * in use. Mostly for prority mutex types, or there
280                  * are condition variables referencing it.
281                  */
282                 if (m->m_owner != NULL || m->m_refcount != 0) {
283                         if (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT)
284                                 set_inherited_priority(curthread, m);
285                         _thr_umutex_unlock(&m->m_lock, id);
286                         ret = EBUSY;
287                 } else {
288                         /*
289                          * Save a pointer to the mutex so it can be free'd
290                          * and set the caller's pointer to NULL.
291                          */
292                         *mutex = NULL;
293
294                         if (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT)
295                                 set_inherited_priority(curthread, m);
296                         _thr_umutex_unlock(&m->m_lock, id);
297
298                         MUTEX_ASSERT_NOT_OWNED(m);
299                         free(m);
300                 }
301         }
302
303         return (ret);
304 }
305
306 #define ENQUEUE_MUTEX(curthread, m)                                     \
307         do {                                                            \
308                 (m)->m_owner = curthread;                               \
309                 /* Add to the list of owned mutexes: */                 \
310                 MUTEX_ASSERT_NOT_OWNED((m));                            \
311                 if (((m)->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)   \
312                         TAILQ_INSERT_TAIL(&curthread->mutexq, (m), m_qe);\
313                 else                                                    \
314                         TAILQ_INSERT_TAIL(&curthread->pp_mutexq, (m), m_qe);\
315         } while (0)
316
317 static int
318 mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
319 {
320         struct pthread_mutex *m;
321         uint32_t id;
322         int ret;
323
324         id = TID(curthread);
325         m = *mutex;
326         if (m->m_private)
327                 THR_CRITICAL_ENTER(curthread);
328         ret = _thr_umutex_trylock(&m->m_lock, id);
329         if (ret == 0) {
330                 ENQUEUE_MUTEX(curthread, m);
331         } else if (m->m_owner == curthread) {
332                 ret = mutex_self_trylock(m);
333         } /* else {} */
334         if (ret && m->m_private)
335                 THR_CRITICAL_LEAVE(curthread);
336         return (ret);
337 }
338
339 int
340 __pthread_mutex_trylock(pthread_mutex_t *mutex)
341 {
342         struct pthread *curthread = _get_curthread();
343         int ret;
344
345         /*
346          * If the mutex is statically initialized, perform the dynamic
347          * initialization:
348          */
349         if (__predict_false(*mutex == NULL)) {
350                 ret = init_static(curthread, mutex);
351                 if (__predict_false(ret))
352                         return (ret);
353         }
354         return (mutex_trylock_common(curthread, mutex));
355 }
356
357 static int
358 mutex_lock_sleep(struct pthread *curthread, struct pthread_mutex *m,
359         const struct timespec *abstime)
360 {
361         uint32_t        id, owner;
362         int     count;
363         int     ret;
364
365         if (m->m_owner == curthread)
366                 return mutex_self_lock(m, abstime);
367
368         id = TID(curthread);
369         /*
370          * For adaptive mutexes, spin for a bit in the expectation
371          * that if the application requests this mutex type then
372          * the lock is likely to be released quickly and it is
373          * faster than entering the kernel
374          */
375         if (m->m_lock.m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT))
376                 goto sleep_in_kernel;
377
378         if (!_thr_is_smp)
379                 goto yield_loop;
380
381         count = m->m_spinloops;
382         while (count--) {
383                 owner = m->m_lock.m_owner;
384                 if ((owner & ~UMUTEX_CONTESTED) == 0) {
385                         if (atomic_cmpset_acq_32(&m->m_lock.m_owner, owner, id|owner)) {
386                                 ret = 0;
387                                 goto done;
388                         }
389                 }
390                 CPU_SPINWAIT;
391         }
392
393 yield_loop:
394         count = m->m_yieldloops;
395         while (count--) {
396                 _sched_yield();
397                 owner = m->m_lock.m_owner;
398                 if ((owner & ~UMUTEX_CONTESTED) == 0) {
399                         if (atomic_cmpset_acq_32(&m->m_lock.m_owner, owner, id|owner)) {
400                                 ret = 0;
401                                 goto done;
402                         }
403                 }
404         }
405
406 sleep_in_kernel:
407         if (abstime == NULL) {
408                 ret = __thr_umutex_lock(&m->m_lock, id);
409         } else if (__predict_false(
410                    abstime->tv_nsec < 0 ||
411                    abstime->tv_nsec >= 1000000000)) {
412                 ret = EINVAL;
413         } else {
414                 ret = __thr_umutex_timedlock(&m->m_lock, id, abstime);
415         }
416 done:
417         if (ret == 0)
418                 ENQUEUE_MUTEX(curthread, m);
419
420         return (ret);
421 }
422
423 static inline int
424 mutex_lock_common(struct pthread *curthread, struct pthread_mutex *m,
425         const struct timespec *abstime)
426 {
427         int ret;
428
429         if (m->m_private)
430                 THR_CRITICAL_ENTER(curthread);
431         if (_thr_umutex_trylock2(&m->m_lock, TID(curthread)) == 0) {
432                 ENQUEUE_MUTEX(curthread, m);
433                 ret = 0;
434         } else {
435                 ret = mutex_lock_sleep(curthread, m, abstime);
436         }
437         if (ret && m->m_private)
438                 THR_CRITICAL_LEAVE(curthread);
439         return (ret);
440 }
441
442 int
443 __pthread_mutex_lock(pthread_mutex_t *mutex)
444 {
445         struct pthread *curthread;
446         struct pthread_mutex *m;
447         int     ret;
448
449         _thr_check_init();
450
451         curthread = _get_curthread();
452
453         /*
454          * If the mutex is statically initialized, perform the dynamic
455          * initialization:
456          */
457         if (__predict_false((m = *mutex) == NULL)) {
458                 ret = init_static(curthread, mutex);
459                 if (__predict_false(ret))
460                         return (ret);
461                 m = *mutex;
462         }
463
464         return (mutex_lock_common(curthread, m, NULL));
465 }
466
467 int
468 __pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
469 {
470         struct pthread *curthread;
471         struct pthread_mutex *m;
472         int     ret;
473
474         _thr_check_init();
475
476         curthread = _get_curthread();
477
478         /*
479          * If the mutex is statically initialized, perform the dynamic
480          * initialization:
481          */
482         if (__predict_false((m = *mutex) == NULL)) {
483                 ret = init_static(curthread, mutex);
484                 if (__predict_false(ret))
485                         return (ret);
486                 m = *mutex;
487         }
488         return (mutex_lock_common(curthread, m, abstime));
489 }
490
491 int
492 _pthread_mutex_unlock(pthread_mutex_t *m)
493 {
494         return (mutex_unlock_common(m));
495 }
496
497 int
498 _mutex_cv_lock(pthread_mutex_t *m, int count)
499 {
500         int     ret;
501
502         ret = mutex_lock_common(_get_curthread(), *m, NULL);
503         if (ret == 0) {
504                 (*m)->m_refcount--;
505                 (*m)->m_count += count;
506         }
507         return (ret);
508 }
509
510 static int
511 mutex_self_trylock(pthread_mutex_t m)
512 {
513         int     ret;
514
515         switch (m->m_type) {
516         case PTHREAD_MUTEX_ERRORCHECK:
517         case PTHREAD_MUTEX_NORMAL:
518                 ret = EBUSY; 
519                 break;
520
521         case PTHREAD_MUTEX_RECURSIVE:
522                 /* Increment the lock count: */
523                 if (m->m_count + 1 > 0) {
524                         m->m_count++;
525                         ret = 0;
526                 } else
527                         ret = EAGAIN;
528                 break;
529
530         default:
531                 /* Trap invalid mutex types; */
532                 ret = EINVAL;
533         }
534
535         return (ret);
536 }
537
538 static int
539 mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
540 {
541         struct timespec ts1, ts2;
542         int     ret;
543
544         switch (m->m_type) {
545         case PTHREAD_MUTEX_ERRORCHECK:
546         case PTHREAD_MUTEX_ADAPTIVE_NP:
547                 if (abstime) {
548                         if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
549                             abstime->tv_nsec >= 1000000000) {
550                                 ret = EINVAL;
551                         } else {
552                                 clock_gettime(CLOCK_REALTIME, &ts1);
553                                 TIMESPEC_SUB(&ts2, abstime, &ts1);
554                                 __sys_nanosleep(&ts2, NULL);
555                                 ret = ETIMEDOUT;
556                         }
557                 } else {
558                         /*
559                          * POSIX specifies that mutexes should return
560                          * EDEADLK if a recursive lock is detected.
561                          */
562                         ret = EDEADLK; 
563                 }
564                 break;
565
566         case PTHREAD_MUTEX_NORMAL:
567                 /*
568                  * What SS2 define as a 'normal' mutex.  Intentionally
569                  * deadlock on attempts to get a lock you already own.
570                  */
571                 ret = 0;
572                 if (abstime) {
573                         if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
574                             abstime->tv_nsec >= 1000000000) {
575                                 ret = EINVAL;
576                         } else {
577                                 clock_gettime(CLOCK_REALTIME, &ts1);
578                                 TIMESPEC_SUB(&ts2, abstime, &ts1);
579                                 __sys_nanosleep(&ts2, NULL);
580                                 ret = ETIMEDOUT;
581                         }
582                 } else {
583                         ts1.tv_sec = 30;
584                         ts1.tv_nsec = 0;
585                         for (;;)
586                                 __sys_nanosleep(&ts1, NULL);
587                 }
588                 break;
589
590         case PTHREAD_MUTEX_RECURSIVE:
591                 /* Increment the lock count: */
592                 if (m->m_count + 1 > 0) {
593                         m->m_count++;
594                         ret = 0;
595                 } else
596                         ret = EAGAIN;
597                 break;
598
599         default:
600                 /* Trap invalid mutex types; */
601                 ret = EINVAL;
602         }
603
604         return (ret);
605 }
606
607 static int
608 mutex_unlock_common(pthread_mutex_t *mutex)
609 {
610         struct pthread *curthread = _get_curthread();
611         struct pthread_mutex *m;
612         uint32_t id;
613
614         if (__predict_false((m = *mutex) == NULL))
615                 return (EINVAL);
616
617         /*
618          * Check if the running thread is not the owner of the mutex.
619          */
620         if (__predict_false(m->m_owner != curthread))
621                 return (EPERM);
622
623         id = TID(curthread);
624         if (__predict_false(
625                 m->m_type == PTHREAD_MUTEX_RECURSIVE &&
626                 m->m_count > 0)) {
627                 m->m_count--;
628         } else {
629                 m->m_owner = NULL;
630                 /* Remove the mutex from the threads queue. */
631                 MUTEX_ASSERT_IS_OWNED(m);
632                 if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
633                         TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
634                 else {
635                         TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe);
636                         set_inherited_priority(curthread, m);
637                 }
638                 MUTEX_INIT_LINK(m);
639                 _thr_umutex_unlock(&m->m_lock, id);
640         }
641         if (m->m_private)
642                 THR_CRITICAL_LEAVE(curthread);
643         return (0);
644 }
645
646 int
647 _mutex_cv_unlock(pthread_mutex_t *mutex, int *count)
648 {
649         struct pthread *curthread = _get_curthread();
650         struct pthread_mutex *m;
651
652         if (__predict_false((m = *mutex) == NULL))
653                 return (EINVAL);
654
655         /*
656          * Check if the running thread is not the owner of the mutex.
657          */
658         if (__predict_false(m->m_owner != curthread))
659                 return (EPERM);
660
661         /*
662          * Clear the count in case this is a recursive mutex.
663          */
664         *count = m->m_count;
665         m->m_refcount++;
666         m->m_count = 0;
667         m->m_owner = NULL;
668         /* Remove the mutex from the threads queue. */
669         MUTEX_ASSERT_IS_OWNED(m);
670         if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
671                 TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
672         else {
673                 TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe);
674                 set_inherited_priority(curthread, m);
675         }
676         MUTEX_INIT_LINK(m);
677         _thr_umutex_unlock(&m->m_lock, TID(curthread));
678
679         if (m->m_private)
680                 THR_CRITICAL_LEAVE(curthread);
681         return (0);
682 }
683
684 int
685 _pthread_mutex_getprioceiling(pthread_mutex_t *mutex,
686                               int *prioceiling)
687 {
688         int ret;
689
690         if (*mutex == NULL)
691                 ret = EINVAL;
692         else if (((*mutex)->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
693                 ret = EINVAL;
694         else {
695                 *prioceiling = (*mutex)->m_lock.m_ceilings[0];
696                 ret = 0;
697         }
698
699         return(ret);
700 }
701
702 int
703 _pthread_mutex_setprioceiling(pthread_mutex_t *mutex,
704                               int ceiling, int *old_ceiling)
705 {
706         struct pthread *curthread = _get_curthread();
707         struct pthread_mutex *m, *m1, *m2;
708         int ret;
709
710         m = *mutex;
711         if (m == NULL || (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
712                 return (EINVAL);
713
714         ret = __thr_umutex_set_ceiling(&m->m_lock, ceiling, old_ceiling);
715         if (ret != 0)
716                 return (ret);
717
718         if (m->m_owner == curthread) {
719                 MUTEX_ASSERT_IS_OWNED(m);
720                 m1 = TAILQ_PREV(m, mutex_queue, m_qe);
721                 m2 = TAILQ_NEXT(m, m_qe);
722                 if ((m1 != NULL && m1->m_lock.m_ceilings[0] > (u_int)ceiling) ||
723                     (m2 != NULL && m2->m_lock.m_ceilings[0] < (u_int)ceiling)) {
724                         TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe);
725                         TAILQ_FOREACH(m2, &curthread->pp_mutexq, m_qe) {
726                                 if (m2->m_lock.m_ceilings[0] > (u_int)ceiling) {
727                                         TAILQ_INSERT_BEFORE(m2, m, m_qe);
728                                         return (0);
729                                 }
730                         }
731                         TAILQ_INSERT_TAIL(&curthread->pp_mutexq, m, m_qe);
732                 }
733         }
734         return (0);
735 }
736
737 int
738 _pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count)
739 {
740         if (*mutex == NULL)
741                 return (EINVAL);
742         *count = (*mutex)->m_spinloops;
743         return (0);
744 }
745
746 int
747 __pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count)
748 {
749         struct pthread *curthread = _get_curthread();
750         int ret;
751
752         if (__predict_false(*mutex == NULL)) {
753                 ret = init_static(curthread, mutex);
754                 if (__predict_false(ret))
755                         return (ret);
756         }
757         (*mutex)->m_spinloops = count;
758         return (0);
759 }
760
761 int
762 _pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count)
763 {
764         if (*mutex == NULL)
765                 return (EINVAL);
766         *count = (*mutex)->m_yieldloops;
767         return (0);
768 }
769
770 int
771 __pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count)
772 {
773         struct pthread *curthread = _get_curthread();
774         int ret;
775
776         if (__predict_false(*mutex == NULL)) {
777                 ret = init_static(curthread, mutex);
778                 if (__predict_false(ret))
779                         return (ret);
780         }
781         (*mutex)->m_yieldloops = count;
782         return (0);
783 }
784
785 int
786 _pthread_mutex_isowned_np(pthread_mutex_t *mutex)
787 {
788         struct pthread *curthread = _get_curthread();
789         int ret;
790
791         if (__predict_false(*mutex == NULL)) {
792                 ret = init_static(curthread, mutex);
793                 if (__predict_false(ret))
794                         return (ret);
795         }
796         return ((*mutex)->m_owner == curthread);
797 }