-
Notifications
You must be signed in to change notification settings - Fork 22
/
PyThreading.hpp
80 lines (66 loc) · 1.79 KB
/
PyThreading.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef MP_PYTHREAD_HPP
#define MP_PYTHREAD_HPP
#include <Python.h>
#include <pythread.h>
#include <atomic>
#include <functional>
#include <string>
#include "NonCopyAble.hpp"
struct PyMutex {
PyThread_type_lock l;
bool enabled;
PyMutex(); ~PyMutex();
PyMutex(const PyMutex&) : PyMutex() {} // ignore
PyMutex& operator=(const PyMutex&) { return *this; } // ignore
void lock();
bool lock_nowait();
void unlock();
};
struct PyScopedLock : noncopyable {
PyMutex& mutex;
PyScopedLock(PyMutex& m);
~PyScopedLock();
};
struct PyScopedUnlock : noncopyable {
PyMutex& mutex;
PyScopedUnlock(PyMutex& m);
~PyScopedUnlock();
};
struct PyScopedGIL : noncopyable {
PyGILState_STATE gstate;
PyScopedGIL() { gstate = PyGILState_Ensure(); }
~PyScopedGIL() {
if(PyThreadState_Get()->gilstate_counter == 1) {
// This means that the thread-state is going to be deleted.
// This can happen when you created this thread state on a new
// thread which was not registered in Python before.
if(PyErr_Occurred())
// Print exception. It would get lost otherwise.
PyErr_Print();
}
PyGILState_Release(gstate);
}
};
struct PyScopedGIUnlock : noncopyable {
PyScopedGIL gstate; // in case we didn't had the GIL
PyThreadState* _save;
PyScopedGIUnlock() : _save(NULL) { Py_UNBLOCK_THREADS }
~PyScopedGIUnlock() { Py_BLOCK_THREADS }
};
struct PyThread : noncopyable {
PyMutex lock;
std::atomic<bool> running;
std::atomic<bool> stopSignal;
std::function<void(std::atomic<bool>& stopSignal)> func;
long ident;
PyThread(); ~PyThread();
bool start();
void wait();
void stop();
};
extern "C" {
// this is defined in <sys/mman.h>. systems which don't have that should provide a dummy/wrapper
int mlock(const void *addr, size_t len);
}
void setCurThreadName(const std::string& name);
#endif // PYTHREAD_HPP