Imported from 6.824 labs
[invirt/third/libt4.git] / rpc / slock.h
1 #ifndef __SCOPED_LOCK__
2 #define __SCOPED_LOCK__
3
4 #include <pthread.h>
5 #include "lang/verify.h"
6 struct ScopedLock {
7         private:
8                 pthread_mutex_t *m_;
9         public:
10                 ScopedLock(pthread_mutex_t *m): m_(m) {
11                         VERIFY(pthread_mutex_lock(m_)==0);
12                 }
13                 ~ScopedLock() {
14                         VERIFY(pthread_mutex_unlock(m_)==0);
15                 }
16 };
17 struct ScopedUnlock {
18         private:
19                 pthread_mutex_t *m_;
20         public:
21                 ScopedUnlock(pthread_mutex_t *m): m_(m) {
22                         VERIFY(pthread_mutex_unlock(m_)==0);
23                 }
24                 ~ScopedUnlock() {
25                         VERIFY(pthread_mutex_lock(m_)==0);
26                 }
27 };
28 #endif  /*__SCOPED_LOCK__*/