123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #ifndef EDK_CXXUTIL_RWLOCK_H_
- #define EDK_CXXUTIL_RWLOCK_H_
- #include <pthread.h>
- #include "cxxutil/noncopy.h"
- namespace edk {
- class RwLock : public NonCopyable {
- public:
-
- RwLock() { pthread_rwlock_init(&rwlock_, NULL); }
-
- ~RwLock() { pthread_rwlock_destroy(&rwlock_); }
-
- void WriteLock() { pthread_rwlock_wrlock(&rwlock_); }
-
- void ReadLock() { pthread_rwlock_rdlock(&rwlock_); }
-
- void Unlock() { pthread_rwlock_unlock(&rwlock_); }
- private:
- pthread_rwlock_t rwlock_;
- };
- class WriteLockGuard {
- public:
-
- explicit WriteLockGuard(RwLock& lock) : lock_(lock) { lock_.WriteLock(); }
-
- ~WriteLockGuard() { lock_.Unlock(); }
- private:
- RwLock& lock_;
- };
- class ReadLockGuard {
- public:
-
- explicit ReadLockGuard(RwLock& lock) : lock_(lock) { lock_.ReadLock(); }
-
- ~ReadLockGuard() { lock_.Unlock(); }
- private:
- RwLock& lock_;
- };
- }
- #endif
|