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
27using namespace std;
28using 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 */
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. */
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
63void
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
74void
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 */
83void
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}
virtual void init()
Initialize the thread.
Definition: thread.cpp:64
virtual void loop()
Thread loop.
Definition: thread.cpp:84
ExampleThread(fawkes::BlockedTimingAspect::WakeupHook hook, const char *name, unsigned int modc)
Constructor.
Definition: thread.cpp:40
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:75
virtual ~ExampleThread()
Destructor.
Definition: thread.cpp:50
Thread aspect to use blocked timing.
WakeupHook
Type to define at which hook the thread is woken up.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.