Skip to content

Commit

Permalink
Move BrokenGreenlet to its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Sep 10, 2023
1 parent 0b108b3 commit cec93b1
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 83 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _find_platform_headers():
return glob.glob(GREENLET_PLATFORM_DIR + "switch_*.h")

def _find_impl_headers():
return glob.glob(GREENLET_SRC_DIR + "*.hpp")
return glob.glob(GREENLET_SRC_DIR + "*.hpp") + glob.glob(GREENLET_SRC_DIR + "*.cpp")

if hasattr(sys, "pypy_version_info"):
ext_modules = []
Expand Down
45 changes: 45 additions & 0 deletions src/greenlet/TBrokenGreenlet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
/**
* Implementation of greenlet::UserGreenlet.
*
* Format with:
* clang-format -i --style=file src/greenlet/greenlet.c
*
*
* Fix missing braces with:
* clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
*/

#include "greenlet_greenlet.hpp"

namespace greenlet {

void* BrokenGreenlet::operator new(size_t UNUSED(count))
{
return allocator.allocate(1);
}


void BrokenGreenlet::operator delete(void* ptr)
{
return allocator.deallocate(static_cast<BrokenGreenlet*>(ptr),
1);
}

greenlet::PythonAllocator<greenlet::BrokenGreenlet> greenlet::BrokenGreenlet::allocator;

bool
BrokenGreenlet::force_slp_switch_error() const noexcept
{
return this->_force_slp_switch_error;
}

UserGreenlet::switchstack_result_t BrokenGreenlet::g_switchstack(void)
{
if (this->_force_switch_error) {
return switchstack_result_t(-1);
}
return UserGreenlet::g_switchstack();
}

}; //namespace greenlet
14 changes: 9 additions & 5 deletions src/greenlet/TGreenlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "TGreenletGlobals.cpp"
#include "TThreadStateDestroy.cpp"

namespace greenlet {

Greenlet::Greenlet(PyGreenlet* p)
{
p ->pimpl = this;
Expand All @@ -43,7 +45,8 @@ Greenlet::force_slp_switch_error() const noexcept
return false;
}

void Greenlet::release_args()
void
Greenlet::release_args()
{
this->switch_args.CLEAR();
}
Expand Down Expand Up @@ -300,7 +303,8 @@ Greenlet::context() const
}


void Greenlet::context(BorrowedObject given)
void
Greenlet::context(BorrowedObject given)
{
using greenlet::PythonStateContext;
if (!given) {
Expand Down Expand Up @@ -333,8 +337,6 @@ void Greenlet::context(BorrowedObject given)
}
}


namespace greenlet {
/**
* CAUTION: May invoke arbitrary Python code.
*
Expand Down Expand Up @@ -404,7 +406,7 @@ g_handle_exit(const OwnedObject& greenlet_result)
return OwnedObject();
}

}; //namespace greenlet


/**
* May run arbitrary Python code.
Expand Down Expand Up @@ -598,3 +600,5 @@ Greenlet::tp_clear()
this->python_state.tp_clear(own_top_frame);
return 0;
}

}; // namespace greenlet
4 changes: 0 additions & 4 deletions src/greenlet/TGreenletGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#include "greenlet_thread_support.hpp"
#include "greenlet_thread_state.hpp"

using greenlet::Mutex;
using greenlet::Require;


namespace greenlet {

// This encapsulates what were previously module global "constants"
Expand Down
7 changes: 3 additions & 4 deletions src/greenlet/TMainGreenlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
#include "greenlet_greenlet.hpp"
#include "greenlet_thread_state.hpp"

using greenlet::ThreadState;
using greenlet::Greenlet;
using greenlet::MainGreenlet;


// Protected by the GIL. Incremented when we create a main greenlet,
// in a new thread, decremented when it is destroyed.
static Py_ssize_t G_TOTAL_MAIN_GREENLETS;

namespace greenlet {
greenlet::PythonAllocator<MainGreenlet> MainGreenlet::allocator;

void* MainGreenlet::operator new(size_t UNUSED(count))
Expand Down Expand Up @@ -154,3 +151,5 @@ MainGreenlet::parent() const
{
return OwnedGreenlet(); // null becomes None
}

}; // namespace greenlet
7 changes: 3 additions & 4 deletions src/greenlet/TThreadStateDestroy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
#include "greenlet_thread_support.hpp"
#include "TGreenletGlobals.cpp"

using greenlet::ThreadState;
using greenlet::MainGreenlet;
using greenlet::LockGuard;
namespace greenlet {

struct ThreadState_DestroyWithGIL
{
Expand Down Expand Up @@ -176,6 +174,7 @@ struct ThreadState_DestroyNoGIL

};

}; // namespace greenlet

// The intent when GET_THREAD_STATE() is needed multiple times in a
// function is to take a reference to its return value in a local
Expand All @@ -184,7 +183,7 @@ struct ThreadState_DestroyNoGIL
// initial function call in each function that uses a thread local);
// in contrast, static volatile variables are at some pre-computed
// offset.
typedef greenlet::ThreadStateCreator<ThreadState_DestroyNoGIL> ThreadStateCreator;
typedef greenlet::ThreadStateCreator<greenlet::ThreadState_DestroyNoGIL> ThreadStateCreator;
static thread_local ThreadStateCreator g_thread_state_global;
#define GET_THREAD_STATE() g_thread_state_global

Expand Down
9 changes: 4 additions & 5 deletions src/greenlet/TUserGreenlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#include "greenlet_thread_state.hpp"
#include "TThreadStateDestroy.cpp"

using greenlet::UserGreenlet;
using greenlet::ThreadState;
using greenlet::refs::BorrowedMainGreenlet;

namespace greenlet {
using greenlet::refs::BorrowedMainGreenlet;
greenlet::PythonAllocator<UserGreenlet> UserGreenlet::allocator;

void* UserGreenlet::operator new(size_t UNUSED(count))
Expand All @@ -40,8 +39,6 @@ UserGreenlet::UserGreenlet(PyGreenlet* p, BorrowedGreenlet the_parent)
this->_self = p;
}



UserGreenlet::~UserGreenlet()
{
// Python 3.11: If we don't clear out the raw frame datastack
Expand Down Expand Up @@ -638,3 +635,5 @@ UserGreenlet::ParentIsCurrentGuard::~ParentIsCurrentGuard()
this->greenlet->_parent = oldparent;
oldparent.CLEAR();
}

}; //namespace greenlet
60 changes: 8 additions & 52 deletions src/greenlet/greenlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@
#include "TGreenlet.cpp"
#include "TMainGreenlet.cpp"
#include "TUserGreenlet.cpp"
#include "TBrokenGreenlet.cpp"


using greenlet::ThreadState;
using greenlet::Mutex;
using greenlet::LockGuard;
using greenlet::LockInitError;
using greenlet::PyErrOccurred;
using greenlet::Require;
using greenlet::PyFatalError;
using greenlet::ExceptionState;
using greenlet::StackState;
using greenlet::Greenlet;
using greenlet::BrokenGreenlet;

using greenlet::g_handle_exit;
using greenlet::single_result;

using greenlet::Greenlet;
using greenlet::UserGreenlet;
using greenlet::MainGreenlet;
using greenlet::BrokenGreenlet;
using greenlet::ThreadState;


// Helpers for reference counting.
// XXX: running the test cases for greenlet 1.1.2 under Python 3.10+pydebug
Expand Down Expand Up @@ -140,21 +141,6 @@ using greenlet::single_result;
// ------------------------------------------------------- ----- ----
// total 117 482

using greenlet::refs::BorrowedObject;
using greenlet::refs::BorrowedGreenlet;
using greenlet::refs::BorrowedMainGreenlet;
using greenlet::refs::OwnedObject;
using greenlet::refs::PyErrFetchParam;
using greenlet::refs::PyArgParseParam;
using greenlet::refs::ImmortalString;
using greenlet::refs::ImmortalObject;
using greenlet::refs::ImmortalEventName;
using greenlet::refs::CreatedModule;
using greenlet::refs::PyErrPieces;
using greenlet::refs::PyObjectPointer;
using greenlet::Greenlet;
using greenlet::UserGreenlet;
using greenlet::MainGreenlet;


// ******* Implementation of things from included files
Expand Down Expand Up @@ -314,36 +300,6 @@ are called from trampoline functions that themselves are declared as
not eligible for inlining.
*/



/* add forward declarations */





void* BrokenGreenlet::operator new(size_t UNUSED(count))
{
return allocator.allocate(1);
}


void BrokenGreenlet::operator delete(void* ptr)
{
return allocator.deallocate(static_cast<BrokenGreenlet*>(ptr),
1);
}

bool
BrokenGreenlet::force_slp_switch_error() const noexcept
{
return this->_force_slp_switch_error;
}


greenlet::PythonAllocator<BrokenGreenlet> BrokenGreenlet::allocator;


extern "C" {
static int GREENLET_NOINLINE(slp_save_state_trampoline)(char* stackref)
{
Expand Down
9 changes: 1 addition & 8 deletions src/greenlet/greenlet_greenlet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,7 @@ class TracingGuard
virtual ~BrokenGreenlet()
{}

virtual switchstack_result_t g_switchstack(void)
{
if (this->_force_switch_error) {
return switchstack_result_t(-1);
}
return UserGreenlet::g_switchstack();
}

virtual switchstack_result_t g_switchstack(void);
virtual bool force_slp_switch_error() const noexcept;

};
Expand Down
6 changes: 6 additions & 0 deletions src/greenlet/greenlet_refs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,12 @@ namespace greenlet {
{
}

ImmortalObject(const ImmortalObject& other)
: PyObjectPointer<>(other.p)
{

}

/**
* Become the new owner of the object. Does not change the
* reference count.
Expand Down

0 comments on commit cec93b1

Please sign in to comment.