00001 00002 // 00003 // Copyright (c) 2000-2003 Intel Corporation 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // * Redistributions of source code must retain the above copyright notice, 00010 // this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above copyright notice, 00012 // this list of conditions and the following disclaimer in the documentation 00013 // and/or other materials provided with the distribution. 00014 // * Neither name of Intel Corporation nor the names of its contributors 00015 // may be used to endorse or promote products derived from this software 00016 // without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 00022 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00026 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00031 00032 #ifndef TIMERTHREAD_H 00033 #define TIMERTHREAD_H 00034 00035 #include "ithread.h" 00036 #include "LinkedList.h" 00037 #include "FreeList.h" 00038 #include "ThreadPool.h" 00039 00040 #ifdef __cplusplus 00041 extern "C" { 00042 #endif 00043 00044 #define INVALID_EVENT_ID (-10 & 1<<29) 00045 00046 //Timeout Types 00047 //absolute means in seconds from Jan 1, 1970 00048 //relative means in seconds from current time 00049 typedef enum timeoutType {ABS_SEC,REL_SEC} TimeoutType; 00050 00051 00052 /**************************************************************************** 00053 * Name: TimerThread 00054 * 00055 * Description: 00056 * A timer thread similar to the one in the Upnp SDK that allows 00057 * the scheduling of a job to run at a specified time in the future 00058 * Because the timer thread uses the thread pool there is no 00059 * gurantee of timing, only approximate timing. 00060 * Uses ThreadPool, Mutex, Condition, Thread 00061 * 00062 * 00063 *****************************************************************************/ 00064 typedef struct TIMERTHREAD 00065 { 00066 ithread_mutex_t mutex; //mutex to protect eventQ 00067 ithread_cond_t condition; //condition variable 00068 int lastEventId; //last event id 00069 LinkedList eventQ; //event q 00070 int shutdown; //whether or not we are shutdown 00071 FreeList freeEvents; //FreeList for events 00072 ThreadPool *tp; //ThreadPool to use 00073 } TimerThread; 00074 00075 00076 /**************************************************************************** 00077 * Name: TimerEvent 00078 * 00079 * Description: 00080 * 00081 * Struct to contain information for a timer event. 00082 * Internal to the TimerThread 00083 * 00084 *****************************************************************************/ 00085 typedef struct TIMEREVENT 00086 { 00087 ThreadPoolJob job; 00088 time_t eventTime; //absolute time for event in seconds since Jan 1, 1970 00089 Duration persistent; //long term or short term job 00090 int id; //id of job 00091 } TimerEvent; 00092 00093 00094 00095 00096 /************************************************************************ 00097 * Function: TimerThreadInit 00098 * 00099 * Description: 00100 * Initializes and starts timer thread. 00101 * 00102 * Parameters: 00103 * timer - valid timer thread pointer. 00104 * tp - valid thread pool to use. Must be 00105 * started. Must be valid for lifetime 00106 * of timer. Timer must be shutdown 00107 * BEFORE thread pool. 00108 * Return: 00109 * 0 on success, nonzero on failure 00110 * Returns error from ThreadPoolAddPersistent on failure. 00111 * 00112 ************************************************************************/ 00113 int TimerThreadInit(TimerThread *timer, 00114 ThreadPool *tp); 00115 00116 00117 /************************************************************************ 00118 * Function: TimerThreadSchedule 00119 * 00120 * Description: 00121 * Schedules an event to run at a specified time. 00122 * 00123 * Parameters: 00124 * timer - valid timer thread pointer. 00125 * time_t - time of event. 00126 * either in absolute seconds, 00127 * or relative seconds in the future. 00128 * timeoutType - either ABS_SEC, or REL_SEC. 00129 * if REL_SEC, then the event 00130 * will be scheduled at the 00131 * current time + REL_SEC. 00132 * job-> valid Thread pool job with following fields 00133 * func - function to schedule 00134 * arg - argument to function 00135 * priority - priority of job. 00136 * 00137 * id - id of timer event. (out, can be null) 00138 * Return: 00139 * 0 on success, nonzero on failure 00140 * EOUTOFMEM if not enough memory to schedule job. 00141 * 00142 ************************************************************************/ 00143 int TimerThreadSchedule(TimerThread* timer, 00144 time_t time, 00145 TimeoutType type, 00146 ThreadPoolJob *job, 00147 Duration duration, 00148 int *id); 00149 00150 /************************************************************************ 00151 * Function: TimerThreadRemove 00152 * 00153 * Description: 00154 * Removes an event from the timer Q. 00155 * Events can only be removed 00156 * before they have been placed in the 00157 * thread pool. 00158 * 00159 * Parameters: 00160 * timer - valid timer thread pointer. 00161 * id - id of event to remove. 00162 * ThreadPoolJob *out - space for thread pool job. 00163 * Return: 00164 * 0 on success, 00165 * INVALID_EVENT_ID on failure 00166 * 00167 ************************************************************************/ 00168 int TimerThreadRemove(TimerThread *timer, 00169 int id, 00170 ThreadPoolJob *out); 00171 00172 /************************************************************************ 00173 * Function: TimerThreadShutdown 00174 * 00175 * Description: 00176 * Shutdown the timer thread 00177 * Events scheduled in the future will NOT be run. 00178 * Timer thread should be shutdown BEFORE it's associated 00179 * thread pool. 00180 * Returns: 00181 * returns 0 if succesfull, 00182 * nonzero otherwise. 00183 * Always returns 0. 00184 ***********************************************************************/ 00185 int TimerThreadShutdown(TimerThread *timer); 00186 00187 #ifdef __cplusplus 00188 } 00189 #endif 00190 00191 #endif //TIMER_THREAD_H
1.6.1