/*
 * Copyright (c) 1996-2003 MIPS Technologies, Inc.
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *      copyright
 *        notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with
 *        the distribution.
 *      * Neither the name of MIPS Technologies Inc. nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
 * conc.h: Transputer C interface
 */


#ifndef __CONC_H
#define __CONC_H

#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199506L
#endif

#include <stdarg.h>
#include <pthread.h>
#include <semaphore.h>

typedef struct {
  pthread_attr_t	tha;
  pthread_t		thid;
  int			running;
  void			(*pfun)();
  int			nParam;
  int			*params;
} Process;

/* Note for Transputer 1 is low priority, 0 is high priority 
 */

Process *ProcAlloc (void (*)(), int, int, ...);
void ProcInit (Process *, void (*)(), void *, int, int, ...);
void ProcParam (Process *p, ...);
void ProcFree (Process *p);

int  ProcToHigh(void);		/* return 0,1 */
int  ProcToLow(void);		/* return 0,1 */
int  ProcGetPriority(void);	/* return 0,1 */

void ProcReschedule(void);	/* == yield */
void ProcStop(void);		/* == exit */

void ProcRun(Process *);
void ProcRunHigh(Process *);
void ProcRunLow(Process *);
void ProcPriPar(Process *, Process *);
void ProcPar(Process *, ...);
void ProcParList(Process *p[]);

void ProcWait(int);	/* NOTE 1us/64us ticks */
void ProcWaitNs(int);
void ProcWaitUs(int);
void ProcWaitMs(int);

#ifdef _TPUTER_PRIORITY_EXTENSIONS
/* extended priority stuff */
int  ProcSetPriority(int);
int  ProcGetPolicy(void);
int  ProcSetPolicy(int);
#endif

typedef sem_t	Semaphore;
#define SEMAPHOREINIT	SEM_INITIALIZER(0)

Semaphore * SemAlloc(void);
void SemFree (Semaphore *);

#define _HSemP(s)	sem_wait(s);
#define _HSemV(s)	sem_post(s);
#define _SemP(s)	_HSemP(s)
#define _SemV(s)	_HSemV(s)

#define SemP(s)		_SemP(&(s))
#define SemV(s)		_SemV(&(s))
#define HSemP(s)	_HSemP(&(s))
#define HSemV(s)	_HSemV(&(s))

#ifdef _POSIX_THREAD_DOORBELL_NP

/* Channel poll/wait 
*/
typedef struct {
  int			chan_state;
  int			chan_value;
#define CHAN_DEAD	0
#define CHAN_EMPTY	1
#define CHAN_FULL	2
  pthread_doorbell_t	chan_db;
} Channel;

int ProcAlt(Channel *cl,...);
int ProcSkipAlt(Channel *cl,...);
int ProcTimerAlt(int time,Channel *cl,...);

/* channel input
 */
int ChanInInt(Channel *cl);

/* 32 hardware channels
 */
extern Channel	_tpuHwChannelTable[32];

#define LINKIN(n)	(&_tpuHwChannelTable[n])
#define LINK0IN		LINKIN(0)
#define LINK1IN		LINKIN(1)
#define LINK2IN		LINKIN(2)
#define LINK3IN		LINKIN(3)
#define LINK4IN		LINKIN(4)
#define LINK5IN		LINKIN(5)
#define LINK6IN		LINKIN(6)
#define LINK7IN		LINKIN(7)

#endif

#endif /* __CONC_H */
