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  */
41  unsigned int modc)
42  : Thread(name, Thread::OPMODE_WAITFORWAKEUP),
44 {
45  this->modc = modc;
46  m = 0;
47 }
48 
49 
50 /** Destructor. */
52 {
53  /** We cannot do the following:
54  * logger->log_info("ExampleThread", "Destroying thread %s", name());
55  *
56  * The reason: We do not know if this thread has been successfully initialized.
57  * It could be, that any other thread that is in the same thread list as this
58  * thread failed to initialize, before the current thread has been initialized.
59  * In this case the LoggingAspect has not been initialized and thus logger is
60  * undefined and this would cause a fatal segfault.
61  */
62 }
63 
64 
65 void
67 {
68 
69  /* Try this code to see a failing init in the middle of the thread list.
70  if ( blockedTimingAspectHook() == WAKEUP_HOOK_WORLDSTATE ) {
71  throw Exception("Boom!");
72  }
73  */
74  logger->log_info("ExampleThread", "%s::init() called", name());
75 }
76 
77 
78 void
80 {
81  logger->log_info("ExampleThread", "%s::finalize() called", name());
82 }
83 
84 
85 /** Thread loop.
86  * If num iterations module modc is 0 print out messaege, otherwise do nothing.
87  */
88 void
90 {
91  if ( (m % modc) == 0 ) {
92  logger->log_info("ExampleThread", "ExampleThread %s called %u times", name(), m);
93  }
94  ++m;
95  usleep(0);
96 }
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
virtual ~ExampleThread()
Destructor.
Definition: thread.cpp:51
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual void init()
Initialize the thread.
Definition: thread.cpp:66
ExampleThread(fawkes::BlockedTimingAspect::WakeupHook hook, const char *name, unsigned int modc)
Constructor.
Definition: thread.cpp:40
Thread aspect to use blocked timing.
WakeupHook
Type to define at which hook the thread is woken up.
virtual void loop()
Thread loop.
Definition: thread.cpp:89
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:79