/*
    Threads.h, Copyright  (c) by Christian Conrad,
    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/Threads/Threads.h,v $
 	Author(s):             Christian Conrad
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 3042 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-06-28 01:28:34 +0200 (Don, 28 Jun 2007) $      by: $Author: sryffel $
	
*/

#ifndef __THREADS_H
#define __THREADS_H

#include "Topsy.h"
#include "Messages.h"
#include "Error.h"
#include "Exception.h"

#define FIRST_KERNELTHREAD IOTHREADID /* besides mm, tm, io */

// chekc bit 0 of permission to see wheter we have a kernel or user thread
// more sophisticated checking can be added easily since permission i s bit
// field
#define THREAD_PERMISSION(tptr)	(((tptr->permission) & KERNEL_PERM_MSK) ? KERNEL : USER)
/* MM thread is *really* the first one (id -1) */
/* this macro makes invalid id's USER_PRIORITY */
#define THREAD_PRIO(tptr) 			((tptr->schedInfo).priority)

/*
 * Exported data types
 */
typedef void (*ExceptionHandler)();
typedef void (*InterruptHandler)(void* arg);

/*
 * These functions may only be invoked inside kernel
 */
InterruptHandler tmSetInterruptHandler( InterruptId id,
                                        InterruptHandler intHdler, void* arg);
ExceptionHandler tmSetExceptionHandler( ExceptionId id,
                                        ExceptionHandler excHdler);
/*
 * Init and Main procedures
 */
void tmInit( Address mmStack,
             Address tmStack,
             Address userInit);

void tmMain( ThreadArg userInitAddress);

/* Initialisation of basic exception handlers for init phase */
void initBasicExceptions(void);

char* getCurrentThreadName(void);

#endif
