1 | /* $Id: semwait-linux.h 92779 2021-12-07 09:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Common semaphore wait code, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef IPRT_INCLUDED_SRC_r3_linux_semwait_linux_h
|
---|
28 | #define IPRT_INCLUDED_SRC_r3_linux_semwait_linux_h
|
---|
29 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 |
|
---|
34 | /* With 2.6.17 futex.h has become C++ unfriendly, so define the bits we need. */
|
---|
35 | #define FUTEX_WAIT 0
|
---|
36 | #define FUTEX_WAKE 1
|
---|
37 | #define FUTEX_WAIT_BITSET 9 /**< @since 2.6.25 - uses absolute timeout. */
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Wrapper for the futex syscall.
|
---|
42 | */
|
---|
43 | DECLINLINE(long) sys_futex(uint32_t volatile *uaddr, int op, int val, struct timespec *utime, int32_t *uaddr2, int val3)
|
---|
44 | {
|
---|
45 | errno = 0;
|
---|
46 | long rc = syscall(__NR_futex, uaddr, op, val, utime, uaddr2, val3);
|
---|
47 | if (rc < 0)
|
---|
48 | {
|
---|
49 | Assert(rc == -1);
|
---|
50 | rc = -errno;
|
---|
51 | }
|
---|
52 | return rc;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | DECL_NO_INLINE(static, void) rtSemLinuxCheckForFutexWaitBitSetSlow(int volatile *pfCanUseWaitBitSet)
|
---|
57 | {
|
---|
58 | uint32_t uTestVar = UINT32_MAX;
|
---|
59 | long rc = sys_futex(&uTestVar, FUTEX_WAIT_BITSET, UINT32_C(0xf0f0f0f0), NULL, NULL, UINT32_MAX);
|
---|
60 | *pfCanUseWaitBitSet = rc == -EAGAIN;
|
---|
61 | AssertMsg(rc == -ENOSYS || rc == -EAGAIN, ("%d\n", rc));
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | DECLINLINE(void) rtSemLinuxCheckForFutexWaitBitSet(int volatile *pfCanUseWaitBitSet)
|
---|
66 | {
|
---|
67 | if (*pfCanUseWaitBitSet != -1)
|
---|
68 | { /* likely */ }
|
---|
69 | else
|
---|
70 | rtSemLinuxCheckForFutexWaitBitSetSlow(pfCanUseWaitBitSet);
|
---|
71 | }
|
---|
72 |
|
---|
73 | #endif /* !IPRT_INCLUDED_SRC_r3_linux_semwait_linux_h */
|
---|
74 |
|
---|