blob: cdf6625e196cadf4f08028d7c0a04bb00463318f [file] [log] [blame]
Mike Vitus27789c02018-01-26 13:51:26 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2018 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: vitus@google.com (Michael Vitus)
30
31#ifndef CERES_INTERNAL_THREAD_POOL_H_
32#define CERES_INTERNAL_THREAD_POOL_H_
33
Mike Vituse809cf02018-04-03 11:06:52 -070034#include <functional>
Mike Vitus27789c02018-01-26 13:51:26 -080035#include <mutex>
36#include <thread>
37#include <vector>
38
39#include "ceres/concurrent_queue.h"
Taylor Braun-Jones3f6d2732020-01-28 12:09:30 -050040#include "ceres/internal/port.h"
Mike Vitus27789c02018-01-26 13:51:26 -080041
42namespace ceres {
43namespace internal {
44
45// A thread-safe thread pool with an unbounded task queue and a resizable number
Kuang Fangjun0d3a84f2018-09-22 11:53:42 +080046// of workers. The size of the thread pool can be increased but never decreased
Mike Vitus27789c02018-01-26 13:51:26 -080047// in order to support the largest number of threads requested. The ThreadPool
48// has three states:
49//
50// (1) The thread pool size is zero. Tasks may be added to the thread pool via
51// AddTask but they will not be executed until the thread pool is resized.
52//
53// (2) The thread pool size is greater than zero. Tasks may be added to the
54// thread pool and will be executed as soon as a worker is available. The
55// thread pool may be resized while the thread pool is running.
56//
57// (3) The thread pool is destructing. The thread pool will signal all the
58// workers to stop. The workers will finish all of the tasks that have already
59// been added to the thread pool.
60//
Taylor Braun-Jones3f6d2732020-01-28 12:09:30 -050061class CERES_EXPORT_INTERNAL ThreadPool {
Mike Vitus27789c02018-01-26 13:51:26 -080062 public:
Alex Stewartdf6e27e2018-09-12 18:30:51 +010063 // Returns the maximum number of hardware threads.
64 static int MaxNumThreadsAvailable();
65
Mike Vitus27789c02018-01-26 13:51:26 -080066 // Default constructor with no active threads. We allow instantiating a
67 // thread pool with no threads to support the use case of single threaded
68 // Ceres where everything will be executed on the main thread. For single
69 // threaded execution this has two benefits: avoid any overhead as threads
70 // are expensive to create, and no unused threads shown in the debugger.
71 ThreadPool();
72
Alex Stewartdf6e27e2018-09-12 18:30:51 +010073 // Instantiates a thread pool with min(MaxNumThreadsAvailable, num_threads)
Mike Vitus27789c02018-01-26 13:51:26 -080074 // number of threads.
75 explicit ThreadPool(int num_threads);
76
77 // Signals the workers to stop and waits for them to finish any tasks that
78 // have been scheduled.
79 ~ThreadPool();
80
81 // Resizes the thread pool if it is currently less than the requested number
Alex Stewartdf6e27e2018-09-12 18:30:51 +010082 // of threads. The thread pool will be resized to min(MaxNumThreadsAvailable,
Mike Vitus27789c02018-01-26 13:51:26 -080083 // num_threads) number of threads. Resize does not support reducing the
84 // thread pool size. If a smaller number of threads is requested, the thread
85 // pool remains the same size. The thread pool is reused within Ceres with
86 // different number of threads, and we need to ensure we can support the
87 // largest number of threads requested. It is safe to resize the thread pool
88 // while the workers are executing tasks, and the resizing is guaranteed to
89 // complete upon return.
90 void Resize(int num_threads);
91
92 // Adds a task to the queue and wakes up a blocked thread. If the thread pool
93 // size is greater than zero, then the task will be executed by a currently
94 // idle thread or when a thread becomes available. If the thread pool has no
95 // threads, then the task will never be executed and the user should use
96 // Resize() to create a non-empty thread pool.
97 void AddTask(const std::function<void()>& func);
98
99 // Returns the current size of the thread pool.
100 int Size();
101
102 private:
103 // Main loop for the threads which blocks on the task queue until work becomes
104 // available. It will return if and only if Stop has been called.
105 void ThreadMainLoop();
106
107 // Signal all the threads to stop. It does not block until the threads are
108 // finished.
109 void Stop();
110
111 // The queue that stores the units of work available for the thread pool. The
112 // task queue maintains its own thread safety.
113 ConcurrentQueue<std::function<void()>> task_queue_;
114 std::vector<std::thread> thread_pool_;
115 std::mutex thread_pool_mutex_;
116};
117
118} // namespace internal
119} // namespace ceres
120
121#endif // CERES_INTERNAL_THREAD_POOL_H_