Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

Locker.h

Go to the documentation of this file.
00001 /********************************************************************
00002 
00003 Copyright 2006, ACCESS Systems Americas, Inc. All rights reserved.
00004 
00005 The contents of this file are subject to the Mozilla Public License Version
00006 1.1 (the "License"); you may not use this file except in compliance with
00007 the License. You may obtain a copy of the License at
00008 http://www.mozilla.org/MPL/
00009 
00010 Software distributed under the License is distributed on an "AS IS" basis,
00011 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012 for the specific language governing rights and limitations under the
00013 License.
00014 
00015 The Original Code is the entire contents of this file.
00016 
00017 The Initial Developer of the Original Code is ACCESS Systems Americas, Inc.
00018 
00019 Portions created by ACCESS Systems Americas, Inc. are Copyright © 2006. All
00020 Rights Reserved.
00021 
00022 Contributor(s): none.
00023 
00024 ********************************************************************/
00032 #ifndef _ALP_LOCKER_H
00033 #define _ALP_LOCKER_H
00034 
00035 #include <pthread.h>
00036 
00037 namespace alp {
00038 
00039 #ifdef LOCK_DEBUG
00040 #include <stdio.h>
00041 #define DEADLOCK_TRACE(x) x
00042 #else
00043 #define DEADLOCK_TRACE(x)
00044 #endif
00045 
00046 
00047 class Locker 
00048 {
00049         public:
00050                 Locker(bool _recursive = true)
00051                 { 
00052                         if( true == _recursive )
00053                         {
00054                                 pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
00055                                 fLock = lock;
00056                         }
00057                         else
00058                         {
00059                                 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
00060                                 fLock = lock;
00061                         }
00062                 }       
00063                 
00064                 ~Locker() { Unlock(); pthread_mutex_destroy(&fLock); }
00065                 
00066         bool Lock() 
00067         { 
00068                 DEADLOCK_TRACE( fprintf(stderr, "%s: %p [%p]", __PRETTY_FUNCTION__, this, pthread_self()); );
00069                 return 0 == pthread_mutex_lock(&fLock); 
00070         }
00071         
00072         void Unlock() 
00073         { 
00074                 pthread_mutex_unlock(&fLock); 
00075                 DEADLOCK_TRACE( fprintf(stderr, "%s: %p [%p]", __PRETTY_FUNCTION__, this, pthread_self()) ); 
00076         }
00077         
00078         private:
00079                 pthread_mutex_t fLock;
00080 };
00081 
00082 // --------------------------------------------------------------
00083 class Autolock
00084 {
00085         public:
00086                                 Autolock(Locker& _lock) : fLock(_lock) { fLock.Lock(); }
00087                                 ~Autolock() { fLock.Unlock(); }
00088                 
00089         private:
00090                 Locker& fLock;
00091 };
00092 
00093 }       // namespace alp
00094 
00095 #endif

Generated on Sat Dec 16 20:29:47 2006 for hiker-0.9 by  doxygen 1.4.4