1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// File: lock.hpp
//
// Copyright (c) 2002,2003,2011
// Kevin Atkinson
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. Kevin Atkinson makes no
// representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
#ifndef DISTRIBNET_LOCK__HPP
#define DISTRIBNET_LOCK__HPP
#include <assert.h>
#include "settings.h"
#ifdef USE_POSIX_MUTEX
# include <pthread.h>
#endif
namespace acommon {
#define LOCK(l) const Lock the_lock(l);
#ifdef USE_POSIX_MUTEX
class Mutex {
pthread_mutex_t l_;
private:
Mutex(const Mutex &);
void operator=(const Mutex &);
public:
Mutex() {pthread_mutex_init(&l_, 0);}
~Mutex() {pthread_mutex_destroy(&l_);}
void lock() {pthread_mutex_lock(&l_);}
void unlock() {pthread_mutex_unlock(&l_);}
};
#else
class Mutex {
private:
Mutex(const Mutex &);
void operator=(const Mutex &);
public:
Mutex() {}
~Mutex() {}
void lock() {}
void unlock() {}
};
#endif
class Lock {
private:
Lock(const Lock &);
void operator= (const Lock &);
Mutex * lock_;
public:
Lock(Mutex * l) : lock_(l) {if (lock_) lock_->lock();}
void set(Mutex * l) {assert(!lock_); lock_ = l; if (lock_) lock_->lock();}
void release() {if (lock_) lock_->unlock(); lock_ = NULL;}
~Lock() {if (lock_) lock_->unlock();}
};
};
#endif
|