PageRenderTime 33ms CodeModel.GetById 27ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/pool/detail/mutex.hpp

http://hadesmem.googlecode.com/
C++ Header | 147 lines | 100 code | 37 blank | 10 comment | 8 complexity | 6b05355a95e92c8e6045739bbd1227b8 MD5 | raw file
  1// Copyright (C) 2000 Stephen Cleary
  2//
  3// Distributed under the Boost Software License, Version 1.0. (See
  4// accompanying file LICENSE_1_0.txt or copy at
  5// http://www.boost.org/LICENSE_1_0.txt)
  6//
  7// See http://www.boost.org for updates, documentation, and revision history.
  8
  9#ifndef BOOST_POOL_MUTEX_HPP
 10#define BOOST_POOL_MUTEX_HPP
 11
 12#include <boost/config.hpp>  // for workarounds
 13
 14// Extremely Light-Weight wrapper classes for OS thread synchronization
 15
 16// Configuration: for now, we just choose between pthread or Win32 mutexes or none
 17
 18#define BOOST_MUTEX_HELPER_NONE         0
 19#define BOOST_MUTEX_HELPER_WIN32        1
 20#define BOOST_MUTEX_HELPER_PTHREAD      2
 21
 22#if !defined(BOOST_HAS_THREADS) && !defined(BOOST_NO_MT)
 23# define BOOST_NO_MT
 24#endif
 25
 26#ifdef BOOST_NO_MT
 27  // No multithreading -> make locks into no-ops
 28  #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_NONE
 29#else
 30  #ifdef BOOST_WINDOWS
 31    #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_WIN32
 32  #else
 33    #if defined(BOOST_HAS_UNISTD_H)
 34      #include <unistd.h>
 35    #endif
 36    #if defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
 37      #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD
 38    #endif
 39  #endif
 40#endif
 41
 42#ifndef BOOST_MUTEX_HELPER
 43  #error Unable to determine platform mutex type; define BOOST_NO_MT to assume single-threaded
 44#endif
 45
 46#ifndef BOOST_NO_MT
 47# ifdef BOOST_WINDOWS
 48#  include <windows.h>
 49# endif
 50# if defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
 51#  include <pthread.h>
 52# endif
 53#endif
 54
 55namespace boost {
 56
 57namespace details {
 58namespace pool {
 59
 60#ifndef BOOST_NO_MT
 61
 62#ifdef BOOST_WINDOWS
 63
 64class win32_mutex
 65{
 66  private:
 67    ::CRITICAL_SECTION mtx;
 68
 69    win32_mutex(const win32_mutex &);
 70    void operator=(const win32_mutex &);
 71
 72  public:
 73    win32_mutex()
 74    { ::InitializeCriticalSection(&mtx); }
 75
 76    ~win32_mutex()
 77    { ::DeleteCriticalSection(&mtx); }
 78
 79    void lock()
 80    { ::EnterCriticalSection(&mtx); }
 81
 82    void unlock()
 83    { ::LeaveCriticalSection(&mtx); }
 84};
 85
 86#endif // defined(BOOST_WINDOWS)
 87
 88#if defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
 89
 90class pthread_mutex
 91{
 92  private:
 93    ::pthread_mutex_t mtx;
 94
 95    pthread_mutex(const pthread_mutex &);
 96    void operator=(const pthread_mutex &);
 97
 98  public:
 99    pthread_mutex()
100    { ::pthread_mutex_init(&mtx, 0); }
101
102    ~pthread_mutex()
103    { ::pthread_mutex_destroy(&mtx); }
104
105    void lock()
106    { ::pthread_mutex_lock(&mtx); }
107
108    void unlock()
109    { ::pthread_mutex_unlock(&mtx); }
110};
111
112#endif // defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS)
113
114#endif // !defined(BOOST_NO_MT)
115
116class null_mutex
117{
118  private:
119    null_mutex(const null_mutex &);
120    void operator=(const null_mutex &);
121
122  public:
123    null_mutex() { }
124
125    static void lock() { }
126    static void unlock() { }
127};
128
129#if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
130  typedef null_mutex default_mutex;
131#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
132  typedef win32_mutex default_mutex;
133#elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
134  typedef pthread_mutex default_mutex;
135#endif
136
137} // namespace pool
138} // namespace details
139
140} // namespace boost
141
142#undef BOOST_MUTEX_HELPER_WIN32
143#undef BOOST_MUTEX_HELPER_PTHREAD
144#undef BOOST_MUTEX_HELPER_NONE
145#undef BOOST_MUTEX_HELPER
146
147#endif