00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #ifndef MMSTIMER_H_ 00034 #define MMSTIMER_H_ 00035 00036 #include <sigc++/sigc++.h> 00037 #include <csignal> 00038 extern "C" 00039 { 00040 #include <sys/types.h> 00041 #include <unistd.h> 00042 } 00043 #include "mmstools/mmsthread.h" 00044 00045 /*! 00046 * \brief The MMSTimer provides timers. 00047 * 00048 * You can use MMSTimer for repetitive or single shot timers 00049 * \see MMSTimer(bool singleShot). After expiration of an interval 00050 * (\see start(unsigned int milliSeconds)) a signal \see timeOut emits. 00051 * <code> 00052 * void fn1() {printf("fn1 called\n);} 00053 * MMSTimer *repetitiveTimer = new MMSTimer(); 00054 * repetitiveTimer->timeOut->connect(sigc::ptr_fun(&fn1)); 00055 * // calls every 1.5 seconds fn1() 00056 * repetitiveTimer->start(1500); 00057 * 00058 * void fn2({printf("fn2 called\n")}); 00059 * MMSTimer *singleShotTimer = new MMSTimer(true); 00060 * singleShotTimer->timeOut->connect(sigc::ptr_fun(&fn2)); 00061 * // calls after 30 seconds fn2() 00062 * singleShotTimer->start(30000); 00063 * </code> 00064 */ 00065 00066 class MMSTimer: public MMSThread 00067 { 00068 public: 00069 /*! 00070 * \brief Creates a new MMSTimer. 00071 * 00072 * \param singleShot If this is set to true, the timer will 00073 * emit only once, else it will do it 00074 * repetitive. 00075 * 00076 * \note If singleShot is set to true, internally the thread will be stopped after the signal is emitted. 00077 */ 00078 MMSTimer(bool singleShot = false); 00079 00080 /*! 00081 * \brief Destroys a MMSTimer. 00082 */ 00083 ~MMSTimer(); 00084 00085 /*! 00086 * \brief Starts timer. 00087 * 00088 * \param milliSeconds the timer interval in milli seconds 00089 * \param firsttime_ms the first timeOut will be emitted after firsttime_ms milliseconds 00090 * default 0 means that the first timeOut will also be emitted after milliSeconds 00091 * 00092 * \note If the timer is stopped (see stop()), it will be restarted using restart() but with new settings. 00093 * \see stop() 00094 */ 00095 bool start(unsigned int milliSeconds, unsigned int firsttime_ms = 0); 00096 00097 /*! 00098 * \brief Restarts timer. 00099 * 00100 * A running timer stops first and no signal emits. After this 00101 * a new timer starts with the full interval (milliSeconds) set with start(). 00102 * 00103 * \see start() 00104 * \see stop() 00105 */ 00106 bool restart(); 00107 00108 /*! 00109 * \brief Stops timer. 00110 * 00111 * A running timer stops and no signal emits. 00112 * 00113 * \note No signal will be emitted anymore, but internally the thread will be hold with pthread_cond_wait(). 00114 * \see restart() 00115 */ 00116 bool stop(); 00117 00118 /*! 00119 * \brief This signal emits if the timer expires. 00120 * 00121 * Connect a function to this signal. 00122 */ 00123 sigc::signal<void> timeOut; 00124 00125 private: 00126 bool singleShot; 00127 enum { 00128 START, 00129 RESTART, 00130 STOP, 00131 QUIT 00132 } action; 00133 00134 bool firsttime; 00135 __time_t secs; 00136 long int nSecs; 00137 __time_t ft_secs; 00138 long int ft_nSecs; 00139 00140 pthread_cond_t cond; 00141 pthread_mutex_t mutex; 00142 00143 void threadMain(); 00144 }; 00145 00146 #endif /* MMSTIMER_H_ */