Skip to content

Commit dbfa939

Browse files
committed
avif: don't call init_max_threads() in module PyInit function
1 parent f28e375 commit dbfa939

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

src/_avif.c

+55-49
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ typedef struct {
2020
avifRange range;
2121
} avifEncOptions;
2222

23-
static int max_threads = 1;
24-
2523
// Encoder type
2624
typedef struct {
2725
PyObject_HEAD
@@ -45,6 +43,53 @@ typedef struct {
4543

4644
static PyTypeObject AvifDecoder_Type;
4745

46+
static int max_threads = 0;
47+
48+
static void
49+
init_max_threads(void) {
50+
PyObject *os = NULL;
51+
PyObject *n = NULL;
52+
long num_cpus;
53+
54+
os = PyImport_ImportModule("os");
55+
if (os == NULL) {
56+
goto error;
57+
}
58+
59+
if (PyObject_HasAttrString(os, "sched_getaffinity")) {
60+
n = PyObject_CallMethod(os, "sched_getaffinity", "i", 0);
61+
if (n == NULL) {
62+
goto error;
63+
}
64+
num_cpus = PySet_Size(n);
65+
} else {
66+
n = PyObject_CallMethod(os, "cpu_count", NULL);
67+
if (n == NULL) {
68+
goto error;
69+
}
70+
num_cpus = PyLong_AsLong(n);
71+
}
72+
73+
if (num_cpus < 1) {
74+
goto error;
75+
}
76+
77+
max_threads = (int)num_cpus;
78+
79+
done:
80+
Py_XDECREF(os);
81+
Py_XDECREF(n);
82+
return;
83+
84+
error:
85+
if (PyErr_Occurred()) {
86+
PyErr_Clear();
87+
}
88+
PyErr_WarnEx(
89+
PyExc_RuntimeWarning, "could not get cpu count: using max_threads=1", 1);
90+
goto done;
91+
}
92+
4893
static int
4994
normalize_quantize_value(int qvalue) {
5095
if (qvalue < AVIF_QUANTIZER_BEST_QUALITY) {
@@ -206,6 +251,11 @@ AvifEncoderNew(PyObject *self_, PyObject *args) {
206251
self->xmp_bytes = NULL;
207252

208253
encoder = avifEncoderCreate();
254+
255+
if (max_threads == 0) {
256+
init_max_threads();
257+
}
258+
209259
encoder->maxThreads = max_threads;
210260
encoder->minQuantizer = enc_options.qmin;
211261
encoder->maxQuantizer = enc_options.qmax;
@@ -506,6 +556,9 @@ AvifDecoderNew(PyObject *self_, PyObject *args) {
506556

507557
self->decoder = avifDecoderCreate();
508558
#if AVIF_VERSION >= 80400
559+
if (max_threads == 0) {
560+
init_max_threads();
561+
}
509562
self->decoder->maxThreads = max_threads;
510563
#endif
511564
self->decoder->codecChoice = codec;
@@ -742,57 +795,10 @@ setup_module(PyObject *m) {
742795
return 0;
743796
}
744797

745-
static void
746-
init_max_threads(void) {
747-
PyObject *os = NULL;
748-
PyObject *n = NULL;
749-
long num_cpus;
750-
751-
os = PyImport_ImportModule("os");
752-
if (os == NULL) {
753-
goto error;
754-
}
755-
756-
if (PyObject_HasAttrString(os, "sched_getaffinity")) {
757-
n = PyObject_CallMethod(os, "sched_getaffinity", "i", 0);
758-
if (n == NULL) {
759-
goto error;
760-
}
761-
num_cpus = PySet_Size(n);
762-
} else {
763-
n = PyObject_CallMethod(os, "cpu_count", NULL);
764-
if (n == NULL) {
765-
goto error;
766-
}
767-
num_cpus = PyLong_AsLong(n);
768-
}
769-
770-
if (num_cpus < 1) {
771-
goto error;
772-
}
773-
774-
max_threads = (int)num_cpus;
775-
776-
done:
777-
Py_XDECREF(os);
778-
Py_XDECREF(n);
779-
return;
780-
781-
error:
782-
if (PyErr_Occurred()) {
783-
PyErr_Clear();
784-
}
785-
PyErr_WarnEx(
786-
PyExc_RuntimeWarning, "could not get cpu count: using max_threads=1", 1);
787-
goto done;
788-
}
789-
790798
PyMODINIT_FUNC
791799
PyInit__avif(void) {
792800
PyObject *m;
793801

794-
init_max_threads();
795-
796802
static PyModuleDef module_def = {
797803
PyModuleDef_HEAD_INIT,
798804
.m_name = "_avif",

0 commit comments

Comments
 (0)