Content-Length: 368482 | pFad | http://github.com/4cecoder/go-python/commit/894982222676379a77af235f9eadbadfc8591ff0

4F added some threading related functions · 4cecoder/go-python@8949822 · GitHub
Skip to content

Commit 8949822

Browse files
Massimiliano Pippisbinet
authored andcommitted
added some threading related functions
1 parent 2d6a486 commit 8949822

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

python.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import (
88
"fmt"
99
)
1010

11+
// PyGILState is the Go alias for the PyGILState_STATE enum
12+
type PyGILState C.PyGILState_STATE
13+
14+
// PyThreadState layer
15+
type PyThreadState struct {
16+
ptr *C.PyThreadState
17+
}
18+
1119
// Initialize initializes the python interpreter and its GIL
1220
func Initialize() error {
1321
// make sure the python interpreter has been initialized
@@ -35,4 +43,62 @@ func Finalize() error {
3543
return nil
3644
}
3745

46+
// PyThreadState* PyEval_SaveThread()
47+
// Release the global interpreter lock (if it has been created and thread
48+
// support is enabled) and reset the thread state to NULL, returning the
49+
// previous thread state (which is not NULL). If the lock has been created,
50+
// the current thread must have acquired it. (This function is available even
51+
// when thread support is disabled at compile time.)
52+
func PyEval_SaveThread() *PyThreadState {
53+
state := C.PyEval_SaveThread()
54+
return &PyThreadState{ptr: state}
55+
}
56+
57+
// void PyEval_RestoreThread(PyThreadState *tstate)
58+
// Acquire the global interpreter lock (if it has been created and thread
59+
// support is enabled) and set the thread state to tstate, which must not be
60+
// NULL. If the lock has been created, the current thread must not have
61+
// acquired it, otherwise deadlock ensues. (This function is available even
62+
// when thread support is disabled at compile time.)
63+
func PyEval_RestoreThread(state *PyThreadState) {
64+
C.PyEval_RestoreThread(state.ptr)
65+
}
66+
67+
// Ensure that the current thread is ready to call the Python C API regardless
68+
// of the current state of Python, or of the global interpreter lock. This may
69+
// be called as many times as desired by a thread as long as each call is
70+
// matched with a call to PyGILState_Release(). In general, other thread-related
71+
// APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls
72+
// as long as the thread state is restored to its previous state before the
73+
// Release(). For example, normal usage of the Py_BEGIN_ALLOW_THREADS and
74+
// Py_END_ALLOW_THREADS macros is acceptable.
75+
//
76+
// The return value is an opaque “handle” to the thread state when
77+
// PyGILState_Ensure() was called, and must be passed to PyGILState_Release()
78+
// to ensure Python is left in the same state. Even though recursive calls are
79+
// allowed, these handles cannot be shared - each unique call to
80+
// PyGILState_Ensure() must save the handle for its call to PyGILState_Release().
81+
//
82+
// When the function returns, the current thread will hold the GIL and be able
83+
// to call arbitrary Python code. Failure is a fatal error.
84+
//
85+
// New in version 2.3.
86+
func PyGILState_Ensure() PyGILState {
87+
return PyGILState(C.PyGILState_Ensure())
88+
}
89+
90+
// void PyGILState_Release(PyGILState_STATE)
91+
// Release any resources previously acquired. After this call, Python’s state
92+
// will be the same as it was prior to the corresponding PyGILState_Ensure()
93+
// call (but generally this state will be unknown to the caller, hence the use
94+
// of the GILState API).
95+
//
96+
// Every call to PyGILState_Ensure() must be matched by a call to
97+
// PyGILState_Release() on the same thread.
98+
//
99+
// New in version 2.3.
100+
func PyGILState_Release(state PyGILState) {
101+
C.PyGILState_Release(C.PyGILState_STATE(state))
102+
}
103+
38104
// EOF

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/4cecoder/go-python/commit/894982222676379a77af235f9eadbadfc8591ff0

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy