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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
// token.h -- lock tokens for gold -*- C++ -*-
// Copyright (C) 2006-2018 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#ifndef GOLD_TOKEN_H
#define GOLD_TOKEN_H
namespace gold
{
class Condvar;
class Task;
// A list of Tasks, managed through the next_locked_ field in the
// class Task. We define this class here because we need it in
// Task_token.
class Task_list
{
public:
Task_list()
: head_(NULL), tail_(NULL)
{ }
~Task_list()
{ gold_assert(this->head_ == NULL && this->tail_ == NULL); }
// Return whether the list is empty.
bool
empty() const
{ return this->head_ == NULL; }
// Add T to the head of the list.
void
push_front(Task* t);
// Add T to the end of the list.
void
push_back(Task* t);
// Remove the first Task on the list and return it. Return NULL if
// the list is empty.
Task*
pop_front();
private:
// The start of the list. NULL if the list is empty.
Task* head_;
// The end of the list. NULL if the list is empty.
Task* tail_;
};
// We support two basic types of locks, which are both implemented
// using the single class Task_token.
// A write lock may be held by a single Task at a time. This is used
// to control access to a single shared resource such as an Object.
// A blocker is used to indicate that a Task A must be run after some
// set of Tasks B. For each of the Tasks B, we increment the blocker
// when the Task is created, and decrement it when the Task is
// completed. When the count goes to 0, the task A is ready to run.
// There are no shared read locks. We always read and write objects
// in predictable patterns. The purpose of the locks is to permit
// some flexibility for the threading system, for cases where the
// execution order does not matter.
// These tokens are only manipulated when the workqueue lock is held
// or when they are first created. They do not require any locking
// themselves.
class Task_token
{
public:
Task_token(bool is_blocker)
: is_blocker_(is_blocker), blockers_(0), writer_(NULL), waiting_()
{ }
~Task_token()
{
gold_assert(this->blockers_ == 0);
gold_assert(this->writer_ == NULL);
}
// Return whether this is a blocker.
bool
is_blocker() const
{ return this->is_blocker_; }
// A write lock token uses these methods.
// Is the token writable?
bool
is_writable() const
{
gold_assert(!this->is_blocker_);
return this->writer_ == NULL;
}
// Add the task as the token's writer (there may only be one
// writer).
void
add_writer(const Task* t)
{
gold_assert(!this->is_blocker_ && this->writer_ == NULL);
this->writer_ = t;
}
// Remove the task as the token's writer.
void
remove_writer(const Task* t)
{
gold_assert(!this->is_blocker_ && this->writer_ == t);
this->writer_ = NULL;
}
// A blocker token uses these methods.
// Add a blocker to the token.
void
add_blocker()
{
gold_assert(this->is_blocker_);
++this->blockers_;
this->writer_ = NULL;
}
// Add some number of blockers to the token.
void
add_blockers(int c)
{
gold_assert(this->is_blocker_);
this->blockers_ += c;
this->writer_ = NULL;
}
// Remove a blocker from the token. Returns true if block count
// drops to zero.
bool
remove_blocker()
{
gold_assert(this->is_blocker_ && this->blockers_ > 0);
--this->blockers_;
this->writer_ = NULL;
return this->blockers_ == 0;
}
// Is the token currently blocked?
bool
is_blocked() const
{
gold_assert(this->is_blocker_);
return this->blockers_ > 0;
}
// Both blocker and write lock tokens use these methods.
// Add T to the list of tasks waiting for this token to be released.
void
add_waiting(Task* t)
{ this->waiting_.push_back(t); }
// Add T to the front of the list of tasks waiting for this token to
// be released.
void
add_waiting_front(Task* t)
{ this->waiting_.push_front(t); }
// Remove the first Task waiting for this token to be released, and
// return it. Return NULL if no Tasks are waiting.
Task*
remove_first_waiting()
{ return this->waiting_.pop_front(); }
private:
// It makes no sense to copy these.
Task_token(const Task_token&);
Task_token& operator=(const Task_token&);
// Whether this is a blocker token.
bool is_blocker_;
// The number of blockers.
int blockers_;
// The single writer.
const Task* writer_;
// The list of Tasks waiting for this token to be released.
Task_list waiting_;
};
// In order to support tokens more reliably, we provide objects which
// handle them using RAII.
// RAII class to get a write lock on a token. This requires
// specifying the task which is doing the lock.
class Task_write_token
{
public:
Task_write_token(Task_token* token, const Task* task)
: token_(token), task_(task)
{ this->token_->add_writer(this->task_); }
~Task_write_token()
{ this->token_->remove_writer(this->task_); }
private:
Task_write_token(const Task_write_token&);
Task_write_token& operator=(const Task_write_token&);
Task_token* token_;
const Task* task_;
};
// RAII class for a blocker.
class Task_block_token
{
public:
// The blocker count must be incremented when the task is created.
// This object is created when the task is run, so we don't do
// anything in the constructor.
Task_block_token(Task_token* token)
: token_(token)
{ gold_assert(this->token_->is_blocked()); }
~Task_block_token()
{ this->token_->remove_blocker(); }
private:
Task_block_token(const Task_block_token&);
Task_block_token& operator=(const Task_block_token&);
Task_token* token_;
};
// An object which implements an RAII lock for any object which
// supports lock and unlock methods.
template<typename Obj>
class Task_lock_obj
{
public:
Task_lock_obj(const Task* task, Obj* obj)
: task_(task), obj_(obj)
{ this->obj_->lock(task); }
~Task_lock_obj()
{ this->obj_->unlock(this->task_); }
private:
Task_lock_obj(const Task_lock_obj&);
Task_lock_obj& operator=(const Task_lock_obj&);
const Task* task_;
Obj* obj_;
};
// A class which holds the set of Task_tokens which must be locked for
// a Task. No Task requires more than four Task_tokens, so we set
// that as a limit.
class Task_locker
{
public:
static const int max_task_count = 4;
Task_locker()
: count_(0)
{ }
~Task_locker()
{ }
// Clear the locker.
void
clear()
{ this->count_ = 0; }
// Add a token to the locker.
void
add(Task* t, Task_token* token)
{
gold_assert(this->count_ < max_task_count);
this->tokens_[this->count_] = token;
++this->count_;
// A blocker will have been incremented when the task is created.
// A writer we need to lock now.
if (!token->is_blocker())
token->add_writer(t);
}
// Iterate over the tokens.
typedef Task_token** iterator;
iterator
begin()
{ return &this->tokens_[0]; }
iterator
end()
{ return &this->tokens_[this->count_]; }
private:
Task_locker(const Task_locker&);
Task_locker& operator=(const Task_locker&);
// The number of tokens.
int count_;
// The tokens.
Task_token* tokens_[max_task_count];
};
} // End namespace gold.
#endif // !defined(GOLD_TOKEN_H)
|