llrun.cpp

Go to the documentation of this file.
00001 
00034 #include "linden_common.h"
00035 #include "llrun.h"
00036 
00037 #include "llframetimer.h"
00038 
00039 static const LLRunner::run_handle_t INVALID_RUN_HANDLE = 0;
00040 
00044 LLRunner::LLRunner() :
00045         mNextHandle(1)
00046 {
00047 }
00048 
00049 LLRunner::~LLRunner()
00050 {
00051         mRunOnce.clear();
00052         mRunEvery.clear();
00053 }
00054 
00055 S32 LLRunner::run()
00056 {
00057         // We collect all of the runnables which should be run. Since the
00058         // runnables are allowed to adjust the run list, we need to copy
00059         // them into a temporary structure which then iterates over them
00060         // to call out of this method into the runnables.
00061         F64 now = LLFrameTimer::getTotalSeconds();
00062         run_list_t run_now;
00063 
00064         // Collect the run once. We erase the matching ones now because
00065         // it's easier. If we find a reason to keep them around for a
00066         // while, we can restructure this method.
00067         LLRunner::run_list_t::iterator iter = mRunOnce.begin();
00068         for( ; iter != mRunOnce.end(); )
00069         {
00070                 if(now > (*iter).mNextRunAt)
00071                 {
00072                         run_now.push_back(*iter);
00073                         iter = mRunOnce.erase(iter);
00074                 }
00075                 else
00076                 {
00077                         ++iter;
00078                 }
00079         }
00080 
00081         // Collect the ones that repeat.
00082         iter = mRunEvery.begin();
00083         LLRunner::run_list_t::iterator end = mRunEvery.end();
00084         for( ; iter != end; ++iter )
00085         {
00086                 if(now > (*iter).mNextRunAt)
00087                 {
00088                         (*iter).mNextRunAt = now + (*iter).mIncrement;
00089                         run_now.push_back(*iter);
00090                 }
00091         }
00092 
00093         // Now, run them.
00094         iter = run_now.begin();
00095         end = run_now.end();
00096         for( ; iter != end; ++iter )
00097         {
00098                 (*iter).mRunnable->run(this, (*iter).mHandle);
00099         }
00100         return run_now.size();
00101 }
00102 
00103 LLRunner::run_handle_t LLRunner::addRunnable(
00104         run_ptr_t runnable,
00105         ERunSchedule schedule,
00106         F64 seconds)
00107 {
00108         if(!runnable) return INVALID_RUN_HANDLE;
00109         run_handle_t handle = mNextHandle++;
00110         F64 next_run = LLFrameTimer::getTotalSeconds() + seconds;
00111         LLRunInfo info(handle, runnable, schedule, next_run, seconds);
00112         switch(schedule)
00113         {
00114         case RUN_IN:
00115                 // We could optimize this a bit by sorting this on entry.
00116                 mRunOnce.push_back(info);
00117                 break;
00118         case RUN_EVERY:
00119                 mRunEvery.push_back(info);
00120                 break;
00121         default:
00122                 handle = INVALID_RUN_HANDLE;
00123                 break;
00124         }
00125         return handle;
00126 }
00127 
00128 LLRunner::run_ptr_t LLRunner::removeRunnable(LLRunner::run_handle_t handle)
00129 {
00130         LLRunner::run_ptr_t rv;
00131         LLRunner::run_list_t::iterator iter = mRunOnce.begin();
00132         LLRunner::run_list_t::iterator end = mRunOnce.end();
00133         for( ; iter != end; ++iter)
00134         {
00135                 if((*iter).mHandle == handle)
00136                 {
00137                         rv = (*iter).mRunnable;
00138                         mRunOnce.erase(iter);
00139                         return rv;
00140                 }
00141         }
00142 
00143         iter = mRunEvery.begin();
00144         end = mRunEvery.end();
00145         for( ; iter != end; ++iter)
00146         {
00147                 if((*iter).mHandle == handle)
00148                 {
00149                         rv = (*iter).mRunnable;
00150                         mRunEvery.erase(iter);
00151                         return rv;
00152                 }
00153         }
00154         return rv;
00155 }
00156 
00160 LLRunner::LLRunInfo::LLRunInfo(
00161         run_handle_t handle,
00162         run_ptr_t runnable,
00163         ERunSchedule schedule,
00164         F64 next_run_after,
00165         F64 increment) :
00166         mHandle(handle),
00167         mRunnable(runnable),
00168         mSchedule(schedule),
00169         mNextRunAt(next_run_after),
00170         mIncrement(increment)
00171 {
00172 }
00173 
00174 LLRunnable::LLRunnable()
00175 { }
00176 
00177 // virtual
00178 LLRunnable::~LLRunnable()
00179 { }

Generated on Thu Jul 1 06:09:05 2010 for Second Life Viewer by  doxygen 1.4.7