libzmq master
The Intelligent Transport Layer

mutex.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2010-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef __ZMQ_MUTEX_HPP_INCLUDED__
00023 #define __ZMQ_MUTEX_HPP_INCLUDED__
00024 
00025 #include "platform.hpp"
00026 #include "err.hpp"
00027 
00028 //  Mutex class encapsulates OS mutex in a platform-independent way.
00029 
00030 #ifdef ZMQ_HAVE_WINDOWS
00031 
00032 #include "windows.hpp"
00033 
00034 namespace zmq
00035 {
00036 
00037     class mutex_t
00038     {
00039     public:
00040         inline mutex_t ()
00041         {
00042             InitializeCriticalSection (&cs);
00043         }
00044 
00045         inline ~mutex_t ()
00046         {
00047             DeleteCriticalSection (&cs);
00048         }
00049 
00050         inline void lock ()
00051         {
00052             EnterCriticalSection (&cs);
00053         }
00054 
00055         inline void unlock ()
00056         {
00057             LeaveCriticalSection (&cs);
00058         }
00059 
00060     private:
00061 
00062         CRITICAL_SECTION cs;
00063 
00064         //  Disable copy construction and assignment.
00065         mutex_t (const mutex_t&);
00066         void operator = (const mutex_t&);
00067     };
00068 
00069 }
00070 
00071 #else
00072 
00073 #include <pthread.h>
00074 
00075 namespace zmq
00076 {
00077  
00078     class mutex_t
00079     {
00080     public:
00081         inline mutex_t ()
00082         {
00083             int rc = pthread_mutex_init (&mutex, NULL);
00084             if (rc)
00085                 posix_assert (rc);
00086         }
00087  
00088         inline ~mutex_t ()
00089         {
00090             int rc = pthread_mutex_destroy (&mutex);
00091             if (rc)
00092                 posix_assert (rc);
00093         }
00094  
00095         inline void lock ()
00096         {
00097             int rc = pthread_mutex_lock (&mutex);
00098             if (rc)
00099                 posix_assert (rc);
00100         }
00101  
00102         inline void unlock ()
00103         {
00104             int rc = pthread_mutex_unlock (&mutex);
00105             if (rc)
00106                 posix_assert (rc);
00107         }
00108  
00109     private:
00110  
00111         pthread_mutex_t mutex;
00112  
00113         // Disable copy construction and assignment.
00114         mutex_t (const mutex_t&);
00115         const mutex_t &operator = (const mutex_t&);
00116     };
00117  
00118 }
00119 
00120 #endif
00121 
00122 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines