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