-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathOMR_VMThread.cpp
192 lines (169 loc) · 5.18 KB
/
OMR_VMThread.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
* Copyright IBM Corp. and others 2013
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include <string.h>
#include "OMR_Runtime.hpp"
#include "OMR_VM.hpp"
#include "OMR_VMThread.hpp"
#include "omrutil.h"
#include "ut_omrvm.h"
extern "C" {
OMR_VMThread *
omr_vmthread_getCurrent(OMR_VM *vm)
{
OMR_VMThread *currentThread = NULL;
omrthread_t self = omrthread_self();
if (NULL != self) {
if (vm->_vmThreadKey > 0) {
currentThread = (OMR_VMThread *)omrthread_tls_get(self, vm->_vmThreadKey);
}
}
return currentThread;
}
omr_error_t
omr_vmthread_alloc(OMR_VM *vm, OMR_VMThread **vmthread)
{
omr_error_t rc = OMR_ERROR_NONE;
OMRPORT_ACCESS_FROM_OMRVM(vm);
*vmthread = (OMR_VMThread *)omrmem_allocate_memory(sizeof(OMR_VMThread), OMRMEM_CATEGORY_VM);
if (NULL == *vmthread) {
rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
return rc;
}
omr_error_t
omr_vmthread_init(OMR_VMThread *vmthread)
{
omr_error_t rc = OMR_ERROR_NONE;
omrthread_monitor_init(&vmthread->threadNameMutex, 0);
if (NULL == vmthread->threadNameMutex) {
rc = OMR_ERROR_FAILED_TO_ALLOCATE_MONITOR;
}
return rc;
}
void
omr_vmthread_destroy(OMR_VMThread *vmthread)
{
if (NULL != vmthread->threadNameMutex) {
omrthread_monitor_destroy(vmthread->threadNameMutex);
vmthread->threadNameMutex = NULL;
}
}
omr_error_t
omr_attach_vmthread_to_vm(OMR_VMThread *vmthread)
{
omr_error_t rc = OMR_ERROR_NONE;
omrthread_t self = NULL;
/* NOTE _attachCount must never be accessed concurrently! */
if (vmthread->_attachCount > 0) {
vmthread->_attachCount += 1;
} else {
if (0 == omrthread_attach_ex(&self, J9THREAD_ATTR_DEFAULT)) {
rc = attachThread(vmthread->_vm, vmthread);
omrthread_detach(self);
vmthread->_attachCount += 1;
} else {
rc = OMR_ERROR_FAILED_TO_ATTACH_NATIVE_THREAD;
}
}
return rc;
}
omr_error_t
omr_detach_vmthread_from_vm(OMR_VMThread *vmthread)
{
omr_error_t rc = OMR_ERROR_NONE;
/* NOTE _attachCount must never be accessed concurrently! */
if (vmthread->_attachCount > 1) {
vmthread->_attachCount -= 1;
} else if (1 == vmthread->_attachCount) {
omrthread_t self = NULL;
if (0 == omrthread_attach_ex(&self, J9THREAD_ATTR_DEFAULT)) {
rc = detachThread(vmthread->_vm, vmthread);
omrthread_detach(self);
vmthread->_attachCount -= 1;
} else {
rc = OMR_ERROR_FAILED_TO_ATTACH_NATIVE_THREAD;
}
} else {
rc = OMR_THREAD_NOT_ATTACHED;
}
return rc;
}
/* vmThread must be the current thread, because of omrthread_self() usage */
omr_error_t
omr_vmthread_firstAttach(OMR_VM *vm, OMR_VMThread **vmThread)
{
omr_error_t rc = OMR_ERROR_NONE;
OMRPORT_ACCESS_FROM_OMRVM(vm);
OMR_VMThread *tempThread = (OMR_VMThread *)omrmem_allocate_memory(sizeof(OMR_VMThread), OMRMEM_CATEGORY_VM);
if (NULL != tempThread) {
memset(tempThread, 0, sizeof(*tempThread));
rc = omr_vmthread_init(tempThread);
if (OMR_ERROR_NONE == rc) {
/* OMRTODO this should go in omr_vmthread_init() */
tempThread->_vm = vm;
tempThread->_language_vmthread = NULL;
tempThread->_os_thread = omrthread_self();
tempThread->_internal = 1; /* unused by JVM */
rc = attachThread(vm, tempThread);
if (OMR_ERROR_NONE == rc) {
tempThread->_attachCount = 1;
*vmThread = tempThread;
}
}
/* If an error occurred, free the newly allocated thread */
if (OMR_ERROR_NONE != rc) {
omrmem_free_memory(tempThread);
}
} else {
rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
return rc;
}
omr_error_t
omr_vmthread_lastDetach(OMR_VMThread *vmThread)
{
omr_error_t rc = OMR_ERROR_NONE;
OMR_VM *omrVM = vmThread->_vm;
OMRPORT_ACCESS_FROM_OMRVM(omrVM);
rc = detachThread(omrVM, vmThread);
if (OMR_ERROR_NONE == rc) {
omr_vmthread_destroy(vmThread);
omrmem_free_memory(vmThread);
}
return rc;
}
void
omr_vmthread_reattach(OMR_VMThread *currentThread, const char *threadName)
{
Assert_OMRVM_true(0 < currentThread->_attachCount);
currentThread->_attachCount += 1;
if (NULL != threadName) {
setOMRVMThreadNameWithFlag(currentThread, currentThread, (char *)threadName, TRUE);
}
}
void
omr_vmthread_redetach(OMR_VMThread *currentThread)
{
Assert_OMRVM_true(0 < currentThread->_attachCount);
currentThread->_attachCount -= 1;
}
}