Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
TGreenletGlobals.cpp114 linesDownload Raw Back to greenlet
1/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2/**
3 * Implementation of GreenletGlobals.
4 *
5 * Format with:
6 *  clang-format -i --style=file src/greenlet/greenlet.c
7 *
8 *
9 * Fix missing braces with:
10 *   clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11*/
12#ifndef T_GREENLET_GLOBALS
13#define T_GREENLET_GLOBALS
14
15#include <algorithm>
16
17#include "greenlet_refs.hpp"
18#include "greenlet_exceptions.hpp"
19#include "greenlet_thread_support.hpp"
20#include "greenlet_internal.hpp"
21
22namespace greenlet {
23
24// This encapsulates what were previously module global "constants"
25// established at init time.
26// This is a step towards Python3 style module state that allows
27// reloading.
28//
29// In an earlier iteration of this code, we used placement new to be
30// able to allocate this object statically still, so that references
31// to its members don't incur an extra pointer indirection.
32// But under some scenarios, that could result in crashes at
33// shutdown because apparently the destructor was getting run twice?
34class GreenletGlobals
35{
36
37public:
38    const greenlet::refs::ImmortalEventName event_switch;
39    const greenlet::refs::ImmortalEventName event_throw;
40    const greenlet::refs::ImmortalException PyExc_GreenletError;
41    const greenlet::refs::ImmortalException PyExc_GreenletExit;
42    const greenlet::refs::ImmortalObject empty_tuple;
43    const greenlet::refs::ImmortalObject empty_dict;
44    const greenlet::refs::ImmortalString str_run;
45    Mutex* const thread_states_to_destroy_lock;
46    greenlet::cleanup_queue_t thread_states_to_destroy;
47
48    GreenletGlobals() :
49        event_switch("switch"),
50        event_throw("throw"),
51        PyExc_GreenletError("greenlet.error"),
52        PyExc_GreenletExit("greenlet.GreenletExit", PyExc_BaseException),
53        empty_tuple(Require(PyTuple_New(0))),
54        empty_dict(Require(PyDict_New())),
55        str_run("run"),
56        thread_states_to_destroy_lock(new Mutex())
57    {}
58
59    ~GreenletGlobals()
60    {
61        // This object is (currently) effectively immortal, and not
62        // just because of those placement new tricks; if we try to
63        // deallocate the static object we allocated, and overwrote,
64        // we would be doing so at C++ teardown time, which is after
65        // the final Python GIL is released, and we can't use the API
66        // then.
67        // (The members will still be destructed, but they also don't
68        // do any deallocation.)
69    }
70
71    /**
72     * Must be holding the ``thread_states_to_destroy`` lock.
73     */
74    void queue_to_destroy(ThreadState* ts) const
75    {
76        // we're currently accessed through a static const object,
77        // implicitly marking our members as const, so code can't just
78        // call push_back (or pop_back) without casting away the
79        // const.
80        //
81        // Do that for callers.
82        greenlet::cleanup_queue_t& q = const_cast<greenlet::cleanup_queue_t&>(
83            this->thread_states_to_destroy);
84        // make sure we don't ever try to clean up a state more than
85        // once. Because they're thread-local, and we ultimately call this
86        // method from the destructor of the thread local variable,
87        // we should never find the item already present. This check
88        // is nominally O(n) in the size of the vector.
89        assert(std::find(q.begin(), q.end(), ts) == q.end());
90        q.push_back(ts);
91    }
92
93    /**
94     * Must be holding the ``thread_states_to_destroy`` lock.
95     */
96    ThreadState* take_next_to_destroy() const
97    {
98        greenlet::cleanup_queue_t& q = const_cast<greenlet::cleanup_queue_t&>(
99            this->thread_states_to_destroy);
100        if (q.empty()) {
101            return nullptr;
102        }
103        ThreadState* result = q.back();
104        q.pop_back();
105        return result;
106    }
107};
108
109}; // namespace greenlet
110
111static const greenlet::GreenletGlobals* mod_globs;
112
113#endif // T_GREENLET_GLOBALS
114 
codekingpro/portable-devtools · Team Ai