PageRenderTime 22ms CodeModel.GetById 10ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  29. #include "llrun.h"
  30. #include "llframetimer.h"
  31. static const LLRunner::run_handle_t INVALID_RUN_HANDLE = 0;
  32. /**
  33. * LLRunner
  34. */
  35. LLRunner::LLRunner() :
  36. mNextHandle(1)
  37. {
  38. }
  39. LLRunner::~LLRunner()
  40. {
  41. mRunOnce.clear();
  42. mRunEvery.clear();
  43. }
  44. S32 LLRunner::run()
  45. {
  46. // We collect all of the runnables which should be run. Since the
  47. // runnables are allowed to adjust the run list, we need to copy
  48. // them into a temporary structure which then iterates over them
  49. // to call out of this method into the runnables.
  50. F64 now = LLFrameTimer::getTotalSeconds();
  51. run_list_t run_now;
  52. // Collect the run once. We erase the matching ones now because
  53. // it's easier. If we find a reason to keep them around for a
  54. // while, we can restructure this method.
  55. LLRunner::run_list_t::iterator iter = mRunOnce.begin();
  56. for( ; iter != mRunOnce.end(); )
  57. {
  58. if(now > (*iter).mNextRunAt)
  59. {
  60. run_now.push_back(*iter);
  61. iter = mRunOnce.erase(iter);
  62. }
  63. else
  64. {
  65. ++iter;
  66. }
  67. }
  68. // Collect the ones that repeat.
  69. iter = mRunEvery.begin();
  70. LLRunner::run_list_t::iterator end = mRunEvery.end();
  71. for( ; iter != end; ++iter )
  72. {
  73. if(now > (*iter).mNextRunAt)
  74. {
  75. (*iter).mNextRunAt = now + (*iter).mIncrement;
  76. run_now.push_back(*iter);
  77. }
  78. }
  79. // Now, run them.
  80. iter = run_now.begin();
  81. end = run_now.end();
  82. for( ; iter != end; ++iter )
  83. {
  84. (*iter).mRunnable->run(this, (*iter).mHandle);
  85. }
  86. return run_now.size();
  87. }
  88. LLRunner::run_handle_t LLRunner::addRunnable(
  89. run_ptr_t runnable,
  90. ERunSchedule schedule,
  91. F64 seconds)
  92. {
  93. if(!runnable) return INVALID_RUN_HANDLE;
  94. run_handle_t handle = mNextHandle++;
  95. F64 next_run = LLFrameTimer::getTotalSeconds() + seconds;
  96. LLRunInfo info(handle, runnable, schedule, next_run, seconds);
  97. switch(schedule)
  98. {
  99. case RUN_IN:
  100. // We could optimize this a bit by sorting this on entry.
  101. mRunOnce.push_back(info);
  102. break;
  103. case RUN_EVERY:
  104. mRunEvery.push_back(info);
  105. break;
  106. default:
  107. handle = INVALID_RUN_HANDLE;
  108. break;
  109. }
  110. return handle;
  111. }
  112. LLRunner::run_ptr_t LLRunner::removeRunnable(LLRunner::run_handle_t handle)
  113. {
  114. LLRunner::run_ptr_t rv;
  115. LLRunner::run_list_t::iterator iter = mRunOnce.begin();
  116. LLRunner::run_list_t::iterator end = mRunOnce.end();
  117. for( ; iter != end; ++iter)
  118. {
  119. if((*iter).mHandle == handle)
  120. {
  121. rv = (*iter).mRunnable;
  122. mRunOnce.erase(iter);
  123. return rv;
  124. }
  125. }
  126. iter = mRunEvery.begin();
  127. end = mRunEvery.end();
  128. for( ; iter != end; ++iter)
  129. {
  130. if((*iter).mHandle == handle)
  131. {
  132. rv = (*iter).mRunnable;
  133. mRunEvery.erase(iter);
  134. return rv;
  135. }
  136. }
  137. return rv;
  138. }
  139. /**
  140. * LLRunner::LLRunInfo
  141. */
  142. LLRunner::LLRunInfo::LLRunInfo(
  143. run_handle_t handle,
  144. run_ptr_t runnable,
  145. ERunSchedule schedule,
  146. F64 next_run_after,
  147. F64 increment) :
  148. mHandle(handle),
  149. mRunnable(runnable),
  150. mSchedule(schedule),
  151. mNextRunAt(next_run_after),
  152. mIncrement(increment)
  153. {
  154. }
  155. LLRunnable::LLRunnable()
  156. { }
  157. // virtual
  158. LLRunnable::~LLRunnable()
  159. { }