xref: /llvm-project/lldb/tools/debugserver/source/PThreadEvent.h (revision 87f2c25f3dad65890fc8e9abbe9765e44979ed9d)
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 #include "PThreadCondition.h"
16 #include "PThreadMutex.h"
17 #include <cstdint>
18 #include <ctime>
19 #include <functional>
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   // pthread condition and mutex variable to control access and allow
49   // blocking between the main thread and the spotlight index thread.
50   mutable PThreadMutex m_mutex;
51   mutable PThreadCondition m_set_condition;
52   mutable PThreadCondition m_reset_condition;
53   uint32_t m_bits;
54   uint32_t m_validBits;
55   uint32_t m_reset_ack_mask;
56 
57   uint32_t GetBitsMasked(uint32_t mask) const { return mask & m_bits; }
58 
59   uint32_t WaitForEventsImpl(const uint32_t mask,
60                              const struct timespec *timeout_abstime,
61                              std::function<bool()> predicate) const;
62 
63 private:
64   PThreadEvent(const PThreadEvent &) = delete;
65   PThreadEvent &operator=(const PThreadEvent &rhs) = delete;
66 };
67 
68 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_PTHREADEVENT_H
69