00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.5 $ */ 00002 /* Copyright (c) 2006, Sun Microsystems, Inc. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 00006 following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 00010 disclaimer in the documentation and/or other materials provided with the distribution. 00011 * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 00012 from this software without specific prior written permission. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00020 00021 00022 */ 00023 00024 00025 # include "incls/_precompiled.incl" 00026 # include "incls/_task.cpp.incl" 00027 00028 const int max_tasks = 10; 00029 int num_tasks = 0; 00030 00031 PeriodicTask* tasks[max_tasks]; 00032 00033 00034 bool pending_tasks(int delay_time) { 00035 bool result = false; 00036 for(int index = 0; index < num_tasks; index++) { 00037 result = tasks[index]->is_pending(delay_time) || result; 00038 } 00039 return result; 00040 } 00041 00042 void real_time_tick(int delay_time) { 00043 // Do not perform any tasks before the bootstrapping is done 00044 if (bootstrapping) return; 00045 00046 if (pending_tasks(delay_time)) { 00047 ThreadCritical tc; 00048 if (!Process::external_suspend_current()) return; 00049 00050 for(int index = 0; index < num_tasks; index++) { 00051 PeriodicTask* task = tasks[index]; 00052 if (task->counter >= task->interval) { 00053 task->task(); 00054 task->counter = 0; 00055 } 00056 } 00057 Process::external_resume_current(); 00058 } 00059 } 00060 00061 PeriodicTask::PeriodicTask(int interval_time) { 00062 counter = 0; 00063 interval = interval_time; 00064 } 00065 00066 PeriodicTask::~PeriodicTask() { 00067 if (is_enrolled()) 00068 deroll(); 00069 } 00070 00071 bool PeriodicTask::is_enrolled() const { 00072 for(int index = 0; index < num_tasks; index++) 00073 if (tasks[index] == this) return true; 00074 return false; 00075 } 00076 00077 void PeriodicTask::enroll() { 00078 if (num_tasks == max_tasks) 00079 fatal("Overflow in PeriodicTask table"); 00080 tasks[num_tasks++] = this; 00081 } 00082 00083 void PeriodicTask::deroll() { 00084 int index; 00085 for(index = 0; index < num_tasks && tasks[index] != this; index++); 00086 if (index == max_tasks) return; 00087 num_tasks--; 00088 for (; index < num_tasks; index++) { 00089 tasks[index] = tasks[index+1]; 00090 } 00091 }