Subversion Repositories shark

Rev

Blame | 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>
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
 *   Luca Abeni          <luca@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: nop.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $

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

 Binary Semaphores. see nop.h for more details...

**/


/*
 * 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 <modules/nop.h>

#include <ll/ll.h>
#include <ll/stdio.h>
#include <ll/string.h>
#include <kernel/const.h>
#include <sys/types.h>
#include <modules/codes.h>
#include <kernel/descr.h>
#include <kernel/var.h>
#include <kernel/func.h>

/* The NOP resource level descriptor */
typedef struct {
  mutex_resource_des m;   /*+ the mutex interface +*/
} NOP_mutex_resource_des;


/* this is the structure normally pointed by the opt field in the
   mutex_t structure */

typedef struct {
  PID owner;
  QQUEUE blocked;
} NOP_mutex_t;


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


/*+ print resource protocol statistics...+*/
static void NOP_resource_status(RLEVEL r)
{
  kern_printf("No status for NOP module\n");
}


static int NOP_level_accept_resource_model(RLEVEL l, RES_MODEL *r)
{
  /* priority inheritance works with all tasks without Resource parameters */
  return -1;
}

static void NOP_res_register(RLEVEL l, PID p, RES_MODEL *r)
{
  /* never called!!! */
}

static void NOP_res_detach(RLEVEL l, PID p)
{
}

static int NOP_level_accept_mutexattr(RLEVEL l, const mutexattr_t *a)
{
  if (a->mclass == NOP_MCLASS || a->mclass == (NOP_MCLASS | l) )
    return 0;
  else
    return -1;
}

static int NOP_init(RLEVEL l, mutex_t *m, const mutexattr_t *a)
{
  NOP_mutex_t *p;

  p = (NOP_mutex_t *) kern_alloc(sizeof(NOP_mutex_t));

  /* control if there is enough memory; no control on init on a
     non- destroyed mutex */


  if (!p)
    return (ENOMEM);

  p->owner = NIL;
  qq_init(&p->blocked);

  m->mutexlevel = l;
  m->opt = (void *)p;

  return 0;
}


static int NOP_destroy(RLEVEL l, mutex_t *m)
{
//  NOP_mutex_resource_des *lev = (NOP_mutex_resource_des *)(resource_table[l]);

  if ( ((NOP_mutex_t *)m->opt)->owner != NIL)
    return (EBUSY);

  kern_cli();
  if (m->opt) {
    kern_free(m->opt,sizeof(NOP_mutex_t));
    m->opt = NULL;
  }
  kern_sti();

  return 0;
}

static int NOP_lock(RLEVEL l, mutex_t *m)
{
  NOP_mutex_t *p;

  kern_cli();

  p = (NOP_mutex_t *)m->opt;
  if (!p) {
    /* if the mutex is not initialized, initialize it! */
    NOP_mutexattr_t a;
    NOP_mutexattr_default(a);
    NOP_init(l, m, &a);
  }

  if (p->owner == exec_shadow) {
    /* the task already owns the mutex */
    kern_sti();
    return (EDEADLK);
  }

  if (p->owner != NIL)  {           /* 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 = NOP_WAIT;
       qq_insertlast(exec_shadow,&p->blocked);

       /* and finally we reschedule */
       exec = exec_shadow = -1;
       scheduler();
       kern_context_load(proc_table[exec_shadow].context);
  }
  else {
    /* the mutex is free, We can lock it! */
    p->owner = exec_shadow;
    kern_sti();
  }

  return 0;
}

static int NOP_trylock(RLEVEL l, mutex_t *m)
{
  NOP_mutex_t *p;

  kern_cli();

  p = (NOP_mutex_t *)m->opt;
  if (!p) {
    /* if the mutex is not initialized, initialize it! */
    NOP_mutexattr_t a;
    NOP_mutexattr_default(a);
    NOP_init(l, m, &a);
  }

  if (p->owner != NIL)  {
    /* a task already owns the mutex */
    kern_sti();
    return (EBUSY);
  }
  else {
    /* the mutex is free, We can lock it! */
    p->owner = exec_shadow;
    kern_sti();
  }

  return 0;
}

static int NOP_unlock(RLEVEL l, mutex_t *m)
{
  NOP_mutex_t *p;

  p = (NOP_mutex_t *)m->opt;
  if (!p)
    return (EINVAL);

  if (p->owner != exec_shadow) {
    /* the mutex is owned by another task!!! */
    kern_sti();
    return (EPERM);
  }

  proc_table[exec_shadow].context = kern_context_save();

  /* the mutex is mine, pop the firsttask to extract */
  p->owner = qq_getfirst(&p->blocked);
  if (p->owner != NIL) {
    l = proc_table[p->owner].task_level;
    level_table[l]->task_insert(l,p->owner);
  }

  scheduler();
  kern_context_load(proc_table[exec_shadow].context);

  return 0;
}

void NOP_register_module(void)
{
  RLEVEL l;                  /* the level that we register */
  NOP_mutex_resource_des *m;  /* for readableness only */

  printk("NOP_register_module\n");

  /* request an entry in the level_table */
  l = resource_alloc_descriptor();

  /* alloc the space needed for the EDF_level_des */
  m = (NOP_mutex_resource_des *)kern_alloc(sizeof(NOP_mutex_resource_des));

  /* update the level_table with the new entry */
  resource_table[l] = (resource_des *)m;

  /* fill the resource_des descriptor */
  strncpy(m->m.r.res_name, NOP_MODULENAME, MAX_MODULENAME);
  m->m.r.res_code                    = NOP_MODULE_CODE;
  m->m.r.res_version                 = NOP_MODULE_VERSION;

  m->m.r.rtype                       = MUTEX_RTYPE;

  m->m.r.resource_status             = NOP_resource_status;
  m->m.r.level_accept_resource_model = NOP_level_accept_resource_model;
  m->m.r.res_register                = NOP_res_register;

  m->m.r.res_detach                  = NOP_res_detach;

  /* fill the mutex_resource_des descriptor */
  m->m.level_accept_mutexattr        = NOP_level_accept_mutexattr;
  m->m.init                          = NOP_init;
  m->m.destroy                       = NOP_destroy;
  m->m.lock                          = NOP_lock;
  m->m.trylock                       = NOP_trylock;
  m->m.unlock                        = NOP_unlock;

}