Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

mutex.hh

00001 /*
00002 *  Name:      mutex.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Mutex class for POSIX 1003.1c compliant mutexes
00005 *  Date:      $Date: 2003/04/14 00:18:32 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1999-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_SYSTEM_POSIX_1_MUTEX__
00027 #define _MPCL_SYSTEM_POSIX_1_MUTEX__
00028 
00029 #include <pthread.h>
00030 #include "../defs.hh"
00031 #include "../exceptions.hh"
00032 #include "../mutex.hh"
00033 
00034 
00036 namespace mpcl
00037 {
00038 
00040   namespace system
00041   {
00042 
00047     class TMutex : public IMutex
00048     {
00049 
00051         friend class TCondition;
00052 
00053 
00054       private:
00055 
00057         pthread_mutex_t   tPosixId;
00058 
00059 
00060       public:
00061 
00062         //
00063         //  C O N S T R U C T O R S
00064         //
00065 
00067         TMutex (void)
00068           throw (TErrorException) :
00069           IMutex   ()             ,
00070           tPosixId () 
00071         {
00072           if ( pthread_mutex_init (&tPosixId, NULL) )
00073           {
00074             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00075           }
00076         }
00077 
00079         ~TMutex()
00080           throw (TErrorException)
00081         {
00082           if ( pthread_mutex_destroy (&tPosixId) )
00083           {
00084             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00085           }
00086         }
00087 
00089         void lock (void)
00090           throw (TErrorException)
00091         {
00092           if ( pthread_mutex_lock (&tPosixId) )
00093           {
00094             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00095           }
00096         }
00097 
00099         void unlock (void)
00100           throw (TErrorException)
00101         {
00102           if ( pthread_mutex_unlock (&tPosixId) )
00103           {
00104             throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00105           }
00106         }
00107 
00112         bool tryLock (void)
00113         {
00114           return ( pthread_mutex_trylock (&tPosixId) != 0 );
00115         }
00116 
00117     };  // class TMutex
00118 
00119   }  // namespace system
00120 
00121 }  // namespace mpcl
00122 
00123 
00124 #endif  // not _MPCL_SYSTEM_POSIX_1_MUTEX__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18