summarylogtreecommitdiffstats
path: root/fix-for-tbb-2021.patch
blob: c0ec3f2e479142f99d43426a8ae4b27fe6cfb06f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
diff --git a/AUTHORS b/AUTHORS
index e8bc62efa4..31e7fedaab 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -1,2 +1,3 @@
+Alex Dewar <alex.dewar@gmx.co.uk>
 Nate Koenig <nkoenig@osrfoundation.org>
 John Hsu <hsu@osrfoundation.org>
diff --git a/cmake/SearchForStuff.cmake b/cmake/SearchForStuff.cmake
index 4085b5aaeb..b57a0ad3ac 100644
--- a/cmake/SearchForStuff.cmake
+++ b/cmake/SearchForStuff.cmake
@@ -301,7 +301,7 @@ if (PKG_CONFIG_FOUND)
 
   #################################################
   # Find TBB
-  pkg_check_modules(TBB tbb<2021)
+  pkg_check_modules(TBB tbb)
   set (TBB_PKG_CONFIG "tbb")
   if (NOT TBB_FOUND)
     message(STATUS "TBB not found, attempting to detect manually")
@@ -325,6 +325,12 @@ if (PKG_CONFIG_FOUND)
       endif(tbb_library)
     endif (NOT TBB_FOUND)
   endif (NOT TBB_FOUND)
+  set(HAVE_TBB_GREATER_OR_EQUAL_2021 OFF)
+  if (DEFINED TBB_VERSION AND NOT ${TBB_VERSION} STREQUAL "")
+    if (${TBB_VERSION} VERSION_GREATER_EQUAL "2021.0")
+      set(HAVE_TBB_GREATER_OR_EQUAL_2021 ON)
+    endif()
+  endif()
 
   #################################################
   # Find OGRE
diff --git a/cmake/gazebo-config.cmake.in b/cmake/gazebo-config.cmake.in
index dccc3f2a08..c896c164de 100644
--- a/cmake/gazebo-config.cmake.in
+++ b/cmake/gazebo-config.cmake.in
@@ -226,3 +226,11 @@ list(APPEND @PKG_NAME@_LIBRARIES ${IGNITION-FUEL_TOOLS_LIBRARIES})
 list(APPEND @PKG_NAME@_LDFLAGS -Wl,-rpath,${GAZEBO_INSTALL_LIB_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/plugins)
 list(APPEND @PKG_NAME@_LDFLAGS -L${GAZEBO_INSTALL_LIB_DIR})
 list(APPEND @PKG_NAME@_LDFLAGS -L${GAZEBO_INSTALL_LIB_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/plugins)
+
+set (GAZEBO_HAS_TBB_GREATER_OR_EQUAL_2021 @HAVE_TBB_GREATER_OR_EQUAL_2021@)
+if (GAZEBO_HAS_TBB_GREATER_OR_EQUAL_2021)
+  find_package(TBB CONFIG)
+  if (TARGET tbb::tbb)
+    list(APPEND @PKG_NAME@_LIBRARIES TBB::tbb)
+  endif ()
+endif ()
diff --git a/gazebo/transport/CMakeLists.txt b/gazebo/transport/CMakeLists.txt
index 8e590207ec..a72d49ba1f 100644
--- a/gazebo/transport/CMakeLists.txt
+++ b/gazebo/transport/CMakeLists.txt
@@ -29,6 +29,7 @@ set (headers
   SubscribeOptions.hh
   Subscriber.hh
   SubscriptionTransport.hh
+  TaskGroup.hh
   TopicManager.hh
   TransportIface.hh
   TransportTypes.hh
@@ -70,6 +71,12 @@ if (WIN32)
   target_link_libraries(gazebo_transport ws2_32 Iphlpapi)
 endif()
 
+if(${CMAKE_VERSION} VERSION_LESS "3.13.0")
+  link_directories(${TBB_LIBRARY_DIRS})
+else()
+  target_link_directories(gazebo_transport PUBLIC ${TBB_LIBRARY_DIRS})
+endif()
+
 if (USE_PCH)
     add_pch(gazebo_transport transport_pch.hh ${Boost_PKGCONFIG_CFLAGS} "-I${PROTOBUF_INCLUDE_DIR}" "-I${TBB_INCLUDEDIR}")
 endif()
diff --git a/gazebo/transport/Connection.hh b/gazebo/transport/Connection.hh
index bb48a73bfc..e36a565879 100644
--- a/gazebo/transport/Connection.hh
+++ b/gazebo/transport/Connection.hh
@@ -17,7 +17,16 @@
 #ifndef _CONNECTION_HH_
 #define _CONNECTION_HH_
 
+#undef emit
 #include <tbb/task.h>
+#define emit
+
+// If TBB_VERSION_MAJOR is not defined, this means that
+// tbb >= 2021 and we can include the tbb/version.h header
+#ifndef TBB_VERSION_MAJOR
+#include <tbb/version.h>
+#endif
+
 #include <google/protobuf/message.h>
 
 #include <boost/asio.hpp>
@@ -37,6 +46,9 @@
 #include "gazebo/common/Console.hh"
 #include "gazebo/common/Exception.hh"
 #include "gazebo/common/WeakBind.hh"
+#if TBB_VERSION_MAJOR >= 2021
+#include "gazebo/transport/TaskGroup.hh"
+#endif
 #include "gazebo/util/system.hh"
 
 #define HEADER_LENGTH 8
@@ -54,7 +66,11 @@ namespace gazebo
     /// \cond
     /// \brief A task instance that is created when data is read from
     /// a socket and used by TBB
+#if TBB_VERSION_MAJOR < 2021
     class GZ_TRANSPORT_VISIBLE ConnectionReadTask : public tbb::task
+#else
+    class GZ_TRANSPORT_VISIBLE ConnectionReadTask
+#endif
     {
       /// \brief Constructor
       /// \param[_in] _func Boost function pointer, which is the function
@@ -68,6 +84,7 @@ namespace gazebo
               {
               }
 
+#if TBB_VERSION_MAJOR < 2021
       /// \bried Overridden function from tbb::task that exectues the data
       /// callback.
       public: tbb::task *execute()
@@ -75,7 +92,11 @@ namespace gazebo
                 this->func(this->data);
                 return NULL;
               }
-
+#else
+      /// \brief Execute the data callback
+      public: void operator()() const
+              { this->func(this->data); }
+#endif
       /// \brief The boost function pointer
       private: boost::function<void (const std::string &)> func;
 
@@ -310,12 +331,16 @@ namespace gazebo
 
                 if (!_e && !transport::is_stopped())
                 {
+#if TBB_VERSION_MAJOR < 2021
                   ConnectionReadTask *task = new(tbb::task::allocate_root())
                         ConnectionReadTask(boost::get<0>(_handler), data);
                   tbb::task::enqueue(*task);
 
                   // Non-tbb version:
                   // boost::get<0>(_handler)(data);
+#else
+                  this->taskGroup.run<ConnectionReadTask>(boost::get<0>(_handler), data);
+#endif
                 }
               }
 
@@ -372,7 +397,7 @@ namespace gazebo
       private: boost::asio::ip::tcp::endpoint GetRemoteEndpoint() const;
 
       /// \brief Gets hostname
-      /// \param[in] _ep The end point to get the hostename of
+      /// \param[in] _ep The end point to get the hostname of
       private: static std::string GetHostname(
                    boost::asio::ip::tcp::endpoint _ep);
 
@@ -465,6 +490,11 @@ namespace gazebo
 
       /// \brief True if the connection is open.
       private: bool isOpen;
+
+#if TBB_VERSION_MAJOR >= 2021
+      /// \brief For managing asynchronous tasks with tbb
+      private: TaskGroup taskGroup;
+#endif
     };
     /// \}
   }
diff --git a/gazebo/transport/ConnectionManager.cc b/gazebo/transport/ConnectionManager.cc
index 116c71b279..e748f892cf 100644
--- a/gazebo/transport/ConnectionManager.cc
+++ b/gazebo/transport/ConnectionManager.cc
@@ -27,6 +27,7 @@
 using namespace gazebo;
 using namespace transport;
 
+#if TBB_VERSION_MAJOR < 2021
 /// TBB task to process nodes.
 class TopicManagerProcessTask : public tbb::task
 {
@@ -37,20 +38,30 @@ class TopicManagerProcessTask : public tbb::task
             return NULL;
           }
 };
+#endif
 
 /// TBB task to establish subscriber to publisher connection.
+#if TBB_VERSION_MAJOR < 2021
 class TopicManagerConnectionTask : public tbb::task
+#else
+class TopicManagerConnectionTask
+#endif
 {
   /// \brief Constructor.
   /// \param[in] _pub Publish message
   public: explicit TopicManagerConnectionTask(msgs::Publish _pub) : pub(_pub) {}
 
   /// Implements the necessary execute function
+#if TBB_VERSION_MAJOR < 2021
   public: tbb::task *execute()
           {
             TopicManager::Instance()->ConnectSubToPub(pub);
             return NULL;
           }
+#else
+  public: void operator()() const
+          { TopicManager::Instance()->ConnectSubToPub(pub); }
+#endif
 
   /// \brief Publish message
   private: msgs::Publish pub;
@@ -272,11 +283,6 @@ void ConnectionManager::RunUpdate()
   if (this->masterConn)
     this->masterConn->ProcessWriteQueue();
 
-  // Use TBB to process nodes. Need more testing to see if this makes
-  // a difference.
-  // TopicManagerProcessTask *task = new(tbb::task::allocate_root())
-  //   TopicManagerProcessTask();
-  // tbb::task::enqueue(*task);
   boost::recursive_mutex::scoped_lock lock(this->connectionMutex);
 
   TopicManager::Instance()->ProcessNodes();
@@ -401,9 +407,13 @@ void ConnectionManager::ProcessMessage(const std::string &_data)
     if (pub.host() != this->serverConn->GetLocalAddress() ||
         pub.port() != this->serverConn->GetLocalPort())
     {
+#if TBB_VERSION_MAJOR < 2021
       TopicManagerConnectionTask *task = new(tbb::task::allocate_root())
       TopicManagerConnectionTask(pub);
       tbb::task::enqueue(*task);
+#else
+      this->taskGroup.run<TopicManagerConnectionTask>(pub);
+#endif
     }
   }
   // publisher_subscribe. This occurs when we try to subscribe to a topic, and
diff --git a/gazebo/transport/ConnectionManager.hh b/gazebo/transport/ConnectionManager.hh
index ef80eaf876..6dc212bc13 100644
--- a/gazebo/transport/ConnectionManager.hh
+++ b/gazebo/transport/ConnectionManager.hh
@@ -27,8 +27,11 @@
 #include "gazebo/msgs/msgs.hh"
 #include "gazebo/common/SingletonT.hh"
 
-#include "gazebo/transport/Publisher.hh"
 #include "gazebo/transport/Connection.hh"
+#include "gazebo/transport/Publisher.hh"
+#if TBB_VERSION_MAJOR >= 2021
+#include "gazebo/transport/TaskGroup.hh"
+#endif
 #include "gazebo/util/system.hh"
 
 /// \brief Explicit instantiation for typed SingletonT.
@@ -194,6 +197,11 @@ namespace gazebo
       /// \brief Condition used for synchronization
       private: boost::condition_variable namespaceCondition;
 
+#if TBB_VERSION_MAJOR >= 2021
+      /// \brief For managing asynchronous tasks with tbb
+      private: TaskGroup taskGroup;
+#endif
+
       // Singleton implementation
       private: friend class SingletonT<ConnectionManager>;
     };
diff --git a/gazebo/transport/Node.hh b/gazebo/transport/Node.hh
index b381781fe6..7afd5f340f 100644
--- a/gazebo/transport/Node.hh
+++ b/gazebo/transport/Node.hh
@@ -18,14 +18,27 @@
 #ifndef GAZEBO_TRANSPORT_NODE_HH_
 #define GAZEBO_TRANSPORT_NODE_HH_
 
+#undef emit
 #include <tbb/task.h>
+#define emit
+#ifndef TBB_VERSION_MAJOR
+#include <tbb/version.h>
+#endif
+
+// This fixes compiler warnings, see #3147 and #3160
+#ifndef BOOST_BIND_GLOBAL_PLACEHOLDERS
+#define BOOST_BIND_GLOBAL_PLACEHOLDERS
+#endif
+
 #include <boost/bind.hpp>
 #include <boost/enable_shared_from_this.hpp>
 #include <map>
 #include <list>
 #include <string>
 #include <vector>
-
+#if TBB_VERSION_MAJOR >= 2021
+#include "gazebo/transport/TaskGroup.hh"
+#endif
 #include "gazebo/transport/TransportTypes.hh"
 #include "gazebo/transport/TopicManager.hh"
 #include "gazebo/util/system.hh"
@@ -36,7 +49,11 @@ namespace gazebo
   {
     /// \cond
     /// \brief Task used by Node::Publish to publish on a one-time publisher
+#if TBB_VERSION_MAJOR < 2021
     class GZ_TRANSPORT_VISIBLE PublishTask : public tbb::task
+#else
+    class GZ_TRANSPORT_VISIBLE PublishTask
+#endif
     {
       /// \brief Constructor
       /// \param[in] _pub Publisher to publish the message on.
@@ -49,16 +66,23 @@ namespace gazebo
         this->msg->CopyFrom(_message);
       }
 
+#if TBB_VERSION_MAJOR < 2021
       /// \brief Overridden function from tbb::task that exectues the
       /// publish task.
       public: tbb::task *execute()
+#else
+      /// \brief Executes the publish task.
+      public: void operator()() const
+#endif
               {
                 this->pub->WaitForConnection();
                 this->pub->Publish(*this->msg, true);
                 this->pub->SendMessage();
                 delete this->msg;
+#if TBB_VERSION_MAJOR < 2021
                 this->pub.reset();
                 return NULL;
+#endif
               }
 
       /// \brief Pointer to the publisher.
@@ -159,11 +183,15 @@ namespace gazebo
                   const google::protobuf::Message &_message)
               {
                 transport::PublisherPtr pub = this->Advertise<M>(_topic);
+#if TBB_VERSION_MAJOR < 2021
                 PublishTask *task = new(tbb::task::allocate_root())
                   PublishTask(pub, _message);
 
                 tbb::task::enqueue(*task);
                 return;
+#else
+                this->taskGroup.run<PublishTask>(pub, _message);
+#endif
               }
 
       /// \brief Advertise a topic
@@ -419,6 +447,11 @@ namespace gazebo
       /// \brief List of newly arrive messages
       private: std::map<std::string, std::list<MessagePtr> > incomingMsgsLocal;
 
+#if TBB_VERSION_MAJOR >= 2021
+      /// \brief For managing asynchronous tasks with tbb
+      private: TaskGroup taskGroup;
+#endif
+
       private: boost::mutex publisherMutex;
       private: boost::mutex publisherDeleteMutex;
       private: boost::recursive_mutex incomingMutex;
diff --git a/gazebo/transport/TaskGroup.hh b/gazebo/transport/TaskGroup.hh
new file mode 100644
index 0000000000..b95b5d863c
--- /dev/null
+++ b/gazebo/transport/TaskGroup.hh
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2021 Open Source Robotics Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+#ifndef GAZEBO_TRANSPORT_TASKGROUP_HH_
+#define GAZEBO_TRANSPORT_TASKGROUP_HH_
+
+#include <utility>
+
+// Emit is both a macro in Qt and a function defined by tbb
+#undef emit
+#include <tbb/tbb.h>
+#define emit
+
+namespace gazebo {
+  namespace transport {
+    class TaskGroup
+    {
+      public: ~TaskGroup() noexcept
+              {
+                // Wait for running tasks to finish
+                this->taskGroup.wait();
+              }
+
+      public: template<class Functor, class... Args> void run(Args&&... args)
+      {
+        this->taskGroup.run(Functor(std::forward<Args>(args)...));
+      }
+
+      private: tbb::task_group taskGroup;
+    };
+  }
+}
+
+#endif
diff --git a/gazebo/transport/transport_pch.hh b/gazebo/transport/transport_pch.hh
index 77f4094741..22023df0b1 100644
--- a/gazebo/transport/transport_pch.hh
+++ b/gazebo/transport/transport_pch.hh
@@ -21,6 +21,7 @@
  * To get a starting list for this file, I use the command:
  *    grep --include="*.hh" --include="*.cc" --no-filename -r "#include <" | sort -u
  */
+
 #include <boost/algorithm/string.hpp>
 #include <boost/asio.hpp>
 #include <boost/bind.hpp>
@@ -44,8 +45,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string>
+#include <tbb/task.h>
+#ifndef TBB_VERSION_MAJOR
+#include <tbb/version.h>
+#endif
+#if TBB_VERSION_MAJOR < 2021
 #include <tbb/blocked_range.h>
 #include <tbb/parallel_for.h>
 #include <tbb/task.h>
+#else
+#undef emit
+#include <tbb/tbb.h>
+#define emit
+#endif
 #include <utility>
 #include <vector>
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 5f2c112105..6d09a11d0f 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -49,6 +49,7 @@ target_link_libraries(gz
  gazebo_gui
  gazebo_physics
  gazebo_sensors
+ gazebo_transport
  ${Qt5Core_LIBRARIES}
  ${Qt5Widgets_LIBRARIES}
  ${Boost_LIBRARIES}