Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/util/futex.h
4545 views
1
/*
2
* Copyright © 2015 Intel
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
24
#ifndef UTIL_FUTEX_H
25
#define UTIL_FUTEX_H
26
27
#if defined(HAVE_LINUX_FUTEX_H)
28
#define UTIL_FUTEX_SUPPORTED 1
29
30
#include <limits.h>
31
#include <stdint.h>
32
#include <unistd.h>
33
#include <linux/futex.h>
34
#include <sys/syscall.h>
35
#include <sys/time.h>
36
37
static inline long sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2, int val3)
38
{
39
return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
40
}
41
42
static inline int futex_wake(uint32_t *addr, int count)
43
{
44
return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
45
}
46
47
static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
48
{
49
/* FUTEX_WAIT_BITSET with FUTEX_BITSET_MATCH_ANY is equivalent to
50
* FUTEX_WAIT, except that it treats the timeout as absolute. */
51
return sys_futex(addr, FUTEX_WAIT_BITSET, value, timeout, NULL,
52
FUTEX_BITSET_MATCH_ANY);
53
}
54
55
#elif defined(__FreeBSD__)
56
#define UTIL_FUTEX_SUPPORTED 1
57
58
#include <assert.h>
59
#include <errno.h>
60
#include <fcntl.h>
61
#include <sys/types.h>
62
#include <sys/umtx.h>
63
#include <sys/time.h>
64
65
static inline int futex_wake(uint32_t *addr, int count)
66
{
67
assert(count == (int)(uint32_t)count); /* Check that bits weren't discarded */
68
return _umtx_op(addr, UMTX_OP_WAKE, (uint32_t)count, NULL, NULL) == -1 ? errno : 0;
69
}
70
71
static inline int futex_wait(uint32_t *addr, int32_t value, struct timespec *timeout)
72
{
73
void *uaddr = NULL, *uaddr2 = NULL;
74
struct _umtx_time tmo = {
75
._flags = UMTX_ABSTIME,
76
._clockid = CLOCK_MONOTONIC
77
};
78
79
assert(value == (int)(uint32_t)value); /* Check that bits weren't discarded */
80
81
if (timeout != NULL) {
82
tmo._timeout = *timeout;
83
uaddr = (void *)(uintptr_t)sizeof(tmo);
84
uaddr2 = (void *)&tmo;
85
}
86
87
return _umtx_op(addr, UMTX_OP_WAIT_UINT, (uint32_t)value, uaddr, uaddr2) == -1 ? errno : 0;
88
}
89
90
#elif defined(__OpenBSD__)
91
#define UTIL_FUTEX_SUPPORTED 1
92
93
#include <sys/time.h>
94
#include <sys/futex.h>
95
96
static inline int futex_wake(uint32_t *addr, int count)
97
{
98
return futex(addr, FUTEX_WAKE, count, NULL, NULL);
99
}
100
101
static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
102
{
103
struct timespec tsnow, tsrel;
104
105
if (timeout == NULL)
106
return futex(addr, FUTEX_WAIT, value, NULL, NULL);
107
108
clock_gettime(CLOCK_MONOTONIC, &tsnow);
109
if (timespeccmp(&tsnow, timeout, <))
110
timespecsub(timeout, &tsnow, &tsrel);
111
else
112
timespecclear(&tsrel);
113
return futex(addr, FUTEX_WAIT, value, &tsrel, NULL);
114
}
115
116
#else
117
#define UTIL_FUTEX_SUPPORTED 0
118
#endif
119
120
#endif /* UTIL_FUTEX_H */
121
122