mutex.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 
00033 #pragma once
00034 
00035 
00036 #include "../api_core.h"
00037 
00038 #ifdef WIN32
00039 #include <windows.h>
00040 #else
00041 #ifndef __USE_UNIX98
00042 #define __USE_UNIX98
00043 #endif
00044 #include <pthread.h>
00045 #endif
00046 
00050 class CL_API_CORE CL_Mutex
00051 {
00054 
00055 public:
00057         CL_Mutex();
00058 
00059         ~CL_Mutex();
00060 
00061 
00065 
00066 public:
00067 
00068 
00072 
00073 public:
00075         void lock();
00076 
00078         bool try_lock();
00079 
00081         void unlock();
00082 
00083 
00087 
00088 private:
00089 #ifdef WIN32
00090         CRITICAL_SECTION critical_section;
00091 #else
00092         pthread_mutex_t handle;
00093 #endif
00094 
00095 };
00096 
00100 class CL_API_CORE CL_MutexSection
00101 {
00104 
00105 public:
00107         CL_MutexSection(CL_Mutex *mutex, bool lock_mutex = true)
00108         : mutex(mutex), lock_count(0)
00109         {
00110                 if (lock_mutex)
00111                         lock();
00112         }
00113 
00114         ~CL_MutexSection()
00115         {
00116                 if (lock_count > 0 && mutex)
00117                         mutex->unlock();
00118                 lock_count = 0;
00119         }
00120 
00121 
00125 
00126 public:
00128         int get_lock_count() const
00129         {
00130                 return lock_count;
00131         }
00132 
00133 
00137 
00138 public:
00140         void lock()
00141         {
00142                 if (mutex)
00143                         mutex->lock();
00144                 lock_count++;
00145         }
00146 
00148         bool try_lock()
00149         {
00150                 if (mutex == 0 || mutex->try_lock())
00151                 {
00152                         lock_count++;
00153                         return true;
00154                 }
00155                 return false;
00156         }
00157 
00159         void unlock()
00160         {
00161                 if (lock_count <= 0)
00162                         return;
00163 
00164                 if (mutex)
00165                         mutex->unlock();
00166                 lock_count--;
00167         }
00168 
00169 
00173 
00174 private:
00175         CL_Mutex *mutex;
00176 
00177         int lock_count;
00179 };
00180 
00181