Max OS 0.3
Loading...
Searching...
No Matches
spinlock.cpp
Go to the documentation of this file.
1
9#include <common/spinlock.h>
10#include <processes/scheduler.h>
11
12using namespace MaxOS;
13using namespace MaxOS::common;
14using namespace MaxOS::processes;
15using namespace MaxOS::system;
16using namespace MaxOS::hardwarecommunication;
17
18Spinlock::Spinlock() = default;
19Spinlock::~Spinlock() = default;
20
25 acquire();
26 m_locked = true;
27}
28
33
34 m_locked = false;
35 release();
36}
37
43bool Spinlock::is_locked() const {
44 return m_locked;
45}
46
47
52 while (__atomic_test_and_set(&m_locked, __ATOMIC_ACQUIRE))
53 asm("nop");
54}
55
62
63BlockingLock::BlockingLock() = default;
64BlockingLock::~BlockingLock() = default;
65
66bool BlockingLock::must_spin() {
68}
69
74 acquire();
75 m_locked = true;
76}
77
82
83 m_locked = false;
84 release();
85}
86
93 return m_locked;
94}
95
102
103 // Try to get the lock
104 for (int i = 0; (i < BLOCKING_FAST_TRY_LIMIT || must_spin()); ++i)
106 return;
107
108 // Add to the queue
110 thread->thread_state = ThreadState::WAITING;
111 m_queue.push_back(thread->tid);
112
113 thread->save_cpu_state();
114
115 // Guard against being resumed here
116 if(thread->thread_state == ThreadState::WAITING){
117
118 // Yield to the next thread
119 cpu_status_t* next = GlobalScheduler::core_scheduler()->schedule_next(&thread->execution_state);
121 }
122
123
124}
125
130
131 // Next thread can be run
132 if(!m_queue.empty()){
133 auto tid = m_queue.pop_front();
134 GlobalScheduler::get_thread(tid)->thread_state = ThreadState::READY;
135 }
136
138}
void acquire()
Acquire the spinlock, spin until the lock is available and sleeping the thread until marked as availa...
Definition spinlock.cpp:101
void lock()
Lock the spinlock once it is available, sleeping until other processes are done with it.
Definition spinlock.cpp:73
void release()
Mark as unlocked, wake the next enqueued thread.
Definition spinlock.cpp:129
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:81
bool is_locked() const
Check if the spinlock is locked.
Definition spinlock.cpp:92
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
void lock()
Lock the spinlock once it is available.
Definition spinlock.cpp:24
void release()
Release the spinlock.
Definition spinlock.cpp:59
void unlock()
Unlock the spinlock.
Definition spinlock.cpp:32
bool is_locked() const
Check if the spinlock is locked.
Definition spinlock.cpp:43
void acquire()
Acquire the spinlock: wait until the lock is available, spinning until that happens.
Definition spinlock.cpp:51
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333
Type pop_front()
Removes the m_first_memory_chunk element from the Vector.
Definition vector.h:390
bool empty() const
Checks if the Vector is empty.
Definition vector.h:321
static void ForceInterruptReturn(system::cpu_status_t *state)
Force the CPU to return from an interrupt (see interrupts.s)
static Scheduler * core_scheduler()
Gets the scheduler for the currently executing core.
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.
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
Defines a GlobalScheduler and Scheduler for managing processes and threads.
Defines Spinlock and BlockingLock classes for thread synchronization.
constexpr uint8_t BLOCKING_FAST_TRY_LIMIT
How many attempts to acquire the lock should fail before queueing.
Definition spinlock.h:40