-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic.c
95 lines (72 loc) · 1.48 KB
/
atomic.c
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
// SPDX-License-Identifier: LGPL-3.0-only
/*
* Copyright (C) 2021 Sean Anderson <[email protected]>
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include <stdalign.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/mman.h>
#include "_mpmetrics.h"
static maybe_unused int PyLong_AsInt(PyObject *obj)
{
long value = PyLong_AsLong(obj);
if (PyErr_Occurred())
return 0;
if (value > INT_MAX || value < INT_MIN) {
PyErr_Format(PyExc_OverflowError,
"%ld too large to convert to int", value);
return 0;
}
return value;
}
static maybe_unused unsigned int PyLong_AsUnsignedInt(PyObject *obj)
{
unsigned long value = PyLong_AsUnsignedLong(obj);
if (PyErr_Occurred())
return 0;
if (value > UINT_MAX) {
PyErr_Format(PyExc_OverflowError,
"%lu too large to convert to int", value);
return 0;
}
return value;
}
/* my apologies */
#define WIDTH 32
#include "atomic.h"
#undef WIDTH
#define WIDTH 64
#include "atomic.h"
#undef WIDTH
#define SIGNED
#define WIDTH 32
#include "atomic.h"
#undef WIDTH
#define WIDTH 64
#include "atomic.h"
#undef WIDTH
#undef SIGNED
#define DOUBLE
#include "atomic.h"
#undef DOUBLE
int AtomicTypes_Add(PyObject *m)
{
if (AtomicInt32Type_Add(m))
return -1;
if (AtomicInt64Type_Add(m))
return -1;
if (AtomicUInt32Type_Add(m))
return -1;
if (AtomicUInt64Type_Add(m))
return -1;
if (AtomicDoubleType_Add(m))
return -1;
return 0;
}