00001 /** 00002 * @file ccn/schedule.h 00003 * 00004 * Event scheduling. 00005 * 00006 * Part of the CCNx C Library. 00007 * 00008 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc. 00009 * 00010 * This library is free software; you can redistribute it and/or modify it 00011 * under the terms of the GNU Lesser General Public License version 2.1 00012 * as published by the Free Software Foundation. 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. You should have received 00017 * a copy of the GNU Lesser General Public License along with this library; 00018 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, 00019 * Fifth Floor, Boston, MA 02110-1301 USA. 00020 */ 00021 00022 #ifndef CCN_SCHEDULE_DEFINED 00023 #define CCN_SCHEDULE_DEFINED 00024 00025 #include <stdint.h> 00026 00027 struct ccn_schedule; 00028 struct ccn_scheduled_event; 00029 00030 /* 00031 * This is a two-part absolute time value, which might be seconds and 00032 * microseconds but does not have to be. The interpretation depends 00033 * on the client-provided gettime object. The distance into the future 00034 * for which events may be scheduled will be limited by the number of 00035 * micros that will fit in an int. 00036 */ 00037 struct ccn_timeval { 00038 long s; 00039 unsigned micros; 00040 }; 00041 00042 struct ccn_gettime; 00043 typedef void (*ccn_gettime_action)(const struct ccn_gettime *, struct ccn_timeval *); 00044 struct ccn_gettime { 00045 char descr[8]; 00046 ccn_gettime_action gettime; 00047 unsigned micros_per_base; /* e.g., 1000000 for seconds, microseconds */ 00048 void *data; /* for private use by gettime */ 00049 }; 00050 00051 /* 00052 * The scheduled action may return a non-positive value 00053 * if the event should not be scheduled to occur again, 00054 * or a positive number of micros. If (flags & CCN_SCHEDULE_CANCEL), 00055 * the action should clean up and not reschedule itself. 00056 * The clienth is the one passed to ccn_schedule_create; event-specific 00057 * client data may be stored in ev->evdata and ev->evint. 00058 */ 00059 #define CCN_SCHEDULE_CANCEL 0x10 00060 typedef int (*ccn_scheduled_action)( 00061 struct ccn_schedule *sched, 00062 void *clienth, 00063 struct ccn_scheduled_event *ev, 00064 int flags); 00065 00066 struct ccn_scheduled_event { 00067 ccn_scheduled_action action; 00068 void *evdata; 00069 intptr_t evint; 00070 }; 00071 00072 /* 00073 * Create and destroy 00074 */ 00075 struct ccn_schedule *ccn_schedule_create(void *clienth, 00076 const struct ccn_gettime *ccnclock); 00077 void ccn_schedule_destroy(struct ccn_schedule **schedp); 00078 00079 /* 00080 * Accessor for the clock passed into create 00081 */ 00082 const struct ccn_gettime *ccn_schedule_get_gettime(struct ccn_schedule *); 00083 00084 /* 00085 * ccn_schedule_event: schedule a new event 00086 */ 00087 struct ccn_scheduled_event *ccn_schedule_event( 00088 struct ccn_schedule *sched, 00089 int micros, 00090 ccn_scheduled_action action, 00091 void *evdata, 00092 intptr_t evint); 00093 00094 /* 00095 * ccn_schedule_cancel: cancel a scheduled event 00096 * Cancels the event (calling action with CCN_SCHEDULE_CANCEL set) 00097 * Returns -1 if this is not possible. 00098 */ 00099 int ccn_schedule_cancel(struct ccn_schedule *, struct ccn_scheduled_event *); 00100 00101 /* 00102 * ccn_schedule_run: do any scheduled events 00103 * This executes any scheduled actions whose time has come. 00104 * The return value is the number of micros until the next 00105 * scheduled event, or -1 if there are none. 00106 */ 00107 int ccn_schedule_run(struct ccn_schedule *); 00108 00109 #endif