1 | //===-- PThreadEvent.h ------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Created by Greg Clayton on 6/16/07. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_PTHREADEVENT_H |
14 | #define LLDB_TOOLS_DEBUGSERVER_SOURCE_PTHREADEVENT_H |
15 | |
16 | #include <cstdint> |
17 | #include <ctime> |
18 | #include <functional> |
19 | #include <mutex> |
20 | |
21 | class PThreadEvent { |
22 | public: |
23 | PThreadEvent(uint32_t bits = 0, uint32_t validBits = 0); |
24 | ~PThreadEvent(); |
25 | |
26 | uint32_t NewEventBit(); |
27 | void FreeEventBits(const uint32_t mask); |
28 | |
29 | void ReplaceEventBits(const uint32_t bits); |
30 | uint32_t GetEventBits() const; |
31 | void SetEvents(const uint32_t mask); |
32 | void ResetEvents(const uint32_t mask); |
33 | // Wait for events to be set or reset. These functions take an optional |
34 | // timeout value. If timeout is NULL an infinite timeout will be used. |
35 | uint32_t |
36 | WaitForSetEvents(const uint32_t mask, |
37 | const struct timespec *timeout_abstime = NULL) const; |
38 | uint32_t |
39 | WaitForEventsToReset(const uint32_t mask, |
40 | const struct timespec *timeout_abstime = NULL) const; |
41 | |
42 | uint32_t GetResetAckMask() const { return m_reset_ack_mask; } |
43 | uint32_t SetResetAckMask(uint32_t mask) { return m_reset_ack_mask = mask; } |
44 | uint32_t WaitForResetAck(const uint32_t mask, |
45 | const struct timespec *timeout_abstime = NULL) const; |
46 | |
47 | protected: |
48 | mutable std::mutex m_mutex; |
49 | mutable std::condition_variable m_set_condition; |
50 | uint32_t m_bits; |
51 | uint32_t m_validBits; |
52 | uint32_t m_reset_ack_mask; |
53 | |
54 | uint32_t GetBitsMasked(uint32_t mask) const { return mask & m_bits; } |
55 | |
56 | uint32_t WaitForEventsImpl(const uint32_t mask, |
57 | const struct timespec *timeout_abstime, |
58 | std::function<bool()> predicate) const; |
59 | |
60 | private: |
61 | PThreadEvent(const PThreadEvent &) = delete; |
62 | PThreadEvent &operator=(const PThreadEvent &rhs) = delete; |
63 | }; |
64 | |
65 | #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_PTHREADEVENT_H |
66 | |