1 #pragma once
2
3 /*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <unistd.h>
20 #include <linux/futex.h>
21 #include <sys/syscall.h>
22
23
24 // Signaling mechanism that allows threads to signal changes to shared
25 // memory and to wait for signals.
26
27 namespace vsoc {
28 /**
29 * Defines the strategy for signaling among threads on a single kernel.
30 */
31 namespace SingleSidedSignal {
32 /**
33 * Waits for a signal, assuming the the word at addr matches expected_state.
34 * Will return immediately if the value does not match.
35 * Callers must be equipped to cope with spurious returns.
36 */
AwaitSignal(uint32_t expected_state,uint32_t * uaddr)37 static void AwaitSignal(uint32_t expected_state, uint32_t* uaddr) {
38 syscall(SYS_futex, uaddr, FUTEX_WAIT, expected_state, nullptr, nullptr, 0);
39 }
40
41 /**
42 * Sends a signal to every thread in AwaitSignal() using the address in
43 * uaddr.
44 */
Signal(std::atomic<uint32_t> * uaddr)45 static void Signal(std::atomic<uint32_t>* uaddr) {
46 syscall(SYS_futex, reinterpret_cast<int32_t*>(uaddr), FUTEX_WAKE, -1, nullptr,
47 nullptr, 0);
48 }
49 } // namespace SingleSidedSignal
50 } // namespace vsoc
51