⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gmain_timeout.c

📁 linux集群服务器软件代码包
💻 C
字号:
/* $Id: Gmain_timeout.c,v 1.7 2004/09/14 15:07:29 gshi Exp $ *//* * Glib mainloop timeout handling code. * * These functions work correctly even if someone resets the  * time-of-day clock.  The g_main_timeout_add() function does not have * this property, since it relies on gettimeofday(). * * Our functions have the same semantics - except they always work ;-) * * This is because we use longclock_t for our time values. * * Copyright (c) 2002 Alan Robertson <alanr@unix.sh> * * * 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 * */#include <glib.h>#include <clplumbing/longclock.h>#include <clplumbing/Gmain_timeout.h>#include <string.h>#define GETAPPEND(src)	(struct GTimeoutAppend*)(src +1)static gbooleanGmain_timeout_prepare(GSource* src,  gint* timeout);static gbooleanGmain_timeout_check(GSource* src);static gbooleanGmain_timeout_dispatch(GSource* src, GSourceFunc func, gpointer user_data);static GSourceFuncs Gmain_timeout_funcs = {	prepare: Gmain_timeout_prepare,	check: Gmain_timeout_check,	dispatch: Gmain_timeout_dispatch,};struct GTimeoutAppend {	longclock_t	nexttime;	guint		interval;};guintGmain_timeout_add(guint interval,	GSourceFunc	function,	gpointer	data){	return Gmain_timeout_add_full(G_PRIORITY_DEFAULT	,	interval, function, data, NULL);}guintGmain_timeout_add_full(gint priority,	guint interval,	GSourceFunc	function,	gpointer	data,	GDestroyNotify	notify){		struct GTimeoutAppend* append;		GSource* source = g_source_new( &Gmain_timeout_funcs, 					sizeof(GSource)					+ sizeof(struct GTimeoutAppend));		append = GETAPPEND(source); 		memset(append, 0, sizeof(struct GTimeoutAppend));	append->nexttime = add_longclock(time_longclock()					 ,msto_longclock(interval));  	append->interval = interval; 		g_source_set_priority(source, priority);		g_source_set_can_recurse(source, FALSE);		g_source_set_callback(source, function, data, notify); 		return g_source_attach(source, NULL);}voidGmain_timeout_remove(guint tag){	GSource* source = g_main_context_find_source_by_id(NULL,tag);		if (source != NULL){		g_source_destroy(source);			}							   	}/* g_main_loop-style prepare function */static gbooleanGmain_timeout_prepare(GSource* src,  gint* timeout){		struct GTimeoutAppend* append = GETAPPEND(src);	longclock_t	lnow = time_longclock();	longclock_t	remain;		if (cmp_longclock(lnow, append->nexttime) >= 0) {		*timeout = 0L;		return TRUE;	}	/* This is safe - we will always have a positive result */	remain = sub_longclock(append->nexttime, lnow);	/* This is also safe - we started out in 'ms' */	*timeout = longclockto_ms(remain);	return ((*timeout) == 0);}/* g_main_loop-style check function */static gbooleanGmain_timeout_check    (GSource* src){	struct GTimeoutAppend* append = GETAPPEND(src);	longclock_t	lnow = time_longclock();		if (cmp_longclock(lnow, append->nexttime) >= 0) {		return TRUE;	}	return FALSE;}/* g_main_loop-style dispatch function */static gbooleanGmain_timeout_dispatch(GSource* src, GSourceFunc func, gpointer user_data){	struct GTimeoutAppend* append = GETAPPEND(src);	/* Schedule our next dispatch */	append->nexttime = add_longclock(time_longclock()					  , msto_longclock(append->interval));	/* Then call the user function */	return func(user_data);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -