HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ThreadedIO.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_ThreadedIO.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_ThreadedIO__
12 #define __UT_ThreadedIO__
13 
14 #include "UT_API.h"
15 #include "UT_NonCopyable.h"
16 #include "UT_Thread.h"
17 #include "UT_ThreadQueue.h"
18 #include "UT_UniquePtr.h"
19 
20 class UT_ThreadedIOTask;
21 
22 ///
23 /// UT_ThreadedIO__ takes care of all the thread administration that
24 /// threaded io requires.
25 ///
26 /// Its features are to maintain single global IO thread
27 /// to which all desired write jobs are posted.
28 ///
30 {
31 public:
32  UT_ThreadedIO(exint maxqueuebytes);
33  ~UT_ThreadedIO();
34 
36 
37  /// Blocks until next IO task is done. Returns the number of
38  /// tasks left.
39  /// Will return immediatenly if no active or pending tasks.
40  int awaitIOTaskDone() { return awaitIOTaskDoneMS(-1); }
41  /// Blocks until next IO task is done or timeout hit,
42  /// Negative timeout will block forever.
43  /// Returns immediately if no active or inflight tasks.
44  int awaitIOTaskDoneMS(int64 timeout_ms);
45 
46  /// Current task count. Includes those blocking waiting to
47  /// be added. Includes those still processing.
48  int numTasks() const;
49 
50  /// Adds the task to the IO queue.
51  /// This will block until there is enough free space
52  /// on the queue.
53  void postTask(UT_UniquePtr<UT_ThreadedIOTask> task);
54 
55 private:
56  static void *threadCB(void *data);
57  void exec();
58 
60  myQueue;
61  UT_UniquePtr<UT_Thread> myIOThread;
62 
63  SYS_AtomicInt32 myTaskCount;
64 
65 };
66 
67 ///
68 /// IO tasks should be subclassed from this.
69 /// Your destructor will be called on the io thread!
70 ///
72 {
73 public:
75  virtual ~UT_ThreadedIOTask() {}
77 
78  /// This is invoked when it is your turn to write.
79  /// when you exit from this, your task will be destructed.
80  virtual void doWrite() = 0;
81 
82  /// A rough estimate of the number of bytes of RAM are
83  /// lost by waiting for this task to finish. This avoids
84  /// us spooling up too much and swapping.
85  virtual exint memoryEstimate() const = 0;
86 };
87 
89 
90 
91 #endif
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
int64 exint
Definition: SYS_Types.h:125
virtual ~UT_ThreadedIOTask()
Definition: UT_ThreadedIO.h:75
#define UT_API
Definition: UT_API.h:14
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
UT_API UT_ThreadedIO * UTgetIOThread()
Definition: format.h:895