/ src / libnml / os_intf / sem.hh
sem.hh
 1  /********************************************************************
 2  * Description: sem.hh
 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  #ifndef SEMAPHORE_HH
15  #define SEMAPHORE_HH
16  
17  extern "C" {
18  #include "_sem.h"		/* rcs_sem_t */
19  #include <sys/stat.h>		/* S_IRUSR, etc. */
20  }
21  /* rw-rw-rw- permissions */
22  #define DEFAULT_SEM_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
23  #define RCS_SEMAPHORE_NOCREATE 0x00	/* just attach to existing semaphore */
24  #define RCS_SEMAPHORE_CREATE 0x01	/* create semaphore */
25  class RCS_SEMAPHORE {
26      /* RCS_SEMAPHORE objects can be used for mutual exclusion of resources
27         shared by multiple processes on the same host.
28  
29         To use:
30  
31         Create or attach to the semaphore by initializing the RCS_SEMAPHORE
32         object. You will need an id agreed on by the all the processes using
33         the semaphore and you must specify which process is responsible for
34         creating the semaphore. Surround accesses to the shared resource with */
35    public:
36      RCS_SEMAPHORE(key_t id, int oflag, double _timeout, int mode =
37  	DEFAULT_SEM_MODE, int state = 0);
38      /* Initializes an RCS_SEMAPHORE object. If _oflag equals
39         RCS_SEMAPHORE_CREATE a semaphore is created. If _oflag equals
40         RCS_SEMAPHORE_NOCREATE the process will try to attach to a semaphore
41         that must already have been created with the same _id. If _timeout is
42         positive, then calls to RCS_SEMAPHORE::wait() will return -1 after
43         timeout seconds. If _timeout is negative, then RCS_SEMAPHORE::wait()
44         will wait indefinitely for the semaphore to be available. If _timeout
45         is zero, then RCS_SEMAPHORE::wait() will return immediately with 0 if
46         the semaphore was available or -1 if it was not. The _mode determines
47         which users will have permission to use the semaphore. You can or
48         together symbolic constants from sys/stat.h. The default value of
49         _mode, DEFAULT_SEM_MODE, allows read and write access to everyone. The 
50         _mode will be ignored if the process is not creating the semaphore.
51         The _state should be 1 to make the semaphore immediately available.
52         The _state will be ignored if the process is not creating the
53         semaphore. */
54       ~RCS_SEMAPHORE();
55      int wait();
56      /* Wait for the semaphore to be available and then take it. See the
57         constructors parameters for several options affecting its behavior.
58         Returns 0 for success or -1 for failure. */
59  
60      int trywait();
61      /* If the semaphore is available take it. Returns 0 for success or -1 for 
62         failure. */
63  
64      int post();
65      /* Release the semaphore. Returns 0 for success or -1 for failure. */
66  
67      int flush();
68      /* Test to see if the semaphore is available but don't take it even if it 
69         is. Returns a positive integer if the semaphore is available or 0 if
70         it is not. */
71  
72      /* additional non-POSIX functions */
73  
74      int setflag(int oflag);	/* change oflag-- one can toggle the state of 
75  				   this being the master, so that flexible
76  				   destruction of OS semaphore can be done */
77      int valid();
78      int clear();		// Make sure this semaphore will not
79      // immediately be available.
80  
81      unsigned long int id;
82      double timeout;
83      int oflag;
84      int mode;
85      int state;
86      rcs_sem_t *sem;
87      unsigned int sval;
88  
89    private:
90        RCS_SEMAPHORE(RCS_SEMAPHORE & sem);	// Don't copy me.
91  
92  };
93  
94  #endif