cython
1# mode: run
2# tag: cpp, cpp11, werror, no-cpp-locals
3
4from cython.operator cimport preincrement as incr, dereference as deref
5from libc.stdint cimport *
6
7from libcpp.atomic cimport atomic
8
9def int_test(int x):
10"""
11>>> int_test(55)
123
13>>> int_test(42)
143
15>>> int_test(100000)
163
17"""
18atom = new atomic[int](x)
19try:
20atom.store(0)
21incr(deref(atom))
22incr(deref(atom))
23incr(deref(atom))
24return atom.load()
25finally:
26del atom
27
28ctypedef atomic[int32_t] atomint32_t
29
30def typedef_test(int x):
31"""
32>>> typedef_test(55)
333
34>>> typedef_test(42)
353
36>>> typedef_test(100000)
373
38"""
39atom = new atomint32_t(x)
40try:
41atom.store(0)
42incr(deref(atom))
43incr(deref(atom))
44incr(deref(atom))
45return atom.load()
46finally:
47del atom
48
49def stack_allocation_test(int x):
50"""
51>>> stack_allocation_test(55)
523
53>>> stack_allocation_test(42)
543
55>>> stack_allocation_test(100000)
563
57"""
58cdef atomint32_t atom
59atom.store(x)
60try:
61atom.store(0)
62incr(atom)
63incr(atom)
64incr(atom)
65return atom.load()
66finally:
67pass
68
69def nogil_int_test(int x):
70"""
71>>> nogil_int_test(55)
7255
73>>> nogil_int_test(42)
7442
75>>> nogil_int_test(100000)
76100000
77"""
78with nogil:
79atom = new atomic[int](0)
80try:
81with nogil:
82atom.store(x)
83return atom.load()
84finally:
85del atom
86