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();
41 
42  /// Current task count. Includes those blocking waiting to
43  /// be added. Includes those still processing.
44  int numTasks() const;
45 
46  /// Adds the task to the IO queue.
47  /// This will block until there is enough free space
48  /// on the queue.
49  void postTask(UT_UniquePtr<UT_ThreadedIOTask> task);
50 
51 private:
52  static void *threadCB(void *data);
53  void exec();
54 
56  myQueue;
57  UT_UniquePtr<UT_Thread> myIOThread;
58 
59  SYS_AtomicInt32 myTaskCount;
60 
61 };
62 
63 ///
64 /// IO tasks should be subclassed from this.
65 /// Your destructor will be called on the io thread!
66 ///
68 {
69 public:
71  virtual ~UT_ThreadedIOTask() {}
73 
74  /// This is invoked when it is your turn to write.
75  /// when you exit from this, your task will be destructed.
76  virtual void doWrite() = 0;
77 
78  /// A rough estimate of the number of bytes of RAM are
79  /// lost by waiting for this task to finish. This avoids
80  /// us spooling up too much and swapping.
81  virtual exint memoryEstimate() const = 0;
82 };
83 
85 
86 
87 #endif
int64 exint
Definition: SYS_Types.h:125
virtual ~UT_ThreadedIOTask()
Definition: UT_ThreadedIO.h:71
#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.
#define const
Definition: zconf.h:214
UT_API UT_ThreadedIO * UTgetIOThread()
Definition: format.h:895