00001 #ifndef EDG_WORKLOAD_COMMON_UTILITIES_FILELOCKER_H
00002 #define EDG_WORKLOAD_COMMON_UTILITIES_FILELOCKER_H
00003
00004 #include <cstdio>
00005
00006 #include <string>
00007
00008 #include <boost/thread/mutex.hpp>
00009
00010 #include "edg/workload/common/common_namespace.h"
00011
00012 #include "fstreamlock.h"
00013
00014 COMMON_NAMESPACE_BEGIN {
00015
00016 namespace utilities {
00017
00018 class CannotOpenLockFile : public std::string {
00019 public:
00020 CannotOpenLockFile( int );
00021
00022 ~CannotOpenLockFile( void );
00023 };
00024
00025 class FileMutexLocked {};
00026
00027 class FileLocker;
00028
00029 class FileMutex {
00030 friend class FileLocker;
00031
00032 public:
00033 FileMutex( const std::string &filename );
00034
00035 ~FileMutex( void );
00036
00037 private:
00038 FileMutex( const FileMutex &fm );
00039 FileMutex &operator=( const FileMutex &fm );
00040
00041 bool fm_locked;
00042 int fm_descriptor;
00043 boost::mutex fm_mutex;
00044 };
00045
00046 class FileLocker {
00047 public:
00048 FileLocker( FileMutex &fm, bool lock = true );
00049
00050 ~FileLocker();
00051
00052 int lock( void );
00053 int unlock( void );
00054
00055 private:
00056 FileLocker( void );
00057 FileLocker( const FileLocker &fl );
00058 FileLocker &operator=( const FileLocker &fl );
00059
00060 bool *fl_locked;
00061 DescriptorLock fl_filelock;
00062 boost::mutex::scoped_lock fl_mutexlock;
00063 };
00064
00065 };
00066
00067 } COMMON_NAMESPACE_END;
00068
00069 #endif
00070
00071
00072
00073