xref: /freebsd/lib/libstdthreads/mtx.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1fc6f0665SEd Schouten /*-
2*4d846d26SWarner 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  * $FreeBSD$
29fc6f0665SEd Schouten  */
30fc6f0665SEd Schouten 
31fc6f0665SEd Schouten #include <sys/cdefs.h>
32fc6f0665SEd Schouten __FBSDID("$FreeBSD$");
33fc6f0665SEd Schouten 
34fc6f0665SEd Schouten #include <errno.h>
35fc6f0665SEd Schouten #include <pthread.h>
36fc6f0665SEd Schouten 
37fc6f0665SEd Schouten #include "threads.h"
38fc6f0665SEd Schouten 
39fc6f0665SEd Schouten void
40fc6f0665SEd Schouten mtx_destroy(mtx_t *mtx)
41fc6f0665SEd Schouten {
42fc6f0665SEd Schouten 
43fc6f0665SEd Schouten 	(void)pthread_mutex_destroy(mtx);
44fc6f0665SEd Schouten }
45fc6f0665SEd Schouten 
46fc6f0665SEd Schouten int
47fc6f0665SEd Schouten mtx_init(mtx_t *mtx, int type)
48fc6f0665SEd Schouten {
49fc6f0665SEd Schouten 	pthread_mutexattr_t attr;
50fc6f0665SEd Schouten 	int mt;
51fc6f0665SEd Schouten 
52fc6f0665SEd Schouten 	switch (type) {
53fc6f0665SEd Schouten 	case mtx_plain:
54fc6f0665SEd Schouten 	case mtx_timed:
55fc6f0665SEd Schouten 		mt = PTHREAD_MUTEX_NORMAL;
56fc6f0665SEd Schouten 		break;
57fc6f0665SEd Schouten 	case mtx_plain | mtx_recursive:
58fc6f0665SEd Schouten 	case mtx_timed | mtx_recursive:
59fc6f0665SEd Schouten 		mt = PTHREAD_MUTEX_RECURSIVE;
60fc6f0665SEd Schouten 		break;
61fc6f0665SEd Schouten 	default:
62fc6f0665SEd Schouten 		return (thrd_error);
63fc6f0665SEd Schouten 	}
64fc6f0665SEd Schouten 
65fc6f0665SEd Schouten 	if (pthread_mutexattr_init(&attr) != 0)
66fc6f0665SEd Schouten 		return (thrd_error);
67fc6f0665SEd Schouten 	if (pthread_mutexattr_settype(&attr, mt) != 0)
68fc6f0665SEd Schouten 		return (thrd_error);
69fc6f0665SEd Schouten 	if (pthread_mutex_init(mtx, &attr) != 0)
70fc6f0665SEd Schouten 		return (thrd_error);
71fc6f0665SEd Schouten 	return (thrd_success);
72fc6f0665SEd Schouten }
73fc6f0665SEd Schouten 
74fc6f0665SEd Schouten int
75fc6f0665SEd Schouten mtx_lock(mtx_t *mtx)
76fc6f0665SEd Schouten {
77fc6f0665SEd Schouten 
78fc6f0665SEd Schouten 	if (pthread_mutex_lock(mtx) != 0)
79fc6f0665SEd Schouten 		return (thrd_error);
80fc6f0665SEd Schouten 	return (thrd_success);
81fc6f0665SEd Schouten }
82fc6f0665SEd Schouten 
83fc6f0665SEd Schouten int
84fc6f0665SEd Schouten mtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts)
85fc6f0665SEd Schouten {
86fc6f0665SEd Schouten 
87fc6f0665SEd Schouten 	switch (pthread_mutex_timedlock(mtx, ts)) {
88fc6f0665SEd Schouten 	case 0:
89fc6f0665SEd Schouten 		return (thrd_success);
90fc6f0665SEd Schouten 	case ETIMEDOUT:
91fc6f0665SEd Schouten 		return (thrd_timedout);
92fc6f0665SEd Schouten 	default:
93fc6f0665SEd Schouten 		return (thrd_error);
94fc6f0665SEd Schouten 	}
95fc6f0665SEd Schouten }
96fc6f0665SEd Schouten 
97fc6f0665SEd Schouten int
98fc6f0665SEd Schouten mtx_trylock(mtx_t *mtx)
99fc6f0665SEd Schouten {
100fc6f0665SEd Schouten 
10185e7eba6SKonstantin Belousov 	switch (pthread_mutex_trylock(mtx)) {
102fc6f0665SEd Schouten 	case 0:
103fc6f0665SEd Schouten 		return (thrd_success);
104fc6f0665SEd Schouten 	case EBUSY:
105fc6f0665SEd Schouten 		return (thrd_busy);
106fc6f0665SEd Schouten 	default:
107fc6f0665SEd Schouten 		return (thrd_error);
108fc6f0665SEd Schouten 	}
109fc6f0665SEd Schouten }
110fc6f0665SEd Schouten 
111fc6f0665SEd Schouten int
112fc6f0665SEd Schouten mtx_unlock(mtx_t *mtx)
113fc6f0665SEd Schouten {
114fc6f0665SEd Schouten 
115fc6f0665SEd Schouten 	if (pthread_mutex_unlock(mtx) != 0)
116fc6f0665SEd Schouten 		return (thrd_error);
117fc6f0665SEd Schouten 	return (thrd_success);
118fc6f0665SEd Schouten }
119