UniSet  1.4.0
Mutex.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00023 // -------------------------------------------------------------------------- 
00024 #ifndef UniSet_MUTEX_H_
00025 #define UniSet_MUTEX_H_
00026 // -----------------------------------------------------------------------------------------
00027 #include <string>
00028 #include <omnithread.h>
00029 #include <signal.h>
00030 // -----------------------------------------------------------------------------------------
00031 namespace UniSetTypes
00032 {
00033     typedef volatile sig_atomic_t mutex_atomic_t;
00034     // typedef _Atomic_word mutex_atomic_t;
00035     
00036 
00043     class uniset_mutex
00044     {
00045         public:
00046             uniset_mutex();
00047             uniset_mutex( std::string name );
00048             ~uniset_mutex();
00049 
00050             bool isRelease();
00051             void lock();
00052             void unlock();
00053             
00054             inline std::string name()
00055             {
00056                 return nm;
00057             };
00058             
00059         protected:
00060         
00061         private:
00062             friend class uniset_mutex_lock;
00063             uniset_mutex (const uniset_mutex& r);
00064             const uniset_mutex &operator=(const uniset_mutex& r);
00065             omni_condition* cnd;
00066             std::string nm;
00067             omni_semaphore sem;
00068             omni_mutex mtx;
00069             mutex_atomic_t locked;
00070     };
00071 
00080     class uniset_mutex_lock
00081     {
00082         public:
00083             uniset_mutex_lock( uniset_mutex& m, int timeoutMS=0 );
00084             ~uniset_mutex_lock();
00085             
00086             bool lock_ok();
00087 
00088         private:
00089             uniset_mutex* mutex;
00090             mutex_atomic_t mlock;
00091             uniset_mutex_lock(const uniset_mutex_lock&);
00092             uniset_mutex_lock& operator=(const uniset_mutex_lock&);
00093     };
00094 
00095     // -------------------------------------------------------------------------
00096     class uniset_spin_mutex
00097     {
00098         public:
00099             uniset_spin_mutex();
00100             ~uniset_spin_mutex();
00101 
00102             void lock( int check_pause_msec=1 );
00103             void unlock();
00104 
00105             uniset_spin_mutex (const uniset_spin_mutex& r);
00106             const uniset_spin_mutex &operator=(const uniset_spin_mutex& r);
00107 
00108         private:
00109             friend class uniset_spin_lock;
00110             mutex_atomic_t m;
00111     };
00112     // -------------------------------------------------------------------------
00113     class uniset_spin_lock
00114     {
00115         public:
00116             uniset_spin_lock( uniset_spin_mutex& m, int check_pause_msec=1 );
00117             ~uniset_spin_lock();
00118 
00119         private:
00120             uniset_spin_lock(const uniset_spin_lock&);
00121             uniset_spin_lock& operator=(const uniset_spin_lock&);
00122             uniset_spin_mutex& m;
00123     };
00124     // -------------------------------------------------------------------------
00125 } // end of UniSetTypes namespace
00126 
00127 #endif