ut_timer_libev.c

Go to the documentation of this file.
00001 #include "ut_timer.h"
00002 
00003 #include "er_errors.h"
00004 
00005 #include <ev.h>
00006 
00007 #include <errno.h>
00008 #include <stdlib.h>
00009 #include <sys/time.h>
00010 #include <time.h>
00011 
00012 struct _ut_Timer {
00013   ev_timer timer; // must be the first member
00014   void* userdata; 
00015   ut_TimerCallback* cb;
00016 };
00017 
00018 static void evCallback(EV_P_ ev_timer* w, int revents);
00019 
00020 static void setTimer(ut_Timer* self, int wait_secs)
00021 {
00022   ev_tstamp after = wait_secs; // coerce to float type of some kind
00023   // Note that ev_TYPE_set may not be called when active.
00024   ev_timer_set(&self->timer, after, 0); // no repeat
00025   ev_timer_start(EV_DEFAULT, &self->timer);
00026 }
00027 
00028 static void evCallback(EV_P_ ev_timer* w, int revents)
00029 {
00030   ut_Timer* self = (ut_Timer*)w;
00031   (*(self->cb))(self->userdata, NULL);
00032 }
00033 
00034 ut_Timer* ut_Timer_new(void* userdata, ut_TimerCallback* cb, GError** error)
00035 {
00036   ut_Timer* self = g_try_new0(ut_Timer, 1);
00037   if (G_UNLIKELY(!self)) {
00038     if (error) *error = gx_error_no_memory;
00039     return NULL;
00040   }
00041   self->userdata = userdata;
00042   self->cb = cb;
00043   ev_init(&self->timer, evCallback);
00044   return self;
00045 }
00046   
00047 void ut_Timer_destroy(ut_Timer* self)
00048 {
00049   if (self) {
00050     ut_Timer_cancel(self);
00051     g_free(self);
00052   }
00053 }
00054 
00055 gboolean ut_Timer_set_after(ut_Timer* self, int secs, GError** error)
00056 {
00057   if (!ut_Timer_is_active(self)) {
00058     setTimer(self, secs);
00059   }
00060   return TRUE;
00061 }
00062 
00063 void ut_Timer_cancel(ut_Timer* self)
00064 {
00065   // Harmless if inactive.
00066   // Frees any timer resources as well.
00067   ev_timer_stop(EV_DEFAULT, &self->timer);
00068 }
00069 
00070 gboolean ut_Timer_is_active(ut_Timer* self)
00071 {
00072   return (ev_is_active(&self->timer));
00073 }
00074 
00075 /**
00076 
00077 ut_timer_libev.c
00078 
00079 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00080 and the authors. All rights reserved.
00081 
00082 Authors: Tero Hasu <tero.hasu@hut.fi>
00083 
00084 Permission is hereby granted, free of charge, to any person
00085 obtaining a copy of this software and associated documentation files
00086 (the "Software"), to deal in the Software without restriction,
00087 including without limitation the rights to use, copy, modify, merge,
00088 publish, distribute, sublicense, and/or sell copies of the Software,
00089 and to permit persons to whom the Software is furnished to do so,
00090 subject to the following conditions:
00091 
00092 The above copyright notice and this permission notice shall be
00093 included in all copies or substantial portions of the Software.
00094 
00095 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00096 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00097 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00098 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00099 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00100 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00101 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00102 SOFTWARE.
00103 
00104  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1