btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / kernel / condition_variable.h
blob4e91baec22bfb40f3a855f0dcda3f2e261a59986
1 /*
2 * Copyright 2007-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _KERNEL_CONDITION_VARIABLE_H
6 #define _KERNEL_CONDITION_VARIABLE_H
9 #include <OS.h>
11 #include <debug.h>
13 #ifdef __cplusplus
15 #include <util/DoublyLinkedList.h>
16 #include <util/OpenHashTable.h>
19 struct ConditionVariable;
22 struct ConditionVariableEntry
23 : DoublyLinkedListLinkImpl<ConditionVariableEntry> {
24 public:
25 #if KDEBUG
26 inline ConditionVariableEntry();
27 inline ~ConditionVariableEntry();
28 #endif
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; }
39 private:
40 inline void AddToVariable(ConditionVariable* variable);
42 private:
43 ConditionVariable* fVariable;
44 Thread* fThread;
45 status_t fWaitStatus;
47 friend struct ConditionVariable;
51 struct ConditionVariable {
52 public:
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);
59 void Unpublish();
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
68 // concurrently
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();
80 void Dump() const;
82 private:
83 void _Notify(bool all, status_t result);
84 void _NotifyLocked(bool all, status_t result);
86 protected:
87 typedef DoublyLinkedList<ConditionVariableEntry> EntryList;
89 const void* fObject;
90 const char* fObjectType;
91 EntryList fEntries;
92 ConditionVariable* fNext;
94 friend struct ConditionVariableEntry;
95 friend struct ConditionVariableHashDefinition;
99 #if KDEBUG
101 inline
102 ConditionVariableEntry::ConditionVariableEntry()
103 : fVariable(NULL)
107 inline
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);
116 #endif
119 inline void
120 ConditionVariable::NotifyOne(status_t result)
122 _Notify(false, result);
126 inline void
127 ConditionVariable::NotifyAll(status_t result)
129 _Notify(true, result);
133 extern "C" {
134 #endif // __cplusplus
136 extern void condition_variable_init();
138 #ifdef __cplusplus
139 } // extern "C"
140 #endif
142 #endif /* _KERNEL_CONDITION_VARIABLE_H */