-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQoreThreadAttachHelper.h
More file actions
104 lines (83 loc) · 2.65 KB
/
Copy pathQoreThreadAttachHelper.h
File metadata and controls
104 lines (83 loc) · 2.65 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* indent-tabs-mode: nil -*- */
/*
qore Python module
Copyright (C) 2020 Qore Technologies, s.r.o.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_PYTHON_QORETHREADATTACHHELPER_H
#define _QORE_PYTHON_QORETHREADATTACHHELPER_H
#include "python-module.h"
constexpr int LogLevel = 10;
class QoreThreadAttacher {
public:
DLLLOCAL QoreThreadAttacher() : attached(false) {
}
DLLLOCAL ~QoreThreadAttacher() {
if (attached) {
detach();
}
}
// returns 0 = attached, -1 = already attached
DLLLOCAL int attach() {
if (!attached) {
attachIntern();
return 0;
}
return -1;
}
DLLLOCAL void detach() {
if (attached) {
detachIntern();
}
}
DLLLOCAL operator bool() const {
return attached;
}
private:
bool attached;
DLLLOCAL void attachIntern() {
assert(!attached);
int rc = q_register_foreign_thread();
if (rc == QFT_OK) {
attached = true;
printd(LogLevel, "Thread %ld attached to Qore\n", pthread_self());
} else if (rc != QFT_REGISTERED) {
printf("unable to register thread to qore; aborting\n");
exit(1);
}
}
DLLLOCAL void detachIntern() {
assert(attached);
printd(LogLevel, "Detaching thread %ld from Qore\n", pthread_self());
q_deregister_foreign_thread();
attached = false;
}
};
// class that serves to attach a thread to Qore if not already attached
// if attached in the constructor, then it will detach in the destructor
class QoreThreadAttachHelper {
public:
DLLLOCAL void attach() {
attached = !attacher.attach();
}
DLLLOCAL ~QoreThreadAttachHelper() {
if (attached) {
attacher.detach();
}
}
private:
QoreThreadAttacher attacher;
bool attached = false;
};
extern thread_local QoreThreadAttacher qoreThreadAttacher;
#endif