haiku/src/system/kernel/condition_variable.cpp

383 lines
8.2 KiB
C++
Raw Normal View History

/*
* Copyright 2007-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <condition_variable.h>
#include <new>
#include <stdlib.h>
#include <string.h>
#include <debug.h>
#include <kscheduler.h>
#include <ksignal.h>
#include <int.h>
* Scheduler/wait object listener: - Moved scheduler listening interface to <listeners.h> and added more convenient to use templatized notification functions. - Added a listener mechanism for the wait objects (semaphores, condition variables, mutex, rw_lock). * system profiler: - Hopefully fixed locking issues related to notifying the profiler thread for good. We still had an inconsistent locking order, since the scheduler notification callbacks are invoked with the thread lock held and have to acquire the object lock then, while the other callbacks acquired the object lock first and as a side effect of ConditionVariable::NotifyOne() acquired the thread lock. Now we make sure the object lock is the innermost lock. - Track the number of dropped events due to a full buffer. _user_system_profiler_next_buffer() returns this count now. - When scheduling profiling events are requested also listen to wait objects and generate the respective profiling events. We send those events lazily and cache the infos to avoid resending an event for the same wait object. - When starting profiling we do now generate "thread scheduled" events for the already running threads. - _user_system_profiler_start(): Check whether the parameters pointer is a userland address at all. - The system_profiler_team_added event does now also contain the team's name. * Added a sem_get_name_unsafe() returning a semaphore's name. It is "unsafe", since the caller has to ensure that the semaphore exists and continues to exist as long as the returned name is used. * Adjusted the "profile" and "scheduling_recorder" according to the system profiling changes. The latter prints the number of dropped events, now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30345 a95241bf-73f2-0310-859d-f6bbb57e9c96
2009-04-23 17:47:52 +04:00
#include <listeners.h>
#include <scheduling_analysis.h>
#include <thread.h>
#include <util/AutoLock.h>
#define STATUS_ADDED 1
#define STATUS_WAITING 2
static const int kConditionVariableHashSize = 512;
struct ConditionVariableHashDefinition {
typedef const void* KeyType;
typedef ConditionVariable ValueType;
size_t HashKey(const void* key) const
{ return (size_t)key; }
size_t Hash(ConditionVariable* variable) const
{ return (size_t)variable->fObject; }
bool Compare(const void* key, ConditionVariable* variable) const
{ return key == variable->fObject; }
ConditionVariable*& GetLink(ConditionVariable* variable) const
{ return variable->fNext; }
};
typedef BOpenHashTable<ConditionVariableHashDefinition> ConditionVariableHash;
static ConditionVariableHash sConditionVariableHash;
static spinlock sConditionVariablesLock;
static int
list_condition_variables(int argc, char** argv)
{
ConditionVariable::ListAll();
return 0;
}
static int
dump_condition_variable(int argc, char** argv)
{
if (argc != 2) {
print_debugger_command_usage(argv[0]);
return 0;
}
addr_t address = parse_expression(argv[1]);
if (address == 0)
return 0;
ConditionVariable* variable = sConditionVariableHash.Lookup((void*)address);
if (variable == NULL) {
// It must be a direct pointer to a condition variable.
variable = (ConditionVariable*)address;
}
if (variable != NULL) {
variable->Dump();
set_debug_variable("_cvar", (addr_t)variable);
set_debug_variable("_object", (addr_t)variable->Object());
} else
kprintf("no condition variable at or with key %p\n", (void*)address);
return 0;
}
// #pragma mark - ConditionVariableEntry
bool
ConditionVariableEntry::Add(const void* object)
{
ASSERT(object != NULL);
fThread = thread_get_current_thread();
InterruptsSpinLocker _(sConditionVariablesLock);
fVariable = sConditionVariableHash.Lookup(object);
* Introduced a set of functions (thread_prepare_to_block(), thread_block(), thread_unblock(),...) that allow a thread to wait for something without needing a semaphore or condition variable. It can simply block and another thread can unblock it. Supports timeouts and interrupting. Both semaphores and condition variables use this common mechanism, now. * Semaphores: - Some simplifications due to the thread blocking mechanism. - Changed locking order to sem -> thread. It was the other way around before and when introducing the wait_for_objects() support I had also introduced a situation where the locking was reverse, which could potentially cause a dead lock on SMP systems. - Instead of queueing thread structures, a semaphore queues queued_thread entries now, which are created on the stack. The thread::sem structure could thus be removed. - Added sem_entry::net_count, which is sem_entry::count plus the acquisition count of all waiting threads. This number is needed in remove_thread_from_sem() and instead of computing it there we maintain it. - Fixed remove_thread_from_sem(). It would not unblock threads, if the sem count was <= 0. - Made sem::last_acquirer unconditional. It is actually needed for sem_info::latest_holder. Fixed fill_sem_info() accordingly. - Added some optional tracing output, though only via ktrace_printf(). * Condition variables: - Could be simplified significantly through the use of the thread blocking mechanism. Removed a good deal of unnecessary code. - Moved the ConditionVariableEntry "flags" parameter from Wait() to Add(), and adjusted all places where condition variables are used accordingly. * snooze() uses thread_block_with_timeout() instead of a semaphore. * Simplified thread interrupting in the signal and user debugger code. Instead of separate functions for threads waiting on a semaphore or condititon variable, we only have a single thread_interrupt(), now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25099 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-04-22 20:22:42 +04:00
if (fVariable == NULL) {
fWaitStatus = B_ENTRY_NOT_FOUND;
* Introduced a set of functions (thread_prepare_to_block(), thread_block(), thread_unblock(),...) that allow a thread to wait for something without needing a semaphore or condition variable. It can simply block and another thread can unblock it. Supports timeouts and interrupting. Both semaphores and condition variables use this common mechanism, now. * Semaphores: - Some simplifications due to the thread blocking mechanism. - Changed locking order to sem -> thread. It was the other way around before and when introducing the wait_for_objects() support I had also introduced a situation where the locking was reverse, which could potentially cause a dead lock on SMP systems. - Instead of queueing thread structures, a semaphore queues queued_thread entries now, which are created on the stack. The thread::sem structure could thus be removed. - Added sem_entry::net_count, which is sem_entry::count plus the acquisition count of all waiting threads. This number is needed in remove_thread_from_sem() and instead of computing it there we maintain it. - Fixed remove_thread_from_sem(). It would not unblock threads, if the sem count was <= 0. - Made sem::last_acquirer unconditional. It is actually needed for sem_info::latest_holder. Fixed fill_sem_info() accordingly. - Added some optional tracing output, though only via ktrace_printf(). * Condition variables: - Could be simplified significantly through the use of the thread blocking mechanism. Removed a good deal of unnecessary code. - Moved the ConditionVariableEntry "flags" parameter from Wait() to Add(), and adjusted all places where condition variables are used accordingly. * snooze() uses thread_block_with_timeout() instead of a semaphore. * Simplified thread interrupting in the signal and user debugger code. Instead of separate functions for threads waiting on a semaphore or condititon variable, we only have a single thread_interrupt(), now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25099 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-04-22 20:22:42 +04:00
return false;
}
fWaitStatus = STATUS_ADDED;
* Introduced a set of functions (thread_prepare_to_block(), thread_block(), thread_unblock(),...) that allow a thread to wait for something without needing a semaphore or condition variable. It can simply block and another thread can unblock it. Supports timeouts and interrupting. Both semaphores and condition variables use this common mechanism, now. * Semaphores: - Some simplifications due to the thread blocking mechanism. - Changed locking order to sem -> thread. It was the other way around before and when introducing the wait_for_objects() support I had also introduced a situation where the locking was reverse, which could potentially cause a dead lock on SMP systems. - Instead of queueing thread structures, a semaphore queues queued_thread entries now, which are created on the stack. The thread::sem structure could thus be removed. - Added sem_entry::net_count, which is sem_entry::count plus the acquisition count of all waiting threads. This number is needed in remove_thread_from_sem() and instead of computing it there we maintain it. - Fixed remove_thread_from_sem(). It would not unblock threads, if the sem count was <= 0. - Made sem::last_acquirer unconditional. It is actually needed for sem_info::latest_holder. Fixed fill_sem_info() accordingly. - Added some optional tracing output, though only via ktrace_printf(). * Condition variables: - Could be simplified significantly through the use of the thread blocking mechanism. Removed a good deal of unnecessary code. - Moved the ConditionVariableEntry "flags" parameter from Wait() to Add(), and adjusted all places where condition variables are used accordingly. * snooze() uses thread_block_with_timeout() instead of a semaphore. * Simplified thread interrupting in the signal and user debugger code. Instead of separate functions for threads waiting on a semaphore or condititon variable, we only have a single thread_interrupt(), now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25099 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-04-22 20:22:42 +04:00
fVariable->fEntries.Add(this);
return true;
}
status_t
ConditionVariableEntry::Wait(uint32 flags, bigtime_t timeout)
{
if (!are_interrupts_enabled()) {
panic("ConditionVariableEntry::Wait() called with interrupts "
"disabled, entry: %p, variable: %p", this, fVariable);
return B_ERROR;
}
InterruptsLocker _;
* Introduced a set of functions (thread_prepare_to_block(), thread_block(), thread_unblock(),...) that allow a thread to wait for something without needing a semaphore or condition variable. It can simply block and another thread can unblock it. Supports timeouts and interrupting. Both semaphores and condition variables use this common mechanism, now. * Semaphores: - Some simplifications due to the thread blocking mechanism. - Changed locking order to sem -> thread. It was the other way around before and when introducing the wait_for_objects() support I had also introduced a situation where the locking was reverse, which could potentially cause a dead lock on SMP systems. - Instead of queueing thread structures, a semaphore queues queued_thread entries now, which are created on the stack. The thread::sem structure could thus be removed. - Added sem_entry::net_count, which is sem_entry::count plus the acquisition count of all waiting threads. This number is needed in remove_thread_from_sem() and instead of computing it there we maintain it. - Fixed remove_thread_from_sem(). It would not unblock threads, if the sem count was <= 0. - Made sem::last_acquirer unconditional. It is actually needed for sem_info::latest_holder. Fixed fill_sem_info() accordingly. - Added some optional tracing output, though only via ktrace_printf(). * Condition variables: - Could be simplified significantly through the use of the thread blocking mechanism. Removed a good deal of unnecessary code. - Moved the ConditionVariableEntry "flags" parameter from Wait() to Add(), and adjusted all places where condition variables are used accordingly. * snooze() uses thread_block_with_timeout() instead of a semaphore. * Simplified thread interrupting in the signal and user debugger code. Instead of separate functions for threads waiting on a semaphore or condititon variable, we only have a single thread_interrupt(), now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25099 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-04-22 20:22:42 +04:00
SpinLocker conditionLocker(sConditionVariablesLock);
if (fVariable == NULL)
return fWaitStatus;
thread_prepare_to_block(fThread, flags,
THREAD_BLOCK_TYPE_CONDITION_VARIABLE, fVariable);
fWaitStatus = STATUS_WAITING;
conditionLocker.Unlock();
status_t error;
if ((flags & (B_RELATIVE_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0)
error = thread_block_with_timeout(flags, timeout);
else
error = thread_block();
conditionLocker.Lock();
* Introduced a set of functions (thread_prepare_to_block(), thread_block(), thread_unblock(),...) that allow a thread to wait for something without needing a semaphore or condition variable. It can simply block and another thread can unblock it. Supports timeouts and interrupting. Both semaphores and condition variables use this common mechanism, now. * Semaphores: - Some simplifications due to the thread blocking mechanism. - Changed locking order to sem -> thread. It was the other way around before and when introducing the wait_for_objects() support I had also introduced a situation where the locking was reverse, which could potentially cause a dead lock on SMP systems. - Instead of queueing thread structures, a semaphore queues queued_thread entries now, which are created on the stack. The thread::sem structure could thus be removed. - Added sem_entry::net_count, which is sem_entry::count plus the acquisition count of all waiting threads. This number is needed in remove_thread_from_sem() and instead of computing it there we maintain it. - Fixed remove_thread_from_sem(). It would not unblock threads, if the sem count was <= 0. - Made sem::last_acquirer unconditional. It is actually needed for sem_info::latest_holder. Fixed fill_sem_info() accordingly. - Added some optional tracing output, though only via ktrace_printf(). * Condition variables: - Could be simplified significantly through the use of the thread blocking mechanism. Removed a good deal of unnecessary code. - Moved the ConditionVariableEntry "flags" parameter from Wait() to Add(), and adjusted all places where condition variables are used accordingly. * snooze() uses thread_block_with_timeout() instead of a semaphore. * Simplified thread interrupting in the signal and user debugger code. Instead of separate functions for threads waiting on a semaphore or condititon variable, we only have a single thread_interrupt(), now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25099 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-04-22 20:22:42 +04:00
// remove entry from variable, if not done yet
if (fVariable != NULL) {
fVariable->fEntries.Remove(this);
fVariable = NULL;
}
* Introduced a set of functions (thread_prepare_to_block(), thread_block(), thread_unblock(),...) that allow a thread to wait for something without needing a semaphore or condition variable. It can simply block and another thread can unblock it. Supports timeouts and interrupting. Both semaphores and condition variables use this common mechanism, now. * Semaphores: - Some simplifications due to the thread blocking mechanism. - Changed locking order to sem -> thread. It was the other way around before and when introducing the wait_for_objects() support I had also introduced a situation where the locking was reverse, which could potentially cause a dead lock on SMP systems. - Instead of queueing thread structures, a semaphore queues queued_thread entries now, which are created on the stack. The thread::sem structure could thus be removed. - Added sem_entry::net_count, which is sem_entry::count plus the acquisition count of all waiting threads. This number is needed in remove_thread_from_sem() and instead of computing it there we maintain it. - Fixed remove_thread_from_sem(). It would not unblock threads, if the sem count was <= 0. - Made sem::last_acquirer unconditional. It is actually needed for sem_info::latest_holder. Fixed fill_sem_info() accordingly. - Added some optional tracing output, though only via ktrace_printf(). * Condition variables: - Could be simplified significantly through the use of the thread blocking mechanism. Removed a good deal of unnecessary code. - Moved the ConditionVariableEntry "flags" parameter from Wait() to Add(), and adjusted all places where condition variables are used accordingly. * snooze() uses thread_block_with_timeout() instead of a semaphore. * Simplified thread interrupting in the signal and user debugger code. Instead of separate functions for threads waiting on a semaphore or condititon variable, we only have a single thread_interrupt(), now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25099 a95241bf-73f2-0310-859d-f6bbb57e9c96
2008-04-22 20:22:42 +04:00
return error;
}
status_t
ConditionVariableEntry::Wait(const void* object, uint32 flags,
bigtime_t timeout)
{
if (Add(object))
return Wait(flags, timeout);
return B_ENTRY_NOT_FOUND;
}
inline void
ConditionVariableEntry::AddToVariable(ConditionVariable* variable)
{
fThread = thread_get_current_thread();
InterruptsSpinLocker _(sConditionVariablesLock);
fVariable = variable;
fWaitStatus = STATUS_ADDED;
fVariable->fEntries.Add(this);
}
// #pragma mark - ConditionVariable
/*! Initialization method for anonymous (unpublished) condition variables.
*/
void
ConditionVariable::Init(const void* object, const char* objectType)
{
fObject = object;
fObjectType = objectType;
new(&fEntries) EntryList;
T_SCHEDULING_ANALYSIS(InitConditionVariable(this, object, objectType));
* Scheduler/wait object listener: - Moved scheduler listening interface to <listeners.h> and added more convenient to use templatized notification functions. - Added a listener mechanism for the wait objects (semaphores, condition variables, mutex, rw_lock). * system profiler: - Hopefully fixed locking issues related to notifying the profiler thread for good. We still had an inconsistent locking order, since the scheduler notification callbacks are invoked with the thread lock held and have to acquire the object lock then, while the other callbacks acquired the object lock first and as a side effect of ConditionVariable::NotifyOne() acquired the thread lock. Now we make sure the object lock is the innermost lock. - Track the number of dropped events due to a full buffer. _user_system_profiler_next_buffer() returns this count now. - When scheduling profiling events are requested also listen to wait objects and generate the respective profiling events. We send those events lazily and cache the infos to avoid resending an event for the same wait object. - When starting profiling we do now generate "thread scheduled" events for the already running threads. - _user_system_profiler_start(): Check whether the parameters pointer is a userland address at all. - The system_profiler_team_added event does now also contain the team's name. * Added a sem_get_name_unsafe() returning a semaphore's name. It is "unsafe", since the caller has to ensure that the semaphore exists and continues to exist as long as the returned name is used. * Adjusted the "profile" and "scheduling_recorder" according to the system profiling changes. The latter prints the number of dropped events, now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30345 a95241bf-73f2-0310-859d-f6bbb57e9c96
2009-04-23 17:47:52 +04:00
NotifyWaitObjectListeners(&WaitObjectListener::ConditionVariableInitialized,
this);
}
void
ConditionVariable::Publish(const void* object, const char* objectType)
{
ASSERT(object != NULL);
fObject = object;
fObjectType = objectType;
new(&fEntries) EntryList;
T_SCHEDULING_ANALYSIS(InitConditionVariable(this, object, objectType));
* Scheduler/wait object listener: - Moved scheduler listening interface to <listeners.h> and added more convenient to use templatized notification functions. - Added a listener mechanism for the wait objects (semaphores, condition variables, mutex, rw_lock). * system profiler: - Hopefully fixed locking issues related to notifying the profiler thread for good. We still had an inconsistent locking order, since the scheduler notification callbacks are invoked with the thread lock held and have to acquire the object lock then, while the other callbacks acquired the object lock first and as a side effect of ConditionVariable::NotifyOne() acquired the thread lock. Now we make sure the object lock is the innermost lock. - Track the number of dropped events due to a full buffer. _user_system_profiler_next_buffer() returns this count now. - When scheduling profiling events are requested also listen to wait objects and generate the respective profiling events. We send those events lazily and cache the infos to avoid resending an event for the same wait object. - When starting profiling we do now generate "thread scheduled" events for the already running threads. - _user_system_profiler_start(): Check whether the parameters pointer is a userland address at all. - The system_profiler_team_added event does now also contain the team's name. * Added a sem_get_name_unsafe() returning a semaphore's name. It is "unsafe", since the caller has to ensure that the semaphore exists and continues to exist as long as the returned name is used. * Adjusted the "profile" and "scheduling_recorder" according to the system profiling changes. The latter prints the number of dropped events, now. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30345 a95241bf-73f2-0310-859d-f6bbb57e9c96
2009-04-23 17:47:52 +04:00
NotifyWaitObjectListeners(&WaitObjectListener::ConditionVariableInitialized,
this);
InterruptsLocker _;
SpinLocker locker(sConditionVariablesLock);
ASSERT_PRINT(sConditionVariableHash.Lookup(object) == NULL,
"condition variable: %p\n", sConditionVariableHash.Lookup(object));
sConditionVariableHash.InsertUnchecked(this);
}
void
ConditionVariable::Unpublish()
{
ASSERT(fObject != NULL);
InterruptsSpinLocker locker(sConditionVariablesLock);
#if KDEBUG
ConditionVariable* variable = sConditionVariableHash.Lookup(fObject);
if (variable != this) {
panic("Condition variable %p not published, found: %p", this, variable);
return;
}
#endif
sConditionVariableHash.RemoveUnchecked(this);
fObject = NULL;
fObjectType = NULL;
if (!fEntries.IsEmpty())
_NotifyLocked(true, B_ENTRY_NOT_FOUND);
}
void
ConditionVariable::Add(ConditionVariableEntry* entry)
{
entry->AddToVariable(this);
}
status_t
ConditionVariable::Wait(uint32 flags, bigtime_t timeout)
{
ConditionVariableEntry entry;
Add(&entry);
return entry.Wait(flags, timeout);
}
/*static*/ void
ConditionVariable::NotifyOne(const void* object, status_t result)
{
InterruptsSpinLocker locker(sConditionVariablesLock);
ConditionVariable* variable = sConditionVariableHash.Lookup(object);
locker.Unlock();
if (variable == NULL)
return;
variable->NotifyOne(result);
}
/*static*/ void
ConditionVariable::NotifyAll(const void* object, status_t result)
{
InterruptsSpinLocker locker(sConditionVariablesLock);
ConditionVariable* variable = sConditionVariableHash.Lookup(object);
locker.Unlock();
if (variable == NULL)
return;
variable->NotifyAll(result);
}
/*static*/ void
ConditionVariable::ListAll()
{
kprintf(" variable object (type) waiting threads\n");
kprintf("------------------------------------------------------------\n");
ConditionVariableHash::Iterator it(&sConditionVariableHash);
while (ConditionVariable* variable = it.Next()) {
// count waiting threads
int count = variable->fEntries.Count();
kprintf("%p %p %-20s %15d\n", variable, variable->fObject,
variable->fObjectType, count);
}
}
void
ConditionVariable::Dump() const
{
kprintf("condition variable %p\n", this);
kprintf(" object: %p (%s)\n", fObject, fObjectType);
kprintf(" threads:");
for (EntryList::ConstIterator it = fEntries.GetIterator();
ConditionVariableEntry* entry = it.Next();) {
kprintf(" %" B_PRId32, entry->fThread->id);
}
kprintf("\n");
}
void
ConditionVariable::_Notify(bool all, status_t result)
{
InterruptsSpinLocker locker(sConditionVariablesLock);
if (!fEntries.IsEmpty()) {
if (result > B_OK) {
panic("tried to notify with invalid result %" B_PRId32 "\n", result);
result = B_ERROR;
}
_NotifyLocked(all, result);
}
}
/*! Called with interrupts disabled and the condition variable spinlock and
scheduler lock held.
*/
void
ConditionVariable::_NotifyLocked(bool all, status_t result)
{
// dequeue and wake up the blocked threads
while (ConditionVariableEntry* entry = fEntries.RemoveHead()) {
entry->fVariable = NULL;
if (entry->fWaitStatus <= 0)
continue;
if (entry->fWaitStatus == STATUS_WAITING) {
SpinLocker _(entry->fThread->scheduler_lock);
thread_unblock_locked(entry->fThread, result);
}
entry->fWaitStatus = result;
if (!all)
break;
}
}
// #pragma mark -
void
condition_variable_init()
{
new(&sConditionVariableHash) ConditionVariableHash;
status_t error = sConditionVariableHash.Init(kConditionVariableHashSize);
if (error != B_OK) {
panic("condition_variable_init(): Failed to init hash table: %s",
strerror(error));
}
add_debugger_command_etc("cvar", &dump_condition_variable,
"Dump condition variable info",
"<address>\n"
"Prints info for the specified condition variable.\n"
" <address> - Address of the condition variable or the object it is\n"
" associated with.\n", 0);
add_debugger_command_etc("cvars", &list_condition_variables,
"List condition variables",
"\n"
"Lists all existing condition variables\n", 0);
}