/*
    TMIPC.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.
*/
/*
 	PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 
	
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Threads/TMIPC.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 __TMIPC_H
#define __TMIPC_H

#include "tm-include.h"

/*
 * IPC interface functions
 *  these makros take care or the necessary locking
 */
#define kRecv(msg, threadPtr)   \
                                int flags; \
                                LOCK_THREAD(threadPtr, flags); \
                                { \
                                  _kRecv(msg, threadPtr); \
                                } \
                                UNLOCK_THREAD(threadPtr, flags); 
#define kSend(destThreadId, msg)   \
                                int flags; \
                                Thread* destTHPtr = getTHbyId(destThreadId); \
                                LOCK_THREAD(destTHPtr, flags); \
                                { \
                                  _kSend(destTHPtr, msg); \
                                } \
                                UNLOCK_THREAD(destTHPtr, flags);

/* the real IPC functions */
Error _kRecv(Message *msg, Thread* threadPtr);
Error _kSend(Thread* destThreadPtr, Message* msgPtr);



/* Invoked by the general exception routine to process SYSCALLS */
extern int msgDispatcher( ThreadId dummyThreadid,    /* not used */
                            Message* msgPtr,           /* message reference */
                            unsigned long int timeout, /* timeout for receiving */
                            MsgOpCode code);           /* SYSCALL_SEND_OP/SYSCALL_RECV_OP */

/* Initialisation of the message queue */
void initMsgQueue( MessageQueue* queuePtr);

void ipcFreeBlockedThreads( ThreadId exitThreadId);

#endif
