libassa  3.5.1
Semaphore.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // $Id: Semaphore.h,v 1.5 2012/05/20 04:12:18 vlg Exp $
4 //------------------------------------------------------------------------------
5 // Semaphore.h
6 //------------------------------------------------------------------------------
7 // Copyright (c) 2000 by Vladislav Grinchenko
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //------------------------------------------------------------------------------
14 
15 #ifndef SEMAPHORE_H
16 #define SEMAPHORE_H
17 
18 #if !defined(WIN32)
19 
20 #include <sys/types.h>
21 #include <sys/ipc.h>
22 #include <sys/sem.h>
23 
24 #include "assa/Assure.h" // trace() & Assert family
25 
26 namespace ASSA {
27 
67 class Semaphore
68 {
69 public:
71  Semaphore ();
72 
74  virtual ~Semaphore ();
75 
83  int create (key_t key_, int initval_ = 1);
84 
94  int open (key_t key_);
95 
105  void close ();
106 
115  void remove ();
116 
120  void wait ();
121 
125  void signal ();
126 
131  void op (int val_);
132 
134  key_t key () const { return m_key; }
135 
137  int id () const { return m_id; }
138 
142  void dump (void) const;
143 
144 protected:
147  void init ();
148 
149 protected:
151  key_t m_key;
152 
154  int m_id;
155 
156 protected:
157  static const int BIGCOUNT;
158 
161  static sembuf m_op_lock [2];
162 
166  static sembuf m_op_endcreate [2];
167 
171  static sembuf m_op_open [2];
172 
176  static sembuf m_op_close [3];
177 
180  static sembuf m_op_unlock [1];
181 
186  static sembuf m_op_op [1];
187 };
188 
189 inline
191 Semaphore ()
192 {
193  trace_with_mask("Semaphore::Semaphore", SEM);
194 
195  init ();
196 }
197 
198 inline
200 ~Semaphore ()
201 {
202  trace_with_mask("Semaphore::~Semaphore", SEM);
203 
204  if (m_id > 0) {
205  this->close ();
206  }
207 }
208 
209 inline void
211 init ()
212 {
213  m_key = (key_t) -1;
214  m_id = -1;
215 }
216 
217 inline void
219 wait ()
220 {
221  trace_with_mask("Semaphore::wait", SEM);
222  op (-1);
223 }
224 
225 inline void
227 signal ()
228 {
229  trace_with_mask("Semaphore::signal", SEM);
230  op (1);
231 }
232 
233 } // end namespace ASSA
234 
235 #endif /* !defined(WIN32) */
236 
237 #endif /* SEMAPHORE_H */
A collection of assert function wrappers.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
int id() const
Get id.
Definition: Semaphore.h:137
static sembuf m_op_open[2]
Decrement process counter with undo on exit.
Definition: Semaphore.h:171
int m_id
Semaphore's id.
Definition: Semaphore.h:154
void remove()
Remove a semaphore.
Definition: Semaphore.cpp:204
int create(key_t key_, int initval_=1)
Create a semaphore with a specified initial value.
Definition: Semaphore.cpp:74
void init()
Initalize by invalidating data members.
Definition: Semaphore.h:211
static sembuf m_op_op[1]
Decrement or increment semaphore with undo on exit.
Definition: Semaphore.h:186
static sembuf m_op_unlock[1]
Decremetn lock back to 0.
Definition: Semaphore.h:180
static sembuf m_op_close[3]
Wait for lock to equal 0, then increment lock to 1 (lock it), then increment process counter.
Definition: Semaphore.h:176
key_t m_key
Semaphore's key.
Definition: Semaphore.h:151
void op(int val_)
General semaphore operation.
Definition: Semaphore.cpp:262
virtual ~Semaphore()
Destructor.
Definition: Semaphore.h:200
static sembuf m_op_lock[2]
Wait for lock to equal 0, then increment lock to 1 - this locks it.
Definition: Semaphore.h:161
static const int BIGCOUNT
Definition: Semaphore.h:157
void dump(void) const
Dump the objects state along with the state of the semaphore (if connected) to the log file.
Definition: Semaphore.cpp:293
Semaphore()
Constructor.
Definition: Semaphore.h:191
static sembuf m_op_endcreate[2]
Decrement process counter with undo on exit, then decrement lock back to 0.
Definition: Semaphore.h:166
void close()
Close a semaphore.
Definition: Semaphore.cpp:219
void wait()
Wait until a semaphore's value is greater then 0, then decrement it by 1 and return.
Definition: Semaphore.h:219
key_t key() const
Get key.
Definition: Semaphore.h:134
int open(key_t key_)
Open a semaphore that must already exist.
Definition: Semaphore.cpp:174
void signal()
Increment a semaphore by 1.
Definition: Semaphore.h:227
Definition: Acceptor.h:40
@ SEM
Class Semaphore messages
Definition: LogMask.h:37