/ src / libnml / os_intf / sem.cc
sem.cc
  1  /********************************************************************
  2  * Description: sem.cc
  3  *
  4  *   Derived from a work by Fred Proctor & Will Shackleford
  5  *
  6  * Author:
  7  * License: LGPL Version 2
  8  * System: Linux
  9  *    
 10  * Copyright (c) 2004 All rights reserved.
 11  *
 12  * Last change: 
 13  ********************************************************************/
 14  
 15  extern "C" {
 16  #include <stdio.h>		/* NULL */
 17  #include "_sem.h"
 18  }
 19  #include "sem.hh"
 20  #include "rcs_print.hh"		// rcs_print_debug(),
 21  				// PRINT_SEMAPHORE_ACTIVITY
 22  #include "timer.hh"		// etime()
 23  RCS_SEMAPHORE::RCS_SEMAPHORE(key_t _id, int _oflag, double _time,
 24      int _mode, int _state)
 25  {
 26      /* save the constructor args */
 27      id = _id;
 28      oflag = _oflag;
 29      mode = _mode;
 30      state = _state;
 31      timeout = _time;
 32  
 33      if (oflag & RCS_SEMAPHORE_CREATE) {
 34  	sem = rcs_sem_create(id, mode, state);
 35      } else {
 36  	sem = rcs_sem_open(id, 0);
 37      }
 38  
 39      if (sem == NULL) {
 40  	rcs_print_error
 41  	    ("can't create semaphore (id = %lu, oflag = %d, timeout = %f, mode = 0x%X, state = %d)\n",
 42  	    id, oflag, timeout, mode, state);
 43      }
 44  }
 45  
 46  int
 47    RCS_SEMAPHORE::valid()
 48  {
 49      return (sem != NULL);
 50  }
 51  
 52  RCS_SEMAPHORE::~RCS_SEMAPHORE()
 53  {
 54      if (sem == NULL)
 55  	return;
 56  
 57      /* need to destroy the semaphore before closing our copy */
 58      if (oflag & RCS_SEMAPHORE_CREATE) {
 59  	rcs_sem_destroy(sem);
 60      }
 61      rcs_sem_close(sem);
 62      sem = NULL;
 63  }
 64  
 65  int
 66    RCS_SEMAPHORE::wait()
 67  {
 68      int retval;
 69      if (sem == NULL)
 70  	return -1;
 71      retval = rcs_sem_wait(sem, timeout);
 72      return retval;
 73  }
 74  
 75  int RCS_SEMAPHORE::trywait()
 76  {
 77      if (sem == NULL)
 78  	return -1;
 79      return rcs_sem_trywait(sem);
 80  }
 81  
 82  int RCS_SEMAPHORE::post()
 83  {
 84      if (sem == NULL)
 85  	return -1;
 86      return rcs_sem_post(sem);
 87  }
 88  
 89  int RCS_SEMAPHORE::flush()
 90  {
 91      if (sem == NULL)
 92  	return -1;
 93      return rcs_sem_flush(sem);
 94  }
 95  
 96  int RCS_SEMAPHORE::setflag(int _oflag)
 97  {
 98      oflag = _oflag;		/* we can reset whether this was the one who
 99  				   created the semaphore */
100      return (0);
101  }
102  
103  int RCS_SEMAPHORE::clear()
104  {
105      return rcs_sem_clear(sem);
106  }
107  
108  // This constructor declared private to prevent copying.
109  RCS_SEMAPHORE::RCS_SEMAPHORE(RCS_SEMAPHORE & sem)
110  {
111  }