Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::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 <block.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
Todo:
move to libcommon

Definition at line 28 of file block.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

circular dependancy resolution

Definition at line 49 of file block.cpp.

49 {
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}
constexpr uint8_t BLOCKING_FAST_TRY_LIMIT
How many attempts to acquire the lock should fail before queueing.
Definition block.h:17
static Thread * current_thread()
Gets the thread on the currently executing core.

References MaxOS::processes::BLOCKING_FAST_TRY_LIMIT, and MaxOS::processes::GlobalScheduler::current_thread().

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 39 of file block.cpp.

39 {
40 return m_locked;
41}

◆ lock()

void BlockingLock::lock ( )

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

Definition at line 23 of file block.cpp.

23 {
24 acquire();
25}
void acquire()
Acquire the spinlock, spin until the lock is available and sleeping the thread until marked as availa...
Definition block.cpp:49

References acquire().

Referenced by MaxOS::processes::SharedMessageEndpoint::send().

◆ release()

void BlockingLock::release ( )

Mark as unlocked, wake the next enqueued thread.

Definition at line 68 of file block.cpp.

68 {
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}
static Thread * get_thread(uint64_t tid)
Gets a thread on the currently executing core by its TID.
volatile thread_state_t thread_state
The current state of the thread.
Definition process.h:72

References MaxOS::processes::GlobalScheduler::get_thread(), and MaxOS::processes::Thread::thread_state.

Referenced by unlock().

◆ unlock()

void BlockingLock::unlock ( )

Unlock the spinlock.

Definition at line 30 of file block.cpp.

30 {
31 release();
32}
void release()
Mark as unlocked, wake the next enqueued thread.
Definition block.cpp:68

References release().

Referenced by MaxOS::processes::SharedMessageEndpoint::send().


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