Főoldal | Névtérlista | Osztályhierarchia | Betűrendes lista | Adatszerkezetek | Könyvtárak | Fájllista | Névtértagok | Adatmezők | Globális elemek

Locking.h

Ugrás a fájl dokumentációjához.
00001 /*
00002     Locking
00003     Copyright (C) 2006  giszo
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 #ifndef LOCKING_H
00020 #define LOCKING_H
00021 #ifndef _WIN32
00022 #include <pthread.h>
00023 #endif
00024 
00025 class CriticalSection {
00026 #ifdef _WIN32
00027   public:
00028     void enter() throw() {
00029       EnterCriticalSection( &cs );
00030     }
00031 
00032     void leave() throw() {
00033       LeaveCriticalSection( &cs );
00034     }
00035 
00036     CriticalSection() throw() {
00037       InitializeCriticalSection( &cs );
00038     }
00039 
00040     ~CriticalSection() throw() {
00041       DeleteCriticalSection( &cs );
00042     }
00043 
00044   private:
00045     CRITICAL_SECTION cs;
00046 #else
00047   public:
00048     CriticalSection() throw() {
00049       pthread_mutexattr_init( &mtx_attr );
00050       pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
00051 
00052       pthread_mutex_init( &mtx, &mtx_attr );
00053     };
00054     ~CriticalSection() throw() {
00055       pthread_mutex_destroy( &mtx );
00056       pthread_mutexattr_destroy( &mtx_attr );
00057     };
00058     void enter() throw() { pthread_mutex_lock( &mtx ); };
00059     void leave() throw() { pthread_mutex_unlock( &mtx ); };
00060     pthread_mutex_t& getMutex() { return mtx; };
00061 
00062   private:
00063     pthread_mutex_t mtx;
00064     pthread_mutexattr_t mtx_attr;
00065 #endif
00066 };
00067 
00068 template<class T>
00069 class LockBase {
00070   public:
00071     LockBase( T& aCs ) throw() : cs( aCs )  { cs.enter(); };
00072     ~LockBase() throw() { cs.leave(); };
00073 
00074   private:
00075     T& cs;
00076 };
00077 
00078 typedef LockBase<CriticalSection> Lock;
00079 
00080 #endif

SourceForge.netLogo