Max OS 0.3
Loading...
Searching...
No Matches
block.cpp
1//
2// Created by Max Tyson on 19/01/2026.
3//
4
5#include <processes/block.h>
7
8using namespace MaxOS;
9using namespace MaxOS::processes;
10using namespace MaxOS::hardwarecommunication;
11using namespace MaxOS::system;
12
13BlockingLock::BlockingLock() = default;
14BlockingLock::~BlockingLock() = default;
15
16bool BlockingLock::must_spin() {
18}
19
24 acquire();
25}
26
31 release();
32}
33
40 return m_locked;
41}
42
50
51 // Try to get the lock
52 for (int i = 0; (i < BLOCKING_FAST_TRY_LIMIT || must_spin()); ++i)
53 if(!__atomic_test_and_set(&m_locked, __ATOMIC_ACQUIRE))
54 return;
55
56 // Add to the queue
57 auto thread = GlobalScheduler::current_thread();
58 thread->thread_state = ThreadState::WAITING;
59 m_queue.push_back(thread->tid);
60
61 // Scheduler will awake when the process that holds this lock marks this thread as READY
62 thread->yield();
63}
64
69
70 // Next thread can be run
71 if(!m_queue.empty())
72 GlobalScheduler::get_thread(m_queue.pop_front())->thread_state = ThreadState::READY;
73
74 __atomic_clear(&m_locked, __ATOMIC_RELEASE);
75}
Defines BlockingLock class for thread synchronization.
constexpr uint8_t BLOCKING_FAST_TRY_LIMIT
How many attempts to acquire the lock should fail before queueing.
Definition block.h:17
void acquire()
Acquire the spinlock, spin until the lock is available and sleeping the thread until marked as availa...
Definition block.cpp:49
void lock()
Lock the spinlock once it is available, sleeping until other processes are done with it.
Definition block.cpp:23
void release()
Mark as unlocked, wake the next enqueued thread.
Definition block.cpp:68
void unlock()
Unlock the spinlock.
Definition block.cpp:30
bool is_locked() const
Check if the spinlock is locked.
Definition block.cpp:39
static Thread * get_thread(uint64_t tid)
Gets a thread on the currently executing core by its TID.
static bool is_active()
Checks if the global scheduler is active.
static Thread * current_thread()
Gets the thread on the currently executing core.
volatile thread_state_t thread_state
The current state of the thread.
Definition process.h:72
Defines a GlobalScheduler and Scheduler for managing processes and threads.