Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ObjLock Class Reference

Manage locks to a resource (file). Manage locks to a resource (file). You can use a shared or a exclusive lock in blocked or non-blocked operation. More...

#include <ObjLock.h>

List of all members.

Public Types

enum  { locked, not_locked }

Public Member Functions

 ObjLock (const string &name)
 ~ObjLock ()
 Destructor.

void lockShBlk (void)
int lockShNoBlk (void)
void lockExBlk (void)
int lockExNoBlk (void)
void unlock (void)

Private Attributes

string name
int fd
flock l


Detailed Description

Manage locks to a resource (file). Manage locks to a resource (file). You can use a shared or a exclusive lock in blocked or non-blocked operation.

Author:
Herbert Straub
Date:
2003

Definition at line 32 of file ObjLock.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
locked 
not_locked 

Definition at line 42 of file ObjLock.h.

00042 { locked, not_locked };


Constructor & Destructor Documentation

ObjLock::ObjLock const string &  name  ) 
 

Constructor

Parameters:
name of Resource
Exceptions:
SystemError Error creating Lock File

Definition at line 28 of file ObjLock.cc.

References fd, and l.

00029 {
00030         this->name = name;
00031 
00032         fd = open(name.c_str(), O_RDWR | O_CREAT | O_NONBLOCK,
00033                   S_IWUSR | S_IRUSR);
00034         if (fd == -1)
00035                 throw(SystemError("Error in ObjLock constructor (open)"),
00036                       errno);
00037 #ifndef USE_FLOCK
00038         memset((void *) &l, 0, sizeof(struct flock));
00039         l.l_whence = SEEK_SET;
00040 #endif
00041 }

ObjLock::~ObjLock  ) 
 

Destructor.

Definition at line 43 of file ObjLock.cc.

References fd, and unlock().

00044 {
00045         unlock();
00046         close(fd);
00047 }


Member Function Documentation

void ObjLock::lockExBlk void   ) 
 

Lock Exclusive Blocked

Exceptions:
SystemError Error in flock or fcntl

Definition at line 81 of file ObjLock.cc.

References ERROR_LOCATION, fd, and l.

Referenced by ArtSpooler::getSpooledArt(), main(), ArtSpooler::spoolArt(), and ArtSpooler::storeBadArt().

00082 {
00083 #ifdef USE_FLOCK
00084         if (flock(fd, LOCK_EX) == -1)
00085                 throw(SystemError("Error in flock", errno,
00086                                   ERROR_LOCATION));
00087 #else
00088         l.l_type = F_WRLCK;
00089         if (fcntl(fd, F_SETLKW, &l) == -1)
00090                 throw(SystemError("Error in fcntl", errno,
00091                                   ERROR_LOCATION));
00092 #endif
00093 }

int ObjLock::lockExNoBlk void   ) 
 

Lock Exclusive Noblocked

Returns:
locked of not_locked
Exceptions:
SystemError Error in flock or fcntl

Definition at line 95 of file ObjLock.cc.

References ERROR_LOCATION, fd, l, locked, and not_locked.

Referenced by main().

00096 {
00097         int s;
00098 
00099 #ifdef USE_FLOCK
00100         s = flock(fd, LOCK_EX | LOCK_NB);
00101 #else
00102         l.l_type = F_WRLCK;
00103         s = fcntl(fd, F_SETLK, &l);
00104 #endif
00105         if (s == 0) {
00106                 return locked;
00107         } else if (errno == EWOULDBLOCK || errno == EAGAIN) {
00108                 return not_locked;
00109         } else {
00110                 throw(SystemError("Error in flock or fcntl", errno,
00111                                   ERROR_LOCATION));
00112         }
00113 }

void ObjLock::lockShBlk void   ) 
 

Lock Shared Block

Exceptions:
SystemError Error in flock or fcntl

Definition at line 49 of file ObjLock.cc.

References fd, and l.

Referenced by main().

00050 {
00051 #ifdef USE_FLOCK
00052         if (flock(fd, LOCK_SH) == -1)
00053                 throw(SystemError("Error flock", errno));
00054 #else
00055         l.l_type = F_RDLCK;
00056         if (fcntl(fd, F_SETLKW, &l) == -1)
00057                 throw(SystemError("Error fcntl", errno));
00058 #endif
00059 }

int ObjLock::lockShNoBlk void   ) 
 

Lock Shared Noblocked

Returns:
locked or not_locked
Exceptions:
SystemError Error in flock or fcntl

Definition at line 61 of file ObjLock.cc.

References ERROR_LOCATION, fd, l, locked, and not_locked.

00062 {
00063         int s;
00064 
00065 #ifdef USE_FLOCK
00066         s = flock(fd, LOCK_SH | LOCK_NB);
00067 #else
00068         l.l_type = F_RDLCK;
00069         s = fcntl(fd, F_SETLK, &l);
00070 #endif
00071         if (s == 0) {
00072                 return locked;
00073         } else if (errno == EWOULDBLOCK) {
00074                 return not_locked;
00075         } else {
00076                 throw(SystemError("Error in lockShNoBlk", errno,
00077                                   ERROR_LOCATION));
00078         }
00079 }

void ObjLock::unlock void   ) 
 

Unlock

Exceptions:
SystemError Error in flock or fcntl

Definition at line 115 of file ObjLock.cc.

References ERROR_LOCATION, fd, and l.

Referenced by ArtSpooler::getSpooledArt(), main(), ArtSpooler::spoolArt(), ArtSpooler::storeBadArt(), and ~ObjLock().

00116 {
00117 #ifdef USE_FLOCK
00118         if (flock(fd, LOCK_UN) == -1)
00119                 throw(SystemError("Error in flock", errno,
00120                                   ERROR_LOCATION));
00121 #else
00122         l.l_type = F_UNLCK;
00123         if (fcntl(fd, F_SETLKW, &l) == -1)
00124                 throw(SystemError("Error in fcntl", errno,
00125                                   ERROR_LOCATION));
00126 #endif
00127 }


Member Data Documentation

int ObjLock::fd [private]
 

Definition at line 68 of file ObjLock.h.

Referenced by lockExBlk(), lockExNoBlk(), lockShBlk(), lockShNoBlk(), ObjLock(), unlock(), and ~ObjLock().

struct flock ObjLock::l [private]
 

Definition at line 70 of file ObjLock.h.

Referenced by lockExBlk(), lockExNoBlk(), lockShBlk(), lockShNoBlk(), ObjLock(), and unlock().

string ObjLock::name [private]
 

Definition at line 67 of file ObjLock.h.


The documentation for this class was generated from the following files:
Generated on Fri Aug 20 10:58:12 2004 for NewsCache by doxygen 1.3.6-20040222