/*
    LWThreads.h, Copyright  (c) by George Fankhauser, Claudio Jeker
    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/ThreadsLight/LWThreads.h,v $
	Author(s):             Claudio Jeker
	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 $
	
*/
#ifndef __LWTHREADS_H__
#define __LWTHREADS_H__

#include "Messages.h"

#define LW_SELF       -3
#define LW_NO_THREAD  -2

#define LW_FAILED     -1
#define LW_OK          0

#define LW_STATE_READY             2
#define LW_STATE_WAIT              3
#define LW_THREAD_DOES_NOT_EXIST  -1

#define LW_STACKSIZE   1024

typedef long LWError;

/* Do not modify these structs without modifying the HAL functions */
/* The size is important for the HAL functions */
typedef struct LWThreadContext_t
{
  unsigned long stackPointer;
  unsigned long framePointer;
  unsigned long programCounter;
}
LWThreadContext;
/* NOTE: the callee saved registers on mips are stored on the stack */

typedef struct LWThreadDesc_t
{
  Message* msg;
  ThreadId from;
  int state;
  ThreadId next;
  ThreadId prev;
  LWThreadContext context;
}
LWThreadDesc;

typedef LWThreadDesc* LWThread;

typedef struct LWThreadArrayDesc_t
{
  long N;   // how many total available
  long n;   // how many used right now
  ThreadId current; // index to running
  char*    stack;   // pointer to the stack
  LWThread a;       // array: LWThreadDesc a[];
}
LWThreadArrayDesc;
typedef LWThreadArrayDesc* LWPool;

typedef void (*LWThreadMainFunction)(LWPool, ThreadArg);

LWError lwInit(LWPool* lw, unsigned int noOfThreads);
void    lwDestroy(LWPool lw);
LWError lwMsgSend(LWPool lw, ThreadId to, Message *msg);
LWError lwMsgRecv(LWPool lw, ThreadId* from,
                  MessageId msgId, // ignored, only for signature compatibility
                  Message* msg,
                  int timeOut);    // ignored, only for signature compatibility
LWError lwCreate(LWPool lw, ThreadId id,
                 LWThreadMainFunction mainFunction,
                 ThreadArg parameter,
                 char *name);     // ignored, only for signature compatibility
LWError lwStart(LWPool lw, ThreadId* id,
                LWThreadMainFunction mainFunction,
                ThreadArg parameter,
                char *name);     // ignored, only for signature compatibility
LWError lwKill(LWPool lw, ThreadId id);
void    lwExit(LWPool lw);
void    lwYield(LWPool lw);

#endif

