ThreadManager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2006- Facebook
  2. // Distributed under the Thrift Software License
  3. //
  4. // See accompanying file LICENSE or visit the Thrift site at:
  5. // http://developers.facebook.com/thrift/
  6. #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_
  7. #define _THRIFT_CONCURRENCY_THREADMANAGER_H_ 1
  8. #include <boost/shared_ptr.hpp>
  9. #include <sys/types.h>
  10. #include "Thread.h"
  11. namespace facebook { namespace thrift { namespace concurrency {
  12. /**
  13. * Thread Pool Manager and related classes
  14. *
  15. * @author marc
  16. * @version $Id:$
  17. */
  18. class ThreadManager;
  19. /**
  20. * ThreadManager class
  21. *
  22. * This class manages a pool of threads. It uses a ThreadFactory to create
  23. * threads. It never actually creates or destroys worker threads, rather
  24. * It maintains statistics on number of idle threads, number of active threads,
  25. * task backlog, and average wait and service times and informs the PoolPolicy
  26. * object bound to instances of this manager of interesting transitions. It is
  27. * then up the PoolPolicy object to decide if the thread pool size needs to be
  28. * adjusted and call this object addWorker and removeWorker methods to make
  29. * changes.
  30. *
  31. * This design allows different policy implementations to used this code to
  32. * handle basic worker thread management and worker task execution and focus on
  33. * policy issues. The simplest policy, StaticPolicy, does nothing other than
  34. * create a fixed number of threads.
  35. */
  36. class ThreadManager {
  37. protected:
  38. ThreadManager() {}
  39. public:
  40. virtual ~ThreadManager() {}
  41. /**
  42. * Starts the thread manager. Verifies all attributes have been properly
  43. * initialized, then allocates necessary resources to begin operation
  44. */
  45. virtual void start() = 0;
  46. /**
  47. * Stops the thread manager. Aborts all remaining unprocessed task, shuts
  48. * down all created worker threads, and realeases all allocated resources.
  49. * This method blocks for all worker threads to complete, thus it can
  50. * potentially block forever if a worker thread is running a task that
  51. * won't terminate.
  52. */
  53. virtual void stop() = 0;
  54. /**
  55. * Joins the thread manager. This is the same as stop, except that it will
  56. * block until all the workers have finished their work. At that point
  57. * the ThreadManager will transition into the STOPPED state.
  58. */
  59. virtual void join() = 0;
  60. enum STATE {
  61. UNINITIALIZED,
  62. STARTING,
  63. STARTED,
  64. JOINING,
  65. STOPPING,
  66. STOPPED
  67. };
  68. virtual const STATE state() const = 0;
  69. virtual boost::shared_ptr<ThreadFactory> threadFactory() const = 0;
  70. virtual void threadFactory(boost::shared_ptr<ThreadFactory> value) = 0;
  71. virtual void addWorker(size_t value=1) = 0;
  72. virtual void removeWorker(size_t value=1) = 0;
  73. /**
  74. * Gets the current number of idle worker threads
  75. */
  76. virtual size_t idleWorkerCount() const = 0;
  77. /**
  78. * Gets the current number of total worker threads
  79. */
  80. virtual size_t workerCount() const = 0;
  81. /**
  82. * Gets the current number of pending tasks
  83. */
  84. virtual size_t pendingTaskCount() const = 0;
  85. /**
  86. * Gets the current number of pending and executing tasks
  87. */
  88. virtual size_t totalTaskCount() const = 0;
  89. /**
  90. * Gets the maximum pending task count. 0 indicates no maximum
  91. */
  92. virtual size_t pendingTaskCountMax() const = 0;
  93. /**
  94. * Adds a task to be executed at some time in the future by a worker thread.
  95. *
  96. * This method will block if pendingTaskCountMax() in not zero and pendingTaskCount()
  97. * is greater than or equalt to pendingTaskCountMax(). If this method is called in the
  98. * context of a ThreadManager worker thread it will throw a
  99. * TooManyPendingTasksException
  100. *
  101. * @param task The task to queue for execution
  102. *
  103. * @param timeout Time to wait in milliseconds to add a task when a pending-task-count
  104. * is specified. Specific cases:
  105. * timeout = 0 : Wait forever to queue task.
  106. * timeout = -1 : Return immediately if pending task count exceeds specified max
  107. *
  108. * @throws TooManyPendingTasksException Pending task count exceeds max pending task count
  109. */
  110. virtual void add(boost::shared_ptr<Runnable>task, int64_t timeout=0LL) = 0;
  111. /**
  112. * Removes a pending task
  113. */
  114. virtual void remove(boost::shared_ptr<Runnable> task) = 0;
  115. static boost::shared_ptr<ThreadManager> newThreadManager();
  116. /**
  117. * Creates a simple thread manager the uses count number of worker threads and has
  118. * a pendingTaskCountMax maximum pending tasks. The default, 0, specified no limit
  119. * on pending tasks
  120. */
  121. static boost::shared_ptr<ThreadManager> newSimpleThreadManager(size_t count=4, size_t pendingTaskCountMax=0);
  122. class Task;
  123. class Worker;
  124. class Impl;
  125. };
  126. }}} // facebook::thrift::concurrency
  127. #endif // #ifndef _THRIFT_CONCURRENCY_THREADMANAGER_H_