1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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 #pragma once 18 19 #include <fbjni/NativeRunnable.h> 20 #include <fbjni/fbjni.h> 21 22 namespace facebook { 23 namespace jni { 24 25 class JThread : public JavaClass<JThread> { 26 public: 27 static constexpr const char* kJavaDescriptor = "Ljava/lang/Thread;"; 28 start()29 void start() { 30 static const auto method = javaClassStatic()->getMethod<void()>("start"); 31 method(self()); 32 } 33 join()34 void join() { 35 static const auto method = javaClassStatic()->getMethod<void()>("join"); 36 method(self()); 37 } 38 create(std::function<void ()> && runnable)39 static local_ref<JThread> create(std::function<void()>&& runnable) { 40 auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable)); 41 return newInstance(static_ref_cast<JRunnable>(jrunnable)); 42 } 43 create(std::function<void ()> && runnable,std::string && name)44 static local_ref<JThread> create( 45 std::function<void()>&& runnable, 46 std::string&& name) { 47 auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable)); 48 return newInstance( 49 static_ref_cast<JRunnable>(jrunnable), make_jstring(std::move(name))); 50 } 51 getCurrent()52 static local_ref<JThread> getCurrent() { 53 static const auto method = 54 javaClassStatic()->getStaticMethod<local_ref<JThread>()>( 55 "currentThread"); 56 return method(javaClassStatic()); 57 } 58 getPriority()59 int getPriority() { 60 static const auto method = getClass()->getMethod<jint()>("getPriority"); 61 return method(self()); 62 } 63 setPriority(int priority)64 void setPriority(int priority) { 65 static const auto method = getClass()->getMethod<void(int)>("setPriority"); 66 method(self(), priority); 67 } 68 setName(const std::string & name)69 void setName(const std::string& name) { 70 static const auto method = 71 getClass()->getMethod<void(alias_ref<JString>)>("setName"); 72 method(self(), make_jstring(std::move(name))); 73 } 74 }; 75 76 } // namespace jni 77 } // namespace facebook 78