Embedded Multicore Building Blocks V1.0.0
thread.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_THREAD_H_
28 #define EMBB_BASE_THREAD_H_
29 
30 #include <embb/base/internal/platform.h>
31 #include <embb/base/internal/thread_closures.h>
32 #include <embb/base/mutex.h>
33 #include <embb/base/core_set.h>
34 #include <embb/base/c/thread.h>
35 #include <ostream>
36 
37 namespace embb {
38 namespace base {
39 
59 class Thread {
60  public:
64  class ID {
65  public:
69  ID() : id_() {}
70 
71  private:
75  explicit ID(internal::IDType id) : id_(id) {}
76 
80  internal::IDType id_;
81 
85  friend class Thread;
86 
90  template<class CharT, class Traits>
91  friend
92  std::basic_ostream<CharT, Traits>&
93  operator<<(std::basic_ostream<CharT, Traits>& os, Thread::ID id);
94 
98  friend bool operator==(Thread::ID lhs, Thread::ID rhs);
99  friend bool operator!=(Thread::ID lhs, Thread::ID rhs);
100  };
101 
112  static unsigned int GetThreadsMaxCount();
113 
120  static void SetThreadsMaxCount(
121  unsigned int max_count
123  );
124 
134  static ID CurrentGetID();
135 
144  static void CurrentYield();
145 
160  template<typename Function>
161  explicit Thread(
162  Function function
164  );
165 
180  template<typename Function>
181  Thread(
182  CoreSet& core_set,
184  Function function
186  );
187 
202  template<typename Function>
203  Thread(
204  CoreSet& core_set,
206  embb_thread_priority_t priority,
208  Function function
210  );
211 
227  template<typename Function, typename Arg>
228  Thread(
229  Function function,
231  Arg arg
233  );
234 
251  template<typename Function, typename Arg1, typename Arg2>
252  Thread(
253  Function function,
255  Arg1 arg1,
257  Arg2 arg2
259  );
260 
269  void Join();
270 
278  ID GetID();
279 
280  private:
286  template<typename ThreadClosure>
287  void CheckThreadCreationErrors(
288  int result,
290  ThreadClosure* closure
292  );
293 
297  Thread(const Thread&);
298  Thread& operator=(const Thread&);
299 
303  internal::ThreadType rep_;
304 };
305 
313 bool operator==(
314  Thread::ID lhs,
316  Thread::ID rhs
318  );
319 
327 bool operator!=(
328  Thread::ID lhs,
330  Thread::ID rhs
332  );
333 
341 template<class CharT, class Traits>
342 std::basic_ostream<CharT, Traits>&
343  operator<<(
344  std::basic_ostream<CharT, Traits>& os,
346  Thread::ID id
348  );
349 
350 } // namespace base
351 } // namespace embb
352 
353 #include <embb/base/internal/thread-inl.h>
354 
355 #endif // EMBB_BASE_THREAD_H_
Definition: lock_free_mpmc_queue.h:40
Represents a set of processor cores, used to set thread-to-core affinities.
Definition: core_set.h:58
Unique ID of a thread that can be compared with other IDs.
Definition: thread.h:64
ID()
Constructs an empty (invalid) thread ID.
Definition: thread.h:69
friend class Thread
A thread needs to set its ID on start.
Definition: thread.h:85
void Join()
Waits until the thread has finished execution.
static ID CurrentGetID()
Returns the ID of the current thread.
static void SetThreadsMaxCount(unsigned int max_count)
Sets the maximum number of threads handled by EMB2.
Wraps function pointers, member function pointers, and functors with up to five arguments.
Definition: function.h:94
Represents a thread of execution.
Definition: thread.h:59
friend bool operator==(Thread::ID lhs, Thread::ID rhs)
Comparison operators need to access the internal ID representation.
static unsigned int GetThreadsMaxCount()
Returns the maximum number of threads handled by EMB2.
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, Thread::ID id)
The streaming operator needs to access the internal ID representation.
embb_thread_priority_t
Thread priority type.
Definition: thread.h:62
static void CurrentYield()
Reschedule the current thread for later execution.
friend bool operator!=(Thread::ID lhs, Thread::ID rhs)
Compares two thread IDs for inequality.
ID GetID()
Returns the thread ID.