Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * thread.cpp - Fawkes Example Plugin Thread 00004 * 00005 * Generated: Wed Nov 22 17:13:57 2006 00006 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include <plugins/examples/basics/thread.h> 00024 00025 #include <unistd.h> 00026 00027 using namespace std; 00028 using namespace fawkes; 00029 00030 /** @class ExampleThread thread.h <plugins/examples/basics/thread.h> 00031 * Thread of example plugin. 00032 * @author Tim Niemueller 00033 */ 00034 00035 /** Constructor. 00036 * @param hook hook to register this thread for 00037 * @param name thread name 00038 * @param modc modulo count, every modc iterations a message is printed to stdout 00039 */ 00040 ExampleThread::ExampleThread(BlockedTimingAspect::WakeupHook hook, const char *name, 00041 unsigned int modc) 00042 : Thread(name, Thread::OPMODE_WAITFORWAKEUP), 00043 BlockedTimingAspect(hook) 00044 { 00045 this->modc = modc; 00046 m = 0; 00047 } 00048 00049 00050 /** Destructor. */ 00051 ExampleThread::~ExampleThread() 00052 { 00053 /** We cannot do the following: 00054 * logger->log_info("ExampleThread", "Destroying thread %s", name()); 00055 * 00056 * The reason: We do not know if this thread has been successfully initialized. 00057 * It could be, that any other thread that is in the same thread list as this 00058 * thread failed to initialize, before the current thread has been initialized. 00059 * In this case the LoggingAspect has not been initialized and thus logger is 00060 * undefined and this would cause a fatal segfault. 00061 */ 00062 } 00063 00064 00065 void 00066 ExampleThread::init() 00067 { 00068 00069 /* Try this code to see a failing init in the middle of the thread list. 00070 if ( blockedTimingAspectHook() == WAKEUP_HOOK_WORLDSTATE ) { 00071 throw Exception("Boom!"); 00072 } 00073 */ 00074 logger->log_info("ExampleThread", "%s::init() called", name()); 00075 } 00076 00077 00078 void 00079 ExampleThread::finalize() 00080 { 00081 logger->log_info("ExampleThread", "%s::finalize() called", name()); 00082 } 00083 00084 00085 /** Thread loop. 00086 * If num iterations module modc is 0 print out messaege, otherwise do nothing. 00087 */ 00088 void 00089 ExampleThread::loop() 00090 { 00091 if ( (m % modc) == 0 ) { 00092 logger->log_info("ExampleThread", "ExampleThread %s called %u times", name(), m); 00093 } 00094 ++m; 00095 usleep(0); 00096 }