asyncUpdate.h

Engine/source/platform/async/asyncUpdate.h

More...

Classes:

class

Extension to update thread that also does automatic periodic updates.

class

This structure keeps track of the objects that need updating.

class

Abstract baseclass for async update threads.

Detailed Description

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