Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
greenlet_internal.hpp110 linesDownload Raw Back to greenlet
1/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2#ifndef GREENLET_INTERNAL_H
3#define GREENLET_INTERNAL_H
4#ifdef __clang__
5#    pragma clang diagnostic push
6#    pragma clang diagnostic ignored "-Wunused-function"
7#endif
8
9/**
10 * Implementation helpers.
11 *
12 * C++ templates and inline functions should go here.
13 */
14#define PY_SSIZE_T_CLEAN
15#include "greenlet_compiler_compat.hpp"
16#include "greenlet_cpython_compat.hpp"
17#include "greenlet_exceptions.hpp"
18#include "TGreenlet.hpp"
19#include "greenlet_allocator.hpp"
20
21#include <vector>
22#include <string>
23
24#define GREENLET_MODULE
25struct _greenlet;
26typedef struct _greenlet PyGreenlet;
27namespace greenlet {
28
29    class ThreadState;
30    // We can't use the PythonAllocator for this, because we push to it
31    // from the thread state destructor, which doesn't have the GIL,
32    // and Python's allocators can only be called with the GIL.
33    typedef std::vector<ThreadState*> cleanup_queue_t;
34};
35
36
37#define implementation_ptr_t greenlet::Greenlet*
38
39
40#include "greenlet.h"
41
42void
43greenlet::refs::MainGreenletExactChecker(void *p)
44{
45    if (!p) {
46        return;
47    }
48    if (greenlet::IsShuttingDown()) {
49        return;
50    }
51    // We control the class of the main greenlet exactly.
52    if (Py_TYPE(p) != &PyGreenlet_Type) {
53        std::string err("MainGreenlet: Expected exactly a greenlet, not a ");
54        err += Py_TYPE(p)->tp_name;
55        throw greenlet::TypeError(err);
56    }
57
58    // Greenlets from dead threads no longer respond to main() with a
59    // true value; so in that case we need to perform an additional
60    // check.
61    Greenlet* g = static_cast<PyGreenlet*>(p)->pimpl;
62    if (g->main()) {
63        return;
64    }
65    if (!dynamic_cast<MainGreenlet*>(g)) {
66        std::string err("MainGreenlet: Expected exactly a main greenlet, not a ");
67        err += Py_TYPE(p)->tp_name;
68        throw greenlet::TypeError(err);
69    }
70}
71
72
73
74template <typename T, greenlet::refs::TypeChecker TC>
75inline greenlet::Greenlet* greenlet::refs::_OwnedGreenlet<T, TC>::operator->() const noexcept
76{
77    return reinterpret_cast<PyGreenlet*>(this->p)->pimpl;
78}
79
80template <typename T, greenlet::refs::TypeChecker TC>
81inline greenlet::Greenlet* greenlet::refs::_BorrowedGreenlet<T, TC>::operator->() const noexcept
82{
83    return reinterpret_cast<PyGreenlet*>(this->p)->pimpl;
84}
85
86#include <memory>
87#include <stdexcept>
88
89
90extern PyTypeObject PyGreenlet_Type;
91
92
93
94/**
95  * Forward declarations needed in multiple files.
96  */
97static PyObject* green_switch(PyGreenlet* self, PyObject* args, PyObject* kwargs);
98
99
100#ifdef __clang__
101#    pragma clang diagnostic pop
102#endif
103
104
105#endif
106
107// Local Variables:
108// flycheck-clang-include-path: ("../../include" "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/include/python3.10")
109// End:
110 
codekingpro/portable-devtools · Team Ai