On Fri, Dec 17, 2010 at 04:49:26PM +0800, Wen Congyang wrote: > * tools/timer.c tools/timer.h: timer implementation > * tools/virsh.c: Initialize timer > * tools/Makefile.am: build timer > > Signed-off-by: Wen Congyang <wency@xxxxxxxxxxxxxx> > > --- > tools/Makefile.am | 1 + > tools/timer.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > tools/timer.h | 38 ++++++++++++ > tools/virsh.c | 3 + > 4 files changed, 212 insertions(+), 0 deletions(-) > create mode 100644 tools/timer.c > create mode 100644 tools/timer.h > > diff --git a/tools/Makefile.am b/tools/Makefile.am > index 8a5fb52..506265d 100644 > --- a/tools/Makefile.am > +++ b/tools/Makefile.am > @@ -38,6 +38,7 @@ virt-pki-validate.1: virt-pki-validate > > virsh_SOURCES = \ > console.c console.h \ > + timer.c timer.h \ > ../daemon/event.c ../daemon/event.h \ > virsh.c > > diff --git a/tools/timer.c b/tools/timer.c > new file mode 100644 > index 0000000..88c42bb > --- /dev/null > +++ b/tools/timer.c > @@ -0,0 +1,170 @@ > +/* > + * timer.c: timer functions > + * > + * Copyright (C) 2010 Fujitsu Limited > + * > + * This library is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * This library is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with this library; if not, write to the Free Software > + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA > + * > + * Author: Wen Congyang <wency@xxxxxxxxxxxxxx> > + */ > + > +#include <config.h> > + > +#include "daemon/event.h" > +#include "event.h" > +#include "memory.h" > +#include "threads.h" > +#include "timer.h" > +#include "virterror_internal.h" > + > +#define VIR_FROM_THIS VIR_FROM_NONE > + > +struct virTimer { > + int timer_id; > + > + int frequency; > + virTimerCallback function; > + void *opaque; > +}; > + > +/* use timerFunc to prevent the user know timer id. */ > +static void timerFunc(int timer_id ATTRIBUTE_UNUSED, void *opaque) > +{ > + virTimerPtr timer = (virTimerPtr)opaque; > + timer->function(timer->opaque); > +} > + > +virTimerPtr virNewTimer(virTimerCallback callback, void *opaque) > +{ > + virTimerPtr timer = NULL; > + > + if (VIR_ALLOC(timer) < 0) { > + virReportOOMError(); > + return NULL; > + } > + > + timer->timer_id = -1; > + timer->function = callback; > + timer->opaque = opaque; > + timer->frequency = -1; > + > + return timer; > +} > + > +int virAddTimer(virTimerPtr timer, int expire_time) > +{ > + int ret; > + > + if ((ret = virEventAddTimeout(expire_time, timerFunc, > + timer, NULL)) < 0) { > + return -1; > + } > + timer->timer_id = ret; > + return 0; > +} > + > +int virModTimer(virTimerPtr timer, int expire_time) > +{ > + if (timer->timer_id == -1) > + return virAddTimer(timer, expire_time); > + > + virEventUpdateTimeout(timer->timer_id, expire_time); > + return 0; > +} > + > +int virDelTimer(virTimerPtr timer) > +{ > + if (timer->timer_id == -1) > + return 0; > + > + if (virEventRemoveTimeout(timer->timer_id) < 0) > + return -1; > + > + timer->timer_id = -1; > + return 0; > +} > + > +void virFreeTimer(virTimerPtr timer) > +{ > + VIR_FREE(timer); > +} > + > +static int timer_initialized = 0; > +static virThread timer_thread; > +static bool timer_running = false; > +static bool timer_quit = false; > +static virMutex timer_lock; > + > +static void timerThreadFunc(void *opaque ATTRIBUTE_UNUSED) > +{ > + while(!timer_quit) { > + virEventRunOnce(); > + } > +} > + > +int virStartTimer(void) Bad name, not really start a timer. This function can be merged with virTimerInitialize(). > +{ > + int ret = -1; > + > + virMutexLock(&timer_lock); > + if (timer_running) { > + ret = 0; > + goto cleanup; > + } > + > + timer_quit = false; > + if (virThreadCreate(&timer_thread, true, timerThreadFunc, NULL) < 0) > + goto cleanup; > + > + timer_running = true; > + ret = 0; > + > +cleanup: > + virMutexUnlock(&timer_lock); > + return ret; > +} > + > +int virStopTimer(void) > +{ > + int ret = -1; > + virMutexLock(&timer_lock); > + if (!timer_running) { > + ret = 0; > + goto cleanup; > + } > + > + timer_quit = true; > + virEventInterrupt(); > + virThreadJoin(&timer_thread); > + timer_running = false; > + > +cleanup: > + virMutexUnlock(&timer_lock); > + return ret; > +} > + > +int virTimerInitialize(void) > +{ > + if (timer_initialized) > + return 0; > + > + if (virMutexInit(&timer_lock) < 0) > + return -1; > + > + timer_initialized = 1; > + timer_running = false; > + timer_quit = false; > + return 0; > +} > diff --git a/tools/timer.h b/tools/timer.h > new file mode 100644 > index 0000000..eba08ec > --- /dev/null > +++ b/tools/timer.h > @@ -0,0 +1,38 @@ > +/* > + * timer.h: structure and entry points for timer support > + * > + * Copyright (C) 2010 Fujitsu Limited > + * > + * This library is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * This library is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with this library; if not, write to the Free Software > + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA > + * > + * Author: Wen Congyang <wency@xxxxxxxxxxxxxx> > + */ > + > +#ifndef __VIR_TIMER_H__ > +# define __VIR_TIMER_H__ > + > +typedef struct virTimer virTimer; > +typedef virTimer *virTimerPtr; > +typedef void (*virTimerCallback)(void *); > + > +extern virTimerPtr virNewTimer(virTimerCallback, void *); > +extern int virAddTimer(virTimerPtr, int); > +extern int virModTimer(virTimerPtr, int); > +extern int virDelTimer(virTimerPtr); > +extern void virFreeTimer(virTimerPtr); > +extern int virStartTimer(void); > +extern int virStopTimer(void); > +extern int virTimerInitialize(void); > +#endif /* __VIR_TIMER_H__ */ > diff --git a/tools/virsh.c b/tools/virsh.c > index 4e37f2d..cbde085 100644 > --- a/tools/virsh.c > +++ b/tools/virsh.c > @@ -54,6 +54,7 @@ > #include "files.h" > #include "../daemon/event.h" > #include "configmake.h" > +#include "timer.h" > > static char *progname; > > @@ -11298,6 +11299,8 @@ vshInit(vshControl *ctl) > virEventRemoveTimeoutImpl); > virEventInit(); > > + virTimerInitialize(); > + > ctl->conn = virConnectOpenAuth(ctl->name, > virConnectAuthPtrDefault, > ctl->readonly ? VIR_CONNECT_RO : 0); > -- > 1.7.1 changes to virsh.c should be in the second patch. -- Thanks, Hu Tao -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list