xref: /freebsd-src/lib/libstdthreads/mtx.c (revision a03f768612ad98a886458197c531a0b92203bf84)
1fc6f0665SEd Schouten /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4fc6f0665SEd Schouten  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
5fc6f0665SEd Schouten  * All rights reserved.
6fc6f0665SEd Schouten  *
7fc6f0665SEd Schouten  * Redistribution and use in source and binary forms, with or without
8fc6f0665SEd Schouten  * modification, are permitted provided that the following conditions
9fc6f0665SEd Schouten  * are met:
10fc6f0665SEd Schouten  * 1. Redistributions of source code must retain the above copyright
11fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer.
12fc6f0665SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
13fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
14fc6f0665SEd Schouten  *    documentation and/or other materials provided with the distribution.
15fc6f0665SEd Schouten  *
16fc6f0665SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fc6f0665SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fc6f0665SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fc6f0665SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fc6f0665SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fc6f0665SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fc6f0665SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fc6f0665SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fc6f0665SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fc6f0665SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fc6f0665SEd Schouten  * SUCH DAMAGE.
27fc6f0665SEd Schouten  */
28fc6f0665SEd Schouten 
29fc6f0665SEd Schouten #include <sys/cdefs.h>
30fc6f0665SEd Schouten #include <errno.h>
31fc6f0665SEd Schouten #include <pthread.h>
32fc6f0665SEd Schouten 
33fc6f0665SEd Schouten #include "threads.h"
34fc6f0665SEd Schouten 
35fc6f0665SEd Schouten void
mtx_destroy(mtx_t * mtx)36fc6f0665SEd Schouten mtx_destroy(mtx_t *mtx)
37fc6f0665SEd Schouten {
38fc6f0665SEd Schouten 
39fc6f0665SEd Schouten 	(void)pthread_mutex_destroy(mtx);
40fc6f0665SEd Schouten }
41fc6f0665SEd Schouten 
42fc6f0665SEd Schouten int
mtx_init(mtx_t * mtx,int type)43fc6f0665SEd Schouten mtx_init(mtx_t *mtx, int type)
44fc6f0665SEd Schouten {
45fc6f0665SEd Schouten 	pthread_mutexattr_t attr;
46*a03f7686SHodong 	int mt, res;
47fc6f0665SEd Schouten 
48fc6f0665SEd Schouten 	switch (type) {
49fc6f0665SEd Schouten 	case mtx_plain:
50fc6f0665SEd Schouten 	case mtx_timed:
51fc6f0665SEd Schouten 		mt = PTHREAD_MUTEX_NORMAL;
52fc6f0665SEd Schouten 		break;
53fc6f0665SEd Schouten 	case mtx_plain | mtx_recursive:
54fc6f0665SEd Schouten 	case mtx_timed | mtx_recursive:
55fc6f0665SEd Schouten 		mt = PTHREAD_MUTEX_RECURSIVE;
56fc6f0665SEd Schouten 		break;
57fc6f0665SEd Schouten 	default:
58fc6f0665SEd Schouten 		return (thrd_error);
59fc6f0665SEd Schouten 	}
60fc6f0665SEd Schouten 
61fc6f0665SEd Schouten 	if (pthread_mutexattr_init(&attr) != 0)
62fc6f0665SEd Schouten 		return (thrd_error);
63*a03f7686SHodong 	res = thrd_success;
64*a03f7686SHodong 	if (pthread_mutexattr_settype(&attr, mt) != 0 ||
65*a03f7686SHodong 	    pthread_mutex_init(mtx, &attr) != 0)
66*a03f7686SHodong 		res = thrd_error;
67*a03f7686SHodong 	pthread_mutexattr_destroy(&attr);
68*a03f7686SHodong 	return (res);
69fc6f0665SEd Schouten }
70fc6f0665SEd Schouten 
71fc6f0665SEd Schouten int
mtx_lock(mtx_t * mtx)72fc6f0665SEd Schouten mtx_lock(mtx_t *mtx)
73fc6f0665SEd Schouten {
74fc6f0665SEd Schouten 
75fc6f0665SEd Schouten 	if (pthread_mutex_lock(mtx) != 0)
76fc6f0665SEd Schouten 		return (thrd_error);
77fc6f0665SEd Schouten 	return (thrd_success);
78fc6f0665SEd Schouten }
79fc6f0665SEd Schouten 
80fc6f0665SEd Schouten int
mtx_timedlock(mtx_t * restrict mtx,const struct timespec * restrict ts)81fc6f0665SEd Schouten mtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts)
82fc6f0665SEd Schouten {
83fc6f0665SEd Schouten 
84fc6f0665SEd Schouten 	switch (pthread_mutex_timedlock(mtx, ts)) {
85fc6f0665SEd Schouten 	case 0:
86fc6f0665SEd Schouten 		return (thrd_success);
87fc6f0665SEd Schouten 	case ETIMEDOUT:
88fc6f0665SEd Schouten 		return (thrd_timedout);
89fc6f0665SEd Schouten 	default:
90fc6f0665SEd Schouten 		return (thrd_error);
91fc6f0665SEd Schouten 	}
92fc6f0665SEd Schouten }
93fc6f0665SEd Schouten 
94fc6f0665SEd Schouten int
mtx_trylock(mtx_t * mtx)95fc6f0665SEd Schouten mtx_trylock(mtx_t *mtx)
96fc6f0665SEd Schouten {
97fc6f0665SEd Schouten 
9885e7eba6SKonstantin Belousov 	switch (pthread_mutex_trylock(mtx)) {
99fc6f0665SEd Schouten 	case 0:
100fc6f0665SEd Schouten 		return (thrd_success);
101fc6f0665SEd Schouten 	case EBUSY:
102fc6f0665SEd Schouten 		return (thrd_busy);
103fc6f0665SEd Schouten 	default:
104fc6f0665SEd Schouten 		return (thrd_error);
105fc6f0665SEd Schouten 	}
106fc6f0665SEd Schouten }
107fc6f0665SEd Schouten 
108fc6f0665SEd Schouten int
mtx_unlock(mtx_t * mtx)109fc6f0665SEd Schouten mtx_unlock(mtx_t *mtx)
110fc6f0665SEd Schouten {
111fc6f0665SEd Schouten 
112fc6f0665SEd Schouten 	if (pthread_mutex_unlock(mtx) != 0)
113fc6f0665SEd Schouten 		return (thrd_error);
114fc6f0665SEd Schouten 	return (thrd_success);
115fc6f0665SEd Schouten }
116