-
Notifications
You must be signed in to change notification settings - Fork 0
/
DKThread.h
139 lines (92 loc) · 4.43 KB
/
DKThread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*****************************************************************************************
DKThread.h
Copyright (c) 2014 Derek W. Nylen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************************************************/
#ifndef _DK_THREAD_H_
#define _DK_THREAD_H_
#ifdef __cplusplus
extern "C"
{
#endif
// DKThread ==============================================================================
typedef struct DKThread * DKThreadRef;
typedef void (*DKThreadProc)( void * context );
typedef void (*DKThreadMethod)( DKObjectRef _self, DKObjectRef param );
typedef enum
{
DKThreadCreated = 0,
DKThreadStarted,
DKThreadRunning,
DKThreadCancelled,
DKThreadFinished,
// The thread was not created by DKThread
DKThreadStateUnknown
} DKThreadState;
DK_API DKClassRef DKThreadClass( void );
DK_API DKThreadRef DKThreadGetCurrentThread( void );
DK_API DKThreadRef DKThreadGetMainThread( void );
DK_API void DKDetachNewThread( DKThreadProc proc, void * context );
DK_API void DKDetachNewThreadToTarget( DKObjectRef target, DKThreadMethod method, DKObjectRef param );
DK_API DKObjectRef DKThreadInit( DKObjectRef _self, DKThreadProc proc, void * context );
DK_API DKObjectRef DKThreadInitWithTarget( DKObjectRef _self, DKObjectRef target, DKThreadMethod method, DKObjectRef param );
DK_API void DKThreadSetLabel( DKThreadRef _self, DKStringRef label );
DK_API void DKThreadStart( DKThreadRef _self );
DK_API void DKThreadJoin( DKThreadRef _self );
DK_API void DKThreadCancel( DKThreadRef _self );
DK_API void DKThreadExit( void );
// Get thread information. Unlike most interfaces, if '_self' is NULL, the values
// associated with the current thread are returned.
DK_API DKThreadState DKThreadGetState( DKThreadRef _self );
DK_API bool DKThreadIsMainThread( DKThreadRef _self );
// DKThreadContext =======================================================================
typedef struct DKThreadContext * DKThreadContextRef;
// The main thread and DKThreads manage their own thread contexts. Any threads created
// manually must allocate a DKThreadContext structure and set it before calling any Duck
// functions, and free the DKThreadContext once it is no longer in use.
DK_API DKThreadContextRef DKAllocThreadContext( void );
DK_API void DKFreeThreadContext( DKThreadContextRef threadContext );
// Get/Set the thread context for the current thread
DK_API void DKSetCurrentThreadContext( DKThreadContextRef threadContext );
DK_API bool DKCurrentThreadContextIsSet( void );
DK_API DKThreadContextRef DKGetCurrentThreadContext( void );
// Get the thread context of the main thread
DK_API DKThreadContextRef DKGetMainThreadContext( void );
// Get rhe thread-specific dictionary for the current thread
DK_API DKMutableDictionaryRef DKGetCurrentThreadDictionary( void );
// Private ===============================================================================
#if DK_THREAD_PRIVATE
struct DKThreadContext
{
DKThreadRef threadObject;
DKMutableDictionaryRef threadDictionary;
uint32_t options;
struct
{
DKGenericArray objects;
DKIndex top;
DKIndex lowWater[DK_AUTORELEASE_POOL_STACK_SIZE];
} arp;
};
void DKThreadContextInit( DKThreadContextRef threadContext, uint32_t options );
void DKThreadContextFinalize( DKThreadContextRef threadContext );
void DKMainThreadContextInit( void );
#endif // DK_RUNTIME_PRIVATE
#ifdef __cplusplus
}
#endif
#endif // _DK_THREAD_H_