blib/lib/SDL2/atomic.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 42 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package SDL2::atomic {
2 use strictures 2;
3 use SDL2::Utils;
4 use experimental 'signatures';
5 #
6 use SDL2::atomic_t;
7 ffi->type( 'int' => 'SDL_SpinLock' );
8  
9 #ffi->type( 'int' => 'SDL_bool' ) if __FILE__ eq $0;
10 attach atomic => {
11 SDL_AtomicTryLock => [ ['SDL_SpinLock*'], 'SDL_bool' ],
12 SDL_AtomicLock => [ ['SDL_SpinLock*'] ],
13 SDL_AtomicUnlock => [ ['SDL_SpinLock*'] ],
14 SDL_MemoryBarrierReleaseFunction => [ [] ], # Undocumented
15 SDL_MemoryBarrierAcquireFunction => [ [] ], # Undocumented
16 SDL_AtomicCAS => [ [ 'SDL_atomic_t', 'int', 'int' ], 'SDL_bool' ],
17 SDL_AtomicSet => [ [ 'SDL_atomic_t', 'int' ], 'int' ],
18 SDL_AtomicAdd => [ [ 'SDL_atomic_t', 'int' ], 'int' ],
19 SDL_AtomicCASPtr => [ [ 'opaque*', 'opaque', 'opaque' ], 'SDL_bool' ],
20 SDL_AtomicSetPtr => [ [ 'opaque*', 'opaque' ], 'opaque' ],
21 SDL_AtomicGetPtr => [ ['opaque*'], 'opaque' ],
22 };
23 define atomic => [
24 [ SDL_AtomicIncRef => sub ($a) { SDL_AtomicAdd( $a, 1 ) } ],
25 [ SDL_AtomicDecRef => sub ($a) { SDL_AtomicAdd( $a, -1 ) } ]
26 ];
27  
28 =encoding utf-8
29  
30 =head1 NAME
31  
32 SDL2::atomic - SDL Atomic Operations
33  
34 =head1 SYNOPSIS
35  
36 use SDL2::FFI qw[:atomic];
37 SDL_assert( 1 == 1 );
38 my $test = 'nope';
39 SDL_assert(
40 sub {
41 warn 'testing';
42 my $retval = $test eq "blah";
43 $test = "blah";
44 $retval;
45 }
46 );
47  
48 =head1 DESCRIPTION
49  
50 If you are not an expert in concurrent lockless programming, you should only be
51 using the atomic lock and reference counting functions in this file. In all
52 other cases you should be protecting your data structures with full mutexes.
53  
54 You can find out a little more about lockless programming and the subtle issues
55 that can arise here:
56 L<http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx>
57  
58 There's also lots of good information here:
59  
60 =over
61  
62 =item L<http://www.1024cores.net/home/lock-free-algorithms>
63  
64 =item L<http://preshing.com/>
65  
66 =back
67  
68 These operations may or may not actually be implemented using processor
69 specific atomic operations. When possible they are implemented as true
70 processor specific atomic operations. When that is not possible the are
71 implemented using locks that *do* use the available atomic operations.
72  
73 All of the atomic operations that modify memory are full memory barriers.
74  
75 =head1 Functions
76  
77 =head2 C<SDL_AtomicTryLock( ... )>
78  
79 Try to lock a spin lock by setting it to a non-zero value.
80  
81 my $lock = 1;
82 my $ok = SDL_AtomicTryLock( \$lock );
83  
84 B<Please note that spinlocks are dangerous if you don't know what you're doing.
85 Please be careful using any sort of spinlock!>
86  
87 Expected parameters include:
88  
89 =over
90  
91 =item C<lock> - a pointer to a lock variable
92  
93 =back
94  
95 Returns C<SDL_TRUE> if the lock succeeded, C<SDL_FALSE> if the lock is already
96 held.
97  
98 =head2 C<SDL_AtomicLock( ... )>
99  
100 Lock a spin lock by setting it to a non-zero value.
101  
102 SDL_AtomicLock( \$lock );
103  
104 B<Please note that spinlocks are dangerous if you don't know what you're doing.
105 Please be careful using any sort of spinlock!>
106  
107 Expected parameters include:
108  
109 =over
110  
111 =item C<lock> - a pointer to a lock variable
112  
113 =back
114  
115 =head2 C<SDL_AtomicUnlock( ... )>
116  
117 Unlock a spin lock by setting it to 0.
118  
119 SDL_AtomicUnlock( \$lock );
120  
121 Always returns immediately.
122  
123 B<Please note that spinlocks are dangerous if you don't know what you're doing.
124 Please be careful using any sort of spinlock!>
125  
126 Expected parameters include:
127  
128 =over
129  
130 =item C<lock> - a pointer to a lock variable
131  
132 =back
133  
134  
135 =head2 C<SDL_MemoryBarrierReleaseFunction( )>
136  
137 Memory barriers are designed to prevent reads and writes from being reordered
138 by the compiler and being seen out of order on multi-core CPUs.
139  
140 =head2 C<SDL_MemoryBarrierAcquireFunction( )>
141  
142 Memory barriers are designed to prevent reads and writes from being reordered
143 by the compiler and being seen out of order on multi-core CPUs.
144  
145 =head2 C<SDL_AtomicCAS( ... )>
146  
147 Set an atomic variable to a new value if it is currently an old value.
148  
149 B<Note: If you don't know what this function is for, you shouldn't use it!>
150  
151 Expected parameters include:
152  
153 =over
154  
155 =item C<a> - a pointer to an L<SDL2::atomic_t> variable to be modified
156  
157 =item C<oldval> - the old value
158  
159 =item C<newval> - the new value
160  
161 =back
162  
163 Returns C<SDL_TRUE> if the atomic variable was set, C<SDL_FALSE> otherwise.
164  
165 =head2 C<SDL_AtomicSet( ... )>
166  
167 Set an atomic variable to a value.
168  
169 my $value = SDL_AtomicSet( $a, 1000 );
170  
171 This function also acts as a full memory barrier.
172  
173 B<Note: If you don't know what this function is for, you shouldn't use it!>
174  
175 Expected parameters include:
176  
177 =over
178  
179 =item C<a> - a pointer to an L<SDL2::atomic_t> variable to be modified
180  
181 =item C<v> - the desired value
182  
183 =back
184  
185 Returns the previous value of the atomic variable.
186  
187 =head2 C<SDL_AtomicGet( ... )>
188  
189 Get the value of an atomic variable.
190  
191 my $value = SDL_AtomicGet( $a );
192  
193 B<Note: If you don't know what this function is for, you shouldn't use it!>
194  
195 Expected parameters include:
196  
197 =over
198  
199 =item C<a> - a pointer to an L<SDL2::atomic_t> variable to be modified
200  
201 =back
202  
203 Returns the current value of the atomic variable.
204  
205 =head2 C<SDL_AtomicAdd( ... )>
206  
207 Add to an atomic variable.
208  
209 my $value = SDL_AtomicAdd( $a, 1000 );
210  
211 This function also acts as a full memory barrier.
212  
213 B<Note: If you don't know what this function is for, you shouldn't use it!>
214  
215 Expected parameters include:
216  
217 =over
218  
219 =item C<a> - a pointer to an L<SDL2::atomic_t> variable to be modified
220  
221 =item C<v> - the desired value to add
222  
223 =back
224  
225 Returns the previous value of the atomic variable.
226  
227 =head2 C<SDL_AtomicIncRef( ... )>
228  
229 Increment an atomic variable used as a reference count.
230  
231 my $value = SDL_AtomicIncRef( $a );
232  
233 This function also acts as a full memory barrier.
234  
235 B<Note: If you don't know what this function is for, you shouldn't use it!>
236  
237 Expected parameters include:
238  
239 =over
240  
241 =item C<a> - a pointer to an L<SDL2::atomic_t> variable to be modified
242  
243 =back
244  
245 Returns the previous value of the atomic variable.
246  
247 =head2 C<SDL_AtomicDecRef( ... )>
248  
249 Decrement an atomic variable used as a reference count.
250  
251 my $okay = SDL_AtomicDecRef( $a );
252  
253 This function also acts as a full memory barrier.
254  
255 B<Note: If you don't know what this function is for, you shouldn't use it!>
256  
257 Expected parameters include:
258  
259 =over
260  
261 =item C<a> - a pointer to an L<SDL2::atomic_t> variable to be modified
262  
263 =back
264  
265 Returns C<SDL_TRUE> if the atomic variable reached zero after decrementing,
266 C<SDL_FALSE> otherwise.
267  
268 =head2 C<SDL_AtomicCASPtr( ... )>
269  
270 Set a pointer to a new value if it is currently an old value.
271  
272 my $alpha = 1000;
273 SDL_AtomicCASPtr( \$alpha, 2000, 10 ); # Returns SDL_FALSE
274 SDL_AtomicCASPtr( \$alpha, 1000, 10 ); # Returns SDL_TRUE
275  
276 B<Note: If you don't know what this function is for, you shouldn't use it!>
277  
278 Expected parameters include:
279  
280 =over
281  
282 =item C<a> - a pointer to a pointer
283  
284 =item C<oldval> - the old pointer value
285  
286 =item C<newval> - the new pointer value
287  
288 =back
289  
290 Returns C<SDL_TRUE> if the pointer was set, C<SDL_FALSE> otherwise.
291  
292 =head2 C<SDL_AtomicSetPtr( ... )>
293  
294 Set a pointer to a value atomically.
295  
296 my $alpha = 200;
297 my $old = SDL_AtomicSetPtr( \$alpha, 100 ); # Returns 200
298  
299 B<Note: If you don't know what this function is for, you shouldn't use it!>
300  
301 Expected parameters include:
302  
303 =over
304  
305 =item C<a> - a pointer to a pointer
306  
307 =item C<v> - the desired pointer value
308  
309 =back
310  
311 Returns the previous value of the pointer.
312  
313 =head2 C<SDL_AtomicGetPtr( ... )>
314  
315 Get the value of a pointer atomically.
316  
317 my $alpha = 200;
318 SDL_AtomicGetPtr( \$alpha ); # Returns 200
319  
320 B<Note: If you don't know what this function is for, you shouldn't use it!>
321  
322 Expected parameters include:
323  
324 =over
325  
326 =item C<a> - a pointer to a pointer
327  
328 =back
329  
330 Returns the current value of a pointer.
331  
332 =head1 Types
333  
334 =head2 C<SDL_SpinLock>
335  
336 The atomic locks are efficient spinlocks using CPU instructions, but are
337 vulnerable to starvation and can spin forever if a thread holding a lock has
338 been terminated. For this reason you should minimize the code executed inside
339 an atomic lock and never do expensive things like API or system calls while
340 holding them.
341  
342 The atomic locks are not safe to lock recursively.
343  
344 =head2 C<SDL2::atomic_t>
345  
346 A type representing an atomic integer value. It is a struct so people don't
347 accidentally use numeric operations on it.
348  
349 =head1 Defines and Enum
350  
351 Defines and Enumerations listed here may be imported from SDL2::FFI with the
352 following tags:
353  
354 =head2 C<:assertState>
355  
356 =over
357  
358 =item C<SDL_ASSERTION_RETRY> - Retry the assert immediately
359  
360 =item C<SDL_ASSERTION_BREAK> - Make the debugger trigger a breakpoint
361  
362 =item C<SDL_ASSERTION_ABORT> - Terminate the program
363  
364 =item C<SDL_ASSERTION_IGNORE> - Ignore the assert
365  
366 =item C<SDL_ASSERTION_ALWAYS_IGNORE> - Ignore the assert from now on
367  
368 =back
369  
370 =head1 Memory barriers
371  
372 Memory barriers are designed to prevent reads and writes from being reordered
373 by the compiler and being seen out of order on multi-core CPUs.
374  
375 A typical pattern would be for thread A to write some data and a flag, and for
376 thread B to read the flag and get the data. In this case you would insert a
377 release barrier between writing the data and the flag, guaranteeing that the
378 data write completes no later than the flag is written, and you would insert an
379 acquire barrier between reading the flag and reading the data, to ensure that
380 all the reads associated with the flag have completed.
381  
382 In this pattern you should always see a release barrier paired with an acquire
383 barrier and you should gate the data reads/writes with a single flag variable.
384  
385 For more information on these semantics, take a look at the blog post:
386 L<http://preshing.com/20120913/acquire-and-release-semantics>
387  
388 =head1 LICENSE
389  
390 Copyright (C) Sanko Robinson.
391  
392 This library is free software; you can redistribute it and/or modify it under
393 the terms found in the Artistic License 2. Other copyrights, terms, and
394 conditions may apply to data transmitted through this module.
395  
396 =head1 AUTHOR
397  
398 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
399  
400 =begin stopwords
401  
402 lockless spinlock spinlocks
403  
404 =end stopwords
405  
406 =cut
407  
408 };
409 1;