blob: 2fcbe72a81c8db31259fcdd1cc827f4b5295724f (
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
59
60
61
62
63
64
65
66
67
68
69
|
//===-- Support/Lock.h - Platform-agnostic mutual exclusion -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains classes that implement locks (mutual exclusion
// variables) in a platform-agnostic way. Basically the user should
// just call Lock::create() to get a Lock object of the correct sort
// for the current platform, and use its acquire() and release()
// methods, or a LockHolder, to protect critical sections of code for
// thread-safety.
//
//===----------------------------------------------------------------------===//
#ifndef SUPPORT_LOCK_H
#define SUPPORT_LOCK_H
#include <pthread.h>
#include <cstdlib>
namespace llvm {
/// Lock - Abstract class that provides mutual exclusion (also known
/// as a mutex.)
///
class Lock {
protected:
virtual ~Lock() {} // Derive from me
public:
virtual void acquire () { abort (); }
virtual void release () { abort (); }
/// create - Static method that returns a Lock of the correct class
/// for the current host OS.
///
static Lock create ();
};
/// POSIXLock - Specialization of Lock class implemented using
/// pthread_mutex_t objects.
///
class POSIXLock : public Lock {
pthread_mutex_t mutex;
public:
POSIXLock () { pthread_mutex_init (&mutex, 0); }
virtual ~POSIXLock () { pthread_mutex_destroy (&mutex); }
virtual void acquire () { pthread_mutex_lock (&mutex); }
virtual void release () { pthread_mutex_unlock (&mutex); }
};
/// LockHolder - Instances of this class acquire a given Lock when
/// constructed and hold that lock until destruction. Uncle Bjarne
/// says, "Resource acquisition is allocation." Or is it the other way
/// around? I never can remember.
///
class LockHolder {
Lock &L;
public:
LockHolder (Lock &_L) : L (_L) { L.acquire (); }
~LockHolder () { L.release (); }
};
} // end namespace llvm
#endif // SUPPORT_LOCK_H
|