JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
ThreadPool.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #ifdef JEVOIS_PRO
19 
20 // This code modified from:
21 
22 // Copyright Mateusz Jaworski 2020 - 2020.
23 // Distributed under the Boost Software License, Version 1.0.
24 // (See accompanying file LICENSE.md or copy at
25 // https://www.boost.org/LICENSE_1_0.txt)
26 
27 #include <jevois/Util/ThreadPool.H>
28 #include <jevois/Debug/Log.H>
29 
30 // ##############################################################################################################
31 jevois::ThreadPool::ThreadPool(unsigned int threads, bool little) :
32  _size(0), _exit(false)
33 {
34  _pool.reserve(threads);
35 
36 #ifdef JEVOIS_PLATFORM
37  // Define a CPU mask:
38  cpu_set_t cpuset;
39  CPU_ZERO(&cpuset);
40 
41  // Little cores are 0, 1; big cores are 2,3,4,5:
42  if (little)
43  {
44  CPU_SET(0, &cpuset);
45  CPU_SET(1, &cpuset);
46  }
47  else
48  {
49  CPU_SET(2, &cpuset);
50  CPU_SET(3, &cpuset);
51  CPU_SET(4, &cpuset);
52  CPU_SET(5, &cpuset);
53  }
54 #endif
55 
56  for(unsigned int i = 0; i < threads; ++i)
57  {
58  _pool.emplace_back([this]{
59  fu2::unique_function<void()> task;
60 
61  for (;;)
62  {
63  {
64  std::unique_lock<std::mutex> lock(_mtx);
65  _new_task.wait(lock, [this]{ return _size || _exit; });
66  }
67 
68  //if (_exit && !_size)) return; // JEVOIS: this can lock up the destructor...
69 
70  if (_exit)
71  {
72  while (_tasks.try_dequeue(task)) { _size--; task(); }
73  return;
74  }
75 
76  if (_tasks.try_dequeue(task))
77  {
78  _size--;
79  task();
80  }
81  }
82 
83  });
84 
85 #ifdef JEVOIS_PLATFORM
86  // JeVois: set CPU affinity for this thread:
87  int rc = pthread_setaffinity_np(_pool.back().native_handle(), sizeof(cpu_set_t), &cpuset);
88  if (rc) LERROR("Error calling pthread_setaffinity_np: " << rc);
89 #endif
90  }
91 
92  // Run a phony job, otherwise the threadpool hangs on destruction if it was never used:
93 #ifdef JEVOIS_PLATFORM
94  auto fut = execute([threads,little](){
95  LINFO("Initialized with " << threads << (little ? " A53 threads." : " A73 threads.")); });
96 #else
97  auto fut = execute([threads](){
98  LINFO("Initialized with " << threads << " threads."); });
99  (void)little; // keep compiler happy
100 #endif
101 }
102 
103 // ##############################################################################################################
105 {
106  {
107  std::scoped_lock<std::mutex> lock(_mtx);
108  _exit = true;
109  }
110 
111  _new_task.notify_all();
112  for (auto & thread: _pool) thread.join();
113 }
114 
115 // ##############################################################################################################
117 {
118  return _pool.size();
119 }
120 
121 #endif // JEVOIS_PRO
jevois::ThreadPool::getPoolSize
auto getPoolSize() -> size_t
Get the pool size.
Definition: ThreadPool.C:116
LERROR
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition: Log.H:211
Log.H
ThreadPool.H
jevois::ThreadPool::ThreadPool
ThreadPool(unsigned int threads=std::thread::hardware_concurrency(), bool little=false)
Constructor.
Definition: ThreadPool.C:31
jevois::ThreadPool::~ThreadPool
~ThreadPool()
Destructor.
Definition: ThreadPool.C:104
jevois::ThreadPool::execute
auto execute(Func &&func, Args &&... args) -> std::future< decltype(func(args...))>
Execute a function and get a future.
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:194