xref: /aosp_15_r20/external/angle/src/common/SimpleMutex.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2024 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // SimpleMutex.cpp:
7 //   Implementation of SimpleMutex.h.
8 
9 #include "common/SimpleMutex.h"
10 
11 #if ANGLE_USE_FUTEX
12 
13 #    include <limits.h>
14 #    include <stdint.h>
15 
16 #    if defined(ANGLE_PLATFORM_LINUX) || defined(ANGLE_PLATFORM_ANDROID)
17 #        include <linux/futex.h>
18 #        include <sys/syscall.h>
19 #        include <unistd.h>
20 #    endif  // defined(ANGLE_PLATFORM_LINUX) || defined(ANGLE_PLATFORM_ANDROID)
21 
22 #    if defined(ANGLE_PLATFORM_WINDOWS)
23 #        include <errno.h>
24 #        include <windows.h>
25 #    endif  // defined(ANGLE_PLATFORM_WINDOWS)
26 
27 namespace angle
28 {
29 namespace priv
30 {
31 #    if defined(ANGLE_PLATFORM_LINUX) || defined(ANGLE_PLATFORM_ANDROID)
32 namespace
33 {
SysFutex(void * addr,int op,int val,int val3)34 ANGLE_INLINE void SysFutex(void *addr, int op, int val, int val3)
35 {
36     syscall(SYS_futex, addr, op, val, nullptr, nullptr, val3);
37 }
38 }  // anonymous namespace
39 
futexWait()40 void MutexOnFutex::futexWait()
41 {
42     SysFutex(&mState, FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG, kBlocked, FUTEX_BITSET_MATCH_ANY);
43 }
futexWake()44 void MutexOnFutex::futexWake()
45 {
46     SysFutex(&mState, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, kLocked, 0);
47 }
48 #    endif  // defined(ANGLE_PLATFORM_LINUX) || defined(ANGLE_PLATFORM_ANDROID)
49 
50 #    if defined(ANGLE_PLATFORM_WINDOWS)
futexWait()51 void MutexOnFutex::futexWait()
52 {
53     int value = kBlocked;
54     WaitOnAddress(&mState, &value, sizeof(value), INFINITE);
55 }
56 
futexWake()57 void MutexOnFutex::futexWake()
58 {
59     WakeByAddressSingle(&mState);
60 }
61 #    endif  // defined(ANGLE_PLATFORM_WINDOWS)
62 }  // namespace priv
63 }  // namespace angle
64 
65 #endif  // ANGLE_USE_FUTEX
66