blob: 1ecf3fbbb1e2473344b2aa51eeb876ea31ae1f38 (
plain)
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
|
// file : cult/sched/lock.hxx
// author : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
#ifndef CULT_SCHED_LOCK_HXX
#define CULT_SCHED_LOCK_HXX
#include <cult/types/fundamental.hxx>
namespace Cult
{
namespace Sched
{
class Lock: public NonCopyable
{
public:
~Lock ()
{
unlock ();
}
template <typename X>
Lock (X& x)
: x_ (reinterpret_cast<Void*>(&x)),
unlock_ (&unlock<X>),
locked_ (true)
{
x.lock ();
}
Void
unlock ()
{
if (locked_)
{
unlock_ (x_);
locked_ = false;
}
}
private:
template <typename X>
static Void
unlock (Void* p)
{
reinterpret_cast<X*> (p)->unlock ();
}
private:
Void* x_;
Void (*unlock_) (Void*);
Boolean locked_;
};
}
}
#endif // CULT_SCHED_LOCK_HXX
|