PageRenderTime 34ms CodeModel.GetById 24ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llrun.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 174 lines | 115 code | 15 blank | 44 comment | 17 complexity | 2fc89e8c1a4b1fc896d7e86ec68558a9 MD5 | raw file
  1/** 
  2 * @file llrun.cpp
  3 * @author Phoenix
  4 * @date 2006-02-16
  5 * @brief Implementation of the LLRunner and related classes
  6 *
  7 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#include "linden_common.h"
 30#include "llrun.h"
 31
 32#include "llframetimer.h"
 33
 34static const LLRunner::run_handle_t INVALID_RUN_HANDLE = 0;
 35
 36/** 
 37 * LLRunner
 38 */
 39LLRunner::LLRunner() :
 40	mNextHandle(1)
 41{
 42}
 43
 44LLRunner::~LLRunner()
 45{
 46	mRunOnce.clear();
 47	mRunEvery.clear();
 48}
 49
 50S32 LLRunner::run()
 51{
 52	// We collect all of the runnables which should be run. Since the
 53	// runnables are allowed to adjust the run list, we need to copy
 54	// them into a temporary structure which then iterates over them
 55	// to call out of this method into the runnables.
 56	F64 now = LLFrameTimer::getTotalSeconds();
 57	run_list_t run_now;
 58
 59	// Collect the run once. We erase the matching ones now because
 60	// it's easier. If we find a reason to keep them around for a
 61	// while, we can restructure this method.
 62	LLRunner::run_list_t::iterator iter = mRunOnce.begin();
 63	for( ; iter != mRunOnce.end(); )
 64	{
 65		if(now > (*iter).mNextRunAt)
 66		{
 67			run_now.push_back(*iter);
 68			iter = mRunOnce.erase(iter);
 69		}
 70		else
 71		{
 72			++iter;
 73		}
 74	}
 75
 76	// Collect the ones that repeat.
 77	iter = mRunEvery.begin();
 78	LLRunner::run_list_t::iterator end = mRunEvery.end();
 79	for( ; iter != end; ++iter )
 80	{
 81		if(now > (*iter).mNextRunAt)
 82		{
 83			(*iter).mNextRunAt = now + (*iter).mIncrement;
 84			run_now.push_back(*iter);
 85		}
 86	}
 87
 88	// Now, run them.
 89	iter = run_now.begin();
 90	end = run_now.end();
 91	for( ; iter != end; ++iter )
 92	{
 93		(*iter).mRunnable->run(this, (*iter).mHandle);
 94	}
 95	return run_now.size();
 96}
 97
 98LLRunner::run_handle_t LLRunner::addRunnable(
 99	run_ptr_t runnable,
100	ERunSchedule schedule,
101	F64 seconds)
102{
103	if(!runnable) return INVALID_RUN_HANDLE;
104	run_handle_t handle = mNextHandle++;
105	F64 next_run = LLFrameTimer::getTotalSeconds() + seconds;
106	LLRunInfo info(handle, runnable, schedule, next_run, seconds);
107	switch(schedule)
108	{
109	case RUN_IN:
110		// We could optimize this a bit by sorting this on entry.
111		mRunOnce.push_back(info);
112		break;
113	case RUN_EVERY:
114		mRunEvery.push_back(info);
115		break;
116	default:
117		handle = INVALID_RUN_HANDLE;
118		break;
119	}
120	return handle;
121}
122
123LLRunner::run_ptr_t LLRunner::removeRunnable(LLRunner::run_handle_t handle)
124{
125	LLRunner::run_ptr_t rv;
126	LLRunner::run_list_t::iterator iter = mRunOnce.begin();
127	LLRunner::run_list_t::iterator end = mRunOnce.end();
128	for( ; iter != end; ++iter)
129	{
130		if((*iter).mHandle == handle)
131		{
132			rv = (*iter).mRunnable;
133			mRunOnce.erase(iter);
134			return rv;
135		}
136	}
137
138	iter = mRunEvery.begin();
139	end = mRunEvery.end();
140	for( ; iter != end; ++iter)
141	{
142		if((*iter).mHandle == handle)
143		{
144			rv = (*iter).mRunnable;
145			mRunEvery.erase(iter);
146			return rv;
147		}
148	}
149	return rv;
150}
151
152/** 
153 * LLRunner::LLRunInfo
154 */
155LLRunner::LLRunInfo::LLRunInfo(
156	run_handle_t handle,
157	run_ptr_t runnable,
158	ERunSchedule schedule,
159	F64 next_run_after,
160	F64 increment) :
161	mHandle(handle),
162	mRunnable(runnable),
163	mSchedule(schedule),
164	mNextRunAt(next_run_after),
165	mIncrement(increment)
166{
167}
168
169LLRunnable::LLRunnable()
170{ }
171
172// virtual
173LLRunnable::~LLRunnable()
174{ }