/home/vlg/develop/libASSA/libassa/assa/Semaphore.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 // $Id: Semaphore.h,v 1.4 2006/07/20 02:30:54 vlg Exp $
00004 //------------------------------------------------------------------------------
00005 //                               Semaphore.h
00006 //------------------------------------------------------------------------------
00007 //  Copyright (c) 2000 by Vladislav Grinchenko
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Library General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2 of the License, or (at your option) any later version.
00013 //------------------------------------------------------------------------------
00014 
00015 #ifndef SEMAPHORE_H
00016 #define SEMAPHORE_H
00017 
00018 #if !defined(WIN32)
00019 
00020 #include <sys/types.h>
00021 #include <sys/ipc.h>
00022 #include <sys/sem.h>
00023 
00024 #include "assa/Assure.h"    // trace() & Assert family
00025 
00026 namespace ASSA {
00027 
00066 class Semaphore
00067 {
00068 public:
00070     Semaphore ();
00071 
00073     virtual ~Semaphore ();
00074 
00082     int create (key_t key_, int initval_ = 1);
00083 
00093     int open (key_t key_);
00094 
00104     void close ();
00105 
00114     void remove ();
00115 
00119     void wait ();
00120 
00124     void signal ();
00125 
00130     void op (int val_);
00131 
00133     key_t key () const { return m_key; }
00134 
00136     int id () const { return m_id; }
00137 
00141     void dump (void) const;
00142 
00143 protected:
00146     void init ();
00147 
00148 protected:
00150     key_t   m_key;
00151 
00153     int     m_id;
00154 
00155 protected:
00156     static const int BIGCOUNT;
00157 
00160     static sembuf m_op_lock      [2];
00161 
00165     static sembuf m_op_endcreate [2];
00166 
00170     static sembuf m_op_open      [2];
00171 
00175     static sembuf m_op_close     [3];
00176 
00179     static sembuf m_op_unlock    [1];
00180 
00185     static sembuf m_op_op        [1];
00186 };
00187 
00188 inline 
00189 Semaphore::
00190 Semaphore ()
00191 {
00192     trace_with_mask("Semaphore::Semaphore", SEM);
00193 
00194     init ();
00195 }
00196 
00197 inline
00198 Semaphore::
00199 ~Semaphore ()
00200 {
00201     trace_with_mask("Semaphore::~Semaphore", SEM);
00202     
00203     if (m_id > 0) {
00204         this->close ();
00205     }
00206 }
00207 
00208 inline void
00209 Semaphore::
00210 init ()
00211 {
00212     m_key = (key_t) -1;
00213     m_id  = -1;
00214 }   
00215 
00216 inline void
00217 Semaphore::
00218 wait ()
00219 {
00220     trace_with_mask("Semaphore::wait", SEM);
00221     op (-1);
00222 }
00223 
00224 inline void
00225 Semaphore::
00226 signal ()
00227 {
00228     trace_with_mask("Semaphore::signal", SEM);
00229     op (1);
00230 }
00231 
00232 } // end namespace ASSA
00233 
00234 #endif /* !defined(WIN32) */
00235 
00236 #endif /* SEMAPHORE_H */  

Generated on Sun Aug 13 15:08:00 2006 for libassa by  doxygen 1.4.6