Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
Exit after vm shutdown
Browse files Browse the repository at this point in the history
On macos this was not exiting due to the vm running on a thread.

Also set exit status to be dependent upon if an exception was thrown out of the main thread, and print the exception.
  • Loading branch information
Adam- committed Dec 23, 2022
1 parent 5c465a7 commit ebf3296
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/main/native/packr/src/macos/packr_macos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void sourceCallBack(void* info) {
*/
static LaunchJavaVMDelegate s_delegate = NULL;
void* launchVM(void* param) {
return s_delegate(param);
s_delegate();
return nullptr;
}

int main(int argc, char** argv) {
Expand All @@ -61,7 +62,7 @@ int main(int argc, char** argv) {
cout << "Starting JVM on main thread (-XstartOnFirstThread found) ..." << endl;
}

delegate(nullptr);
delegate();
return;
}
}
Expand Down
28 changes: 18 additions & 10 deletions src/main/native/packr/src/packr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ void launchJavaVM(LaunchJavaVMCallback callback) {
Done as lambda to capture local variables, and remain in function scope.
*/

callback([&](void*) {
callback([&]() {

// create JVM

Expand Down Expand Up @@ -585,6 +585,21 @@ void launchJavaVM(LaunchJavaVMCallback callback) {

env->CallStaticVoidMethod(mainClass, mainMethod, appArgs);

// check if main thread threw an exception
jboolean exception = env->ExceptionCheck();
int status = EXIT_SUCCESS;
if (exception) {
env->ExceptionDescribe();
status = EXIT_FAILURE;
}

// blocks this thread until all non-daemon threads unload
jvm->DestroyJavaVM();

if (verbose) {
cout << "Destroyed Java VM ..." << endl;
}

// cleanup

for (size_t vmArg = 0; vmArg < vmArgc; vmArg++) {
Expand All @@ -599,14 +614,7 @@ void launchJavaVM(LaunchJavaVMCallback callback) {

delete[] cmdLineArgv;

// blocks this thread until the Java main() method exits

jvm->DestroyJavaVM();

if (verbose) {
cout << "Destroyed Java VM ..." << endl;
}

return nullptr;
// on macOS this is run in a thread, so use exit() to exit the process
exit(status);
}, args);
}
4 changes: 2 additions & 2 deletions src/main/native/packr/src/packr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
typedef jint(JNICALL *GetDefaultJavaVMInitArgs)(void*);
typedef jint(JNICALL *CreateJavaVM)(JavaVM**, void**, void*);

typedef std::function<void* (void*)> LaunchJavaVMDelegate;
typedef std::function<void ()> LaunchJavaVMDelegate;
typedef std::function<void (LaunchJavaVMDelegate delegate, const JavaVMInitArgs& args)> LaunchJavaVMCallback;

#define defaultLaunchVMDelegate \
[](LaunchJavaVMDelegate delegate, const JavaVMInitArgs&) { delegate(nullptr); }
[](LaunchJavaVMDelegate delegate, const JavaVMInitArgs&) { delegate(); }

extern "C" {

Expand Down

0 comments on commit ebf3296

Please sign in to comment.