00001 /************************************************************************** 00002 Lightspark, a free flash player implementation 00003 00004 Copyright (C) 2009,2010 Alessandro Pignotti (a.pignotti@sssup.it) 00005 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 **************************************************************************/ 00019 00020 #ifndef _TIMER_H 00021 #define _TIMER_H 00022 00023 #include "compat.h" 00024 #include <list> 00025 #include <pthread.h> 00026 #include <time.h> 00027 #include <semaphore.h> 00028 #include <inttypes.h> 00029 #include "thread_pool.h" 00030 00031 namespace lightspark 00032 { 00033 00034 //Jobs that run on tick are supposed to be very short 00035 //For longer jobs use ThreadPool 00036 class ITickJob 00037 { 00038 public: 00039 virtual void tick()=0; 00040 virtual ~ITickJob(){}; 00041 }; 00042 00043 uint64_t timespecToUsecs(timespec t); 00044 uint64_t timespecToMsecs(timespec t); 00045 timespec msecsToTimespec(uint64_t time); 00046 00047 typedef void* (*thread_worker)(void*); 00048 class TimerThread 00049 { 00050 private: 00051 class TimingEvent 00052 { 00053 public: 00054 bool isTick; 00055 ITickJob* job; 00056 //Timing are in milliseconds 00057 uint64_t timing; 00058 uint32_t tickTime; 00059 }; 00060 sem_t mutex; 00061 sem_t newEvent; 00062 pthread_t t; 00063 std::list<TimingEvent*> pendingEvents; 00064 SystemState* m_sys; 00065 ITickJob* volatile currentJob; 00066 bool stopped; 00067 static void* timer_worker(TimerThread*); 00068 void insertNewEvent(TimingEvent* e); 00069 void insertNewEvent_nolock(TimingEvent* e); 00070 void dumpJobs(); 00071 public: 00072 TimerThread(SystemState* s); 00073 void stop(); 00074 void wait(); 00075 ~TimerThread(); 00076 void addTick(uint32_t tickTime, ITickJob* job); 00077 void addWait(uint32_t waitTime, ITickJob* job); 00078 //Returns if the job has been found or not 00079 //If the canceled job is currently executing this waits for it to complete 00080 bool removeJob(ITickJob* job); 00081 }; 00082 00083 class Chronometer 00084 { 00085 private: 00086 uint64_t start; 00087 public: 00088 Chronometer(); 00089 uint32_t checkpoint(); 00090 }; 00091 00092 }; 00093 #endif
1.6.3