/src/brpc/src/bthread/sys_futex.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // bthread - An M:N threading library to make applications more concurrent. |
19 | | |
20 | | // Date: Tue Jul 10 17:40:58 CST 2012 |
21 | | |
22 | | #ifndef BTHREAD_SYS_FUTEX_H |
23 | | #define BTHREAD_SYS_FUTEX_H |
24 | | |
25 | | #include "butil/build_config.h" // OS_MACOSX |
26 | | #include <unistd.h> // syscall |
27 | | #include <time.h> // timespec |
28 | | #if defined(OS_LINUX) |
29 | | #include <syscall.h> // SYS_futex |
30 | | #include <linux/futex.h> // FUTEX_WAIT, FUTEX_WAKE |
31 | | |
32 | | namespace bthread { |
33 | | |
34 | | #ifndef FUTEX_PRIVATE_FLAG |
35 | | #define FUTEX_PRIVATE_FLAG 128 |
36 | | #endif |
37 | | |
38 | | inline int futex_wait_private( |
39 | 0 | void* addr1, int expected, const timespec* timeout) { |
40 | 0 | return syscall(SYS_futex, addr1, (FUTEX_WAIT | FUTEX_PRIVATE_FLAG), |
41 | 0 | expected, timeout, NULL, 0); |
42 | 0 | } |
43 | | |
44 | 0 | inline int futex_wake_private(void* addr1, int nwake) { |
45 | 0 | return syscall(SYS_futex, addr1, (FUTEX_WAKE | FUTEX_PRIVATE_FLAG), |
46 | 0 | nwake, NULL, NULL, 0); |
47 | 0 | } |
48 | | |
49 | 0 | inline int futex_requeue_private(void* addr1, int nwake, void* addr2) { |
50 | 0 | return syscall(SYS_futex, addr1, (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG), |
51 | 0 | nwake, NULL, addr2, 0); |
52 | 0 | } |
53 | | |
54 | | } // namespace bthread |
55 | | |
56 | | #elif defined(OS_MACOSX) |
57 | | |
58 | | namespace bthread { |
59 | | |
60 | | int futex_wait_private(void* addr1, int expected, const timespec* timeout); |
61 | | |
62 | | int futex_wake_private(void* addr1, int nwake); |
63 | | |
64 | | int futex_requeue_private(void* addr1, int nwake, void* addr2); |
65 | | |
66 | | } // namespace bthread |
67 | | |
68 | | #else |
69 | | #error "Unsupported OS" |
70 | | #endif |
71 | | |
72 | | #endif // BTHREAD_SYS_FUTEX_H |