Skip to content

Commit bb3cd9b

Browse files
Yifan ZhuSchrodingerZhu
authored andcommitted
fix all bugs
1 parent 860db17 commit bb3cd9b

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

libc/src/__support/threads/linux/rwlock.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -445,31 +445,30 @@ class RwLock {
445445
// Phase 6: do futex wait until the lock is available or timeout is
446446
// reached.
447447
bool timeout_flag = false;
448-
if (!old.can_acquire<role>(get_preference())) {
448+
if (!old.can_acquire<role>(get_preference()))
449449
timeout_flag = (queue.wait<role>(serial_number, timeout, is_pshared) ==
450450
-ETIMEDOUT);
451451

452-
// Phase 7: unregister ourselves as a pending reader.
453-
{
454-
// Similarly, the unregister operation should also be an atomic
455-
// transaction.
456-
WaitingQueue::Guard guard = queue.acquire(is_pshared);
457-
guard.pending_count<role>()--;
458-
// Clear the flag if we are the last reader. The flag must be
459-
// cleared otherwise operations like trylock may fail even though
460-
// there is no competitors.
461-
if (guard.pending_count<role>() == 0)
462-
State::fetch_clear_pending_bit<role>(state,
463-
cpp::MemoryOrder::RELAXED);
464-
}
452+
// Phase 7: unregister ourselves as a pending reader.
453+
{
454+
// Similarly, the unregister operation should also be an atomic
455+
// transaction.
456+
WaitingQueue::Guard guard = queue.acquire(is_pshared);
457+
guard.pending_count<role>()--;
458+
// Clear the flag if we are the last reader. The flag must be
459+
// cleared otherwise operations like trylock may fail even though
460+
// there is no competitors.
461+
if (guard.pending_count<role>() == 0)
462+
State::fetch_clear_pending_bit<role>(state,
463+
cpp::MemoryOrder::RELAXED);
464+
}
465465

466-
// Phase 8: exit the loop is timeout is reached.
467-
if (timeout_flag)
468-
return LockResult::TimedOut;
466+
// Phase 8: exit the loop is timeout is reached.
467+
if (timeout_flag)
468+
return LockResult::TimedOut;
469469

470-
// Phase 9: reload the state and retry the acquisition.
471-
old = State::spin_reload<role>(state, get_preference(), spin_count);
472-
}
470+
// Phase 9: reload the state and retry the acquisition.
471+
old = State::spin_reload<role>(state, get_preference(), spin_count);
473472
}
474473
}
475474

libc/test/integration/src/pthread/pthread_rwlock_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ randomized_process_operation(SharedData &data,
350350
ASSERT_EQ(LIBC_NAMESPACE::pthread_create(
351351
&i, nullptr,
352352
[](void *arg) -> void * {
353-
for (int i = 0; i < 10; ++i)
353+
for (int i = 0; i < 64; ++i)
354354
randomized_thread_operation(
355355
reinterpret_cast<SharedData *>(arg));
356356
return nullptr;

0 commit comments

Comments
 (0)