00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 sync.h - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: sync.h 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00031 00032 #ifndef __SYNC_H__ 00033 #define __SYNC_H__ 00034 00035 #include "common.h" 00036 #include <pthread.h> 00037 00038 #define AUTOLOCK_DEFINE_ONLY() zmm::Ref<MutexAutolock> mutex_autolock; 00039 #define AUTOLOCK_NOLOCK(mutex) zmm::Ref<MutexAutolock> mutex_autolock = mutex->getAutolock(true); 00040 #define AUTOLOCK(mutex) zmm::Ref<MutexAutolock> mutex_autolock = mutex->getAutolock(); 00041 #define AUTOLOCK_NO_DEFINE(mutex) mutex_autolock = mutex->getAutolock(); 00042 #define AUTOUNLOCK() mutex_autolock->unlock(); 00043 #define AUTORELOCK() mutex_autolock->relock(); 00044 #define LOCK(mutex) mutex->lock(); 00045 #define UNLOCK(mutex) mutex->unlock(); 00046 00047 class Mutex; 00048 00049 class MutexAutolock : public zmm::Object 00050 { 00051 public: 00052 00053 #ifndef TOMBDEBUG 00054 inline ~MutexAutolock() { if (locked) pthread_mutex_unlock(pmutex); } 00055 inline void unlock() { pthread_mutex_unlock(pmutex); locked = false; } 00056 inline void relock() { pthread_mutex_lock(pmutex); locked = true; } 00057 #else 00058 ~MutexAutolock(); 00059 void unlock(); 00060 void relock(); 00061 #endif 00062 protected: 00063 MutexAutolock(zmm::Ref<Mutex> mutex, bool unlocked = false); 00064 zmm::Ref<Mutex> mutex; 00065 bool locked; 00066 #ifndef TOMBDEBUG 00067 pthread_mutex_t *pmutex; 00068 #endif 00069 00070 friend class Mutex; 00071 }; 00072 00073 class Mutex : public zmm::Object 00074 { 00075 public: 00076 Mutex(bool recursive = false); 00077 virtual ~Mutex(); 00078 #ifndef TOMBDEBUG 00079 inline void lock() { pthread_mutex_lock(&mutex_struct); } 00080 inline void unlock() { pthread_mutex_unlock(&mutex_struct); } 00081 #else 00082 void lock(); 00083 void unlock(bool autolock = false); 00084 #endif 00085 inline zmm::Ref<MutexAutolock> getAutolock(bool unlocked = false) { return zmm::Ref<MutexAutolock>(new MutexAutolock(zmm::Ref<Mutex>(this), unlocked)); } 00086 protected: 00087 pthread_mutex_t mutex_struct; 00088 inline pthread_mutex_t* getMutex() { return &mutex_struct; } 00089 00090 #ifdef TOMBDEBUG 00091 void errorExit(zmm::String error); 00092 inline pthread_t getLockingThread() { return locking_thread; } 00093 void doLock(bool cond); 00094 void doUnlock(bool cond); 00095 public: 00096 inline bool isLocked() { return lock_level > 0; } 00097 protected: 00098 inline void lockAutolock() { lock(); autolock = true; } 00099 void unlockAutolock(); 00100 int lock_level; 00101 bool recursive; 00102 bool autolock; 00103 pthread_t locking_thread; 00104 #endif 00105 00106 friend class MutexAutolock; 00107 friend class Cond; 00108 }; 00109 00110 class Cond : public zmm::Object 00111 { 00112 public: 00113 Cond(zmm::Ref<Mutex> mutex); 00114 virtual ~Cond(); 00115 inline void signal() { pthread_cond_signal(&cond_struct); } 00116 inline void broadcast() { pthread_cond_broadcast(&cond_struct); } 00117 #ifndef TOMBDEBUG 00118 inline void wait() { pthread_cond_wait(&cond_struct, mutex->getMutex()); } 00119 inline int timedwait(struct timespec *timeout) { return pthread_cond_timedwait(&cond_struct, mutex->getMutex(), timeout); } 00120 #else 00121 void wait(); 00122 int timedwait(struct timespec *timeout); 00123 //void checkwait(); 00124 #endif 00125 protected: 00126 pthread_cond_t cond_struct; 00127 zmm::Ref<Mutex> mutex; 00128 }; 00129 00130 #endif // __SYNC_H__
1.6.1