2007-08-10 00:03:17 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _KERNEL_CONDITION_VARIABLE_H
|
|
|
|
#define _KERNEL_CONDITION_VARIABLE_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <OS.h>
|
|
|
|
|
2007-09-03 02:21:26 +04:00
|
|
|
#include <debug.h>
|
|
|
|
|
2007-08-10 00:03:17 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
class PrivateConditionVariable;
|
|
|
|
|
2007-08-27 03:53:12 +04:00
|
|
|
|
|
|
|
struct PrivateConditionVariableEntry {
|
|
|
|
public:
|
2007-09-03 02:21:26 +04:00
|
|
|
#if KDEBUG
|
|
|
|
inline PrivateConditionVariableEntry()
|
|
|
|
: fVariable(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ~PrivateConditionVariableEntry()
|
|
|
|
{
|
|
|
|
if (fVariable != NULL) {
|
|
|
|
panic("Destroying condition variable entry %p, but it's still "
|
|
|
|
"attached to variable %p\n", this, fVariable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-27 03:53:12 +04:00
|
|
|
inline PrivateConditionVariable* Variable() const
|
|
|
|
{ return fVariable; }
|
|
|
|
|
|
|
|
inline PrivateConditionVariableEntry* ThreadNext() const
|
|
|
|
{ return fThreadNext; }
|
2007-09-03 02:21:26 +04:00
|
|
|
inline PrivateConditionVariableEntry* ThreadPrevious() const
|
|
|
|
{ return fThreadPrevious; }
|
2007-08-27 03:53:12 +04:00
|
|
|
|
|
|
|
class Private;
|
|
|
|
|
2007-08-10 00:03:17 +04:00
|
|
|
protected:
|
2007-08-27 03:53:12 +04:00
|
|
|
bool Add(const void* object,
|
|
|
|
PrivateConditionVariableEntry* threadNext);
|
2007-08-28 00:17:31 +04:00
|
|
|
status_t Wait(uint32 flags);
|
|
|
|
status_t Wait(const void* object, uint32 flags);
|
2007-08-27 03:53:12 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void _Remove();
|
2007-08-10 00:03:17 +04:00
|
|
|
|
|
|
|
protected:
|
2007-08-27 03:53:12 +04:00
|
|
|
PrivateConditionVariableEntry* fVariableNext;
|
2007-08-10 00:03:17 +04:00
|
|
|
PrivateConditionVariable* fVariable;
|
|
|
|
struct thread* fThread;
|
2007-08-27 03:53:12 +04:00
|
|
|
uint32 fFlags;
|
2007-08-28 00:17:31 +04:00
|
|
|
status_t fResult;
|
2007-08-27 03:53:12 +04:00
|
|
|
|
|
|
|
PrivateConditionVariableEntry* fThreadPrevious;
|
|
|
|
PrivateConditionVariableEntry* fThreadNext;
|
2007-08-10 00:03:17 +04:00
|
|
|
|
|
|
|
friend class PrivateConditionVariable;
|
2007-08-27 03:53:12 +04:00
|
|
|
friend class Private;
|
2007-08-10 00:03:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-08-27 03:53:12 +04:00
|
|
|
class PrivateConditionVariable
|
2007-08-10 00:03:17 +04:00
|
|
|
: protected HashTableLink<PrivateConditionVariable> {
|
|
|
|
public:
|
|
|
|
static void ListAll();
|
|
|
|
void Dump();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void Publish(const void* object,
|
|
|
|
const char* objectType);
|
2007-08-28 06:25:14 +04:00
|
|
|
void Unpublish(bool threadsLocked);
|
|
|
|
void Notify(bool all, bool threadsLocked);
|
2007-08-10 00:03:17 +04:00
|
|
|
|
|
|
|
private:
|
2007-10-02 02:24:42 +04:00
|
|
|
void _Notify(bool all, status_t result);
|
2007-08-10 00:03:17 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const void* fObject;
|
|
|
|
const char* fObjectType;
|
|
|
|
PrivateConditionVariableEntry* fEntries;
|
|
|
|
|
|
|
|
friend class PrivateConditionVariableEntry;
|
|
|
|
friend class ConditionVariableHashDefinition;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-09-29 01:40:25 +04:00
|
|
|
template<typename Type = void>
|
2007-08-10 00:03:17 +04:00
|
|
|
class ConditionVariable : private PrivateConditionVariable {
|
|
|
|
public:
|
|
|
|
inline void Publish(const Type* object,
|
|
|
|
const char* objectType);
|
|
|
|
|
2007-08-28 06:25:14 +04:00
|
|
|
inline void Unpublish(bool threadsLocked = false);
|
|
|
|
inline void NotifyOne(bool threadsLocked = false);
|
|
|
|
inline void NotifyAll(bool threadsLocked = false);
|
2007-08-10 00:03:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-09-29 01:40:25 +04:00
|
|
|
template<typename Type = void>
|
2007-08-27 03:53:12 +04:00
|
|
|
class ConditionVariableEntry : public PrivateConditionVariableEntry {
|
2007-08-10 00:03:17 +04:00
|
|
|
public:
|
2007-08-27 03:53:12 +04:00
|
|
|
inline bool Add(const Type* object,
|
|
|
|
PrivateConditionVariableEntry* threadNext
|
|
|
|
= NULL);
|
2007-08-28 00:17:31 +04:00
|
|
|
inline status_t Wait(uint32 flags = 0);
|
|
|
|
inline status_t Wait(const Type* object, uint32 flags = 0);
|
2007-08-10 00:03:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline void
|
|
|
|
ConditionVariable<Type>::Publish(const Type* object, const char* objectType)
|
|
|
|
{
|
|
|
|
PrivateConditionVariable::Publish(object, objectType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline void
|
2007-08-28 06:25:14 +04:00
|
|
|
ConditionVariable<Type>::Unpublish(bool threadsLocked)
|
2007-08-10 00:03:17 +04:00
|
|
|
{
|
2007-08-28 06:25:14 +04:00
|
|
|
PrivateConditionVariable::Unpublish(threadsLocked);
|
2007-08-10 00:03:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline void
|
2007-08-28 06:25:14 +04:00
|
|
|
ConditionVariable<Type>::NotifyOne(bool threadsLocked)
|
2007-08-10 00:03:17 +04:00
|
|
|
{
|
2007-08-28 06:25:14 +04:00
|
|
|
PrivateConditionVariable::Notify(false, threadsLocked);
|
2007-08-10 00:03:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
2007-08-27 03:53:12 +04:00
|
|
|
inline void
|
2007-08-28 06:25:14 +04:00
|
|
|
ConditionVariable<Type>::NotifyAll(bool threadsLocked)
|
2007-08-10 00:03:17 +04:00
|
|
|
{
|
2007-08-28 06:25:14 +04:00
|
|
|
PrivateConditionVariable::Notify(true, threadsLocked);
|
2007-08-10 00:03:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
2007-08-27 03:53:12 +04:00
|
|
|
inline bool
|
|
|
|
ConditionVariableEntry<Type>::Add(const Type* object,
|
|
|
|
PrivateConditionVariableEntry* threadNext)
|
2007-08-10 00:03:17 +04:00
|
|
|
{
|
2007-08-27 03:53:12 +04:00
|
|
|
return PrivateConditionVariableEntry::Add(object, threadNext);
|
2007-08-10 00:03:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
2007-08-28 00:17:31 +04:00
|
|
|
inline status_t
|
2007-08-27 03:53:12 +04:00
|
|
|
ConditionVariableEntry<Type>::Wait(uint32 flags)
|
2007-08-10 00:03:17 +04:00
|
|
|
{
|
2007-08-28 00:17:31 +04:00
|
|
|
return PrivateConditionVariableEntry::Wait(flags);
|
2007-08-10 00:03:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-27 03:53:12 +04:00
|
|
|
template<typename Type>
|
2007-08-28 00:17:31 +04:00
|
|
|
inline status_t
|
2007-08-27 03:53:12 +04:00
|
|
|
ConditionVariableEntry<Type>::Wait(const Type* object, uint32 flags)
|
|
|
|
{
|
2007-08-28 00:17:31 +04:00
|
|
|
return PrivateConditionVariableEntry::Wait(object, flags);
|
2007-08-27 03:53:12 +04:00
|
|
|
}
|
2007-08-10 00:03:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
2007-08-27 03:53:12 +04:00
|
|
|
struct thread;
|
|
|
|
|
|
|
|
extern status_t condition_variable_interrupt_thread(struct thread* thread);
|
|
|
|
|
2007-08-10 00:03:17 +04:00
|
|
|
extern void condition_variable_init();
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _KERNEL_CONDITION_VARIABLE_H */
|