|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef JNI_WEBRTC_NATIVE_HANDLE_H_ |
| 18 | +#define JNI_WEBRTC_NATIVE_HANDLE_H_ |
| 19 | + |
| 20 | +#include <jni.h> |
| 21 | + |
| 22 | +#include <functional> |
| 23 | + |
| 24 | +namespace jni |
| 25 | +{ |
| 26 | + // The life of the native object behind NativeObject.nativeHandle, shared |
| 27 | + // by every thread that calls into it. |
| 28 | + // |
| 29 | + // A native method uses the object through a NativeLease, which marks the |
| 30 | + // handle in use for as long as the call runs. dispose() and close() take |
| 31 | + // the handle with Take(), which unbinds it in one step: of several threads |
| 32 | + // disposing the same object, one gets the handle, the others nothing, and |
| 33 | + // no call starts using it afterwards. The object's owning reference is then |
| 34 | + // given up with Release(): at once if no call uses the handle, otherwise by |
| 35 | + // the last of those calls when it returns. Nothing waits for another |
| 36 | + // thread, so a dispose from within a callback cannot deadlock with a call |
| 37 | + // that waits for that callback's thread. |
| 38 | + namespace NativeHandle |
| 39 | + { |
| 40 | + // Returns the bound handle of a NativeObject and marks it in use, or 0 |
| 41 | + // when the object is null or no longer bound. A non-zero handle must be |
| 42 | + // given back with Unuse(). |
| 43 | + jlong Use(JNIEnv * env, jobject object); |
| 44 | + |
| 45 | + // Ends a use begun with Use(), and runs the releases that waited for it |
| 46 | + // if it was the last. |
| 47 | + void Unuse(JNIEnv * env, jlong handle) noexcept; |
| 48 | + |
| 49 | + // Unbinds the handle of a NativeObject and returns it. Returns 0 when |
| 50 | + // the object is null or another thread has taken the handle before. |
| 51 | + jlong Take(JNIEnv * env, jobject object); |
| 52 | + |
| 53 | + // Gives up what a taken handle owns. The release runs right away, with |
| 54 | + // deferred false, unless a call still uses the handle; then it runs on |
| 55 | + // the thread of the last such call, when that call returns, with |
| 56 | + // deferred true. A deferred release must not use the JNIEnv or the Java |
| 57 | + // object of the call that took the handle. |
| 58 | + void Release(JNIEnv * env, jlong handle, std::function<void(bool deferred)> release); |
| 59 | + |
| 60 | + // Throws the IllegalStateException of a disposed object, unless an |
| 61 | + // exception is pending already. |
| 62 | + void ThrowDisposed(JNIEnv * env, jobject object) noexcept; |
| 63 | + |
| 64 | + // Reports that a release left references to the native object behind: |
| 65 | + // as a java.lang.Error when it ran right away, as a warning in the log |
| 66 | + // when it was deferred, since the call it ran in is not the one to fail. |
| 67 | + void ReportNotDeleted(bool deferred) noexcept; |
| 68 | + } |
| 69 | + |
| 70 | + // The native object of a NativeObject, kept from being released while the |
| 71 | + // lease lives. Converts to T *, which is null when the object has been |
| 72 | + // disposed: check it with CHECK_HANDLE, which then throws the object's |
| 73 | + // IllegalStateException. |
| 74 | + template <typename T> |
| 75 | + class NativeLease |
| 76 | + { |
| 77 | + public: |
| 78 | + NativeLease(JNIEnv * env, jobject object) : |
| 79 | + env(env), |
| 80 | + object(object), |
| 81 | + handle(NativeHandle::Use(env, object)) |
| 82 | + { |
| 83 | + } |
| 84 | + |
| 85 | + ~NativeLease() |
| 86 | + { |
| 87 | + if (handle != 0) { |
| 88 | + NativeHandle::Unuse(env, handle); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + NativeLease(const NativeLease &) = delete; |
| 93 | + NativeLease & operator=(const NativeLease &) = delete; |
| 94 | + |
| 95 | + T * get() const |
| 96 | + { |
| 97 | + return reinterpret_cast<T *>(handle); |
| 98 | + } |
| 99 | + |
| 100 | + operator T * () const |
| 101 | + { |
| 102 | + return get(); |
| 103 | + } |
| 104 | + |
| 105 | + T * operator->() const |
| 106 | + { |
| 107 | + return get(); |
| 108 | + } |
| 109 | + |
| 110 | + void throwDisposed() const |
| 111 | + { |
| 112 | + NativeHandle::ThrowDisposed(env, object); |
| 113 | + } |
| 114 | + |
| 115 | + private: |
| 116 | + JNIEnv * env; |
| 117 | + jobject object; |
| 118 | + jlong handle; |
| 119 | + }; |
| 120 | + |
| 121 | + // Found by argument-dependent lookup from CHECK_HANDLE. |
| 122 | + template <typename T> |
| 123 | + void ThrowNullHandle(const NativeLease<T> & lease) |
| 124 | + { |
| 125 | + lease.throwDisposed(); |
| 126 | + } |
| 127 | + |
| 128 | + // NativeHandle::Take() for the native type the handle stands for. Null |
| 129 | + // when the object has been disposed already. |
| 130 | + template <typename T> |
| 131 | + T * TakeHandle(JNIEnv * env, jobject object) |
| 132 | + { |
| 133 | + return reinterpret_cast<T *>(NativeHandle::Take(env, object)); |
| 134 | + } |
| 135 | + |
| 136 | + // NativeHandle::Release() for a handle taken with TakeHandle(). |
| 137 | + template <typename T> |
| 138 | + void ReleaseHandle(JNIEnv * env, T * object, std::function<void(bool deferred)> release) |
| 139 | + { |
| 140 | + NativeHandle::Release(env, reinterpret_cast<jlong>(object), std::move(release)); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +#endif |
0 commit comments