Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
added some threading related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Massimiliano Pippi authored and sbinet committed Apr 1, 2016
1 parent 2d6a486 commit 8949822
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions python.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
"fmt"
)

// PyGILState is the Go alias for the PyGILState_STATE enum
type PyGILState C.PyGILState_STATE

// PyThreadState layer
type PyThreadState struct {
ptr *C.PyThreadState
}

// Initialize initializes the python interpreter and its GIL
func Initialize() error {
// make sure the python interpreter has been initialized
Expand Down Expand Up @@ -35,4 +43,62 @@ func Finalize() error {
return nil
}

// PyThreadState* PyEval_SaveThread()
// Release the global interpreter lock (if it has been created and thread
// support is enabled) and reset the thread state to NULL, returning the
// previous thread state (which is not NULL). If the lock has been created,
// the current thread must have acquired it. (This function is available even
// when thread support is disabled at compile time.)
func PyEval_SaveThread() *PyThreadState {
state := C.PyEval_SaveThread()
return &PyThreadState{ptr: state}
}

// void PyEval_RestoreThread(PyThreadState *tstate)
// Acquire the global interpreter lock (if it has been created and thread
// support is enabled) and set the thread state to tstate, which must not be
// NULL. If the lock has been created, the current thread must not have
// acquired it, otherwise deadlock ensues. (This function is available even
// when thread support is disabled at compile time.)
func PyEval_RestoreThread(state *PyThreadState) {
C.PyEval_RestoreThread(state.ptr)
}

// Ensure that the current thread is ready to call the Python C API regardless
// of the current state of Python, or of the global interpreter lock. This may
// be called as many times as desired by a thread as long as each call is
// matched with a call to PyGILState_Release(). In general, other thread-related
// APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls
// as long as the thread state is restored to its previous state before the
// Release(). For example, normal usage of the Py_BEGIN_ALLOW_THREADS and
// Py_END_ALLOW_THREADS macros is acceptable.
//
// The return value is an opaque “handle” to the thread state when
// PyGILState_Ensure() was called, and must be passed to PyGILState_Release()
// to ensure Python is left in the same state. Even though recursive calls are
// allowed, these handles cannot be shared - each unique call to
// PyGILState_Ensure() must save the handle for its call to PyGILState_Release().
//
// When the function returns, the current thread will hold the GIL and be able
// to call arbitrary Python code. Failure is a fatal error.
//
// New in version 2.3.
func PyGILState_Ensure() PyGILState {
return PyGILState(C.PyGILState_Ensure())
}

// void PyGILState_Release(PyGILState_STATE)
// Release any resources previously acquired. After this call, Python’s state
// will be the same as it was prior to the corresponding PyGILState_Ensure()
// call (but generally this state will be unknown to the caller, hence the use
// of the GILState API).
//
// Every call to PyGILState_Ensure() must be matched by a call to
// PyGILState_Release() on the same thread.
//
// New in version 2.3.
func PyGILState_Release(state PyGILState) {
C.PyGILState_Release(C.PyGILState_STATE(state))
}

// EOF

0 comments on commit 8949822

Please sign in to comment.