Scopira 20080306

mutex_pthreads.h

00001 
00002 /*
00003  *  Copyright (c) 2001-2004    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED_SCOPIRA_TOOL_MUTEX_PTHREADS_H__
00015 #define __INCLUDED_SCOPIRA_TOOL_MUTEX_PTHREADS_H__
00016 
00017 #include <scopira/tool/platform.h>
00018 #include <scopira/tool/export.h>
00019 
00020 #include <pthread.h>
00021 
00022 #include <errno.h>
00023 
00024 namespace scopira
00025 {
00026   namespace tool
00027   {
00028     class mutex;
00029     class locker;
00030   }
00031 }
00032 
00040 class scopira::tool::mutex
00041 {
00042   protected:
00043     pthread_mutex_t dm_mut;
00044 
00045   public:
00047     SCOPIRA_EXPORT mutex(void);
00048     SCOPIRA_EXPORT ~mutex(void);
00049 
00055     SCOPIRA_EXPORT void lock(void) { pthread_mutex_lock(&dm_mut); }
00056 
00063     SCOPIRA_EXPORT void unlock(void) { pthread_mutex_unlock(&dm_mut); }
00064 
00075     SCOPIRA_EXPORT bool try_lock(void) { return EBUSY != pthread_mutex_trylock(&dm_mut); }
00076 
00081     SCOPIRA_EXPORT inline pthread_mutex_t * get_os_mutex(void) { return &dm_mut; }
00082 };
00083 
00092 class scopira::tool::locker
00093 {
00094   private:
00095     mutex &dm_mut;
00096   public:
00101     locker(mutex &mut) : dm_mut(mut) { dm_mut.lock(); }
00106     ~locker() { dm_mut.unlock(); }
00107 };
00108 
00109 #endif
00110