1 // workqueue-internal.h -- internal work queue header for gold -*- C++ -*-
3 // Copyright 2006, 2007 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.
23 #ifndef GOLD_WORKQUEUE_INTERNAL_H
24 #define GOLD_WORKQUEUE_INTERNAL_H
28 #include "gold-threads.h"
29 #include "workqueue.h"
31 // This is an internal header file for different gold workqueue
37 class Workqueue_thread;
39 // The Workqueue_runner abstract class. This is the interface used by
40 // the general workqueue code to actually run a task.
42 class Workqueue_runner
45 Workqueue_runner(Workqueue* workqueue)
46 : workqueue_(workqueue)
48 virtual ~Workqueue_runner()
51 // Run a task. This is always called in the main thread.
53 run(Task*, Task_locker*) = 0;
55 // Set the number of threads to use. This is ignored when not using
58 set_thread_count(int) = 0;
61 // This is called by an implementation when a task is completed.
62 void completed(Task* t, Task_locker* tl)
63 { this->workqueue_->completed(t, tl); }
65 Workqueue* get_workqueue() const
66 { return this->workqueue_; }
69 Workqueue* workqueue_;
72 // The threaded instantiation of Workqueue_runner.
74 class Workqueue_runner_threadpool : public Workqueue_runner
77 Workqueue_runner_threadpool(Workqueue* workqueue);
79 ~Workqueue_runner_threadpool();
82 run(Task*, Task_locker*);
85 set_thread_count(int);
88 // This class can not be copied.
89 Workqueue_runner_threadpool(const Workqueue_runner_threadpool&);
90 Workqueue_runner_threadpool& operator=(const Workqueue_runner_threadpool&);
92 // Return the next Task and Task_locker to run. This returns false
93 // if the calling thread should simply exit.
95 get_next(Task**, Task_locker**);
97 // This is called when the thread completes a task.
99 thread_completed(Task*, Task_locker*);
101 // The Workqueue_thread class calls functions from this and from the
102 // parent Workqueue_runner.
103 friend class Workqueue_thread;
105 // An entry on the queue of tasks to run.
106 typedef std::pair<Task*, Task_locker*> Task_queue_entry;
108 // A queue of tasks to run.
109 typedef std::queue<Task_queue_entry> Task_queue;
111 // The number of threads we want to create. This is only changed in
112 // the main thread or when only one thread is running. This is set
113 // to zero when all threads should exit.
114 int desired_thread_count_;
115 // A lock controlling access to the remaining fields.
117 // The number of threads we have created.
118 int actual_thread_count_;
119 // The number of threads which are running a task.
120 int running_thread_count_;
121 // A queue of tasks to run.
122 Task_queue task_queue_;
123 // A condition variable which signals when the task_queue_ changed.
124 Condvar task_queue_condvar_;
127 } // End namespace gold.
129 #endif // !defined(GOLD_WORKQUEUE_INTERNAL_H)