ReturnCode VirtualThreadSupport::setMountCallbacksEnabled()

in jvmti-access/src/main/jni/VirtualThreadSupport.cpp [160:212]


        ReturnCode VirtualThreadSupport::setMountCallbacksEnabled(JNIEnv* jniEnv, bool enabled) {
            if (eventsEnabled == enabled) {
                return ReturnCode::SUCCESS;
            }
            if (unsupportedReason != "") {
                return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, unsupportedReason);
            }
            
            if (enabled) {
                auto error = jvmti->SetExtensionEventCallback(mountEventIdx, (jvmtiExtensionEvent) &vtMountHandler);
                if (error != JVMTI_ERROR_NONE) {
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to set mount event handler, error code is  ", error);
                }
                error = jvmti->SetExtensionEventCallback(unmountEventIdx, (jvmtiExtensionEvent) &vtUnmountHandler);
                if (error != JVMTI_ERROR_NONE) {
                    jvmti->SetExtensionEventCallback(mountEventIdx, nullptr);
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to set unmount event handler, error code is  ", error);
                }
                error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, static_cast<jvmtiEvent>(mountEventIdx), nullptr);
                if (error != JVMTI_ERROR_NONE) {
                    jvmti->SetExtensionEventCallback(unmountEventIdx, nullptr);
                    jvmti->SetExtensionEventCallback(mountEventIdx, nullptr);
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to set mount event enabled, error code is  ", error);
                }
                error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, static_cast<jvmtiEvent>(unmountEventIdx), nullptr);
                if (error != JVMTI_ERROR_NONE) {
                    jvmti->SetEventNotificationMode(JVMTI_DISABLE, static_cast<jvmtiEvent>(mountEventIdx), nullptr);
                    jvmti->SetExtensionEventCallback(unmountEventIdx, nullptr);
                    jvmti->SetExtensionEventCallback(mountEventIdx, nullptr);
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to set unmount event enabled, error code is  ", error);
                }
                eventsEnabled = true;
            } else {
                auto err1 = jvmti->SetEventNotificationMode(JVMTI_DISABLE, static_cast<jvmtiEvent>(mountEventIdx), nullptr);
                auto err2 = jvmti->SetEventNotificationMode(JVMTI_DISABLE, static_cast<jvmtiEvent>(unmountEventIdx), nullptr);
                auto err3 = jvmti->SetExtensionEventCallback(mountEventIdx, nullptr);
                auto err4 = jvmti->SetExtensionEventCallback(unmountEventIdx, nullptr);
                eventsEnabled = false;
                if (err1 != JVMTI_ERROR_NONE) {
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to set mount event mode to disabled, error code is  ", err1);
                }
                if (err2 != JVMTI_ERROR_NONE) {
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to set unmount event mode to disabled, error code is  ", err2);
                }
                if (err3 != JVMTI_ERROR_NONE) {
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to unset mount event handler, error code is  ", err3);
                }
                if (err4 != JVMTI_ERROR_NONE) {
                    return raiseExceptionAndReturn(jniEnv, ReturnCode::ERROR, "Failed to to unset unmount event handler, error code is  ", err4);
                }
            }
            return ReturnCode::SUCCESS;
        }