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