summaryrefslogblamecommitdiff
path: root/Source/Kernel/TaskManager/Mutex.class.cpp
blob: 8ba274f5a83ecc308967ec16455bf8cad71fc212 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                                


                                                              



                          

                                                                                                             


                        



                                                                                           


                      
                               




                        
#include "Mutex.class.h"
#include <TaskManager/Task.ns.h>

extern "C" u32int atomic_exchange(u32int* ptr, u32int newval);

Mutex::Mutex(u32int locked) {
	m_locked = locked;
}

bool Mutex::lock() {
	if (atomic_exchange(&m_locked, MUTEX_TRUE) == MUTEX_TRUE) return false;	//The lock was already locked
	return true;
}

void Mutex::waitLock() {
	while (atomic_exchange(&m_locked, MUTEX_TRUE) == MUTEX_TRUE) {
		if (Task::currThread() != 0) Task::currThread()->sleep(10);	//Wait 10ms
		else return;
	}
}

void Mutex::unlock() {
	m_locked = MUTEX_FALSE;
}

bool Mutex::locked() {
	return m_locked;
}