/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : Semaphore.h                                                   */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _SEMAPHORE_H
#define _SEMAPHORE_H

// Name of a macro in sys/kernel.h; disable it when using Semaphore class
#undef Semaphore

#include <semaphore.h>

/*
CLASS 
Semaphore

DESCRIPTION
Semaphore class

AUTHOR
Tom O'Reilly
*/

class Semaphore {

public:

  ////////////////////////////////////////////////////////////////////
  // Constructor
  // [input] name: Name of semaphore
  // [input] initialValue: Initial value of semaphore
  Semaphore(const char *semName);
  Semaphore(const char *name, int initialValue = 1);
  ~Semaphore();

  ////////////////////////////////////////////////////////////////////
  // Name of semaphore
  const char *name();

  ////////////////////////////////////////////////////////////////////
  // Post operation. Returns 0 on success, returns -1 and sets errno
  // on error.
  int post();

  ////////////////////////////////////////////////////////////////////
  // Wait operation. Returns 0 on success, returns -1 and sets errno
  // on error.
  int wait();

  ////////////////////////////////////////////////////////////////////
  // Try to wait. Returns 0 on success, returns -1 and sets errno
  // on error.
  int tryWait();

protected:

     int init( const char *semName );

  // Name of shared memory segment
  const char *_name;

  // Pointer to semaphore structure mapped in shared memory
  sem_t *_semaphore;
};

#endif
