/src/brpc/src/bthread/butex.h
Line | Count | Source (jump to first uncovered line) |
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 22 17:30:12 CST 2014 |
21 | | |
22 | | #ifndef BTHREAD_BUTEX_H |
23 | | #define BTHREAD_BUTEX_H |
24 | | |
25 | | #include <errno.h> // users need to check errno |
26 | | #include <time.h> // timespec |
27 | | #include "butil/macros.h" // BAIDU_CASSERT |
28 | | #include "bthread/types.h" // bthread_t |
29 | | |
30 | | namespace bthread { |
31 | | |
32 | | // If a thread would suspend for less than so many microseconds, return |
33 | | // ETIMEDOUT directly. |
34 | | // Use 1: sleeping for less than 2 microsecond is inefficient and useless. |
35 | | static const int64_t MIN_SLEEP_US = 2; |
36 | | |
37 | | // Create a butex which is a futex-like 32-bit primitive for synchronizing |
38 | | // bthreads/pthreads. |
39 | | // Returns a pointer to 32-bit data, NULL on failure. |
40 | | // NOTE: all butexes are private(not inter-process). |
41 | | void* butex_create(); |
42 | | |
43 | | // Check width of user type before casting. |
44 | 0 | template <typename T> T* butex_create_checked() { |
45 | 0 | BAIDU_CASSERT(sizeof(T) == sizeof(int), sizeof_T_must_equal_int); |
46 | 0 | return static_cast<T*>(butex_create()); |
47 | 0 | } Unexecuted instantiation: unsigned int* bthread::butex_create_checked<unsigned int>() Unexecuted instantiation: butil::atomic<int>* bthread::butex_create_checked<butil::atomic<int> >() |
48 | | |
49 | | // Destroy the butex. |
50 | | void butex_destroy(void* butex); |
51 | | |
52 | | // Wake up at most 1 thread waiting on |butex|. |
53 | | // Returns # of threads woken up. |
54 | | int butex_wake(void* butex, bool nosignal = false); |
55 | | |
56 | | // Wake up all threads waiting on |butex| if n is zero, |
57 | | // Otherwise, wake up at most n thread waiting on |butex|. |
58 | | // Returns # of threads woken up. |
59 | | int butex_wake_n(void* butex, size_t n, bool nosignal = false); |
60 | | |
61 | | // Wake up all threads waiting on |butex|. |
62 | | // Returns # of threads woken up. |
63 | | int butex_wake_all(void* butex, bool nosignal = false); |
64 | | |
65 | | // Wake up all threads waiting on |butex| except a bthread whose identifier |
66 | | // is |excluded_bthread|. This function does not yield. |
67 | | // Returns # of threads woken up. |
68 | | int butex_wake_except(void* butex, bthread_t excluded_bthread); |
69 | | |
70 | | // Wake up at most 1 thread waiting on |butex1|, let all other threads wait |
71 | | // on |butex2| instead. |
72 | | // Returns # of threads woken up. |
73 | | int butex_requeue(void* butex1, void* butex2); |
74 | | |
75 | | // Atomically wait on |butex| if *butex equals |expected_value|, until the |
76 | | // butex is woken up by butex_wake*, or CLOCK_REALTIME reached |abstime| if |
77 | | // abstime is not NULL. |
78 | | // About |abstime|: |
79 | | // Different from FUTEX_WAIT, butex_wait uses absolute time. |
80 | | // About |prepend|: |
81 | | // If |prepend| is true, queue the bthread at the head of the queue, |
82 | | // otherwise at the tail. |
83 | | // Returns 0 on success, -1 otherwise and errno is set. |
84 | | int butex_wait(void* butex, int expected_value, |
85 | | const timespec* abstime, |
86 | | bool prepend = false); |
87 | | |
88 | | } // namespace bthread |
89 | | |
90 | | #endif // BTHREAD_BUTEX_H |