23 #include "fatal-error.h" |
23 #include "fatal-error.h" |
24 #include "system-condition.h" |
24 #include "system-condition.h" |
25 #include "log.h" |
25 #include "log.h" |
26 |
26 |
27 |
27 |
|
28 /** |
|
29 * \file |
|
30 * \ingroup thread |
|
31 * Thread conditional wait implementation for Unix-like systems. |
|
32 */ |
|
33 |
28 namespace ns3 { |
34 namespace ns3 { |
29 |
35 |
30 NS_LOG_COMPONENT_DEFINE ("SystemCondition"); |
36 NS_LOG_COMPONENT_DEFINE ("SystemCondition"); |
31 |
37 |
|
38 /** |
|
39 * \ingroup thread |
|
40 * Implementation of SystemCondition for Unix-like systems. |
|
41 */ |
32 class SystemConditionPrivate { |
42 class SystemConditionPrivate { |
33 public: |
43 public: |
34 /// Conversion from ns to s. |
44 /// Conversion from ns to s. |
35 static const uint64_t NS_PER_SEC = (uint64_t)1000000000; |
45 static const uint64_t NS_PER_SEC = (uint64_t)1000000000; |
36 |
46 |
|
47 /** Constructor. */ |
37 SystemConditionPrivate (); |
48 SystemConditionPrivate (); |
|
49 /** Destructor. */ |
38 ~SystemConditionPrivate (); |
50 ~SystemConditionPrivate (); |
39 |
51 |
|
52 /** |
|
53 * Set the condition. |
|
54 * |
|
55 * \param condition The new condition value. |
|
56 */ |
40 void SetCondition (bool condition); |
57 void SetCondition (bool condition); |
|
58 /** |
|
59 * Get the condition value. |
|
60 * |
|
61 * \returns The condition value. |
|
62 */ |
41 bool GetCondition (void); |
63 bool GetCondition (void); |
|
64 /** Signal the condition. */ |
42 void Signal (void); |
65 void Signal (void); |
|
66 /** Broadcast the condition. */ |
43 void Broadcast (void); |
67 void Broadcast (void); |
|
68 /** |
|
69 * Unset the condition, then wait for another thread |
|
70 * to set it with SetCondition. */ |
44 void Wait (void); |
71 void Wait (void); |
|
72 /** |
|
73 * Unset the condition, then wait for a limited amount of wall-clock |
|
74 * time for another thread to set it with SetCondition. |
|
75 * |
|
76 * \param ns Maximum time to wait, in ns. |
|
77 * \returns \c true if the condition timed out; \c false if the other |
|
78 * thread set it. |
|
79 */ |
45 bool TimedWait (uint64_t ns); |
80 bool TimedWait (uint64_t ns); |
46 |
81 |
47 private: |
82 private: |
|
83 /** Mutex controlling access to the condition. */ |
48 pthread_mutex_t m_mutex; |
84 pthread_mutex_t m_mutex; |
|
85 /** The pthread condition variable. */ |
49 pthread_cond_t m_cond; |
86 pthread_cond_t m_cond; |
|
87 /** The condition state. */ |
50 bool m_condition; |
88 bool m_condition; |
51 }; |
89 }; |
52 |
90 |
53 SystemConditionPrivate::SystemConditionPrivate () |
91 SystemConditionPrivate::SystemConditionPrivate () |
54 { |
92 { |