↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

sync.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:          synch.c
DESCRIPTION:   Synchronisation events
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "..\..\memory\memory.h"
#include "..\..\process\process.h"
#include "..\process\process.h"
#include "..\ipc\ipc.h"

#include "sync.h"
#include "..\..\init\init.h"

// Globals
//
struct SYEvent_Internal{
    unsigned char bValid;
    unsigned char bRaised;

    unsigned long dwEvent;
    PID           dwOwnerPID;
    PID           dwResetAccessPID;
    unsigned char arProcessWaiting[MAX_PROCESS_CNT];
};

struct SYEvent_Internal g_arEvent[SY_MAX_EVENT_CNT];
unsigned long g_dwTick=0;

// Initialize synchronization services
//
void k_syInit() {
    SYHEVENT i;
   
    for(i=0; i<SY_MAX_EVENT_CNT; i++)
        g_arEvent[i].bValid = 0;
}

// The kernel can appropriate event objects
//
void k_syThisEventIsKernels(SYHEVENT hEvent) {
    g_arEvent[hEvent].dwOwnerPID = PID_KERNEL;
}

// Create an event
//
void syCreateEvent(struct SYEvent *p)
{
    SYHEVENT i,j;
   
    // Find a free descriptor
    //
    for(i=0; i<SY_MAX_EVENT_CNT; i++)
        if(!g_arEvent[i].bValid)break;
       
    if(i>=SY_MAX_EVENT_CNT) {
        p->st = SY_S_OUTOFRESOURCES;
        return;
    }

    // Setup event
    //
    g_arEvent[i].bValid     = 1;
    g_arEvent[i].bRaised    = 0;
    g_arEvent[i].dwOwnerPID = tmGetCrProcessID();
   
    for(j=0; j<MAX_PROCESS_CNT; j++)
        g_arEvent[i].arProcessWaiting[i]=0;
       
    p->hEvent = i;
    p->st = SY_S_OK;
}

// Delete event
//
void syDeleteEvent(struct SYEventHandle *p)
{
    PID i;
   
    // Validate request
    //
    if(p->hEvent>=SY_MAX_EVENT_CNT   ||
      (!g_arEvent[p->hEvent].bValid) ||
      (g_arEvent[p->hEvent].dwOwnerPID!=tmGetCrProcessID()))
    {
        p->st = SY_S_INVALIDHANDLE;
        return;
    }

    // Wake all processes waiting for this event
    // in order to prevent deadlocks
    //
    for(i=0; i<MAX_PROCESS_CNT; i++)
        if(g_arEvent[p->hEvent].arProcessWaiting[i])
            tmWakeUp(i);

    // Devete event
    //
    g_arEvent[p->hEvent].bValid = 0;
    p->st = SY_S_OK;
}

// Block client process until event occurs
//
void syWaitEvent(struct SYEvent *p)
{
    PID dwPID;
    SYHEVENT i;
   
    // Validate refernece PID
    //
    p->st = k_pGetPID(&(p->paRaiser), &dwPID);
   
    if(p->st!=P_S_OK) {
        p->st = SY_S_NOSUCHEVENT;
        return;
    }

    // Locate the event
    //
    for(i=0; i<SY_MAX_EVENT_CNT; i++)
    {
        if(g_arEvent[i].bValid &&
          (g_arEvent[i].dwOwnerPID==dwPID) &&
          (g_arEvent[i].dwEvent==p->dwEvent))
            break;
    }
   
    if(i>=SY_MAX_EVENT_CNT) {
        p->st = SY_S_NOSUCHEVENT;
        return;
    }

    // Set process state to sleeping if the requested
    // event is not raised
    //
    if(!g_arEvent[i].bRaised) {
        p->hEvent = i;
        gdtLDTDatasPutBufer(p->pHandle, (unsigned char*)&(p->hEvent), sizeof(SYHEVENT));
        g_arEvent[i].arProcessWaiting[tmGetCrProcessID()] = 1;
        tmSleep(tmGetCrProcessID());
        tmAddCharge();
    }
   
    p->st = SY_S_OK;
}

// Raise an event
//
void syRaiseEvent(struct SYEventHandle *p)
{
    PID i;
    unsigned char r;
   
    // Validate request
    //
    if(p->hEvent>=SY_MAX_EVENT_CNT   ||
      (!g_arEvent[p->hEvent].bValid) ||
      (g_arEvent[p->hEvent].dwOwnerPID!=tmGetCrProcessID())) {

        p->st = SY_S_INVALIDHANDLE;
        return;
    }

    // Raise the event
    g_arEvent[p->hEvent].bRaised = 1;

    // Reset access
    //
    r = k_pGetPID(&(p->paResetAccess), &i);
   
    if(r!=P_S_OK) i = PID_KERNEL; //no reset access req
   
    g_arEvent[p->hEvent].dwResetAccessPID = i;

    // Wake all processes waiting for this event
    //
    for(i=0; i<MAX_PROCESS_CNT; i++)
        if(g_arEvent[p->hEvent].arProcessWaiting[i])
            tmWakeUp(i);

    p->st = SY_S_OK;
}

// Reset an event
//
void syResetEvent(struct SYEventHandle *p)
{
    PID i;
   
    // Validate request
    //
    if(p->hEvent>=SY_MAX_EVENT_CNT   ||
      (!g_arEvent[p->hEvent].bValid) ||
      (
        (g_arEvent[p->hEvent].dwOwnerPID!=tmGetCrProcessID()) &&
        (g_arEvent[p->hEvent].dwResetAccessPID!=tmGetCrProcessID())
      )) {

        p->st = SY_S_INVALIDHANDLE;
        return;
    }

    // Clear the event
    //
    g_arEvent[p->hEvent].bRaised = 0;
    p->st = SY_S_OK;
}

// Timing service
//
void syGetMiliseconds(struct SYCount *p) {
    p->dwCnt = (g_dwTick*1000)/KRNL_TIMER_HZ;
    p->st = SY_S_OK;
}