1228904Sed/*-
2228904Sed * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3228904Sed * All rights reserved.
4228904Sed *
5228904Sed * Redistribution and use in source and binary forms, with or without
6228904Sed * modification, are permitted provided that the following conditions
7228904Sed * are met:
8228904Sed * 1. Redistributions of source code must retain the above copyright
9228904Sed *    notice, this list of conditions and the following disclaimer.
10228904Sed * 2. Redistributions in binary form must reproduce the above copyright
11228904Sed *    notice, this list of conditions and the following disclaimer in the
12228904Sed *    documentation and/or other materials provided with the distribution.
13228904Sed *
14228904Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15228904Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16228904Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17228904Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18228904Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19228904Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20228904Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21228904Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22228904Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23228904Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24228904Sed * SUCH DAMAGE.
25228904Sed *
26228904Sed * $FreeBSD$
27228904Sed */
28228904Sed
29228904Sed#include <sys/cdefs.h>
30228904Sed__FBSDID("$FreeBSD$");
31228904Sed
32228904Sed#include <errno.h>
33228904Sed#include <pthread.h>
34228904Sed
35228904Sed#include "threads.h"
36228904Sed
37228904Sedvoid
38228904Sedmtx_destroy(mtx_t *mtx)
39228904Sed{
40228904Sed
41228904Sed	(void)pthread_mutex_destroy(mtx);
42228904Sed}
43228904Sed
44228904Sedint
45228904Sedmtx_init(mtx_t *mtx, int type)
46228904Sed{
47228904Sed	pthread_mutexattr_t attr;
48228904Sed	int mt;
49228904Sed
50228904Sed	switch (type) {
51228904Sed	case mtx_plain:
52228904Sed	case mtx_timed:
53228904Sed		mt = PTHREAD_MUTEX_NORMAL;
54228904Sed		break;
55228904Sed	case mtx_plain | mtx_recursive:
56228904Sed	case mtx_timed | mtx_recursive:
57228904Sed		mt = PTHREAD_MUTEX_RECURSIVE;
58228904Sed		break;
59228904Sed	default:
60228904Sed		return (thrd_error);
61228904Sed	}
62228904Sed
63228904Sed	if (pthread_mutexattr_init(&attr) != 0)
64228904Sed		return (thrd_error);
65228904Sed	if (pthread_mutexattr_settype(&attr, mt) != 0)
66228904Sed		return (thrd_error);
67228904Sed	if (pthread_mutex_init(mtx, &attr) != 0)
68228904Sed		return (thrd_error);
69228904Sed	return (thrd_success);
70228904Sed}
71228904Sed
72228904Sedint
73228904Sedmtx_lock(mtx_t *mtx)
74228904Sed{
75228904Sed
76228904Sed	if (pthread_mutex_lock(mtx) != 0)
77228904Sed		return (thrd_error);
78228904Sed	return (thrd_success);
79228904Sed}
80228904Sed
81228904Sedint
82228904Sedmtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts)
83228904Sed{
84228904Sed
85228904Sed	switch (pthread_mutex_timedlock(mtx, ts)) {
86228904Sed	case 0:
87228904Sed		return (thrd_success);
88228904Sed	case ETIMEDOUT:
89228904Sed		return (thrd_timedout);
90228904Sed	default:
91228904Sed		return (thrd_error);
92228904Sed	}
93228904Sed}
94228904Sed
95228904Sedint
96228904Sedmtx_trylock(mtx_t *mtx)
97228904Sed{
98228904Sed
99228904Sed	switch (pthread_mutex_lock(mtx)) {
100228904Sed	case 0:
101228904Sed		return (thrd_success);
102228904Sed	case EBUSY:
103228904Sed		return (thrd_busy);
104228904Sed	default:
105228904Sed		return (thrd_error);
106228904Sed	}
107228904Sed}
108228904Sed
109228904Sedint
110228904Sedmtx_unlock(mtx_t *mtx)
111228904Sed{
112228904Sed
113228904Sed	if (pthread_mutex_unlock(mtx) != 0)
114228904Sed		return (thrd_error);
115228904Sed	return (thrd_success);
116228904Sed}
117