#include "mutex.h"
#include "kernel.h"

void Mutex::Lock()
{
#if 0
    TCritSect cs;

    ProcessMap prioTag = Kernel.getPrioTag(Kernel._curProcPriority);
    while (_valueTag) {
        SetPrioTag(ProcessMap, prioTag);             // mutex already locked by another process, put current process to the wait map
        ClrPrioTag(Kernel._readyProcessMap, prioTag); // remove current process from the ready map
        Kernel.Scheduler();
    }
    _valueTag = prioTag;                              // mutex has been successfully locked
#endif
}

void Mutex::Unlock()
{
#if 0
    TCritSect cs;

    TProcessMap PrioTag = GetPrioTag(Kernel._CurProcPriority);
    if (ValueTag != PrioTag)
    	return;                  // the only process that had locked mutex can unlock the mutex
    ValueTag = 0;
    if (ProcessMap) {
        uint8_t pr = GetHighPriority(ProcessMap);
        PrioTag = GetPrioTag(pr);
        ClrPrioTag(ProcessMap, PrioTag);             // remove next ready process from the wait map
        SetPrioTag(Kernel.ReadyProcessMap, PrioTag); // place next process to the ready map
        Kernel.Scheduler();
    }
#endif
}

void Mutex::UnlockISR()
{
#if 0
    TCritSect cs;

    _ValueTag = 0;
    if (_ProcessMap) {
        uint8_t pr = GetHighPriority(ProcessMap);
        TProcessMap PrioTag = GetPrioTag(pr);
        ClrPrioTag(ProcessMap, PrioTag);             // remove next ready process from the wait map
        SetPrioTag(Kernel.ReadyProcessMap, PrioTag); // place next process to the ready map
    }
#endif
}
