blib/lib/SDL2/thread.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 70 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package SDL2::thread 0.01 {
2 use SDL2::Utils;
3 use experimental 'signatures';
4 #
5 use SDL2::stdinc;
6 use SDL2::error;
7  
8 # Thread synchronization primitives
9 use SDL2::atomic;
10 use SDL2::mutex;
11 #
12 package SDL2::Thread {
13 use SDL2::Utils;
14 our $TYPE = has();
15 };
16 #
17 ffi->type( 'ulong' => 'SDL_threadID' );
18 ffi->type( 'uint' => 'SDL_TLSID' );
19 ffi->type( '(opaque)->void' => '__destructor' );
20 enum SDL_ThreadPriority => [
21 qw[
22 SDL_THREAD_PRIORITY_LOW
23 SDL_THREAD_PRIORITY_NORMAL
24 SDL_THREAD_PRIORITY_HIGH
25 SDL_THREAD_PRIORITY_TIME_CRITICAL]
26 ];
27 ffi->type( '(opaque)->int' => 'SDL_ThreadFunction' );
28 attach thread => {
29 SDL_CreateThread => [
30 [ 'SDL_ThreadFunction', 'string', 'opaque' ],
31 'SDL_Thread' => sub ( $inner, $fn, $name, $data = () ) {
32 my $cb = ffi->closure( sub ($ref) { $fn->($data) } );
33 $cb->sticky;
34 $inner->( $cb, $name, $data );
35 }
36 ],
37 SDL_CreateThreadWithStackSize => [
38 [ 'SDL_ThreadFunction', 'string', 'size_t', 'opaque' ],
39 'SDL_Thread' => sub ( $inner, $fn, $name, $stacksize, $data = () ) {
40 my $cb = ffi->closure( sub ($ref) { $fn->($data) } );
41 $cb->sticky;
42 $inner->( $cb, $name, $stacksize, $data );
43 }
44 ],
45 SDL_GetThreadName => [ ['SDL_Thread'], 'string' ],
46 SDL_ThreadID => [ [], 'SDL_threadID' ],
47 SDL_GetThreadID => [ ['SDL_Thread'], 'SDL_threadID' ],
48 SDL_SetThreadPriority => [ ['SDL_ThreadPriority'], 'int' ],
49 SDL_WaitThread => [ [ 'SDL_Thread', 'int*' ] ],
50 SDL_DetachThread => [ ['SDL_Thread'] ],
51 SDL_TLSCreate => [ [], 'SDL_TLSID' ],
52 SDL_TLSGet => [ ['SDL_TLSID'], 'opaque' ],
53 SDL_TLSSet => [
54 [ 'SDL_TLSID', 'string', '__destructor' ],
55 'int' => sub ( $inner, $id, $value, $cb ) {
56 my $destructor = ffi->closure($cb);
57 $destructor->sticky;
58 $inner->( $id, $value, $destructor );
59 }
60 ],
61 SDL_TLSCleanup => [ [] ]
62 };
63  
64 =encoding utf-8
65  
66 =head1 NAME
67  
68 SDL2::thread - SDL Thread Management Routines
69  
70 =head1 SYNOPSIS
71  
72 use SDL2 qw[:thread];
73  
74 # Very simple thread - counts 0 to 9 delaying 50ms between increments
75 sub TestThread ($ptr) {
76 my $cnt;
77 for ( $cnt = 0; $cnt < 10; ++$cnt ) {
78 printf( "Thread counter: %d\n", $cnt );
79 SDL_Delay(50);
80 }
81 return $cnt;
82 }
83 #
84 printf("Simple SDL_CreateThread test:\n");
85  
86 # Simply create a thread
87 my $thread = SDL_CreateThread( \&TestThread, 'TestThread', () );
88 if ( !defined $thread ) {
89 printf( "SDL_CreateThread failed: %s\n", SDL_GetError() );
90 }
91 else {
92 SDL_WaitThread( $thread, \my $threadReturnValue );
93 printf( "Thread returned value: %d\n", $threadReturnValue );
94 }
95  
96 =head1 DESCRIPTION
97  
98 This package contains functions for system independent thread management
99 routines.
100  
101 B<NOTE>: You should not expect to be able to create a window, render, or
102 receive events on any thread other than the main one.
103  
104 =head1 Functions
105  
106 These may be imported by name or with the C<:thread> tag.
107  
108 =head2 C<SDL_CreateThread( ... )>
109  
110 Create a new thread with a default stack size.
111  
112 This is equivalent to calling:
113  
114 SDL_CreateThreadWithStackSize($fn, $name, 0, $data);
115  
116 Expected parameters include:
117  
118 =over
119  
120 =item C<fn> - the C<SDL_ThreadFunction> function to call in the new thread
121  
122 =item C<name> - the name of the thread
123  
124 =item C<data> - a pointer that is passed to C<fn>
125  
126 =back
127  
128 Returns an opaque pointer to the new thread object on success, C<undef> if the
129 new thread could not be created; call C<SDL_GetError( )> for more information.
130  
131 =head2 C<SDL_CreateThreadWithStackSize( ... )>
132  
133 Create a new thread with a specific stack size.
134  
135 SDL makes an attempt to report C<name> to the system, so that debuggers can
136 display it. Not all platforms support this.
137  
138 Thread naming is a little complicated: Most systems have very small limits for
139 the string length (Haiku has 32 bytes, Linux currently has 16, Visual C++ 6.0
140 has _nine_!), and possibly other arbitrary rules. You'll have to see what
141 happens with your system's debugger. The name should be UTF-8 (but using the
142 naming limits of C identifiers is a better bet). There are no requirements for
143 thread naming conventions, so long as the string is null-terminated UTF-8, but
144 these guidelines are helpful in choosing a name:
145  
146 L<https://stackoverflow.com/questions/149932/naming-conventions-for-threads>
147  
148 If a system imposes requirements, SDL will try to munge the string for it
149 (truncate, etc), but the original string contents will be available from L<<
150 C<SDL_GetThreadName( ... )>|/C<SDL_GetThreadName( ... )> >>.
151  
152 The size (in bytes) of the new stack can be specified. Zero means "use the
153 system default" which might be wildly different between platforms. x86 Linux
154 generally defaults to eight megabytes, an embedded device might be a few
155 kilobytes instead. You generally need to specify a stack that is a multiple of
156 the system's page size (in many cases, this is 4 kilobytes, but check your
157 system documentation).
158  
159 In SDL 2.1, stack size will be folded into the original SDL_CreateThread
160 function, but for backwards compatibility, this is currently a separate
161 function.
162  
163 Expected parameters include:
164  
165 =over
166  
167 =item C<fn> - the C<SDL_ThreadFunction> function to call in the new thread
168  
169 =item C<name> - the name of the thread
170  
171 =item C<stacksize> - the size, in bytes, to allocate for the new thread stack
172  
173 =item C<data> - a pointer that is passed to C<fn>
174  
175 =back
176  
177 Returns an opaque pointer to the new thread object on success, C<undef> if the
178 new thread could not be created; call C<SDL_GetError( )> for more information.
179  
180 =head2 C<SDL_GetThreadName( ... )>
181  
182 Get the thread name as it was specified in L<< C<SDL_CreateThread( ...
183 )>|/C<SDL_CreateThread( ... )> >>.
184  
185 This is internal memory, not to be freed by the caller, and remains valid until
186 the specified thread is cleaned up by L<< C<SDL_WaitThread( ...
187 )>|/C<SDL_WaitThread( ... )> >>.
188  
189 Expected parameters include:
190  
191 =over
192  
193 =item C<thread> - the thread to query
194  
195 =back
196  
197 Returns a pointer to a UTF-8 string that names the specified thread, or
198 C<undef> if it doesn't have a name.
199  
200 =head2 C<SDL_ThreadID( )>
201  
202 Get the thread identifier for the current thread.
203  
204 This thread identifier is as reported by the underlying operating system. If
205 SDL is running on a platform that does not support threads the return value
206 will always be zero.
207  
208 This function also returns a valid thread ID when called from the main thread.
209  
210 Returns the ID of the current thread.
211  
212 =head2 C<SDL_GetThreadID( ... )>
213  
214 Get the thread identifier for the specified thread.
215  
216 This thread identifier is as reported by the underlying operating system. If
217 SDL is running on a platform that does not support threads the return value
218 will always be zero.
219  
220 Expected parameters include:
221  
222 =over
223  
224 =item C<thread> - the thread to query
225  
226 =back
227  
228 Returns the ID of the specified thread, or the ID of the current thread if
229 C<thread> is C<undef>.
230  
231 =head2 C<SDL_SetThreadPriority( ... )>
232  
233 Set the priority for the current thread.
234  
235 Note that some platforms will not let you alter the priority (or at least,
236 promote the thread to a higher priority) at all, and some require you to be an
237 administrator account. Be prepared for this to fail.
238  
239 Expected parameters include:
240  
241 =over
242  
243 =item C<priority> the C<SDL_ThreadPriority> to set
244  
245 =back
246  
247 Returns 0 on success or a negative error code on failure; call C<SDL_GetError(
248 )> for more information.
249  
250 =head2 C<SDL_WaitThread( ... )>
251  
252 Wait for a thread to finish.
253  
254 Threads that haven't been detached will remain (as a "zombie") until this
255 function cleans them up. Not doing so is a resource leak.
256  
257 Once a thread has been cleaned up through this function, the SDL_Thread that
258 references it becomes invalid and should not be referenced again. As such, only
259 one thread may call C<SDL_WaitThread( ... )> on another.
260  
261 The return code for the thread function is placed in the area pointed to by
262 C<status>, if C<status> is not C<undef>.
263  
264 You may not wait on a thread that has been used in a call to L<<
265 C<SDL_WaitThread( ... )>|/C<SDL_WaitThread( ... )> >>
266  
267 L<< C<SDL_DetachThread( ... )>|/C<SDL_DetachThread( ... )> >>. Use either that
268 function or this one, but not both, or behavior is undefined.
269  
270 It is safe to pass a C<undef> thread to this function; it is a no-op.
271  
272 Note that the thread pointer is freed by this function and is not valid
273 afterward.
274  
275 Expected parameters include:
276  
277 =over
278  
279 =item C<thread> - the L<SDL2::Thread> pointer that was returned from the L<< C<SDL_CreateThread( ... )>|/C<SDL_CreateThread( ... )> >> call that started this thread
280  
281 =item C<status> - pointer to an integer that will receive the value returned from the thread function by its 'return', or C<undef> to not receive such value back
282  
283 =back
284  
285 =head2 C<SDL_DetachThread( ... )>
286  
287 Let a thread clean up on exit without intervention.
288  
289 A thread may be "detached" to signify that it should not remain until another
290 thread has called SDL_WaitThread() on it. Detaching a thread is useful for
291 long-running threads that nothing needs to synchronize with or further manage.
292 When a detached thread is done, it simply goes away.
293  
294 There is no way to recover the return code of a detached thread. If you need
295 this, don't detach the thread and instead use SDL_WaitThread().
296  
297 Once a thread is detached, you should usually assume the SDL_Thread isn't safe
298 to reference again, as it will become invalid immediately upon the detached
299 thread's exit, instead of remaining until someone has called L<<
300 C<SDL_WaitThread( ... )>|/C<SDL_WaitThread( ... )> >> to finally clean it up.
301 As such, don't detach the same thread more than once.
302  
303 If a thread has already exited when passed to C<SDL_DetachThread( ... )>, it
304 will stop waiting for a call to L<< C<SDL_WaitThread( ... )>|/C<SDL_WaitThread(
305 ... )> >> and clean up immediately. It is not safe to detach a thread that
306 might be used with L<< C<SDL_WaitThread( ... )>|/C<SDL_WaitThread( ... )> >>.
307  
308 You may not call L<< C<SDL_WaitThread( ... )>|/C<SDL_WaitThread( ... )> >> on a
309 thread that has been detached. Use either that function or this one, but not
310 both, or behavior is undefined.
311  
312 It is safe to pass C<undef> to this function; it is a no-op.
313  
314 Expected parameters include:
315  
316 =over
317  
318 =item C<thread> the L<SDL2::Thread> pointer that was returned from the L<< C<SDL_CreateThread( ... )>|/C<SDL_CreateThread( ... )> >> call that started this thread
319  
320 =back
321  
322 =head2 C<SDL_TLSCreate( )>
323  
324 Create a piece of thread-local storage.
325  
326 This creates an identifier that is globally visible to all threads but refers
327 to data that is thread-specific.
328  
329 Returns the newly created thread local storage identifier or C<0> on error.
330  
331 =head2 C<SDL_TLSGet( ... )>
332  
333 Get the current thread's value associated with a thread local storage ID.
334  
335 Expected parameters include:
336  
337 =over
338  
339 =item C<id> - the thread local storage ID
340  
341 =back
342  
343 Returns the value associated with the ID for the current thread or C<undef> if
344 no value has been set; call C<SDL_GetError( )> for more information.
345  
346 =head2 C<SDL_TLSSet( ... )>
347  
348 Set the current thread's value associated with a thread local storage ID.
349  
350 The function prototype for C<destructor> is:
351  
352 sub ($value);
353  
354 where its parameter C<value> is what was passed as C<value> to L<<
355 C<SDL_TLSSet( ... )>|/C<SDL_TLSSet( ... )> >>. The return value is ignored.
356  
357 Expected parameters include:
358  
359 =over
360  
361 =item C<id> - the thread local storage ID
362  
363 =item C<value> - the value to associate with the ID for the current thread
364  
365 =item C<destructor> - a function called when the thread exits, to free the value
366  
367 =back
368  
369 Returns C<0> on success or a negative error code on failure; call
370 C<SDL_GetError( )> for more information.
371  
372 =head2 C<SDL_TLSCleanup( )>
373  
374 Cleanup all TLS data for this thread.
375  
376 =head1 Defined Values, Types, and Enumerations
377  
378 Values and types may be imported by name or with the C<:thread> tag.
379 Enumerations may be imported with their given tags.
380  
381 =head2 C<SDL_threadID>
382  
383 The SDL thread ID.
384  
385 =head2 C<SDL_TLSID>
386  
387 Thread local storage ID, C<0> is the invalid ID.
388  
389 =head2 C<SDL_ThreadPriority>
390  
391 The SDL thread priority.
392  
393 SDL will make system changes as necessary in order to apply the thread
394 priority. Code which attempts to control thread state related to priority
395 should be aware that calling L<< C<SDL_SetThreadPriority( ...
396 )>|/C<SDL_SetThreadPriority( ... )> >> may alter such state.
397 C<SDL_HINT_THREAD_PRIORITY_POLICY> can be used to control aspects of this
398 behavior.
399  
400 On many systems you require special privileges to set high or time critical
401 priority.
402  
403 =over
404  
405 =item C<SDL_THREAD_PRIORITY_LOW>
406  
407 =item C<SDL_THREAD_PRIORITY_NORMAL>
408  
409 =item C<SDL_THREAD_PRIORITY_HIGH>
410  
411 =item C<SDL_THREAD_PRIORITY_TIME_CRITICAL>
412  
413 =back
414  
415 =head2 C<SDL_ThreadFunction>
416  
417 The function passed to L<< C<SDL_CreateThread( ... )>|/C<SDL_CreateThread( ...
418 )> >>.
419  
420 Parameters to expect include:
421  
422 =over
423  
424 =item C<data> - what was passed as C<data> to C<SDL_CreateThread( ... )>
425  
426 =back
427  
428 Return a value that can be reported through C<SDL_WaitThread( ... )>.
429  
430 =head1 LICENSE
431  
432 Copyright (C) Sanko Robinson.
433  
434 This library is free software; you can redistribute it and/or modify it under
435 the terms found in the Artistic License 2. Other copyrights, terms, and
436 conditions may apply to data transmitted through this module.
437  
438 =head1 AUTHOR
439  
440 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
441  
442 =begin stopwords
443  
444 =end stopwords
445  
446 =cut
447  
448 };
449 1;