2 * Copyright 2007-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef _KERNEL_CONDITION_VARIABLE_H
6 #define _KERNEL_CONDITION_VARIABLE_H
15 #include <util/DoublyLinkedList.h>
16 #include <util/OpenHashTable.h>
19 struct ConditionVariable
;
22 struct ConditionVariableEntry
23 : DoublyLinkedListLinkImpl
<ConditionVariableEntry
> {
26 inline ConditionVariableEntry();
27 inline ~ConditionVariableEntry();
30 bool Add(const void* object
);
31 status_t
Wait(uint32 flags
= 0, bigtime_t timeout
= 0);
32 status_t
Wait(const void* object
, uint32 flags
= 0,
33 bigtime_t timeout
= 0);
35 inline status_t
WaitStatus() const { return fWaitStatus
; }
37 inline ConditionVariable
* Variable() const { return fVariable
; }
40 inline void AddToVariable(ConditionVariable
* variable
);
43 ConditionVariable
* fVariable
;
47 friend struct ConditionVariable
;
51 struct ConditionVariable
{
53 void Init(const void* object
,
54 const char* objectType
);
55 // for anonymous (unpublished) cvars
57 void Publish(const void* object
,
58 const char* objectType
);
61 inline void NotifyOne(status_t result
= B_OK
);
62 inline void NotifyAll(status_t result
= B_OK
);
64 static void NotifyOne(const void* object
, status_t result
);
65 static void NotifyAll(const void* object
, status_t result
);
66 // (both methods) caller must ensure that
67 // the variable is not unpublished
70 void Add(ConditionVariableEntry
* entry
);
72 status_t
Wait(uint32 flags
= 0, bigtime_t timeout
= 0);
73 // all-in one, i.e. doesn't need a
74 // ConditionVariableEntry
76 const void* Object() const { return fObject
; }
77 const char* ObjectType() const { return fObjectType
; }
79 static void ListAll();
83 void _Notify(bool all
, status_t result
);
84 void _NotifyLocked(bool all
, status_t result
);
87 typedef DoublyLinkedList
<ConditionVariableEntry
> EntryList
;
90 const char* fObjectType
;
92 ConditionVariable
* fNext
;
94 friend struct ConditionVariableEntry
;
95 friend struct ConditionVariableHashDefinition
;
102 ConditionVariableEntry::ConditionVariableEntry()
108 ConditionVariableEntry::~ConditionVariableEntry()
110 if (fVariable
!= NULL
) {
111 panic("Destroying condition variable entry %p, but it's still "
112 "attached to variable %p\n", this, fVariable
);
120 ConditionVariable::NotifyOne(status_t result
)
122 _Notify(false, result
);
127 ConditionVariable::NotifyAll(status_t result
)
129 _Notify(true, result
);
134 #endif // __cplusplus
136 extern void condition_variable_init();
142 #endif /* _KERNEL_CONDITION_VARIABLE_H */