xref: /haiku/headers/private/kernel/condition_variable.h (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2  * Copyright 2007-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2019, Haiku, Inc. All rights reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _KERNEL_CONDITION_VARIABLE_H
7 #define _KERNEL_CONDITION_VARIABLE_H
8 
9 
10 #include <OS.h>
11 
12 #include <debug.h>
13 
14 #ifdef __cplusplus
15 
16 #include <util/DoublyLinkedList.h>
17 #include <util/OpenHashTable.h>
18 
19 
20 struct ConditionVariable;
21 
22 
23 struct ConditionVariableEntry
24 	: DoublyLinkedListLinkImpl<ConditionVariableEntry> {
25 public:
26 								ConditionVariableEntry();
27 								~ConditionVariableEntry();
28 
29 			bool				Add(const void* object);
30 			status_t			Wait(uint32 flags = 0, bigtime_t timeout = 0);
31 			status_t			Wait(const void* object, uint32 flags = 0,
32 									bigtime_t timeout = 0);
33 
34 	inline	status_t			WaitStatus() const { return fWaitStatus; }
35 
36 	inline	ConditionVariable*	Variable() const { return fVariable; }
37 
38 private:
39 	inline	void				_AddToLockedVariable(ConditionVariable* variable);
40 			void				_RemoveFromVariable();
41 
42 private:
43 			ConditionVariable*	fVariable;
44 			Thread*				fThread;
45 			status_t			fWaitStatus;
46 
47 			friend struct ConditionVariable;
48 };
49 
50 
51 struct ConditionVariable {
52 public:
53 			void				Init(const void* object,
54 									const char* objectType);
55 									// for anonymous (unpublished) cvars
56 
57 			void				Publish(const void* object,
58 									const char* objectType);
59 			void				Unpublish();
60 
61 	inline	void				NotifyOne(status_t result = B_OK);
62 	inline	void				NotifyAll(status_t result = B_OK);
63 
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
69 
70 			void				Add(ConditionVariableEntry* entry);
71 
72 			status_t			Wait(uint32 flags = 0, bigtime_t timeout = 0);
73 									// all-in one, i.e. doesn't need a
74 									// ConditionVariableEntry
75 
76 			const void*			Object() const		{ return fObject; }
77 			const char*			ObjectType() const	{ return fObjectType; }
78 
79 	static	void				ListAll();
80 			void				Dump() const;
81 
82 private:
83 			void				_Notify(bool all, status_t result);
84 			void				_NotifyLocked(bool all, status_t result);
85 
86 protected:
87 			typedef DoublyLinkedList<ConditionVariableEntry> EntryList;
88 
89 			const void*			fObject;
90 			const char*			fObjectType;
91 
92 			spinlock			fLock;
93 			EntryList			fEntries;
94 			int32				fEntriesCount;
95 
96 			ConditionVariable*	fNext;
97 
98 			friend struct ConditionVariableEntry;
99 			friend struct ConditionVariableHashDefinition;
100 };
101 
102 
103 inline void
104 ConditionVariable::NotifyOne(status_t result)
105 {
106 	_Notify(false, result);
107 }
108 
109 
110 inline void
111 ConditionVariable::NotifyAll(status_t result)
112 {
113 	_Notify(true, result);
114 }
115 
116 
117 extern "C" {
118 #endif	// __cplusplus
119 
120 extern void condition_variable_init();
121 
122 #ifdef __cplusplus
123 }	// extern "C"
124 #endif
125 
126 #endif	/* _KERNEL_CONDITION_VARIABLE_H */
127