xref: /freebsd/lib/libthr/thread/thr_umtx.c (revision 3d11b6c8f01e1fca5936a11d6996448467851a94)
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
10  *    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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29 
30 #include "thr_private.h"
31 #include "thr_umtx.h"
32 
33 int
34 __thr_umtx_lock(volatile umtx_t *mtx, long id)
35 {
36 	 while (_umtx_op(__DEVOLATILE(struct umtx *, mtx),
37 		UMTX_OP_LOCK, id, 0, 0))
38 		;
39 	return (0);
40 }
41 
42 int
43 __thr_umtx_timedlock(volatile umtx_t *mtx, long id,
44 	const struct timespec *timeout)
45 {
46 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
47 		timeout->tv_nsec <= 0)))
48 		return (ETIMEDOUT);
49 	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_LOCK, id, 0,
50 		__DECONST(void *, timeout)) == 0)
51 		return (0);
52 	return (errno);
53 }
54 
55 int
56 __thr_umtx_unlock(volatile umtx_t *mtx, long id)
57 {
58 	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_UNLOCK,
59 		id, 0, 0) == 0)
60 		return (0);
61 	return (errno);
62 }
63 
64 int
65 _thr_umtx_wait(volatile umtx_t *mtx, long id, const struct timespec *timeout)
66 {
67 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
68 		timeout->tv_nsec <= 0)))
69 		return (ETIMEDOUT);
70 	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_WAIT, id, 0,
71 		__DECONST(void*, timeout)) == 0)
72 		return (0);
73 	return (errno);
74 }
75 
76 int
77 _thr_umtx_wake(volatile umtx_t *mtx, int nr_wakeup)
78 {
79 	if (_umtx_op(__DEVOLATILE(struct umtx *, mtx), UMTX_OP_WAKE,
80 		nr_wakeup, 0, 0) == 0)
81 		return (0);
82 	return (errno);
83 }
84