cython

Форк
0
/
cpp_stl_atomic.pyx 
85 строк · 1.5 Кб
1
# mode: run
2
# tag: cpp, cpp11, werror, no-cpp-locals
3

4
from cython.operator cimport preincrement as incr, dereference as deref
5
from libc.stdint cimport *
6

7
from libcpp.atomic cimport atomic
8

9
def int_test(int x):
10
    """
11
    >>> int_test(55)
12
    3
13
    >>> int_test(42)
14
    3
15
    >>> int_test(100000)
16
    3
17
    """
18
    atom = new atomic[int](x)
19
    try:
20
        atom.store(0)
21
        incr(deref(atom))
22
        incr(deref(atom))
23
        incr(deref(atom))
24
        return atom.load()
25
    finally:
26
        del atom
27

28
ctypedef atomic[int32_t] atomint32_t
29

30
def typedef_test(int x):
31
    """
32
    >>> typedef_test(55)
33
    3
34
    >>> typedef_test(42)
35
    3
36
    >>> typedef_test(100000)
37
    3
38
    """
39
    atom = new atomint32_t(x)
40
    try:
41
        atom.store(0)
42
        incr(deref(atom))
43
        incr(deref(atom))
44
        incr(deref(atom))
45
        return atom.load()
46
    finally:
47
        del atom
48

49
def stack_allocation_test(int x):
50
    """
51
    >>> stack_allocation_test(55)
52
    3
53
    >>> stack_allocation_test(42)
54
    3
55
    >>> stack_allocation_test(100000)
56
    3
57
    """
58
    cdef atomint32_t atom
59
    atom.store(x)
60
    try:
61
        atom.store(0)
62
        incr(atom)
63
        incr(atom)
64
        incr(atom)
65
        return atom.load()
66
    finally:
67
        pass
68

69
def nogil_int_test(int x):
70
    """
71
    >>> nogil_int_test(55)
72
    55
73
    >>> nogil_int_test(42)
74
    42
75
    >>> nogil_int_test(100000)
76
    100000
77
    """
78
    with nogil:
79
        atom = new atomic[int](0)
80
    try:
81
        with nogil:
82
            atom.store(x)
83
        return atom.load()
84
    finally:
85
        del atom
86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.