This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients eric.snow, ncoghlan, serhiy.storchaka, vstinner
Date 2017-03-23.10:00:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I like this idea in principle, and suspect it may be helpful in the implementation of the new thread-specific-storage API proposed in PEP 539 (also see ).

How would you feel about calling it _Py_ONCE_VAR? The use case here is quite similar to the concepts behind the pthread_once API (just with the lifecycle tied to Py_Initialize/Py_Finalize rather than the C level process), and it's the "initialise-on-first-use" behaviour that's significant here, moreso than the fact that the typical storage target is a static variable.

As far as the linked list goes, the nice aspect of Victor's approach is that it doesn't need to do any additional runtime memory allocations - all the storage involved is still statically allocated in the modules that initialise the values, there are just some extra  pointer assignments to link everything together (with the GIL protecting against race conditions).

However, the user visible ".obj" indirection could still be avoided at the cost of an additional pointer per entry:

    typedef struct _Py_OnceVar {
        struct _Py_OnceVar *next;
        PyObject **obj_ref;
    } _Py_OnceVar;

    #define _Py_ONCE_VAR(var_decl, var) \
      var_decl var = NULL;
      static _Py_OnceVar var ## _once_meta = {.next = NULL, .obj_ref = (PyObject **) &var}

Intended declaration:

    _Py_ONCE_VAR(static PyObject *, array_reconstructor);

_Py_ONCE_VAR_INIT would similarly be adjusted to assign the supplied value to "var", while also setting "" to hook the value into the linked list.
Date User Action Args
2017-03-23 10:00:58ncoghlansetrecipients: + ncoghlan, vstinner, eric.snow, serhiy.storchaka
2017-03-23 10:00:57ncoghlansetmessageid: <>
2017-03-23 10:00:57ncoghlanlinkissue29881 messages
2017-03-23 10:00:57ncoghlancreate