]>
Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // gold-threads.h -- thread support for gold -*- C++ -*- |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
bae7f79e ILT |
23 | // gold can be configured to support threads. If threads are |
24 | // supported, the user can specify at runtime whether or not to | |
25 | // support them. This provides an interface to manage locking | |
26 | // accordingly. | |
27 | ||
28 | // Lock | |
29 | // A simple lock class. | |
30 | ||
31 | #ifndef GOLD_THREADS_H | |
32 | #define GOLD_THREADS_H | |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
bae7f79e ILT |
37 | class Condvar; |
38 | ||
c7912668 ILT |
39 | // The interface for the implementation of a Lock. |
40 | ||
41 | class Lock_impl | |
42 | { | |
43 | public: | |
44 | Lock_impl() | |
45 | { } | |
46 | ||
47 | virtual | |
48 | ~Lock_impl() | |
49 | { } | |
50 | ||
51 | virtual void | |
52 | acquire() = 0; | |
53 | ||
54 | virtual void | |
55 | release() = 0; | |
56 | }; | |
57 | ||
bae7f79e ILT |
58 | // A simple lock class. |
59 | ||
60 | class Lock | |
61 | { | |
62 | public: | |
63 | Lock(); | |
c7912668 | 64 | |
bae7f79e ILT |
65 | ~Lock(); |
66 | ||
67 | // Acquire the lock. | |
68 | void | |
c7912668 ILT |
69 | acquire() |
70 | { this->lock_->acquire(); } | |
bae7f79e ILT |
71 | |
72 | // Release the lock. | |
73 | void | |
c7912668 ILT |
74 | release() |
75 | { this->lock_->release(); } | |
bae7f79e ILT |
76 | |
77 | private: | |
78 | // This class can not be copied. | |
79 | Lock(const Lock&); | |
80 | Lock& operator=(const Lock&); | |
81 | ||
82 | friend class Condvar; | |
83 | Lock_impl* | |
84 | get_impl() const | |
85 | { return this->lock_; } | |
86 | ||
87 | Lock_impl* lock_; | |
88 | }; | |
89 | ||
90 | // RAII for Lock. | |
91 | ||
92 | class Hold_lock | |
93 | { | |
94 | public: | |
95 | Hold_lock(Lock& lock) | |
96 | : lock_(lock) | |
97 | { this->lock_.acquire(); } | |
98 | ||
99 | ~Hold_lock() | |
100 | { this->lock_.release(); } | |
101 | ||
102 | private: | |
103 | // This class can not be copied. | |
104 | Hold_lock(const Hold_lock&); | |
105 | Hold_lock& operator=(const Hold_lock&); | |
106 | ||
107 | Lock& lock_; | |
108 | }; | |
109 | ||
c7912668 ILT |
110 | // The interface for the implementation of a condition variable. |
111 | ||
112 | class Condvar_impl | |
113 | { | |
114 | public: | |
115 | Condvar_impl() | |
116 | { } | |
117 | ||
118 | virtual | |
119 | ~Condvar_impl() | |
120 | { } | |
121 | ||
122 | virtual void | |
123 | wait(Lock_impl*) = 0; | |
124 | ||
125 | virtual void | |
126 | signal() = 0; | |
127 | ||
128 | virtual void | |
129 | broadcast() = 0; | |
130 | }; | |
bae7f79e ILT |
131 | |
132 | // A simple condition variable class. It is always associated with a | |
133 | // specific lock. | |
134 | ||
135 | class Condvar | |
136 | { | |
137 | public: | |
138 | Condvar(Lock& lock); | |
139 | ~Condvar(); | |
140 | ||
141 | // Wait for the condition variable to be signalled. This should | |
142 | // only be called when the lock is held. | |
143 | void | |
c7912668 ILT |
144 | wait() |
145 | { this->condvar_->wait(this->lock_.get_impl()); } | |
146 | ||
147 | // Signal the condition variable--wake up at least one thread | |
148 | // waiting on the condition variable. This should only be called | |
149 | // when the lock is held. | |
150 | void | |
151 | signal() | |
152 | { this->condvar_->signal(); } | |
bae7f79e | 153 | |
c7912668 ILT |
154 | // Broadcast the condition variable--wake up all threads waiting on |
155 | // the condition variable. This should only be called when the lock | |
156 | // is held. | |
bae7f79e | 157 | void |
c7912668 ILT |
158 | broadcast() |
159 | { this->condvar_->broadcast(); } | |
bae7f79e ILT |
160 | |
161 | private: | |
162 | // This class can not be copied. | |
163 | Condvar(const Condvar&); | |
164 | Condvar& operator=(const Condvar&); | |
165 | ||
166 | Lock& lock_; | |
167 | Condvar_impl* condvar_; | |
168 | }; | |
169 | ||
170 | } // End namespace gold. | |
171 | ||
172 | #endif // !defined(GOLD_THREADS_H) |