/*
    ThreadsDemo.c, Copyright  (c) by ,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
	
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/User/ThreadsDemo.c,v $
 	Author(s):             
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 2922 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
	
	
	$Log: ThreadsDemo.c,v $
	Revision 1.5.12.1  2002/02/18 15:09:35  blutz
	added CLEAR_MSGENV to set enveloppe to NULL when not used, did some clean up and a few bug fixes
	
	Revision 1.5  2001/02/13 12:54:50  lukas
	tmTimerTest to Shell added
	
	Revision 1.4  2000/11/15 10:22:05  ruf
	Repository Reorganisation and PCI Merge
	
	Revision 1.3  1999/12/13 21:48:42  ruf
	GNU General Public Licence Update
	
	Revision 1.2  1998/04/07 17:04:07  fiedler
	new file
	
*/

#include "../Topsy/Syscall.h"
#include "UserSupport.h"

void Child()
{
  Message sendmsg;
  ThreadId console, myid, parentid;

  ioOpen(IO_CONSOLE, &console);
  ioInit(console);
  display(console, "child started\n");
  CLEAR_MSGENV(sendmsg);

  tmGetInfo(SELF, &myid, &parentid);
  tmMsgSend(parentid, &sendmsg);

  display(console, "child running\n");
  tmExit();
}

void Demo()
{
  ThreadId console, child;
  Message reply;

  ioOpen(IO_CONSOLE, &console);
  ioInit(console);
  display(console, "parent started\n");

  tmStart(&child, (ThreadMainFunction)Child, (ThreadArg)0, "child");

  display(console, "parent running\n");

  if (tmMsgRecv(&child, ANYMSGTYPE, &reply, INFINITY) == TM_MSGRECVOK)
  {
    display(console, "parent received message from child\n");
  }

  display(console, "parent continuing\n");

  if (tmMsgRecv(&child, ANYMSGTYPE, &reply, INFINITY) == TM_MSGRECVFAILED)
  {
    display(console, "exit of child\n");
  }
  tmExit();
}
