I don't know why I though the block chain had to be stored fully in the ThreadBlocker, that did not even fix the problem I was trying to fix when I last rewrote it. Roll back to doubly linked list of block chain and now just check that the node is contained within the ThreadBlocker before removing and after acquiring the ThreadBlocker's lock. Also there is no need to have a separate lock the node's blocker field. We can just perform an atomic reads and writes to it. We can still get a blocker that the node is no longer part of, but this can be resolved with a simple check. This patch reduces ThreadBlocker's size from over 200 bytes to just 12 bytes +4 bytes padding
41 lines
1.0 KiB
C++
41 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <BAN/Math.h>
|
|
#include <kernel/Lock/SpinLock.h>
|
|
#include <kernel/Scheduler.h>
|
|
|
|
namespace Kernel
|
|
{
|
|
|
|
class ThreadBlocker
|
|
{
|
|
public:
|
|
void block_indefinite(BaseMutex*);
|
|
void block_with_timeout_ns(uint64_t timeout_ns, BaseMutex*);
|
|
void block_with_wake_time_ns(uint64_t wake_time_ns, BaseMutex*);
|
|
void unblock();
|
|
|
|
void block_with_timeout_ms(uint64_t timeout_ms, BaseMutex* mutex)
|
|
{
|
|
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(timeout_ms, 1'000'000));
|
|
return block_with_timeout_ns(timeout_ms * 1'000'000, mutex);
|
|
}
|
|
void block_with_wake_time_ms(uint64_t wake_time_ms, BaseMutex* mutex)
|
|
{
|
|
ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(wake_time_ms, 1'000'000));
|
|
return block_with_wake_time_ns(wake_time_ms * 1'000'000, mutex);
|
|
}
|
|
|
|
private:
|
|
void add_thread_to_block_queue(SchedulerQueue::Node*);
|
|
void remove_thread_from_block_queue(SchedulerQueue::Node*);
|
|
|
|
private:
|
|
SchedulerQueue::Node* m_block_chain { nullptr };
|
|
SpinLock m_lock;
|
|
|
|
friend class Scheduler;
|
|
};
|
|
|
|
}
|