00001 /* -*- Mode: c++ -*- 00002 * 00003 * Copyright (C) 2010 Jim Stichnoth, Daniel Kristjansson 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 00020 #include "mythsignalingtimer.h" 00021 #include "mythlogging.h" 00022 00023 MythSignalingTimer::MythSignalingTimer( 00024 QObject *parent, const char *slot) : 00025 QObject(parent), MThread("SignalingTimer"), 00026 dorun(false), running(false), microsec(0) 00027 { 00028 connect(this, SIGNAL(timeout()), parent, slot, 00029 Qt::QueuedConnection); 00030 } 00031 00032 MythSignalingTimer::~MythSignalingTimer() 00033 { 00034 stop(); 00035 wait(); 00036 } 00037 00038 void MythSignalingTimer::start(int msec) 00039 { 00040 if (msec <= 0) 00041 return; 00042 00043 microsec = 1000 * msec; 00044 00045 QMutexLocker locker(&startStopLock); 00046 if (!running) 00047 { 00048 dorun = true; 00049 MThread::start(); 00050 while (dorun && !running) 00051 usleep(10 * 1000); 00052 } 00053 } 00054 00055 void MythSignalingTimer::stop(void) 00056 { 00057 if (is_current_thread(this)) 00058 { 00059 dorun = false; 00060 return; 00061 } 00062 00063 QMutexLocker locker(&startStopLock); 00064 if (running) 00065 { 00066 dorun = false; 00067 wait(); 00068 } 00069 } 00070 00071 void MythSignalingTimer::run(void) 00072 { 00073 running = true; 00074 RunProlog(); 00075 while (dorun) 00076 { 00077 usleep(microsec); 00078 if (dorun) 00079 emit timeout(); 00080 } 00081 RunEpilog(); 00082 running = false; 00083 }
1.6.3