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 mutex; 21 struct recursive_lock; 22 struct ConditionVariable; 23 24 25 struct ConditionVariableEntry 26 : DoublyLinkedListLinkImpl<ConditionVariableEntry> { 27 public: 28 ConditionVariableEntry(); 29 ~ConditionVariableEntry(); 30 31 bool Add(const void* object); 32 status_t Wait(uint32 flags = 0, bigtime_t timeout = 0); 33 status_t Wait(const void* object, uint32 flags = 0, 34 bigtime_t timeout = 0); 35 36 ConditionVariable* Variable() const; 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 67 void Add(ConditionVariableEntry* entry); 68 69 // Convenience methods, no ConditionVariableEntry required. 70 status_t Wait(uint32 flags = 0, bigtime_t timeout = 0); 71 status_t Wait(mutex* lock, uint32 flags = 0, bigtime_t timeout = 0); 72 status_t Wait(recursive_lock* lock, uint32 flags = 0, bigtime_t timeout = 0); 73 74 const void* Object() const { return fObject; } 75 const char* ObjectType() const { return fObjectType; } 76 77 static void ListAll(); 78 void Dump() const; 79 80 private: 81 static void _Notify(const void* object, bool all, status_t result); 82 void _Notify(bool all, status_t result); 83 void _NotifyLocked(bool all, status_t result); 84 85 protected: 86 typedef DoublyLinkedList<ConditionVariableEntry> EntryList; 87 88 const void* fObject; 89 const char* fObjectType; 90 91 spinlock fLock; 92 EntryList fEntries; 93 int32 fEntriesCount; 94 95 ConditionVariable* fNext; 96 97 friend struct ConditionVariableEntry; 98 friend struct ConditionVariableHashDefinition; 99 }; 100 101 102 inline void 103 ConditionVariable::NotifyOne(status_t result) 104 { 105 _Notify(false, result); 106 } 107 108 109 inline void 110 ConditionVariable::NotifyAll(status_t result) 111 { 112 _Notify(true, result); 113 } 114 115 116 extern "C" { 117 #endif // __cplusplus 118 119 extern void condition_variable_init(); 120 121 #ifdef __cplusplus 122 } // extern "C" 123 #endif 124 125 #endif /* _KERNEL_CONDITION_VARIABLE_H */ 126