Loading...
Searching...
No Matches
Semaphore.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: Semaphore.h,v 1.5 2012/05/20 04:12:18 vlg Exp $
//------------------------------------------------------------------------------
// Semaphore.h
//------------------------------------------------------------------------------
// Copyright (c) 2000 by Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#if !defined(WIN32)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "assa/Assure.h" // trace() & Assert family
namespace ASSA {
class Semaphore
{
public:
Semaphore ();
virtual ~Semaphore ();
int create (key_t key_, int initval_ = 1);
int open (key_t key_);
void close ();
void remove ();
void wait ();
void signal ();
void op (int val_);
key_t key () const { return m_key; }
int id () const { return m_id; }
void dump (void) const;
protected:
void init ();
protected:
key_t m_key;
int m_id;
protected:
static const int BIGCOUNT;
static sembuf m_op_lock [2];
static sembuf m_op_endcreate [2];
static sembuf m_op_open [2];
static sembuf m_op_close [3];
static sembuf m_op_unlock [1];
static sembuf m_op_op [1];
};
inline
Semaphore::
Semaphore ()
{
trace_with_mask("Semaphore::Semaphore", SEM);
init ();
}
inline
Semaphore::
~Semaphore ()
{
trace_with_mask("Semaphore::~Semaphore", SEM);
if (m_id > 0) {
this->close ();
}
}
inline void
Semaphore::
init ()
{
m_key = (key_t) -1;
m_id = -1;
}
inline void
Semaphore::
wait ()
{
trace_with_mask("Semaphore::wait", SEM);
op (-1);
}
inline void
Semaphore::
signal ()
{
trace_with_mask("Semaphore::signal", SEM);
op (1);
}
} // end namespace ASSA
#endif /* !defined(WIN32) */
#endif /* SEMAPHORE_H */
Assure.h
A collection of assert function wrappers.
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::Semaphore
Definition Semaphore.h:68
ASSA::Semaphore::id
int id() const
Get id.
Definition Semaphore.h:137
ASSA::Semaphore::m_op_open
static sembuf m_op_open[2]
Decrement process counter with undo on exit.
Definition Semaphore.h:171
ASSA::Semaphore::m_id
int m_id
Semaphore's id.
Definition Semaphore.h:154
ASSA::Semaphore::remove
void remove()
Remove a semaphore.
Definition Semaphore.cpp:204
ASSA::Semaphore::create
int create(key_t key_, int initval_=1)
Create a semaphore with a specified initial value.
Definition Semaphore.cpp:74
ASSA::Semaphore::init
void init()
Initalize by invalidating data members.
Definition Semaphore.h:211
ASSA::Semaphore::m_op_op
static sembuf m_op_op[1]
Decrement or increment semaphore with undo on exit.
Definition Semaphore.h:186
ASSA::Semaphore::m_op_unlock
static sembuf m_op_unlock[1]
Decremetn lock back to 0.
Definition Semaphore.h:180
ASSA::Semaphore::m_op_close
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
ASSA::Semaphore::m_key
key_t m_key
Semaphore's key.
Definition Semaphore.h:151
ASSA::Semaphore::op
void op(int val_)
General semaphore operation.
Definition Semaphore.cpp:262
ASSA::Semaphore::~Semaphore
virtual ~Semaphore()
Destructor.
Definition Semaphore.h:200
ASSA::Semaphore::m_op_lock
static sembuf m_op_lock[2]
Wait for lock to equal 0, then increment lock to 1 - this locks it.
Definition Semaphore.h:161
ASSA::Semaphore::BIGCOUNT
static const int BIGCOUNT
Definition Semaphore.h:157
ASSA::Semaphore::dump
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
ASSA::Semaphore::Semaphore
Semaphore()
Constructor.
Definition Semaphore.h:191
ASSA::Semaphore::m_op_endcreate
static sembuf m_op_endcreate[2]
Decrement process counter with undo on exit, then decrement lock back to 0.
Definition Semaphore.h:166
ASSA::Semaphore::close
void close()
Close a semaphore.
Definition Semaphore.cpp:219
ASSA::Semaphore::wait
void wait()
Wait until a semaphore's value is greater then 0, then decrement it by 1 and return.
Definition Semaphore.h:219
ASSA::Semaphore::key
key_t key() const
Get key.
Definition Semaphore.h:134
ASSA::Semaphore::open
int open(key_t key_)
Open a semaphore that must already exist.
Definition Semaphore.cpp:174
ASSA::Semaphore::signal
void signal()
Increment a semaphore by 1.
Definition Semaphore.h:227
ASSA
Definition Acceptor.h:40
ASSA::SEM
@ SEM
Class Semaphore messages
Definition LogMask.h:37