Fawkes API
Fawkes Development Version
thread.cpp
1
2
/***************************************************************************
3
* thread.cpp - Fawkes Example Plugin Thread
4
*
5
* Generated: Wed Nov 22 17:13:57 2006
6
* Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7
*
8
****************************************************************************/
9
10
/* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU Library General Public License for more details.
19
*
20
* Read the full text in the LICENSE.GPL file in the doc directory.
21
*/
22
23
#include <plugins/examples/basics/thread.h>
24
25
#include <unistd.h>
26
27
using namespace
std;
28
using namespace
fawkes
;
29
30
/** @class ExampleThread thread.h <plugins/examples/basics/thread.h>
31
* Thread of example plugin.
32
* @author Tim Niemueller
33
*/
34
35
/** Constructor.
36
* @param hook hook to register this thread for
37
* @param name thread name
38
* @param modc modulo count, every modc iterations a message is printed to stdout
39
*/
40
ExampleThread::ExampleThread
(
BlockedTimingAspect::WakeupHook
hook,
41
const
char
* name,
42
unsigned
int
modc)
43
:
Thread
(name,
Thread
::OPMODE_WAITFORWAKEUP),
BlockedTimingAspect
(hook)
44
{
45
this->modc = modc;
46
m = 0;
47
}
48
49
/** Destructor. */
50
ExampleThread::~ExampleThread
()
51
{
52
/** We cannot do the following:
53
* logger->log_info("ExampleThread", "Destroying thread %s", name());
54
*
55
* The reason: We do not know if this thread has been successfully initialized.
56
* It could be, that any other thread that is in the same thread list as this
57
* thread failed to initialize, before the current thread has been initialized.
58
* In this case the LoggingAspect has not been initialized and thus logger is
59
* undefined and this would cause a fatal segfault.
60
*/
61
}
62
63
void
64
ExampleThread::init
()
65
{
66
/* Try this code to see a failing init in the middle of the thread list.
67
if ( blockedTimingAspectHook() == WAKEUP_HOOK_WORLDSTATE ) {
68
throw Exception("Boom!");
69
}
70
*/
71
logger
->
log_info
(
"ExampleThread"
,
"%s::init() called"
,
name
());
72
}
73
74
void
75
ExampleThread::finalize
()
76
{
77
logger
->
log_info
(
"ExampleThread"
,
"%s::finalize() called"
,
name
());
78
}
79
80
/** Thread loop.
81
* If num iterations module modc is 0 print out messaege, otherwise do nothing.
82
*/
83
void
84
ExampleThread::loop
()
85
{
86
if
((m % modc) == 0) {
87
logger
->
log_info
(
"ExampleThread"
,
"ExampleThread %s called %u times"
,
name
(), m);
88
}
89
++m;
90
usleep(0);
91
}
ExampleThread::ExampleThread
ExampleThread(fawkes::BlockedTimingAspect::WakeupHook hook, const char *name, unsigned int modc)
Constructor.
Definition:
thread.cpp:39
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
ExampleThread::finalize
virtual void finalize()
Finalize the thread.
Definition:
thread.cpp:74
fawkes::BlockedTimingAspect
Definition:
blocked_timing.h:54
fawkes::Thread::name
const char * name() const
Definition:
thread.h:99
fawkes::LoggingAspect::logger
Logger * logger
Definition:
logging.h:50
fawkes
ExampleThread::~ExampleThread
virtual ~ExampleThread()
Destructor.
Definition:
thread.cpp:49
fawkes::BlockedTimingAspect::WakeupHook
WakeupHook
Type to define at which hook the thread is woken up.
Definition:
blocked_timing.h:62
fawkes::Thread
Definition:
thread.h:44
ExampleThread::init
virtual void init()
Initialize the thread.
Definition:
thread.cpp:63
ExampleThread::loop
virtual void loop()
Thread loop.
Definition:
thread.cpp:83
src
plugins
examples
basics
thread.cpp
Generated by
1.8.16