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 7 #include <condition_variable.h> 8 9 #include <new> 10 #include <stdlib.h> 11 #include <string.h> 12 13 #include <debug.h> 14 #include <kscheduler.h> 15 #include <ksignal.h> 16 #include <int.h> 17 #include <listeners.h> 18 #include <scheduling_analysis.h> 19 #include <thread.h> 20 #include <util/AutoLock.h> 21 #include <util/atomic.h> 22 23 24 #define STATUS_ADDED 1 25 #define STATUS_WAITING 2 26 27 28 static const int kConditionVariableHashSize = 512; 29 30 31 struct ConditionVariableHashDefinition { 32 typedef const void* KeyType; 33 typedef ConditionVariable ValueType; 34 35 size_t HashKey(const void* key) const 36 { return (size_t)key; } 37 size_t Hash(ConditionVariable* variable) const 38 { return (size_t)variable->fObject; } 39 bool Compare(const void* key, ConditionVariable* variable) const 40 { return key == variable->fObject; } 41 ConditionVariable*& GetLink(ConditionVariable* variable) const 42 { return variable->fNext; } 43 }; 44 45 typedef BOpenHashTable<ConditionVariableHashDefinition> ConditionVariableHash; 46 static ConditionVariableHash sConditionVariableHash; 47 static rw_spinlock sConditionVariableHashLock; 48 49 50 // #pragma mark - ConditionVariableEntry 51 52 53 ConditionVariableEntry::ConditionVariableEntry() 54 : fVariable(NULL) 55 { 56 } 57 58 59 ConditionVariableEntry::~ConditionVariableEntry() 60 { 61 // We can use an "unsafe" non-atomic access of fVariable here, since we only 62 // care whether it is non-NULL, not what its specific value is. 63 if (fVariable != NULL) 64 _RemoveFromVariable(); 65 } 66 67 68 bool 69 ConditionVariableEntry::Add(const void* object) 70 { 71 ASSERT(object != NULL); 72 73 InterruptsLocker _; 74 ReadSpinLocker hashLocker(sConditionVariableHashLock); 75 76 ConditionVariable* variable = sConditionVariableHash.Lookup(object); 77 78 if (variable == NULL) { 79 fWaitStatus = B_ENTRY_NOT_FOUND; 80 return false; 81 } 82 83 SpinLocker variableLocker(variable->fLock); 84 hashLocker.Unlock(); 85 86 _AddToLockedVariable(variable); 87 88 return true; 89 } 90 91 92 ConditionVariable* 93 ConditionVariableEntry::Variable() const 94 { 95 return atomic_pointer_get(&fVariable); 96 } 97 98 99 inline void 100 ConditionVariableEntry::_AddToLockedVariable(ConditionVariable* variable) 101 { 102 ASSERT(fVariable == NULL); 103 104 fThread = thread_get_current_thread(); 105 fVariable = variable; 106 fWaitStatus = STATUS_ADDED; 107 fVariable->fEntries.Add(this); 108 atomic_add(&fVariable->fEntriesCount, 1); 109 } 110 111 112 void 113 ConditionVariableEntry::_RemoveFromVariable() 114 { 115 // This section is critical because it can race with _NotifyLocked on the 116 // variable's thread, so we must not be interrupted during it. 117 InterruptsLocker _; 118 119 ConditionVariable* variable = atomic_pointer_get(&fVariable); 120 if (atomic_pointer_get_and_set(&fThread, (Thread*)NULL) == NULL) { 121 // If fThread was already NULL, that means the variable is already 122 // in the process of clearing us out (or already has finished doing so.) 123 // We thus cannot access fVariable, and must spin until it is cleared. 124 int32 tries = 0; 125 while (atomic_pointer_get(&fVariable) != NULL) { 126 tries++; 127 if ((tries % 10000) == 0) 128 dprintf("variable pointer was not unset for a long time!\n"); 129 cpu_pause(); 130 } 131 132 return; 133 } 134 135 while (true) { 136 if (atomic_pointer_get(&fVariable) == NULL) { 137 // The variable must have cleared us out. Acknowledge this and return. 138 atomic_add(&variable->fEntriesCount, -1); 139 return; 140 } 141 142 // There is of course a small race between checking the pointer and then 143 // the try_acquire in which the variable might clear out our fVariable. 144 // However, in the case where we were the ones to clear fThread, the 145 // variable will notice that and then wait for us to acknowledge the 146 // removal by decrementing fEntriesCount, as we do above; and until 147 // we do that, we may validly use our cached pointer to the variable. 148 if (try_acquire_spinlock(&variable->fLock)) 149 break; 150 } 151 152 // We now hold the variable's lock. Remove ourselves. 153 if (fVariable->fEntries.Contains(this)) 154 fVariable->fEntries.Remove(this); 155 156 atomic_pointer_set(&fVariable, (ConditionVariable*)NULL); 157 atomic_add(&variable->fEntriesCount, -1); 158 release_spinlock(&variable->fLock); 159 } 160 161 162 status_t 163 ConditionVariableEntry::Wait(uint32 flags, bigtime_t timeout) 164 { 165 #if KDEBUG 166 if (!are_interrupts_enabled()) { 167 panic("ConditionVariableEntry::Wait() called with interrupts " 168 "disabled, entry: %p, variable: %p", this, fVariable); 169 return B_ERROR; 170 } 171 #endif 172 173 ConditionVariable* variable = atomic_pointer_get(&fVariable); 174 if (variable == NULL) 175 return fWaitStatus; 176 177 if ((flags & B_RELATIVE_TIMEOUT) != 0 && timeout <= 0) { 178 _RemoveFromVariable(); 179 return B_WOULD_BLOCK; 180 } 181 182 InterruptsLocker _; 183 SpinLocker schedulerLocker(thread_get_current_thread()->scheduler_lock); 184 185 if (fWaitStatus <= 0) 186 return fWaitStatus; 187 fWaitStatus = STATUS_WAITING; 188 189 thread_prepare_to_block(thread_get_current_thread(), flags, 190 THREAD_BLOCK_TYPE_CONDITION_VARIABLE, variable); 191 192 schedulerLocker.Unlock(); 193 194 status_t error; 195 if ((flags & (B_RELATIVE_TIMEOUT | B_ABSOLUTE_TIMEOUT)) != 0) 196 error = thread_block_with_timeout(flags, timeout); 197 else 198 error = thread_block(); 199 200 _RemoveFromVariable(); 201 return error; 202 } 203 204 205 status_t 206 ConditionVariableEntry::Wait(const void* object, uint32 flags, 207 bigtime_t timeout) 208 { 209 if (Add(object)) 210 return Wait(flags, timeout); 211 return B_ENTRY_NOT_FOUND; 212 } 213 214 215 // #pragma mark - ConditionVariable 216 217 218 /*! Initialization method for anonymous (unpublished) condition variables. 219 */ 220 void 221 ConditionVariable::Init(const void* object, const char* objectType) 222 { 223 fObject = object; 224 fObjectType = objectType; 225 new(&fEntries) EntryList; 226 fEntriesCount = 0; 227 B_INITIALIZE_SPINLOCK(&fLock); 228 229 T_SCHEDULING_ANALYSIS(InitConditionVariable(this, object, objectType)); 230 NotifyWaitObjectListeners(&WaitObjectListener::ConditionVariableInitialized, 231 this); 232 } 233 234 235 void 236 ConditionVariable::Publish(const void* object, const char* objectType) 237 { 238 ASSERT(object != NULL); 239 240 Init(object, objectType); 241 242 InterruptsWriteSpinLocker _(sConditionVariableHashLock); 243 244 ASSERT_PRINT(sConditionVariableHash.Lookup(object) == NULL, 245 "condition variable: %p\n", sConditionVariableHash.Lookup(object)); 246 247 sConditionVariableHash.InsertUnchecked(this); 248 } 249 250 251 void 252 ConditionVariable::Unpublish() 253 { 254 ASSERT(fObject != NULL); 255 256 InterruptsLocker _; 257 WriteSpinLocker hashLocker(sConditionVariableHashLock); 258 SpinLocker selfLocker(fLock); 259 260 #if KDEBUG 261 ConditionVariable* variable = sConditionVariableHash.Lookup(fObject); 262 if (variable != this) { 263 panic("Condition variable %p not published, found: %p", this, variable); 264 return; 265 } 266 #endif 267 268 sConditionVariableHash.RemoveUnchecked(this); 269 fObject = NULL; 270 fObjectType = NULL; 271 272 hashLocker.Unlock(); 273 274 if (!fEntries.IsEmpty()) 275 _NotifyLocked(true, B_ENTRY_NOT_FOUND); 276 } 277 278 279 void 280 ConditionVariable::Add(ConditionVariableEntry* entry) 281 { 282 InterruptsSpinLocker _(fLock); 283 entry->_AddToLockedVariable(this); 284 } 285 286 287 status_t 288 ConditionVariable::Wait(uint32 flags, bigtime_t timeout) 289 { 290 ConditionVariableEntry entry; 291 Add(&entry); 292 return entry.Wait(flags, timeout); 293 } 294 295 296 status_t 297 ConditionVariable::Wait(mutex* lock, uint32 flags, bigtime_t timeout) 298 { 299 ConditionVariableEntry entry; 300 Add(&entry); 301 mutex_unlock(lock); 302 status_t res = entry.Wait(flags, timeout); 303 mutex_lock(lock); 304 return res; 305 } 306 307 308 status_t 309 ConditionVariable::Wait(recursive_lock* lock, uint32 flags, bigtime_t timeout) 310 { 311 ConditionVariableEntry entry; 312 Add(&entry); 313 int32 recursion = recursive_lock_get_recursion(lock); 314 315 for (int32 i = 0; i < recursion; i++) 316 recursive_lock_unlock(lock); 317 318 status_t res = entry.Wait(flags, timeout); 319 320 for (int32 i = 0; i < recursion; i++) 321 recursive_lock_lock(lock); 322 323 return res; 324 } 325 326 327 /*static*/ int32 328 ConditionVariable::NotifyOne(const void* object, status_t result) 329 { 330 return _Notify(object, false, result); 331 } 332 333 334 /*static*/ int32 335 ConditionVariable::NotifyAll(const void* object, status_t result) 336 { 337 return _Notify(object, true, result); 338 } 339 340 341 /*static*/ int32 342 ConditionVariable::_Notify(const void* object, bool all, status_t result) 343 { 344 InterruptsLocker ints; 345 ReadSpinLocker hashLocker(sConditionVariableHashLock); 346 ConditionVariable* variable = sConditionVariableHash.Lookup(object); 347 if (variable == NULL) 348 return 0; 349 SpinLocker variableLocker(variable->fLock); 350 hashLocker.Unlock(); 351 352 return variable->_NotifyLocked(all, result); 353 } 354 355 356 int32 357 ConditionVariable::_Notify(bool all, status_t result) 358 { 359 InterruptsSpinLocker _(fLock); 360 if (!fEntries.IsEmpty()) { 361 if (result > B_OK) { 362 panic("tried to notify with invalid result %" B_PRId32 "\n", result); 363 result = B_ERROR; 364 } 365 366 return _NotifyLocked(all, result); 367 } 368 return 0; 369 } 370 371 372 /*! Called with interrupts disabled and the condition variable's spinlock held. 373 */ 374 int32 375 ConditionVariable::_NotifyLocked(bool all, status_t result) 376 { 377 int32 notified = 0; 378 379 // Dequeue and wake up the blocked threads. 380 while (ConditionVariableEntry* entry = fEntries.RemoveHead()) { 381 Thread* thread = atomic_pointer_get_and_set(&entry->fThread, (Thread*)NULL); 382 if (thread == NULL) { 383 // The entry must be in the process of trying to remove itself from us. 384 // Clear its variable and wait for it to acknowledge this in fEntriesCount, 385 // as it is the one responsible for decrementing that. 386 const int32 oldCount = atomic_get(&fEntriesCount); 387 atomic_pointer_set(&entry->fVariable, (ConditionVariable*)NULL); 388 389 // As fEntriesCount is only modified while our lock is held, nothing else 390 // will modify it while we are spinning, since we hold it at present. 391 int32 tries = 0; 392 while (atomic_get(&fEntriesCount) == oldCount) { 393 tries++; 394 if ((tries % 10000) == 0) 395 dprintf("entries count was not decremented for a long time!\n"); 396 cpu_pause(); 397 } 398 } else { 399 SpinLocker schedulerLocker(thread->scheduler_lock); 400 status_t lastWaitStatus = entry->fWaitStatus; 401 entry->fWaitStatus = result; 402 if (lastWaitStatus == STATUS_WAITING && thread->state != B_THREAD_WAITING) { 403 // The thread is not in B_THREAD_WAITING state, so we must unblock it early, 404 // in case it tries to re-block itself immediately after we unset fVariable. 405 thread_unblock_locked(thread, result); 406 lastWaitStatus = result; 407 } 408 409 // No matter what the thread is doing, as we were the ones to clear its 410 // fThread, so we are the ones responsible for decrementing fEntriesCount. 411 // (We may not validly access the entry once we unset its fVariable.) 412 atomic_pointer_set(&entry->fVariable, (ConditionVariable*)NULL); 413 atomic_add(&fEntriesCount, -1); 414 415 // If the thread was in B_THREAD_WAITING state, we unblock it after unsetting 416 // fVariable, because otherwise it will wake up before thread_unblock returns 417 // and spin while waiting for us to do so. 418 if (lastWaitStatus == STATUS_WAITING) 419 thread_unblock_locked(thread, result); 420 } 421 422 notified++; 423 if (!all) 424 break; 425 } 426 427 return notified; 428 } 429 430 431 // #pragma mark - 432 433 434 /*static*/ void 435 ConditionVariable::ListAll() 436 { 437 kprintf(" variable object (type) waiting threads\n"); 438 kprintf("------------------------------------------------------------\n"); 439 ConditionVariableHash::Iterator it(&sConditionVariableHash); 440 while (ConditionVariable* variable = it.Next()) { 441 // count waiting threads 442 int count = variable->fEntries.Count(); 443 444 kprintf("%p %p %-20s %15d\n", variable, variable->fObject, 445 variable->fObjectType, count); 446 } 447 } 448 449 450 void 451 ConditionVariable::Dump() const 452 { 453 kprintf("condition variable %p\n", this); 454 kprintf(" object: %p (%s)\n", fObject, fObjectType); 455 kprintf(" threads:"); 456 457 for (EntryList::ConstIterator it = fEntries.GetIterator(); 458 ConditionVariableEntry* entry = it.Next();) { 459 kprintf(" %" B_PRId32, entry->fThread->id); 460 } 461 kprintf("\n"); 462 } 463 464 465 static int 466 list_condition_variables(int argc, char** argv) 467 { 468 ConditionVariable::ListAll(); 469 return 0; 470 } 471 472 473 static int 474 dump_condition_variable(int argc, char** argv) 475 { 476 if (argc != 2) { 477 print_debugger_command_usage(argv[0]); 478 return 0; 479 } 480 481 addr_t address = parse_expression(argv[1]); 482 if (address == 0) 483 return 0; 484 485 ConditionVariable* variable = sConditionVariableHash.Lookup((void*)address); 486 487 if (variable == NULL) { 488 // It must be a direct pointer to a condition variable. 489 variable = (ConditionVariable*)address; 490 } 491 492 if (variable != NULL) { 493 variable->Dump(); 494 495 set_debug_variable("_cvar", (addr_t)variable); 496 set_debug_variable("_object", (addr_t)variable->Object()); 497 498 } else 499 kprintf("no condition variable at or with key %p\n", (void*)address); 500 501 return 0; 502 } 503 504 505 // #pragma mark - 506 507 508 void 509 condition_variable_init() 510 { 511 new(&sConditionVariableHash) ConditionVariableHash; 512 513 status_t error = sConditionVariableHash.Init(kConditionVariableHashSize); 514 if (error != B_OK) { 515 panic("condition_variable_init(): Failed to init hash table: %s", 516 strerror(error)); 517 } 518 519 add_debugger_command_etc("cvar", &dump_condition_variable, 520 "Dump condition variable info", 521 "<address>\n" 522 "Prints info for the specified condition variable.\n" 523 " <address> - Address of the condition variable or the object it is\n" 524 " associated with.\n", 0); 525 add_debugger_command_etc("cvars", &list_condition_variables, 526 "List condition variables", 527 "\n" 528 "Lists all published condition variables\n", 0); 529 } 530 531 532 ssize_t 533 debug_condition_variable_type_strlcpy(ConditionVariable* cvar, char* name, size_t size) 534 { 535 const int32 typePointerOffset = offsetof(ConditionVariable, fObjectType); 536 537 const char* pointer; 538 status_t status = debug_memcpy(B_CURRENT_TEAM, &pointer, 539 (int8*)cvar + typePointerOffset, sizeof(const char*)); 540 if (status != B_OK) 541 return status; 542 543 return debug_strlcpy(B_CURRENT_TEAM, name, pointer, size); 544 } 545