1 // token.h -- lock tokens for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
32 // A list of Tasks, managed through the next_locked_ field in the
33 // class Task. We define this class here because we need it in
40 : head_(NULL
), tail_(NULL
)
44 { gold_assert(this->head_
== NULL
&& this->tail_
== NULL
); }
46 // Return whether the list is empty.
49 { return this->head_
== NULL
; }
51 // Add T to the head of the list.
55 // Add T to the end of the list.
59 // Remove the first Task on the list and return it. Return NULL if
65 // The start of the list. NULL if the list is empty.
67 // The end of the list. NULL if the list is empty.
71 // We support two basic types of locks, which are both implemented
72 // using the single class Task_token.
74 // A write lock may be held by a single Task at a time. This is used
75 // to control access to a single shared resource such as an Object.
77 // A blocker is used to indicate that a Task A must be run after some
78 // set of Tasks B. For each of the Tasks B, we increment the blocker
79 // when the Task is created, and decrement it when the Task is
80 // completed. When the count goes to 0, the task A is ready to run.
82 // There are no shared read locks. We always read and write objects
83 // in predictable patterns. The purpose of the locks is to permit
84 // some flexibility for the threading system, for cases where the
85 // execution order does not matter.
87 // These tokens are only manipulated when the workqueue lock is held
88 // or when they are first created. They do not require any locking
94 Task_token(bool is_blocker
)
95 : is_blocker_(is_blocker
), blockers_(0), writer_(NULL
), waiting_()
100 gold_assert(this->blockers_
== 0);
101 gold_assert(this->writer_
== NULL
);
104 // Return whether this is a blocker.
107 { return this->is_blocker_
; }
109 // A write lock token uses these methods.
111 // Is the token writable?
115 gold_assert(!this->is_blocker_
);
116 return this->writer_
== NULL
;
119 // Add the task as the token's writer (there may only be one
122 add_writer(const Task
* t
)
124 gold_assert(!this->is_blocker_
&& this->writer_
== NULL
);
128 // Remove the task as the token's writer.
130 remove_writer(const Task
* t
)
132 gold_assert(!this->is_blocker_
&& this->writer_
== t
);
133 this->writer_
= NULL
;
136 // A blocker token uses these methods.
138 // Add a blocker to the token.
142 gold_assert(this->is_blocker_
);
144 this->writer_
= NULL
;
147 // Add some number of blockers to the token.
151 gold_assert(this->is_blocker_
);
152 this->blockers_
+= c
;
153 this->writer_
= NULL
;
156 // Remove a blocker from the token. Returns true if block count
161 gold_assert(this->is_blocker_
&& this->blockers_
> 0);
163 this->writer_
= NULL
;
164 return this->blockers_
== 0;
167 // Is the token currently blocked?
171 gold_assert(this->is_blocker_
);
172 return this->blockers_
> 0;
175 // Both blocker and write lock tokens use these methods.
177 // Add T to the list of tasks waiting for this token to be released.
180 { this->waiting_
.push_back(t
); }
182 // Add T to the front of the list of tasks waiting for this token to
185 add_waiting_front(Task
* t
)
186 { this->waiting_
.push_front(t
); }
188 // Remove the first Task waiting for this token to be released, and
189 // return it. Return NULL if no Tasks are waiting.
191 remove_first_waiting()
192 { return this->waiting_
.pop_front(); }
195 // It makes no sense to copy these.
196 Task_token(const Task_token
&);
197 Task_token
& operator=(const Task_token
&);
199 // Whether this is a blocker token.
201 // The number of blockers.
203 // The single writer.
205 // The list of Tasks waiting for this token to be released.
209 // In order to support tokens more reliably, we provide objects which
210 // handle them using RAII.
212 // RAII class to get a write lock on a token. This requires
213 // specifying the task which is doing the lock.
215 class Task_write_token
218 Task_write_token(Task_token
* token
, const Task
* task
)
219 : token_(token
), task_(task
)
220 { this->token_
->add_writer(this->task_
); }
223 { this->token_
->remove_writer(this->task_
); }
226 Task_write_token(const Task_write_token
&);
227 Task_write_token
& operator=(const Task_write_token
&);
233 // RAII class for a blocker.
235 class Task_block_token
238 // The blocker count must be incremented when the task is created.
239 // This object is created when the task is run, so we don't do
240 // anything in the constructor.
241 Task_block_token(Task_token
* token
)
243 { gold_assert(this->token_
->is_blocked()); }
246 { this->token_
->remove_blocker(); }
249 Task_block_token(const Task_block_token
&);
250 Task_block_token
& operator=(const Task_block_token
&);
255 // An object which implements an RAII lock for any object which
256 // supports lock and unlock methods.
258 template<typename Obj
>
262 Task_lock_obj(const Task
* task
, Obj
* obj
)
263 : task_(task
), obj_(obj
)
264 { this->obj_
->lock(task
); }
267 { this->obj_
->unlock(this->task_
); }
270 Task_lock_obj(const Task_lock_obj
&);
271 Task_lock_obj
& operator=(const Task_lock_obj
&);
277 // A class which holds the set of Task_tokens which must be locked for
278 // a Task. No Task requires more than four Task_tokens, so we set
284 static const int max_task_count
= 4;
296 { this->count_
= 0; }
298 // Add a token to the locker.
300 add(Task
* t
, Task_token
* token
)
302 gold_assert(this->count_
< max_task_count
);
303 this->tokens_
[this->count_
] = token
;
305 // A blocker will have been incremented when the task is created.
306 // A writer we need to lock now.
307 if (!token
->is_blocker())
308 token
->add_writer(t
);
311 // Iterate over the tokens.
313 typedef Task_token
** iterator
;
317 { return &this->tokens_
[0]; }
321 { return &this->tokens_
[this->count_
]; }
324 Task_locker(const Task_locker
&);
325 Task_locker
& operator=(const Task_locker
&);
327 // The number of tokens.
330 Task_token
* tokens_
[max_task_count
];
333 } // End namespace gold.
335 #endif // !defined(GOLD_TOKEN_H)
This page took 0.041069 seconds and 4 git commands to generate.