YARP
Yet Another Robot Platform
Thread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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 #include <yarp/os/Thread.h>
11 
13 
14 using namespace yarp::os::impl;
15 using namespace yarp::os;
16 
18 {
19 private:
20  Thread& owner;
21 
22 public:
23  explicit Private(Thread& owner) :
24  owner(owner),
25  stopping(false)
26  {
27  }
28 
29  ~Private() override = default;
30 
31  void beforeStart() override
32  {
33  owner.beforeStart();
34  }
35 
36  void afterStart(bool success) override
37  {
38  owner.afterStart(success);
39  }
40 
41  void run() override
42  {
43  owner.run();
44  }
45 
46  void close() override
47  {
48  if (isRunning()) {
49  owner.onStop();
50  }
52  }
53 
54  bool threadInit() override
55  {
56  return owner.threadInit();
57  }
58 
59  void threadRelease() override
60  {
61  owner.threadRelease();
62  }
63 
64  bool stopping;
65 };
66 
67 
69  mPriv(new Private(*this))
70 {
71 }
72 
74 {
75  mPriv->close();
76  delete mPriv;
77 }
78 
79 bool Thread::join(double seconds)
80 {
81  return mPriv->join(seconds) == 0;
82 }
83 
85 {
86  mPriv->stopping = true;
87  mPriv->close();
88  return true;
89 }
90 
92 {
93  // by default this does nothing
94 }
95 
97 {
98  mPriv->stopping = false;
99  return mPriv->start();
100 }
101 
103 {
104  //return mPriv->isClosing();
105  return mPriv->stopping;
106 }
107 
109 {
110  return mPriv->isRunning();
111 }
112 
114 {
115 }
116 
117 void Thread::afterStart(bool success)
118 {
119  YARP_UNUSED(success);
120 }
121 
123 {
124  return ThreadImpl::getCount();
125 }
126 
127 // get a unique key
128 long int Thread::getKey()
129 {
130  return mPriv->getKey();
131 }
132 
134 {
135  return ThreadImpl::getKeyOfCaller();
136 }
137 
138 int Thread::setPriority(int priority, int policy)
139 {
140  return mPriv->setPriority(priority, policy);
141 }
142 
144 {
145  return mPriv->getPriority();
146 }
147 
149 {
150  return mPriv->getPolicy();
151 }
152 
154 {
156 }
void close() override
Definition: Thread.cpp:46
void beforeStart() override
Definition: Thread.cpp:31
void threadRelease() override
Definition: Thread.cpp:59
void afterStart(bool success) override
Definition: Thread.cpp:36
void run() override
Definition: Thread.cpp:41
Private(Thread &owner)
Definition: Thread.cpp:23
~Private() override=default
bool threadInit() override
Definition: Thread.cpp:54
An abstraction for a thread of execution.
Definition: Thread.h:25
virtual void threadRelease()
Release method.
Definition: Thread.h:119
virtual bool threadInit()
Initialization method.
Definition: Thread.h:108
static long int getKeyOfCaller()
Get a unique identifier for the calling thread.
Definition: Thread.cpp:133
virtual ~Thread()
Destructor.
Definition: Thread.cpp:73
bool stop()
Stop the thread.
Definition: Thread.cpp:84
virtual void onStop()
Call-back, called while halting the thread (before join).
Definition: Thread.cpp:91
static void yield()
Reschedule the execution of current thread, allowing other threads to run.
Definition: Thread.cpp:153
static int getCount()
Check how many threads are running.
Definition: Thread.cpp:122
bool join(double seconds=-1)
The function returns when the thread execution has completed.
Definition: Thread.cpp:79
virtual void beforeStart()
Called just before a new thread starts.
Definition: Thread.cpp:113
Thread()
Constructor.
Definition: Thread.cpp:68
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition: Thread.cpp:102
int getPolicy()
Query the current scheduling policy of the thread, if the OS supports that.
Definition: Thread.cpp:148
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:108
long int getKey()
Get a unique identifier for the thread.
Definition: Thread.cpp:128
bool start()
Start the new thread running.
Definition: Thread.cpp:96
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
Definition: Thread.cpp:138
virtual void afterStart(bool success)
Called just after a new thread starts (or fails to start), this is executed by the same thread that c...
Definition: Thread.cpp:117
int getPriority()
Query the current priority of the thread, if the OS supports that.
Definition: Thread.cpp:143
virtual void run()=0
Main body of the new thread.
An abstraction for a thread of execution.
Definition: ThreadImpl.h:26
void yield()
The calling thread releases its remaining quantum upon calling this function.
Definition: Time.cpp:141
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
#define YARP_UNUSED(var)
Definition: api.h:165