Jamoma API  0.6.0.a19
CAPThread.h
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #if !defined(__CAPThread_h__)
42 #define __CAPThread_h__
43 
44 //==================================================================================================
45 // Includes
46 //==================================================================================================
47 
48 // System Includes
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50  #include <CoreFoundation/CFBase.h>
51 #else
52  #include <CFBase.h>
53 #endif
54 
55 #if TARGET_OS_MAC
56  #include <pthread.h>
57  #include <unistd.h>
58 #elif TARGET_OS_WIN32
59  #include <windows.h>
60 #else
61  #error Unsupported operating system
62 #endif
63 
64 //==================================================================================================
65 // CAPThread
66 //
67 // This class wraps a pthread and a Win32 thread.
68 // caution: long-running fixed priority threads can make the system unresponsive
69 //==================================================================================================
70 
71 class CAPThread
72 {
73 
74 // Types
75 public:
76  typedef void* (*ThreadRoutine)(void* inParameter);
77 
78 // Constants
79 public:
80  enum
81  {
82 #if TARGET_OS_MAC
83  kMinThreadPriority = 1,
84  kMaxThreadPriority = 63,
85  kDefaultThreadPriority = 31
86 #elif TARGET_OS_WIN32
87  kMinThreadPriority = 1,
88  kMaxThreadPriority = 31,
89  kDefaultThreadPriority = THREAD_PRIORITY_NORMAL
90 #endif
91  };
92 
93 // Construction/Destruction
94 public:
95  CAPThread(ThreadRoutine inThreadRoutine, void* inParameter, UInt32 inPriority = kDefaultThreadPriority, bool inFixedPriority=false, bool inAutoDelete=false);
96  CAPThread(ThreadRoutine inThreadRoutine, void* inParameter, UInt32 inPeriod, UInt32 inComputation, UInt32 inConstraint, bool inIsPreemptible, bool inAutoDelete=false);
97  virtual ~CAPThread();
98 
99 // Properties
100 public:
101 #if TARGET_OS_MAC
102  typedef pthread_t NativeThread;
103 
104  NativeThread GetNativeThread() { return mPThread; }
105  static NativeThread GetCurrentThread() { return pthread_self(); }
106  static bool IsNativeThreadsEqual(NativeThread a, NativeThread b) { return (a==b); }
107 
108  bool operator==(NativeThread b) { return pthread_equal(mPThread,b); }
109 
110  pthread_t GetPThread() const { return mPThread; }
111  bool IsCurrentThread() const { return (0 != mPThread) && (pthread_self() == mPThread); }
112  bool IsRunning() const { return 0 != mPThread; }
113 #elif TARGET_OS_WIN32
114  typedef unsigned long NativeThread;
115 
116  NativeThread GetNativeThread() { return mThreadID; }
117  static NativeThread GetCurrentThread() { return GetCurrentThreadId(); }
118  static bool IsNativeThreadsEqual(NativeThread a, NativeThread b) { return (a==b); }
119 
120  bool operator ==(NativeThread b) { return (mThreadID==b); }
121 
122  HANDLE GetThreadHandle() const { return mThreadHandle; }
123  UInt32 GetThreadID() const { return mThreadID; }
124  bool IsCurrentThread() const { return (0 != mThreadID) && (GetCurrentThreadId() == mThreadID); }
125  bool IsRunning() const { return 0 != mThreadID; }
126 #endif
127 
128  bool IsTimeShareThread() const { return !mTimeConstraintSet; }
129  bool IsTimeConstraintThread() const { return mTimeConstraintSet; }
130 
131  UInt32 GetPriority() const { return mPriority; }
132  UInt32 GetScheduledPriority();
133  void SetPriority(UInt32 inPriority, bool inFixedPriority=false);
134 
135  void GetTimeConstraints(UInt32& outPeriod, UInt32& outComputation, UInt32& outConstraint, bool& outIsPreemptible) const { outPeriod = mPeriod; outComputation = mComputation; outConstraint = mConstraint; outIsPreemptible = mIsPreemptible; }
136  void SetTimeConstraints(UInt32 inPeriod, UInt32 inComputation, UInt32 inConstraint, bool inIsPreemptible);
137  void ClearTimeConstraints() { SetPriority(mPriority); }
138 
139  bool WillAutoDelete() const { return mAutoDelete; }
140  void SetAutoDelete(bool b) { mAutoDelete = b; }
141 
142 #if CoreAudio_Debug
143  void DebugPriority(const char *label);
144 #endif
145 
146 // Actions
147 public:
148  virtual void Start();
149 
150 // Implementation
151 protected:
152 #if TARGET_OS_MAC
153  static void* Entry(CAPThread* inCAPThread);
154  static UInt32 getScheduledPriority(pthread_t inThread, int inPriorityKind);
155 #elif TARGET_OS_WIN32
156  static UInt32 WINAPI Entry(CAPThread* inCAPThread);
157 #endif
158 
159 #if TARGET_OS_MAC
160  pthread_t mPThread;
161  UInt32 mSpawningThreadPriority;
162 #elif TARGET_OS_WIN32
163  HANDLE mThreadHandle;
164  unsigned long mThreadID;
165 #endif
166  ThreadRoutine mThreadRoutine;
167  void* mThreadParameter;
168  SInt32 mPriority;
169  UInt32 mPeriod;
170  UInt32 mComputation;
171  UInt32 mConstraint;
172  bool mIsPreemptible;
173  bool mTimeConstraintSet;
174  bool mFixedPriority;
175  bool mAutoDelete; // delete self when thread terminates
176 };
177 
178 #endif
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.
Definition: TTObject.cpp:167