YARP
Yet Another Robot Platform
Thread.cpp
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 #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 }
yarp::os::Thread::Private::threadRelease
void threadRelease() override
Definition: Thread.cpp:59
yarp::os::Thread::Private::beforeStart
void beforeStart() override
Definition: Thread.cpp:31
yarp::os::Thread::join
bool join(double seconds=-1)
The function returns when the thread execution has completed.
Definition: Thread.cpp:79
yarp::os::Thread::yield
static void yield()
Reschedule the execution of current thread, allowing other threads to run.
Definition: Thread.cpp:153
yarp::os::Thread::threadRelease
virtual void threadRelease()
Release method.
Definition: Thread.h:119
yarp::os::Thread::Private::close
void close() override
Definition: Thread.cpp:46
ThreadImpl.h
yarp::os::Thread::onStop
virtual void onStop()
Call-back, called while halting the thread (before join).
Definition: Thread.cpp:91
yarp::os::Thread::Private::threadInit
bool threadInit() override
Definition: Thread.cpp:54
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
yarp::os::Thread::beforeStart
virtual void beforeStart()
Called just before a new thread starts.
Definition: Thread.cpp:113
yarp::os::Thread::getKey
long int getKey()
Get a unique identifier for the thread.
Definition: Thread.cpp:128
yarp::os::Thread::Private::~Private
~Private() override=default
yarp::os::Thread::isRunning
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:108
yarp::os::Thread::Private::run
void run() override
Definition: Thread.cpp:41
yarp::os::Thread::afterStart
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
yarp::os::Thread::getKeyOfCaller
static long int getKeyOfCaller()
Get a unique identifier for the calling thread.
Definition: Thread.cpp:133
yarp::os::Time::yield
void yield()
The calling thread releases its remaining quantum upon calling this function.
Definition: Time.cpp:141
Thread.h
yarp::os::Thread::run
virtual void run()=0
Main body of the new thread.
yarp::os::Thread::Private::Private
Private(Thread &owner)
Definition: Thread.cpp:23
yarp::os::Thread::getPolicy
int getPolicy()
Query the current scheduling policy of the thread, if the OS supports that.
Definition: Thread.cpp:148
yarp::os::Thread::isStopping
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition: Thread.cpp:102
yarp::os::Thread::~Thread
virtual ~Thread()
Destructor.
Definition: Thread.cpp:73
yarp::os::impl::ThreadImpl
An abstraction for a thread of execution.
Definition: ThreadImpl.h:26
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::Thread::getPriority
int getPriority()
Query the current priority of the thread, if the OS supports that.
Definition: Thread.cpp:143
yarp::os::Thread::getCount
static int getCount()
Check how many threads are running.
Definition: Thread.cpp:122
yarp::os::Thread::Thread
Thread()
Constructor.
Definition: Thread.cpp:68
yarp::os::Thread::start
bool start()
Start the new thread running.
Definition: Thread.cpp:96
yarp::os::impl::ThreadImpl::close
virtual void close()
Definition: ThreadImpl.cpp:158
yarp::os::Thread::stop
bool stop()
Stop the thread.
Definition: Thread.cpp:84
yarp::os::Thread::Private::afterStart
void afterStart(bool success) override
Definition: Thread.cpp:36
yarp::os::impl
The components from which ports and connections are built.
yarp::os::Thread::Private::stopping
bool stopping
Definition: Thread.cpp:64
yarp::os::Thread::Private
Definition: Thread.cpp:18
yarp::os::Thread::setPriority
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
yarp::os::Thread::threadInit
virtual bool threadInit()
Initialization method.
Definition: Thread.h:108