YARP
Yet Another Robot Platform
ThreadImpl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_IMPL_THREADIMPL_H
11 #define YARP_OS_IMPL_THREADIMPL_H
12 
13 #include <yarp/os/Semaphore.h>
14 
15 #include <atomic>
16 #include <thread>
17 
18 namespace yarp {
19 namespace os {
20 namespace impl {
21 
26 {
27 public:
28  virtual ~ThreadImpl();
29 
30  int join(double seconds = -1);
31  virtual void run();
32  virtual void close();
33 
34  // similar to close, but it does not call join (does not wait for thread termination)
35  void askToClose();
36 
37  // should throw if no success
38  virtual bool start();
39 
40  bool isClosing();
41  bool isRunning();
42 
43  virtual void beforeStart();
44  virtual void afterStart(bool success);
45 
46  virtual bool threadInit();
47  virtual void threadRelease();
48 
49  static int getCount();
50 
51  // get a unique key
52  long int getKey();
53  static long int getKeyOfCaller();
54 
55  //should become private, when the thread is friend
56  void notify(bool s);
57  void notifyOpened(bool s)
58  {
59  opened = s;
60  }
61  void synchroWait();
62  void synchroPost();
63 
64  int setPriority(int priority = -1, int policy = -1);
65  int getPriority();
66  int getPolicy();
67  long getTid();
68 
69  long tid{-1};
71 
72  static void yield();
73 
74 private:
75  int defaultPriority{-1};
76  int defaultPolicy{-1};
77  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::thread) thread;
78  YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(std::atomic<bool>) active{false};
79  bool opened{false};
80  bool closing{false};
81  bool needJoin{false};
82 
83  yarp::os::Semaphore synchro{0};
84 
85  bool initWasSuccessful{false};
86 };
87 
88 } // namespace impl
89 } // namespace os
90 } // namespace yarp
91 
92 #endif // YARP_OS_IMPL_THREADIMPL_H
yarp::os::Semaphore
A class for thread synchronization and mutual exclusion.
Definition: Semaphore.h:29
yarp::os::impl::ThreadImpl::notifyOpened
void notifyOpened(bool s)
Definition: ThreadImpl.h:57
YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING_ARG(x)
Suppress MSVC C4251 warning for the declaration.
Definition: system.h:339
yarp::os::impl::ThreadImpl::id
std::thread::id id
Definition: ThreadImpl.h:70
yarp::os::Time::yield
void yield()
The calling thread releases its remaining quantum upon calling this function.
Definition: Time.cpp:141
Semaphore.h
yarp::os::impl::ThreadImpl
An abstraction for a thread of execution.
Definition: ThreadImpl.h:26
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
YARP_os_impl_API
#define YARP_os_impl_API
Definition: api.h:45