Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
TMainGreenlet.cpp164 linesDownload Raw Back to greenlet
1/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2/**
3 * Implementation of greenlet::MainGreenlet.
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_MAIN_GREENLET_CPP
13#define T_MAIN_GREENLET_CPP
14
15#include "TGreenlet.hpp"
16
17#ifdef Py_GIL_DISABLED
18#include <atomic>
19#endif
20
21// Incremented when we create a main greenlet, in a new thread, decremented
22// when it is destroyed.
23#ifdef Py_GIL_DISABLED
24static std::atomic<Py_ssize_t> G_TOTAL_MAIN_GREENLETS(0);
25#else
26// Protected by the GIL.
27static Py_ssize_t G_TOTAL_MAIN_GREENLETS;
28#endif
29
30namespace greenlet {
31greenlet::PythonAllocator<MainGreenlet> MainGreenlet::allocator;
32
33void* MainGreenlet::operator new(size_t UNUSED(count))
34{
35    return allocator.allocate(1);
36}
37
38
39void MainGreenlet::operator delete(void* ptr)
40{
41    return allocator.deallocate(static_cast<MainGreenlet*>(ptr),
42                                1);
43}
44
45
46MainGreenlet::MainGreenlet(PyGreenlet* p, ThreadState* state)
47    : Greenlet(p, StackState::make_main()),
48      _self(p),
49      _thread_state(state)
50{
51    G_TOTAL_MAIN_GREENLETS++;
52}
53
54MainGreenlet::~MainGreenlet()
55{
56    G_TOTAL_MAIN_GREENLETS--;
57    this->tp_clear();
58}
59
60ThreadState*
61MainGreenlet::thread_state() const noexcept
62{
63    return this->_thread_state;
64}
65
66void
67MainGreenlet::thread_state(ThreadState* t) noexcept
68{
69    // this method is only used during thread tear down, when it is
70    // called with nullptr, signalling the thread is dead.
71    assert(!t);
72    this->_thread_state = t;
73}
74
75
76const BorrowedMainGreenlet
77MainGreenlet::main_greenlet() const
78{
79    return this->_self;
80}
81
82BorrowedMainGreenlet
83MainGreenlet::find_main_greenlet_in_lineage() const
84{
85    return BorrowedMainGreenlet(this->_self);
86}
87
88bool
89MainGreenlet::was_running_in_dead_thread() const noexcept
90{
91    return !this->_thread_state;
92}
93
94OwnedObject
95MainGreenlet::g_switch()
96{
97    try {
98        this->check_switch_allowed();
99    }
100    catch (const PyErrOccurred&) {
101        this->release_args();
102        throw;
103    }
104
105    switchstack_result_t err = this->g_switchstack();
106    if (err.status < 0) {
107        // XXX: This code path is untested, but it is shared
108        // with the UserGreenlet path that is tested.
109        return this->on_switchstack_or_initialstub_failure(
110            this,
111            err,
112            true, // target was me
113            false // was initial stub
114        );
115    }
116
117    return err.the_new_current_greenlet->g_switch_finish(err);
118}
119
120int
121MainGreenlet::tp_traverse(visitproc visit, void* arg)
122{
123    ThreadState* thread_state = this->_thread_state.load();
124    if (thread_state) {
125        // we've already traversed main, (self), don't do it again.
126        int result = thread_state->tp_traverse(visit, arg, false);
127        if (result) {
128            return result;
129        }
130    }
131    return Greenlet::tp_traverse(visit, arg);
132}
133
134const OwnedObject&
135MainGreenlet::run() const
136{
137    throw AttributeError("Main greenlets do not have a run attribute.");
138}
139
140void
141MainGreenlet::run(const BorrowedObject UNUSED(nrun))
142{
143   throw AttributeError("Main greenlets do not have a run attribute.");
144}
145
146void
147MainGreenlet::parent(const BorrowedObject raw_new_parent)
148{
149    if (!raw_new_parent) {
150        throw AttributeError("can't delete attribute");
151    }
152    throw AttributeError("cannot set the parent of a main greenlet");
153}
154
155const OwnedGreenlet
156MainGreenlet::parent() const
157{
158    return OwnedGreenlet(); // null becomes None
159}
160
161}; // namespace greenlet
162
163#endif
164 
codekingpro/portable-devtools · Team Ai