/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident "$Header: fifonode.h 1.6 89/08/07 $"

#ifndef	_SYS_FIFONODE_H_

/*
 * fifonode.h
 *	Fifonode definitions.
 */

/* $Log:	fifonode.h,v $
 */
#ifdef INKERNEL

/* fifonodes start with an snode so that 'spec_xxx()' routines work */
struct fifonode {
	struct snode	fn_snode;	/* must be first */
	sema_t		fn_rwait;	/* reader wait semaphore */
	sema_t		fn_rmutex;	/* reader mutex semaphore */
	sema_t		fn_wwait;	/* writer wait semaphore */
	sema_t		fn_wmutex;	/* writer mutex semaphore */
	lock_t		fn_lock;	/* lock for entire fifo */
	char		fn_flag;	/* see below */
	ushort		fn_mode;	/* modes for pipes (not named pipes) */
	uid_t		fn_uid;		/* uid for pipes (not named pipes) */
	gid_t		fn_gid;		/* gid for pipes (not named pipes) */
	short		fn_size;	/* number of bytes in fifo */
	short		fn_wcnt;	/* number of waiting readers */
	short		fn_rcnt;	/* number of waiting writers */
	short		fn_wptr;	/* write offset */
	short		fn_rptr;	/* read offset */
	struct buf	*fn_bp;		/* buffer header pointer */
	caddr_t		fn_data;	/* pointer to buffer */
};

/*
 * fifo flags
 */
#define FN_RBUSY		1	/* the reader side of fifo is busy */
#define FN_WBUSY		2	/* the writer side of fifo is busy */

#define fn_vnode	fn_snode.s_vnode

/*
 * Convert between fifonode, snode, and vnode pointers
 */
#define VTOF(VP)        ((struct fifonode *)(VP)->v_data)
#define STOF(SN)	((struct fifonode *)(SN))
#define FTOV(FP)        (&(FP)->fn_vnode)
#define FTOS(FP)        (&(FP)->fn_snode)

/* define fifonode handling routines */
#define FIFOMARK(fp,flag)	SMARK(FTOS(fp), flag)
#define FIFOLOCK(fp)		p_lock(&(fp)->fn_lock, SPLFS)
#define FIFOUNLOCK(fp, ipl)	v_lock(&(fp)->fn_lock, ipl)
#define FIFO_RWAIT(fp)		(void)p_sema_v_lock(&(fp)->fn_rwait, PPIPE, \
					&(fp)->fn_lock)
#define FIFO_RMUTEX(fp)		(void)p_sema_v_lock(&(fp)->fn_rmutex, PVNOD, \
					&(fp)->fn_lock)
#define FIFO_WWAIT(fp)		(void)p_sema_v_lock(&(fp)->fn_wwait, PPIPE, \
					&(fp)->fn_lock)
#define FIFO_WMUTEX(fp)		(void)p_sema_v_lock(&(fp)->fn_wmutex, PVNOD, \
					&(fp)->fn_lock)

#endif /*INKERNEL*/

#define	_SYS_FIFONODE_H_
#endif	/* _SYS_FIFONODE_H_ */
