Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / binutils-2.20 / gold / gold-threads.h
1 // gold-threads.h -- thread support for gold  -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
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
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
37 class Condvar;
38 class Initialize_lock_once;
39
40 // The interface for the implementation of a Lock.
41
42 class Lock_impl
43 {
44  public:
45   Lock_impl()
46   { }
47
48   virtual
49   ~Lock_impl()
50   { }
51
52   virtual void
53   acquire() = 0;
54
55   virtual void
56   release() = 0;
57 };
58
59 // A simple lock class.
60
61 class Lock
62 {
63  public:
64   Lock();
65
66   ~Lock();
67
68   // Acquire the lock.
69   void
70   acquire()
71   { this->lock_->acquire(); }
72
73   // Release the lock.
74   void
75   release()
76   { this->lock_->release(); }
77
78  private:
79   // This class can not be copied.
80   Lock(const Lock&);
81   Lock& operator=(const Lock&);
82
83   friend class Condvar;
84   Lock_impl*
85   get_impl() const
86   { return this->lock_; }
87
88   Lock_impl* lock_;
89 };
90
91 // RAII for Lock.
92
93 class Hold_lock
94 {
95  public:
96   Hold_lock(Lock& lock)
97     : lock_(lock)
98   { this->lock_.acquire(); }
99
100   ~Hold_lock()
101   { this->lock_.release(); }
102
103  private:
104   // This class can not be copied.
105   Hold_lock(const Hold_lock&);
106   Hold_lock& operator=(const Hold_lock&);
107
108   Lock& lock_;
109 };
110
111 class Hold_optional_lock
112 {
113  public:
114   Hold_optional_lock(Lock* lock)
115     : lock_(lock)
116   {
117     if (this->lock_ != NULL)
118       this->lock_->acquire();
119   }
120
121   ~Hold_optional_lock()
122   {
123     if (this->lock_ != NULL)
124       this->lock_->release();
125   }
126
127  private:
128   Hold_optional_lock(const Hold_optional_lock&);
129   Hold_optional_lock& operator=(const Hold_optional_lock&);
130
131   Lock* lock_;
132 };
133
134 // The interface for the implementation of a condition variable.
135
136 class Condvar_impl
137 {
138  public:
139   Condvar_impl()
140   { }
141
142   virtual
143   ~Condvar_impl()
144   { }
145
146   virtual void
147   wait(Lock_impl*) = 0;
148
149   virtual void
150   signal() = 0;
151
152   virtual void
153   broadcast() = 0;
154 };
155
156 // A simple condition variable class.  It is always associated with a
157 // specific lock.
158
159 class Condvar
160 {
161  public:
162   Condvar(Lock& lock);
163   ~Condvar();
164
165   // Wait for the condition variable to be signalled.  This should
166   // only be called when the lock is held.
167   void
168   wait()
169   { this->condvar_->wait(this->lock_.get_impl()); }
170
171   // Signal the condition variable--wake up at least one thread
172   // waiting on the condition variable.  This should only be called
173   // when the lock is held.
174   void
175   signal()
176   { this->condvar_->signal(); }
177
178   // Broadcast the condition variable--wake up all threads waiting on
179   // the condition variable.  This should only be called when the lock
180   // is held.
181   void
182   broadcast()
183   { this->condvar_->broadcast(); }
184
185  private:
186   // This class can not be copied.
187   Condvar(const Condvar&);
188   Condvar& operator=(const Condvar&);
189
190   Lock& lock_;
191   Condvar_impl* condvar_;
192 };
193
194 // A class used to initialize a lock exactly once, after the options
195 // have been read.  This is needed because the implementation of locks
196 // depends on whether we've seen the --threads option.  Before the
197 // options have been read, we know we are single-threaded, so we can
198 // get by without using a lock.  This class should be an instance
199 // variable of the class which has a lock which needs to be
200 // initialized.
201
202 class Initialize_lock
203 {
204  public:
205   // The class which uses this will have a pointer to a lock.  This
206   // must be constructed with a pointer to that pointer.
207   Initialize_lock(Lock** pplock);
208
209   // Initialize the lock.  Return true if the lock is now initialized,
210   // false if it is not (because the options have not yet been read).
211   bool
212   initialize();
213
214  private:
215   // A pointer to the lock pointer which must be initialized.
216   Lock** const pplock_;
217   // If needed, a pointer to a pthread_once_t structure.
218   Initialize_lock_once* once_;
219 };
220
221 } // End namespace gold.
222
223 #endif // !defined(GOLD_THREADS_H)