asyncUpdate.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ASYNCUPDATE_H_
  23. #define _ASYNCUPDATE_H_
  24. #ifndef _PLATFORM_THREADS_THREAD_H_
  25. # include "platform/threads/thread.h"
  26. #endif
  27. #ifndef _THREADSAFEREFCOUNT_H_
  28. # include "platform/threads/threadSafeRefCount.h"
  29. #endif
  30. #ifndef _THREADSAFEDEQUE_H_
  31. # include "platform/threads/threadSafeDeque.h"
  32. #endif
  33. class IPolled;
  34. //--------------------------------------------------------------------------
  35. // Async update list.
  36. //--------------------------------------------------------------------------
  37. /// This structure keeps track of the objects that need
  38. /// updating.
  39. class AsyncUpdateList : public ThreadSafeRefCount< AsyncUpdateList >
  40. {
  41. protected:
  42. typedef ThreadSafeDeque< IPolled* > UpdateList;
  43. /// List of structures currently in the update loop.
  44. UpdateList mUpdateList;
  45. public:
  46. virtual ~AsyncUpdateList() {}
  47. /// Update the structures currently on the processing list.
  48. ///
  49. /// @param timeOut Soft limit in milliseconds on the time
  50. /// spent on flushing the list. Default of -1 means no
  51. /// limit and function will only return, if update list
  52. /// has been fully flushed.
  53. virtual void process( S32 timeOut = -1 );
  54. /// Add the structure to the update list. It will stay
  55. /// on this list, until its update() method returns false.
  56. ///
  57. /// @note This can be called on different threads.
  58. virtual void add( IPolled* ptr )
  59. {
  60. mUpdateList.pushBack( ptr );
  61. }
  62. };
  63. //--------------------------------------------------------------------------
  64. // Async update thread.
  65. //--------------------------------------------------------------------------
  66. /// Abstract baseclass for async update threads.
  67. class AsyncUpdateThread : public Thread, public ThreadSafeRefCount< AsyncUpdateThread >
  68. {
  69. public:
  70. typedef Thread Parent;
  71. protected:
  72. /// Name of this thread.
  73. String mName;
  74. /// Platform-dependent event data.
  75. void* mUpdateEvent;
  76. /// The update list processed on this thread.
  77. ThreadSafeRef< AsyncUpdateList > mUpdateList;
  78. /// Wait for an update event being triggered and
  79. /// immediately reset the event.
  80. ///
  81. /// @note Note that this must be an atomic operation to avoid
  82. /// a race condition. Immediately resetting the event shields
  83. /// us from event releases happening during us updating getting
  84. /// ignored.
  85. virtual void _waitForEventAndReset();
  86. public:
  87. /// Create the update thread.
  88. /// The thread won't immediately start (we have virtual functions
  89. /// so construction needs to finish first) and will not auto-delete
  90. /// itself.
  91. AsyncUpdateThread( String name, AsyncUpdateList* updateList );
  92. virtual ~AsyncUpdateThread();
  93. virtual void run( void* );
  94. /// Trigger the update event to notify the thread about
  95. /// pending updates.
  96. virtual void triggerUpdate();
  97. ///
  98. const String& getName() const { return mName; }
  99. ///
  100. void* getUpdateEvent() const { return mUpdateEvent; }
  101. };
  102. /// Extension to update thread that also does automatic
  103. /// periodic updates.
  104. class AsyncPeriodicUpdateThread : public AsyncUpdateThread
  105. {
  106. typedef AsyncUpdateThread Parent;
  107. protected:
  108. /// Platform-dependent timer event.
  109. void* mUpdateTimer;
  110. /// Time between periodic updates in milliseconds.
  111. U32 mIntervalMS;
  112. virtual void _waitForEventAndReset();
  113. public:
  114. enum
  115. {
  116. /// Default interval between periodic updates in milliseconds.
  117. DEFAULT_UPDATE_INTERVAL = 4000
  118. };
  119. ///
  120. AsyncPeriodicUpdateThread( String name,
  121. AsyncUpdateList* updateList,
  122. U32 intervalMS = DEFAULT_UPDATE_INTERVAL );
  123. virtual ~AsyncPeriodicUpdateThread();
  124. };
  125. #endif // _TORQUE_CORE_ASYNC_ASYNCUPDATE_H_