-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSynchronize.h
52 lines (41 loc) · 1.28 KB
/
Synchronize.h
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
#ifndef SynchronizeH
#define SynchronizeH
//---------------------------------------------------------------------------
template<typename LockObj>
class synchronize_lk
{
public:
explicit synchronize_lk( LockObj& obj ) : obj_( obj ) { obj_.Enter(); }
~synchronize_lk() /*nothrow*/ { obj_.Leave(); }
operator bool() const { return true; }
private:
LockObj& obj_;
};
template<typename LockObj>
class synchronize_read_lk
{
public:
explicit synchronize_read_lk( LockObj& obj ) : obj_( obj ) { obj_.EnterRead(); }
~synchronize_read_lk() /*nothrow*/ { obj_.LeaveRead(); }
operator bool() const { return true; }
private:
LockObj& obj_;
};
template<typename LockObj>
inline synchronize_lk<LockObj> make_lk_obj( LockObj& obj )
{
return synchronize_lk<LockObj>( obj );
}
template<typename LockObj>
inline synchronize_read_lk<LockObj> make_read_lk_obj( LockObj& obj )
{
return synchronize_read_lk<LockObj>( obj );
}
#define synchronize( o ) \
if ( bool const __lk_obj_inst_of__ = make_lk_obj( o ) )
#define synchronize_read( o ) \
if ( bool const __lk_obj_inst_of__ = make_read_lk_obj( o ) )
#define synchronize_write( o ) \
if ( bool const __lk_obj_inst_of__ = make_lk_obj( o ) )
//---------------------------------------------------------------------------
#endif