/************************************************************************/
/* Copyright 2002 - 2016 MBARI											*/
/************************************************************************/
/* Summary	: Definitions for Linked List library for OASIS				*/
/* Filename : list.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: OASIS Mooring Replacement (OASIS5)						*/
/* Revision: 1.0														*/
/* Created  : 07/06/2016 from oasis3/4 task.h							*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/************************************************************************/
/* Modification History:												*/
/* 10oct2002 rah - OASIS3 list.h created from OASIS io.h				*/
/* 7jul20016 rah - OASIS5 olist.h created from OASIS3 list.h			*/
/* Renamed to olist.h to avoid conflict with FreeRTOS list.h			*/
/************************************************************************/

#ifndef OLIST_H
#define OLIST_H			1

#include <mbariTypes.h>
#include <FreeRTOS.h>
#include <FreeRTOSConfig.h>
#include <semphr.h>


#define NULLNODE		((Node *)0)
typedef struct node		Node;			/* Node in linked list				*/

struct node								/************************************/
{										/* List Node structure				*/
	Node		*lst_next;				/* Pointer to next node				*/
	Node		*lst_prev;				/* Pointer to previous node			*/
};										/************************************/

typedef struct list_head				/************************************/
{										/* LstHead STRUCTURE				*/
	Node		*lst_head;				/* Head of linked list of tasks		*/
	Node		*lst_tail;				/* Tail of linked list of tasks		*/
#if (configUSE_PREEMPTION != 0)
	SemaphoreHandle_t lstSem;			/* Semaphore Handle					*/
	StaticSemaphore_t lstSemBuf;		/* Semaphore structure				*/
#endif
} LstHead;								/************************************/


/****************************************/
/* Function Declarations				*/
/****************************************/

#if (configUSE_PREEMPTION == 0)
#define lockList(lp)		TRUE
#define lockListT(lp,t)		TRUE
#define unlockList(lp)
#else
#define lockList(lp)	xSemaphoreTakeRecursive((lp)->lstSem, portMAX_DELAY)
#define lockListT(lp,t)	xSemaphoreTakeRecursive((lp)->lstSem, t)
#define unlockList(lp)	xSemaphoreGiveRecursive((lp)->lstSem)
#endif

void	list_init(LstHead *lp);
Node	*list_head(LstHead *lp);
Void	list_add(LstHead *lp, Node *np);
Node	*list_get(LstHead *lp, Node *np);
Node	*list_first(LstHead *lp);
Node	*list_next(Node *np);
Nat32	list_count(LstHead *lp);

#endif	/* OLIST_H */
