Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::common::BlockingLock Class Reference

Enables a resource to be used by only one instance at a time through a combination of spinning and queuing. When waiting enqueued, thread will sleep. More...

#include <spinlock.h>

Public Member Functions

void lock ()
 Lock the spinlock once it is available, sleeping until other processes are done with it.
 
void unlock ()
 Unlock the spinlock.
 
bool is_locked () const
 Check if the spinlock is locked.
 
void acquire ()
 Acquire the spinlock, spin until the lock is available and sleeping the thread until marked as available.
 
void release ()
 Mark as unlocked, wake the next enqueued thread.
 

Detailed Description

Enables a resource to be used by only one instance at a time through a combination of spinning and queuing. When waiting enqueued, thread will sleep.

Note
Repeated API that could be made a class that isn't because lock types shouldn't be interchangeable
See also
Spinlock

Definition at line 49 of file spinlock.h.

Member Function Documentation

◆ acquire()

void BlockingLock::acquire ( )

Acquire the spinlock, spin until the lock is available and sleeping the thread until marked as available.

Todo:
Move the yielding logic into process/thread code so that it can be reused

Definition at line 101 of file spinlock.cpp.

101 {
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}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Definition vector.h:333
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 * current_thread()
Gets the thread on the currently executing core.
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
constexpr uint8_t BLOCKING_FAST_TRY_LIMIT
How many attempts to acquire the lock should fail before queueing.
Definition spinlock.h:40

References MaxOS::common::BLOCKING_FAST_TRY_LIMIT, MaxOS::processes::GlobalScheduler::core_scheduler(), MaxOS::processes::GlobalScheduler::current_thread(), MaxOS::hardwarecommunication::InterruptManager::ForceInterruptReturn(), and MaxOS::common::Vector< Type >::push_back().

Referenced by lock().

◆ is_locked()

bool BlockingLock::is_locked ( ) const

Check if the spinlock is locked.

Returns
True if the spinlock is locked, false otherwise

Definition at line 92 of file spinlock.cpp.

92 {
93 return m_locked;
94}

◆ lock()

void BlockingLock::lock ( )

Lock the spinlock once it is available, sleeping until other processes are done with it.

Definition at line 73 of file spinlock.cpp.

73 {
74 acquire();
75 m_locked = true;
76}
void acquire()
Acquire the spinlock, spin until the lock is available and sleeping the thread until marked as availa...
Definition spinlock.cpp:101

References acquire().

◆ release()

void BlockingLock::release ( )

Mark as unlocked, wake the next enqueued thread.

Definition at line 129 of file spinlock.cpp.

129 {
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}
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 Thread * get_thread(uint64_t tid)
Gets a thread on the currently executing core by its TID.

References MaxOS::common::Vector< Type >::empty(), MaxOS::processes::GlobalScheduler::get_thread(), and MaxOS::common::Vector< Type >::pop_front().

Referenced by unlock().

◆ unlock()

void BlockingLock::unlock ( )

Unlock the spinlock.

Definition at line 81 of file spinlock.cpp.

81 {
82
83 m_locked = false;
84 release();
85}
void release()
Mark as unlocked, wake the next enqueued thread.
Definition spinlock.cpp:129

References release().


The documentation for this class was generated from the following files: