cppthread 1.1.16
C++ Thread Library
runner.cpp
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
27// self
28//
29#include "cppthread/runner.h"
30
31#include "cppthread/guard.h"
32#include "cppthread/log.h"
33#include "cppthread/thread.h"
34
35
36// last include
37//
38#include <snapdev/poison.h>
39
40
41
42
43namespace cppthread
44{
45
46
47
65runner::runner(std::string const & name)
66 : f_name(name)
67{
68 // TBD: should we forbid starting a runner without a name?
69}
70
71
79{
80 // the thread should never be set when the runner gets deleted
81 if(f_thread)
82 {
83 // this is a bug; it could be that the object that derived from
84 // the snap_runner calls gets destroyed under the thread controller's
85 // nose and that could break a lot of things.
86 log << log_level_t::fatal
87 << "The Snap! thread runner named \""
88 << f_name
89 << "\" is still marked as running when its object is being destroyed."
90 << end;
91 std::terminate();
92 }
93}
94
95
106std::string const & runner::get_name() const
107{
108 return f_name;
109}
110
111
121{
122 return true;
123}
124
125
154{
155 guard lock(f_mutex);
156 if(f_thread == nullptr)
157 {
158 return true;
159 }
160 return !f_thread->is_stopping();
161}
162
163
184{
185 // there is a mutex lock in the get_thread_tid() so try to avoid a
186 // potential deadlock by getting the value ahead
187 //
188 pid_t const tid(f_thread->get_thread_tid());
189
190 log << log_level_t::info
191 << "entering thread \""
192 << get_name()
193 << "\" #"
194 << tid
195 << "."
196 << end;
197}
198
199
232{
233 // there is a mutex lock in the get_thread_tid() so try to avoid a
234 // potential deadlock by getting the value ahead
235 //
236 pid_t const tid(f_thread->get_thread_tid());
237
238 log << log_level_t::info
239 << "leaving thread \""
240 << get_name()
241 << "\" #"
242 << tid
243 << " with status "
244 << static_cast<int>(status) // TODO: write name too
245 << "."
246 << end;
247}
248
249
259{
260 return f_thread;
261}
262
263
274pid_t runner::gettid() const
275{
276 return f_thread->get_thread_tid();
277}
278
279
280
409} // namespace cppthread
410// vim: ts=4 sw=4 et
Lock a mutex in an RAII manner.
Definition guard.h:42
virtual bool continue_running() const
Whether the thread should continue running.
Definition runner.cpp:153
virtual ~runner()
The destructor checks that the thread was stopped.
Definition runner.cpp:78
mutex f_mutex
The mutex of this thread.
Definition runner.h:86
runner(std::string const &name)
Initializes the runner.
Definition runner.cpp:65
std::string const f_name
The name of this thread.
Definition runner.h:99
virtual void enter()
Signal that the run() function is about to be entered.
Definition runner.cpp:183
thread * get_thread() const
Retrieve the thread controller linked to this runner.
Definition runner.cpp:258
std::string const & get_name() const
Retrieve the name of the runner.
Definition runner.cpp:106
virtual void leave(leave_status_t status)
Signal that the run() function has returned.
Definition runner.cpp:231
thread * f_thread
A pointer back to the owner ("parent") of this runner.
Definition runner.h:98
virtual bool is_ready() const
Check whether this thread runner is ready.
Definition runner.cpp:120
pid_t gettid() const
Get this runner thread identifier.
Definition runner.cpp:274
A thread object that ensures proper usage of system threads.
Definition thread.h:64
bool is_stopping() const
Check whether the thread was asked to stop.
Definition thread.cpp:269
pid_t get_thread_tid() const
Retrieve the thread identifier of this thread.
Definition thread.cpp:726
Thread Runner and Managers.
logger log
The logger object used to send logs out.
Definition log.cpp:90
Declaration of the log class used to send error messages.
logger & end(logger &l)
Close a log statement.
Definition log.h:90
Thread Runner and Managers.
leave_status_t
The exit status.
Definition runner.h:53
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.