Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: HARTIK (HA-rd R-eal TI-me K-ernel)
 *
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
 *               Gerardo Lamastra <gerardo@sssup.it>
 *
 * Authors     : Paolo Gai <pj@hartik.sssup.it>
 * (see authors.txt for full list of hartik's authors)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://hartik.sssup.it
 */


/**
 ------------
 CVS :        $Id: testr.c,v 1.1.1.1 2002-09-02 09:37:48 pj Exp $

 File:        $File$
 Revision:    $Revision: 1.1.1.1 $
 Last update: $Date: 2002-09-02 09:37:48 $
 ------------

 Test Number 27 (R):

 This test verify the correctness of the condition variables.
 (... it doesn't test all...)

 The test uses 1 mutex

 the main task (NRT) creates three tasks.

 J0, J1, J3
   starts, lock the mutex, and wait on a condition variable

 J2
   at t = 0.5 lock the mutex and call cond_signal
   at t = 1   lock the mutex and call cond_signal


**/


/*
 * 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/kern.h"
#include "drivers/keyb.h"


mutex_t  m0;
cond_t   c0;

int number = 0;

PID p0,p1,p2,p3;

TASK j0()
{
  kern_printf("J0: before locking   m0\n");
  mutex_lock(&m0);
  kern_printf("J0: locked           m0, waiting on c0, number =%d\n", number);
  while (!number) {
    cond_wait(&c0,&m0);
    kern_printf("J0: number = %d, if >0 unlocking m0\n",number);
  }
  number--;
  mutex_unlock(&m0);
  kern_printf("J0: unlocked         m0, end task\n");
  return 0;
}


TASK j1()
{
  kern_printf("J1: before locking   m0\n");
  mutex_lock(&m0);
  kern_printf("J1: locked           m0, waiting on c0, number =%d\n", number);
  while (!number) {
    cond_wait(&c0,&m0);
    kern_printf("J1: number = %d, if >0 unlocking m0\n",number);
  }
  number--;
  mutex_unlock(&m0);
  kern_printf("J1: unlocked         m0, end task\n");
  return 0;
}


TASK j2()
{
//  struct timespec t;

  kern_printf("J2: started, waiting t=0.5 sec\n");
  while (sys_gettime(NULL) < 500000);

  kern_printf("J2: before locking   m0\n");
  mutex_lock(&m0);
  kern_printf("J2: locked           m0, number++ (was %d), cond_signal\n", number);

  number++;
  cond_signal(&c0);
//  cond_broadcast(&c0);

  kern_printf("J2: unlocking m0\n");
  mutex_unlock(&m0);

  kern_printf("J2: waiting t=1 sec\n");
  while (sys_gettime(NULL) < 1000000);

  kern_printf("J2: Killing J3\n");
  task_kill(p3);
  kern_printf("J2: before locking   m0\n");
  mutex_lock(&m0);
  kern_printf("J2: locked           m0, number++ (was %d), cond_signal\n", number);

  number++;
  cond_signal(&c0);
//  cond_broadcast(&c0);

  kern_printf("J2: unlocking m0\n");
  mutex_unlock(&m0);
  kern_printf("J2: unlocked         m0, end task\n");
  return 0;
}

void cleanup_lock(void *arg)
{
  kern_printf("J3: KILL!!!\n");
  mutex_unlock(&m0);
  kern_printf("J3: unlocked m0 by the cleanup function\n");
}

TASK j3()
{
  kern_printf("J3: before locking   m0\n");
  mutex_lock(&m0);
  kern_printf("J3: locked           m0, waiting on c0, number =%d\n", number);
  task_cleanup_push(cleanup_lock, (void *)&m0);
  while (!number) {
    cond_wait(&c0,&m0);
    kern_printf("J3: number = %d, if >0 unlocking m0\n",number);
  }
  task_cleanup_pop(0);
  // I hope this task never reach this point... it is killed by J2!!!
  number--;
  mutex_unlock(&m0);
  kern_printf("J3: unlocked         m0, end task\n");
  return 0;
}

void fine(KEY_EVT *e)
{
  sys_end();
}


int main(int argc, char **argv)
{
//  struct timespec t;

  NRT_TASK_MODEL m;

  PI_mutexattr_t a;

  KEY_EVT emerg;

  //keyb_set_map(itaMap);
  emerg.ascii = 'x';
  emerg.scan = KEY_X;
  emerg.flag = ALTL_BIT;
  keyb_hook(emerg,fine);

  /* ---------------------------------------------------------------------
     Task creation
     --------------------------------------------------------------------- */


  nrt_task_default_model(m);
  nrt_task_def_group(m,1);
  p0 = task_create("J0", j0, &m, NULL);
  if (p0 == NIL)
  { kern_printf("Can't create J0 task...\n"); return 1; }

  p1 = task_create("J1", j1, &m, NULL);
  if (p1 == NIL)
  { kern_printf("Can't create J1 task...\n"); return 1; }

  p2 = task_create("J2", j2, &m, NULL);
  if (p2 == NIL)
  { kern_printf("Can't create J2 task...\n"); return 1; }

  p3 = task_create("J3", j3, &m, NULL);
  if (p3 == NIL)
  { kern_printf("Can't create J3 task...\n"); return 1; }

  /* ---------------------------------------------------------------------
     Mutex creation
     --------------------------------------------------------------------- */


  PI_mutexattr_default(a);
  mutex_init(&m0,&a);

  cond_init(&c0);

  /* ---------------------------------------------------------------------
     Event post
     --------------------------------------------------------------------- */


  group_activate(1);

  kern_printf("END main\n");

  return 0;
}