1 //===-- PThreadEvent.h ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Created by Greg Clayton on 6/16/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef __PThreadEvent_h__
15 #define __PThreadEvent_h__
16 #include "PThreadCondition.h"
17 #include "PThreadMutex.h"
18 #include <stdint.h>
19 #include <time.h>
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   //----------------------------------------------------------------------
49   // pthread condition and mutex variable to control access and allow
50   // blocking between the main thread and the spotlight index thread.
51   //----------------------------------------------------------------------
52   mutable PThreadMutex m_mutex;
53   mutable PThreadCondition m_set_condition;
54   mutable PThreadCondition m_reset_condition;
55   uint32_t m_bits;
56   uint32_t m_validBits;
57   uint32_t m_reset_ack_mask;
58 
59 private:
60   PThreadEvent(const PThreadEvent &); // Outlaw copy constructor
61   PThreadEvent &operator=(const PThreadEvent &rhs);
62 };
63 
64 #endif // #ifndef __PThreadEvent_h__
65