Subversion Repositories shark

Rev

Rev 2 | Rev 38 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/**
 ------------
 CVS :        $Id: int_sem.c,v 1.1.1.1 2002-03-29 14:12:51 pj Exp $

 File:        $File$
 Revision:    $Revision: 1.1.1.1 $
 Last update: $Date: 2002-03-29 14:12:51 $
 ------------

 Internal semaphores.

 They are different from the Posix semaphores and the mutexes because:
 - internal_sem_wait is not a cancellation point
 - there are no limits on the semaphores that can be created
   (they works like a mutex_t...)
 - the queuing policy is FIFO
 - Be Careful!
   they are made to be fast... so not so many controls are done!!!

**/


/*
 * Copyright (C) 2000 Paolo Gai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */



#include <kernel/int_sem.h>
#include <kernel/var.h>
#include <kernel/func.h>

/* Wait status for this library */
#define INTERNAL_SEM_WAIT LIB_STATUS_BASE


void internal_sem_init(internal_sem_t *s, int value)
{
  s->count = value;
  qq_init(&s->blocked);
}

void internal_sem_wait(internal_sem_t *s)
{
  SYS_FLAGS f;

  //kern_cli();
  f = kern_fsave();

  if (s->count) {
    s->count--;
    //kern_sti();
    kern_frestore(f);
    return;
  }
  else {                /* We must block exec task   */
    LEVEL l;            /* for readableness only */
    TIME tx;            /* a dummy TIME for timespec operations */
    struct timespec ty; /* a dummy timespec for timespec operations */

    proc_table[exec_shadow].context = kern_context_save();
    /* SAME AS SCHEDULER... manage the capacity event and the load_info */
    ll_gettime(TIME_EXACT, &schedule_time);
    SUBTIMESPEC(&schedule_time, &cap_lasttime, &ty);
    tx = TIMESPEC2USEC(&ty);
    proc_table[exec_shadow].avail_time -= tx;
    jet_update_slice(tx);
    if (cap_timer != NIL) {
      event_delete(cap_timer);
      cap_timer = NIL;
    }
 
    l = proc_table[exec_shadow].task_level;
    level_table[l]->task_extract(l,exec_shadow);

    /* we insert the task in the semaphore queue */
    proc_table[exec_shadow].status = INTERNAL_SEM_WAIT;
    qq_insertlast(exec_shadow,&s->blocked);

    /* and finally we reschedule */
    exec = exec_shadow = -1;
    scheduler();

    ll_context_to(proc_table[exec_shadow].context);
    kern_after_dispatch();
    kern_frestore(f);
  }
 
}

/* return 0 if the counter is decremented, -1 if not */
int internal_sem_trywait(internal_sem_t *s)
{
  SYS_FLAGS f;

  //kern_cli();
  f = kern_fsave();

  if (s->count) {
    s->count--;
    //kern_sti();
    kern_frestore(f);
    return 0;
  }

  //kern_sti();
  kern_frestore(f);
  return -1;
}


void internal_sem_post(internal_sem_t *s)
{
  proc_table[exec_shadow].context = kern_context_save();

  if (s->blocked.first != -1) {
    register PID p;
    register LEVEL l;

    p = qq_getfirst(&s->blocked);
    l = proc_table[p].task_level;
    level_table[l]->task_insert(l,p);

    scheduler();
  }
  else
    s->count++;

  kern_context_load(proc_table[exec_shadow].context);
}

int internal_sem_getvalue(internal_sem_t *s)
{
  register int returnvalue;
  SYS_FLAGS f;

  //kern_cli();
  f = kern_fsave();
  if (s->blocked.first == -1)
    returnvalue = s->count;
  else
    returnvalue = -1;

  //kern_sti();
  kern_frestore(f);
  return returnvalue;
}