Skip to content

Commit a15bb57

Browse files
committed
use atomic op in public APIs
1 parent e311708 commit a15bb57

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Objects/funcobject.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ _PyFunction_LookupByVersion(uint32_t version, PyObject **p_code)
397397
uint32_t
398398
_PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
399399
{
400-
return func->func_version;
400+
return FT_ATOMIC_LOAD_UINT32_RELAXED(func->func_version);
401401
}
402402

403403
PyObject *
@@ -413,7 +413,7 @@ PyFunction_GetCode(PyObject *op)
413413
PyErr_BadInternalCall();
414414
return NULL;
415415
}
416-
return ((PyFunctionObject *) op) -> func_code;
416+
return FT_ATOMIC_LOAD_PTR(((PyFunctionObject *) op) -> func_code);
417417
}
418418

419419
PyObject *
@@ -423,7 +423,7 @@ PyFunction_GetGlobals(PyObject *op)
423423
PyErr_BadInternalCall();
424424
return NULL;
425425
}
426-
return ((PyFunctionObject *) op) -> func_globals;
426+
return FT_ATOMIC_LOAD_PTR(((PyFunctionObject *) op) -> func_globals);
427427
}
428428

429429
PyObject *
@@ -433,7 +433,7 @@ PyFunction_GetModule(PyObject *op)
433433
PyErr_BadInternalCall();
434434
return NULL;
435435
}
436-
return ((PyFunctionObject *) op) -> func_module;
436+
return FT_ATOMIC_LOAD_PTR(((PyFunctionObject *) op) -> func_module);
437437
}
438438

439439
PyObject *
@@ -443,7 +443,7 @@ PyFunction_GetDefaults(PyObject *op)
443443
PyErr_BadInternalCall();
444444
return NULL;
445445
}
446-
return ((PyFunctionObject *) op) -> func_defaults;
446+
return FT_ATOMIC_LOAD_PTR(((PyFunctionObject *) op) -> func_defaults);
447447
}
448448

449449
int
@@ -462,19 +462,23 @@ PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
462462
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
463463
return -1;
464464
}
465+
Py_BEGIN_CRITICAL_SECTION(op);
465466
handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS,
466467
(PyFunctionObject *) op, defaults);
467468
_PyFunction_ClearVersion((PyFunctionObject *)op);
468469
Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
470+
Py_END_CRITICAL_SECTION();
469471
return 0;
470472
}
471473

472474
void
473475
PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
474476
{
475477
assert(func != NULL);
478+
Py_BEGIN_CRITICAL_SECTION(func);
476479
_PyFunction_ClearVersion(func);
477480
func->vectorcall = vectorcall;
481+
Py_END_CRITICAL_SECTION();
478482
}
479483

480484
PyObject *
@@ -484,7 +488,7 @@ PyFunction_GetKwDefaults(PyObject *op)
484488
PyErr_BadInternalCall();
485489
return NULL;
486490
}
487-
return ((PyFunctionObject *) op) -> func_kwdefaults;
491+
return FT_ATOMIC_LOAD_PTR(((PyFunctionObject *) op) -> func_kwdefaults);
488492
}
489493

490494
int
@@ -504,10 +508,12 @@ PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
504508
"non-dict keyword only default args");
505509
return -1;
506510
}
511+
Py_BEGIN_CRITICAL_SECTION(op);
507512
handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS,
508513
(PyFunctionObject *) op, defaults);
509514
_PyFunction_ClearVersion((PyFunctionObject *)op);
510515
Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
516+
Py_END_CRITICAL_SECTION();
511517
return 0;
512518
}
513519

@@ -518,7 +524,7 @@ PyFunction_GetClosure(PyObject *op)
518524
PyErr_BadInternalCall();
519525
return NULL;
520526
}
521-
return ((PyFunctionObject *) op) -> func_closure;
527+
return FT_ATOMIC_LOAD_PTR(((PyFunctionObject *) op) -> func_closure);
522528
}
523529

524530
int
@@ -539,8 +545,10 @@ PyFunction_SetClosure(PyObject *op, PyObject *closure)
539545
Py_TYPE(closure)->tp_name);
540546
return -1;
541547
}
548+
Py_BEGIN_CRITICAL_SECTION(op);
542549
_PyFunction_ClearVersion((PyFunctionObject *)op);
543550
Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
551+
Py_END_CRITICAL_SECTION();
544552
return 0;
545553
}
546554

@@ -597,7 +605,11 @@ PyFunction_GetAnnotations(PyObject *op)
597605
PyErr_BadInternalCall();
598606
return NULL;
599607
}
600-
return func_get_annotation_dict((PyFunctionObject *)op);
608+
PyObject *d = NULL;
609+
Py_BEGIN_CRITICAL_SECTION(op);
610+
d = func_get_annotation_dict((PyFunctionObject *)op);
611+
Py_END_CRITICAL_SECTION();
612+
return d;
601613
}
602614

603615
int
@@ -618,8 +630,10 @@ PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
618630
return -1;
619631
}
620632
PyFunctionObject *func = (PyFunctionObject *)op;
633+
Py_BEGIN_CRITICAL_SECTION(op);
621634
Py_XSETREF(func->func_annotations, annotations);
622635
Py_CLEAR(func->func_annotate);
636+
Py_END_CRITICAL_SECTION();
623637
return 0;
624638
}
625639

@@ -1231,8 +1245,9 @@ static PyObject*
12311245
func_repr(PyObject *self)
12321246
{
12331247
PyFunctionObject *op = _PyFunction_CAST(self);
1248+
PyObject *func_name = FT_ATOMIC_LOAD_PTR(op->func_qualname);
12341249
return PyUnicode_FromFormat("<function %U at %p>",
1235-
op->func_qualname, op);
1250+
func_name, op);
12361251
}
12371252

12381253
static int

0 commit comments

Comments
 (0)