Threads.h
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:16k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*
  2.      File:       Threads.h
  3.  
  4.      Contains:   Thread Manager Interfaces.
  5.  
  6.      Version:    Technology: Mac OS 8
  7.                  Release:    QuickTime 6.0.2
  8.  
  9.      Copyright:  (c) 1991-2001 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:      For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. */
  17. #ifndef __THREADS__
  18. #define __THREADS__
  19. #ifndef __MACTYPES__
  20. #include "MacTypes.h"
  21. #endif
  22. #ifndef __MIXEDMODE__
  23. #include "MixedMode.h"
  24. #endif
  25. #ifndef __MACERRORS__
  26. #include "MacErrors.h"
  27. #endif
  28. #if PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #if PRAGMA_IMPORT
  35. #pragma import on
  36. #endif
  37. #if PRAGMA_STRUCT_ALIGN
  38.     #pragma options align=mac68k
  39. #elif PRAGMA_STRUCT_PACKPUSH
  40.     #pragma pack(push, 2)
  41. #elif PRAGMA_STRUCT_PACK
  42.     #pragma pack(2)
  43. #endif
  44. /* Thread states*/
  45. typedef UInt16 ThreadState;
  46. enum {
  47.     kReadyThreadState           = 0,
  48.     kStoppedThreadState         = 1,
  49.     kRunningThreadState         = 2
  50. };
  51. /* Error codes have been meoved to Errors.(pah)*/
  52. /* Thread environment characteristics*/
  53. typedef void *                          ThreadTaskRef;
  54. /* Thread characteristics*/
  55. typedef UInt32 ThreadStyle;
  56. enum {
  57.     kCooperativeThread          = 1L << 0,
  58.     kPreemptiveThread           = 1L << 1
  59. };
  60. /* Thread identifiers*/
  61. typedef UInt32 ThreadID;
  62. enum {
  63.     kNoThreadID                 = 0,
  64.     kCurrentThreadID            = 1,
  65.     kApplicationThreadID        = 2
  66. };
  67. /* Options when creating a thread*/
  68. typedef UInt32 ThreadOptions;
  69. enum {
  70.     kNewSuspend                 = (1 << 0),
  71.     kUsePremadeThread           = (1 << 1),
  72.     kCreateIfNeeded             = (1 << 2),
  73.     kFPUNotNeeded               = (1 << 3),
  74.     kExactMatchThread           = (1 << 4)
  75. };
  76. /* Information supplied to the custom scheduler*/
  77. struct SchedulerInfoRec {
  78.     UInt32                          InfoRecSize;
  79.     ThreadID                        CurrentThreadID;
  80.     ThreadID                        SuggestedThreadID;
  81.     ThreadID                        InterruptedCoopThreadID;
  82. };
  83. typedef struct SchedulerInfoRec         SchedulerInfoRec;
  84. typedef SchedulerInfoRec *              SchedulerInfoRecPtr;
  85. /*
  86.     The following ProcPtrs cannot be interchanged with UniversalProcPtrs because
  87.     of differences between 680x0 and PowerPC runtime architectures with regard to
  88.     the implementation of the Thread Manager.
  89.  */
  90. typedef void *                          voidPtr;
  91. /* Prototype for thread's entry (main) routine*/
  92. typedef CALLBACK_API( voidPtr , ThreadEntryProcPtr )(void *threadParam);
  93. /* Prototype for custom thread scheduler routine*/
  94. typedef CALLBACK_API( ThreadID , ThreadSchedulerProcPtr )(SchedulerInfoRecPtr schedulerInfo);
  95. /* Prototype for custom thread switcher routine*/
  96. typedef CALLBACK_API( void , ThreadSwitchProcPtr )(ThreadID threadBeingSwitched, void *switchProcParam);
  97. /* Prototype for thread termination notification routine*/
  98. typedef CALLBACK_API( void , ThreadTerminationProcPtr )(ThreadID threadTerminated, void *terminationProcParam);
  99. /* Prototype for debugger NewThread notification*/
  100. typedef CALLBACK_API( void , DebuggerNewThreadProcPtr )(ThreadID threadCreated);
  101. /* Prototype for debugger DisposeThread notification*/
  102. typedef CALLBACK_API( void , DebuggerDisposeThreadProcPtr )(ThreadID threadDeleted);
  103. /* Prototype for debugger schedule notification*/
  104. typedef CALLBACK_API( ThreadID , DebuggerThreadSchedulerProcPtr )(SchedulerInfoRecPtr schedulerInfo);
  105. typedef TVECTOR_UPP_TYPE(ThreadEntryProcPtr)                    ThreadEntryUPP;
  106. typedef TVECTOR_UPP_TYPE(ThreadSchedulerProcPtr)                ThreadSchedulerUPP;
  107. typedef TVECTOR_UPP_TYPE(ThreadSwitchProcPtr)                   ThreadSwitchUPP;
  108. typedef TVECTOR_UPP_TYPE(ThreadTerminationProcPtr)              ThreadTerminationUPP;
  109. typedef TVECTOR_UPP_TYPE(DebuggerNewThreadProcPtr)              DebuggerNewThreadUPP;
  110. typedef TVECTOR_UPP_TYPE(DebuggerDisposeThreadProcPtr)          DebuggerDisposeThreadUPP;
  111. typedef TVECTOR_UPP_TYPE(DebuggerThreadSchedulerProcPtr)        DebuggerThreadSchedulerUPP;
  112. #if OPAQUE_UPP_TYPES
  113.     EXTERN_API(ThreadEntryUPP)
  114.     NewThreadEntryUPP              (ThreadEntryProcPtr      userRoutine);
  115.     EXTERN_API(ThreadSchedulerUPP)
  116.     NewThreadSchedulerUPP          (ThreadSchedulerProcPtr  userRoutine);
  117.     EXTERN_API(ThreadSwitchUPP)
  118.     NewThreadSwitchUPP             (ThreadSwitchProcPtr     userRoutine);
  119.     EXTERN_API(ThreadTerminationUPP)
  120.     NewThreadTerminationUPP        (ThreadTerminationProcPtr userRoutine);
  121.     EXTERN_API(DebuggerNewThreadUPP)
  122.     NewDebuggerNewThreadUPP        (DebuggerNewThreadProcPtr userRoutine);
  123.     EXTERN_API(DebuggerDisposeThreadUPP)
  124.     NewDebuggerDisposeThreadUPP    (DebuggerDisposeThreadProcPtr userRoutine);
  125.     EXTERN_API(DebuggerThreadSchedulerUPP)
  126.     NewDebuggerThreadSchedulerUPP    (DebuggerThreadSchedulerProcPtr userRoutine);
  127.     EXTERN_API(void)
  128.     DisposeThreadEntryUPP          (ThreadEntryUPP          userUPP);
  129.     EXTERN_API(void)
  130.     DisposeThreadSchedulerUPP      (ThreadSchedulerUPP      userUPP);
  131.     EXTERN_API(void)
  132.     DisposeThreadSwitchUPP         (ThreadSwitchUPP         userUPP);
  133.     EXTERN_API(void)
  134.     DisposeThreadTerminationUPP    (ThreadTerminationUPP    userUPP);
  135.     EXTERN_API(void)
  136.     DisposeDebuggerNewThreadUPP    (DebuggerNewThreadUPP    userUPP);
  137.     EXTERN_API(void)
  138.     DisposeDebuggerDisposeThreadUPP    (DebuggerDisposeThreadUPP userUPP);
  139.     EXTERN_API(void)
  140.     DisposeDebuggerThreadSchedulerUPP    (DebuggerThreadSchedulerUPP userUPP);
  141.     EXTERN_API(voidPtr)
  142.     InvokeThreadEntryUPP           (void *                  threadParam,
  143.                                     ThreadEntryUPP          userUPP);
  144.     EXTERN_API(ThreadID)
  145.     InvokeThreadSchedulerUPP       (SchedulerInfoRecPtr     schedulerInfo,
  146.                                     ThreadSchedulerUPP      userUPP);
  147.     EXTERN_API(void)
  148.     InvokeThreadSwitchUPP          (ThreadID                threadBeingSwitched,
  149.                                     void *                  switchProcParam,
  150.                                     ThreadSwitchUPP         userUPP);
  151.     EXTERN_API(void)
  152.     InvokeThreadTerminationUPP     (ThreadID                threadTerminated,
  153.                                     void *                  terminationProcParam,
  154.                                     ThreadTerminationUPP    userUPP);
  155.     EXTERN_API(void)
  156.     InvokeDebuggerNewThreadUPP     (ThreadID                threadCreated,
  157.                                     DebuggerNewThreadUPP    userUPP);
  158.     EXTERN_API(void)
  159.     InvokeDebuggerDisposeThreadUPP    (ThreadID             threadDeleted,
  160.                                     DebuggerDisposeThreadUPP userUPP);
  161.     EXTERN_API(ThreadID)
  162.     InvokeDebuggerThreadSchedulerUPP    (SchedulerInfoRecPtr schedulerInfo,
  163.                                     DebuggerThreadSchedulerUPP userUPP);
  164. #else
  165.     #define NewThreadEntryUPP(userRoutine)                          (userRoutine)
  166.     #define NewThreadSchedulerUPP(userRoutine)                      (userRoutine)
  167.     #define NewThreadSwitchUPP(userRoutine)                         (userRoutine)
  168.     #define NewThreadTerminationUPP(userRoutine)                    (userRoutine)
  169.     #define NewDebuggerNewThreadUPP(userRoutine)                    (userRoutine)
  170.     #define NewDebuggerDisposeThreadUPP(userRoutine)                (userRoutine)
  171.     #define NewDebuggerThreadSchedulerUPP(userRoutine)              (userRoutine)
  172.     #define DisposeThreadEntryUPP(userUPP)                          
  173.     #define DisposeThreadSchedulerUPP(userUPP)                      
  174.     #define DisposeThreadSwitchUPP(userUPP)                         
  175.     #define DisposeThreadTerminationUPP(userUPP)                    
  176.     #define DisposeDebuggerNewThreadUPP(userUPP)                    
  177.     #define DisposeDebuggerDisposeThreadUPP(userUPP)                
  178.     #define DisposeDebuggerThreadSchedulerUPP(userUPP)              
  179.     #define InvokeThreadEntryUPP(threadParam, userUPP)              (*userUPP)(threadParam)
  180.     #define InvokeThreadSchedulerUPP(schedulerInfo, userUPP)        (*userUPP)(schedulerInfo)
  181.     #define InvokeThreadSwitchUPP(threadBeingSwitched, switchProcParam, userUPP)  (*userUPP)(threadBeingSwitched, switchProcParam)
  182.     #define InvokeThreadTerminationUPP(threadTerminated, terminationProcParam, userUPP)  (*userUPP)(threadTerminated, terminationProcParam)
  183.     #define InvokeDebuggerNewThreadUPP(threadCreated, userUPP)      (*userUPP)(threadCreated)
  184.     #define InvokeDebuggerDisposeThreadUPP(threadDeleted, userUPP)  (*userUPP)(threadDeleted)
  185.     #define InvokeDebuggerThreadSchedulerUPP(schedulerInfo, userUPP)  (*userUPP)(schedulerInfo)
  186. #endif
  187. /*
  188.    Thread Manager function pointers (TPP):
  189.    on classic 68k use raw function pointers (same as UPP's)
  190.    on classic PowerPC, use raw function pointers
  191.    on CFM-68K, use UPP's
  192.    on Carbon, use UPP's
  193. */
  194. #if TARGET_OS_MAC && TARGET_CPU_PPC && !TARGET_API_MAC_CARBON
  195. /* use raw function pointers*/
  196. typedef ThreadEntryProcPtr              ThreadEntryTPP;
  197. typedef ThreadSchedulerProcPtr          ThreadSchedulerTPP;
  198. typedef ThreadSwitchProcPtr             ThreadSwitchTPP;
  199. typedef ThreadTerminationProcPtr        ThreadTerminationTPP;
  200. typedef DebuggerNewThreadProcPtr        DebuggerNewThreadTPP;
  201. typedef DebuggerDisposeThreadProcPtr    DebuggerDisposeThreadTPP;
  202. typedef DebuggerThreadSchedulerProcPtr  DebuggerThreadSchedulerTPP;
  203. #else
  204. /* use UPP's*/
  205. typedef ThreadEntryUPP                  ThreadEntryTPP;
  206. typedef ThreadSchedulerUPP              ThreadSchedulerTPP;
  207. typedef ThreadSwitchUPP                 ThreadSwitchTPP;
  208. typedef ThreadTerminationUPP            ThreadTerminationTPP;
  209. typedef DebuggerNewThreadUPP            DebuggerNewThreadTPP;
  210. typedef DebuggerDisposeThreadUPP        DebuggerDisposeThreadTPP;
  211. typedef DebuggerThreadSchedulerUPP      DebuggerThreadSchedulerTPP;
  212. #endif  /* TARGET_OS_MAC && TARGET_CPU_PPC && !TARGET_API_MAC_CARBON */
  213. EXTERN_API( OSErr )
  214. NewThread                       (ThreadStyle            threadStyle,
  215.                                  ThreadEntryTPP         threadEntry,
  216.                                  void *                 threadParam,
  217.                                  Size                   stackSize,
  218.                                  ThreadOptions          options,
  219.                                  void **                threadResult,
  220.                                  ThreadID *             threadMade)                         THREEWORDINLINE(0x303C, 0x0E03, 0xABF2);
  221. EXTERN_API( OSErr )
  222. SetThreadScheduler              (ThreadSchedulerTPP     threadScheduler)                    THREEWORDINLINE(0x303C, 0x0209, 0xABF2);
  223. EXTERN_API( OSErr )
  224. SetThreadSwitcher               (ThreadID               thread,
  225.                                  ThreadSwitchTPP        threadSwitcher,
  226.                                  void *                 switchProcParam,
  227.                                  Boolean                inOrOut)                            THREEWORDINLINE(0x303C, 0x070A, 0xABF2);
  228. EXTERN_API( OSErr )
  229. SetThreadTerminator             (ThreadID               thread,
  230.                                  ThreadTerminationTPP   threadTerminator,
  231.                                  void *                 terminationProcParam)               THREEWORDINLINE(0x303C, 0x0611, 0xABF2);
  232. EXTERN_API( OSErr )
  233. SetDebuggerNotificationProcs    (DebuggerNewThreadTPP   notifyNewThread,
  234.                                  DebuggerDisposeThreadTPP  notifyDisposeThread,
  235.                                  DebuggerThreadSchedulerTPP  notifyThreadScheduler)         THREEWORDINLINE(0x303C, 0x060D, 0xABF2);
  236. EXTERN_API( OSErr )
  237. CreateThreadPool                (ThreadStyle            threadStyle,
  238.                                  SInt16                 numToCreate,
  239.                                  Size                   stackSize)                          THREEWORDINLINE(0x303C, 0x0501, 0xABF2);
  240. EXTERN_API( OSErr )
  241. GetFreeThreadCount              (ThreadStyle            threadStyle,
  242.                                  SInt16 *               freeCount)                          THREEWORDINLINE(0x303C, 0x0402, 0xABF2);
  243. EXTERN_API( OSErr )
  244. GetSpecificFreeThreadCount      (ThreadStyle            threadStyle,
  245.                                  Size                   stackSize,
  246.                                  SInt16 *               freeCount)                          THREEWORDINLINE(0x303C, 0x0615, 0xABF2);
  247. EXTERN_API( OSErr )
  248. GetDefaultThreadStackSize       (ThreadStyle            threadStyle,
  249.                                  Size *                 stackSize)                          THREEWORDINLINE(0x303C, 0x0413, 0xABF2);
  250. EXTERN_API( OSErr )
  251. ThreadCurrentStackSpace         (ThreadID               thread,
  252.                                  UInt32 *               freeStack)                          THREEWORDINLINE(0x303C, 0x0414, 0xABF2);
  253. EXTERN_API( OSErr )
  254. DisposeThread                   (ThreadID               threadToDump,
  255.                                  void *                 threadResult,
  256.                                  Boolean                recycleThread)                      THREEWORDINLINE(0x303C, 0x0504, 0xABF2);
  257. EXTERN_API( OSErr )
  258. YieldToThread                   (ThreadID               suggestedThread)                    THREEWORDINLINE(0x303C, 0x0205, 0xABF2);
  259. EXTERN_API( OSErr )
  260. YieldToAnyThread                (void)                                                      FOURWORDINLINE(0x42A7, 0x303C, 0x0205, 0xABF2);
  261. #if TARGET_OS_MAC
  262.     #define MacGetCurrentThread GetCurrentThread
  263. #endif
  264. EXTERN_API( OSErr )
  265. MacGetCurrentThread             (ThreadID *             currentThreadID)                    THREEWORDINLINE(0x303C, 0x0206, 0xABF2);
  266. EXTERN_API( OSErr )
  267. GetThreadState                  (ThreadID               threadToGet,
  268.                                  ThreadState *          threadState)                        THREEWORDINLINE(0x303C, 0x0407, 0xABF2);
  269. EXTERN_API( OSErr )
  270. SetThreadState                  (ThreadID               threadToSet,
  271.                                  ThreadState            newState,
  272.                                  ThreadID               suggestedThread)                    THREEWORDINLINE(0x303C, 0x0508, 0xABF2);
  273. EXTERN_API( OSErr )
  274. SetThreadStateEndCritical       (ThreadID               threadToSet,
  275.                                  ThreadState            newState,
  276.                                  ThreadID               suggestedThread)                    THREEWORDINLINE(0x303C, 0x0512, 0xABF2);
  277. EXTERN_API( OSErr )
  278. ThreadBeginCritical             (void)                                                      THREEWORDINLINE(0x303C, 0x000B, 0xABF2);
  279. EXTERN_API( OSErr )
  280. ThreadEndCritical               (void)                                                      THREEWORDINLINE(0x303C, 0x000C, 0xABF2);
  281. EXTERN_API( OSErr )
  282. GetThreadCurrentTaskRef         (ThreadTaskRef *        threadTRef)                         THREEWORDINLINE(0x303C, 0x020E, 0xABF2);
  283. EXTERN_API( OSErr )
  284. GetThreadStateGivenTaskRef      (ThreadTaskRef          threadTRef,
  285.                                  ThreadID               threadToGet,
  286.                                  ThreadState *          threadState)                        THREEWORDINLINE(0x303C, 0x060F, 0xABF2);
  287. EXTERN_API( OSErr )
  288. SetThreadReadyGivenTaskRef      (ThreadTaskRef          threadTRef,
  289.                                  ThreadID               threadToSet)                        THREEWORDINLINE(0x303C, 0x0410, 0xABF2);
  290. #if PRAGMA_STRUCT_ALIGN
  291.     #pragma options align=reset
  292. #elif PRAGMA_STRUCT_PACKPUSH
  293.     #pragma pack(pop)
  294. #elif PRAGMA_STRUCT_PACK
  295.     #pragma pack()
  296. #endif
  297. #ifdef PRAGMA_IMPORT_OFF
  298. #pragma import off
  299. #elif PRAGMA_IMPORT
  300. #pragma import reset
  301. #endif
  302. #ifdef __cplusplus
  303. }
  304. #endif
  305. #endif /* __THREADS__ */