Files
haiku-beta6/headers/private/kernel/condition_variable.h
T

143 lines
3.0 KiB
C++
Raw Normal View History

/*
* Copyright 2007-2011, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef _KERNEL_CONDITION_VARIABLE_H
#define _KERNEL_CONDITION_VARIABLE_H
#include <OS.h>
#include <debug.h>
#ifdef __cplusplus
#include <util/DoublyLinkedList.h>
#include <util/OpenHashTable.h>
struct ConditionVariable;
2007-08-26 23:53:12 +00:00
struct ConditionVariableEntry
: DoublyLinkedListLinkImpl<ConditionVariableEntry> {
2007-08-26 23:53:12 +00:00
public:
#if KDEBUG
inline ConditionVariableEntry();
inline ~ConditionVariableEntry();
#endif
2008-05-17 10:21:37 +00:00
bool Add(const void* object);
status_t Wait(uint32 flags = 0, bigtime_t timeout = 0);
2008-04-22 21:46:23 +00:00
status_t Wait(const void* object, uint32 flags = 0,
bigtime_t timeout = 0);
2007-08-26 23:53:12 +00:00
inline status_t WaitStatus() const { return fWaitStatus; }
inline ConditionVariable* Variable() const { return fVariable; }
2008-04-22 21:46:23 +00:00
private:
2008-07-16 22:43:50 +00:00
inline void AddToVariable(ConditionVariable* variable);
2008-04-22 21:46:23 +00:00
private:
ConditionVariable* fVariable;
Thread* fThread;
2008-05-17 10:21:37 +00:00
status_t fWaitStatus;
2007-08-26 23:53:12 +00:00
friend struct ConditionVariable;
};
struct ConditionVariable {
public:
2008-04-22 21:46:23 +00:00
void Init(const void* object,
const char* objectType);
// for anonymous (unpublished) cvars
void Publish(const void* object,
const char* objectType);
2013-11-08 02:41:26 +01:00
void Unpublish();
inline void NotifyOne(status_t result = B_OK);
inline void NotifyAll(status_t result = B_OK);
static void NotifyOne(const void* object, status_t result);
static void NotifyAll(const void* object, status_t result);
2009-12-03 12:47:29 +00:00
// (both methods) caller must ensure that
// the variable is not unpublished
// concurrently
2008-07-16 22:43:50 +00:00
void Add(ConditionVariableEntry* entry);
2008-04-22 21:46:23 +00:00
status_t Wait(uint32 flags = 0, bigtime_t timeout = 0);
// all-in one, i.e. doesn't need a
// ConditionVariableEntry
const void* Object() const { return fObject; }
const char* ObjectType() const { return fObjectType; }
static void ListAll();
void Dump() const;
private:
2013-11-08 02:41:26 +01:00
void _Notify(bool all, status_t result);
void _NotifyLocked(bool all, status_t result);
protected:
typedef DoublyLinkedList<ConditionVariableEntry> EntryList;
const void* fObject;
const char* fObjectType;
EntryList fEntries;
ConditionVariable* fNext;
friend struct ConditionVariableEntry;
friend struct ConditionVariableHashDefinition;
};
#if KDEBUG
inline
ConditionVariableEntry::ConditionVariableEntry()
: fVariable(NULL)
{
}
inline
ConditionVariableEntry::~ConditionVariableEntry()
{
if (fVariable != NULL) {
panic("Destroying condition variable entry %p, but it's still "
"attached to variable %p\n", this, fVariable);
}
}
#endif
2007-08-26 23:53:12 +00:00
inline void
2013-11-08 02:41:26 +01:00
ConditionVariable::NotifyOne(status_t result)
{
2013-11-08 02:41:26 +01:00
_Notify(false, result);
}
inline void
2013-11-08 02:41:26 +01:00
ConditionVariable::NotifyAll(status_t result)
2007-08-26 23:53:12 +00:00
{
2013-11-08 02:41:26 +01:00
_Notify(true, result);
2007-08-26 23:53:12 +00:00
}
extern "C" {
#endif // __cplusplus
extern void condition_variable_init();
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* _KERNEL_CONDITION_VARIABLE_H */