• Main Page
  • Modules
  • Classes
  • Files
  • File List

rtclock.h

00001 /*  Time Based Text - Real Time Clock for Linux
00002  *
00003  *  (C) Copyright 2006 - 2007 Denis Rojo <jaromil@dyne.org>
00004  *
00005  * This source code is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Public License as published 
00007  * by the Free Software Foundation; either version 2 of the License,
00008  * or (at your option) any later version.
00009  *
00010  * This source code 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.
00013  * Please refer to the GNU Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Public License along with
00016  * this source code; if not, write to:
00017  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  *
00019  */
00020 
00021 #ifndef __RTCLOCK_H__
00022 #define __RTCLOCK_H__
00023 
00024 
00025 #include <pthread.h>
00026 
00027 
00028 /*
00029   Clock timer abstraction
00030   using RTC in Linux/BSD
00031   multithreaded
00032  */
00033 class RTClock {
00034  public:
00035   RTClock();
00036   ~RTClock();
00037 
00038   int init();
00039 
00040   int set_freq(unsigned long freq);
00041 
00042   int start();
00043 
00044   void run();
00045 
00046   int sleep(uint64_t sec);
00047 
00048   bool quit;
00049 
00050   uint64_t msec;
00051 
00052  private:
00053 
00054   int tick();
00055 
00056   int rtcfd;
00057   unsigned long rtctime;
00058 
00059   pthread_t _thread;
00060   pthread_attr_t _attr;
00061 
00062   pthread_mutex_t _mutex;
00063   pthread_cond_t _cond;
00064 
00065   void lock() { pthread_mutex_lock(&_mutex); };
00066   void unlock() { pthread_mutex_unlock(&_mutex); };
00067 
00068   /* MUTEX MUST BE LOCKED AND UNLOCKED WHILE USING WAIT */
00069   void wait() { pthread_cond_wait(&_cond,&_mutex); };
00070   void signal() { pthread_cond_signal(&_cond); };
00071 
00072   bool sleeping;
00073 
00074  protected:
00075   // threading stuff
00076   static void* kickoff(void *arg) {
00077     ((RTClock *) arg)->run();
00078     return NULL;
00079   };
00080   
00081 };
00082 
00083 
00084 
00085 #endif

Generated on Mon Apr 9 2012 15:29:22 for TimeBasedText_code_doc by  doxygen 1.7.1