workqueue-internal.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // workqueue-internal.h -- internal work queue header for gold -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_WORKQUEUE_INTERNAL_H
  18. #define GOLD_WORKQUEUE_INTERNAL_H
  19. #include <queue>
  20. #include <csignal>
  21. #include "gold-threads.h"
  22. #include "workqueue.h"
  23. // This is an internal header file for different gold workqueue
  24. // implementations.
  25. namespace gold
  26. {
  27. class Workqueue_thread;
  28. // The Workqueue_threader abstract class. This is the interface used
  29. // by the general workqueue code to manage threads.
  30. class Workqueue_threader
  31. {
  32. public:
  33. Workqueue_threader(Workqueue* workqueue)
  34. : workqueue_(workqueue)
  35. { }
  36. virtual ~Workqueue_threader()
  37. { }
  38. // Set the number of threads to use. This is ignored when not using
  39. // threads.
  40. virtual void
  41. set_thread_count(int) = 0;
  42. // Return whether to cancel the current thread.
  43. virtual bool
  44. should_cancel_thread(int thread_number) = 0;
  45. protected:
  46. // Get the Workqueue.
  47. Workqueue*
  48. get_workqueue()
  49. { return this->workqueue_; }
  50. private:
  51. // The Workqueue.
  52. Workqueue* workqueue_;
  53. };
  54. // The threaded instantiation of Workqueue_threader.
  55. class Workqueue_threader_threadpool : public Workqueue_threader
  56. {
  57. public:
  58. Workqueue_threader_threadpool(Workqueue*);
  59. ~Workqueue_threader_threadpool();
  60. // Set the thread count.
  61. void
  62. set_thread_count(int);
  63. // Return whether to cancel a thread.
  64. bool
  65. should_cancel_thread(int thread_number);
  66. // Process all tasks. This keeps running until told to cancel.
  67. void
  68. process(int thread_number)
  69. { this->get_workqueue()->process(thread_number); }
  70. private:
  71. // This is set if we need to check the thread count.
  72. volatile sig_atomic_t check_thread_count_;
  73. // Lock for the remaining members.
  74. Lock lock_;
  75. // The number of threads we want to create. This is set to zero
  76. // when all threads should exit.
  77. int desired_thread_count_;
  78. // The number of threads currently running.
  79. int threads_;
  80. };
  81. } // End namespace gold.
  82. #endif // !defined(GOLD_WORKQUEUE_INTERNAL_H)