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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2020-2022 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef DMSDK_THREAD_H
#define DMSDK_THREAD_H
#include <stdint.h>
// These headers define the dmThread::Thread and dmThread::TlsKey with the native types
#if defined(_WIN32)
#include <dmsdk/dlib/thread_native_win32.h>
#elif defined(__NX__)
#include <dmsdk/dlib/thread_native_nx64.h>
#else
#include <dmsdk/dlib/thread_native_posix.h>
#endif
/*# SDK Thread API documentation
* [file:<dmsdk/dlib/thread.h>]
*
* Thread functions.
*
* @document
* @name Thread
* @namespace dmThread
*/
namespace dmThread
{
typedef void (*ThreadStart)(void*);
/*# create a new thread
* Create a new named thread
* @note thread name currently not supported on win32
* @name dmThread::New
* @param thread_start Thread entry function
* @param stack_size Stack size
* @param arg Thread argument
* @param name Thread name
* @return Thread handle
* @examples
*
* Create a thread
*
* ```cpp
* #include <dmsdk/sdk.h>
*
* struct Context
* {
* bool m_DoWork;
* int m_Work;
* };
*
* static void Worker(void* _ctx)
* {
* Context* ctx = (Context*)_ctx;
* while (ctx->m_DoWork)
* {
* ctx->m_Work++; // do work
* dmTime::Sleep(10*1000); // yield
* }
* }
*
* int StartThread()
* {
* Context ctx;
* ctx.m_DoWork = true;
* ctx.m_Work = 0;
* dmThread::Thread thread = dmThread::New(Worker, 0x80000, (void*)&ctx, "my_thread");
*
* // do other work...
* // ..eventually stop the thread:
* ctx.m_DoWork = false;
*
* // wait for thread
* dmThread::Join(thread);
*
* printf("work done: %d\n", ctx.m_Work);
* }
* ```
*/
Thread New(ThreadStart thread_start, uint32_t stack_size, void* arg, const char* name);
/*# join thread
*
* Join thread. Waits for the thread specified by thread to terminate. If
* that thread has already terminated, then Join() returns immediately. The
* thread specified by thread must be joinable (see Detach()).
* @name dmThread::Join
* @param thread Thread to join
*/
void Join(Thread thread);
/*# detach thread
*
* Detach thread. When a detached thread terminates, its resources are
* automatically released back to the system without the need for another
* thread to join with the terminated thread.
* @name dmThread::Detach
* @param thread Thread to detach
*/
void Detach(Thread thread);
/*# allocate thread local storage key
* Allocate thread local storage key
* @name dmThread::AllocTls
* @return Key
*/
TlsKey AllocTls();
/*# free thread local storage key
* Free thread local storage key
* @name dmThread::FreeTls
* @param key Key
*/
void FreeTls(TlsKey key);
/*# set thread specific data
* Set thread specific data
* @name dmThread::SetTlsValue
* @param key Key
* @param value Value
*/
void SetTlsValue(TlsKey key, void* value);
/*# get thread specific data
* Get thread specific data
* @name dmThread::GetTlsValue
* @param key Key
*/
void* GetTlsValue(TlsKey key);
/*# gets the current thread
* Gets the current thread
* @name dmThread::GetCurrentThread
* @return the current thread
*/
Thread GetCurrentThread();
/*# sets the current thread name
* Sets the current thread name
* @name dmThread::SetThreadName
* @param thread the thread
* @param name the thread name
* @note The thread argument is unused on Darwin (uses current thread)
*/
void SetThreadName(Thread thread, const char* name);
}
#endif // DMSDK_THREAD_H