Repository navigation
Expand file tree
/
Copy pathserver_jni.cc
More file actions
104 lines (76 loc) · 3.39 KB
/
Copy pathserver_jni.cc
File metadata and controls
104 lines (76 loc) · 3.39 KB
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
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/java/src/main/native/server_jni.h"
#include "tensorflow/c/c_api.h"
#include "tensorflow/java/src/main/native/exception_jni.h"
#include "tensorflow/java/src/main/native/utils_jni.h"
namespace {
TF_Server* requireHandle(JNIEnv* env, jlong handle) {
static_assert(sizeof(jlong) >= sizeof(TF_Server*),
"Cannot package C object pointers as a Java long");
if (handle == 0) {
throwException(env, kIllegalStateException,
"close() has been called on the Server");
return nullptr;
}
return reinterpret_cast<TF_Server*>(handle);
}
} // namespace
JNIEXPORT jlong JNICALL Java_org_tensorflow_Server_allocate(
JNIEnv* env, jclass clazz, jbyteArray server_def) {
TF_Status* status = TF_NewStatus();
jbyte* server_def_ptr = env->GetByteArrayElements(server_def, nullptr);
TF_Server* server = TF_NewServer(
server_def_ptr, static_cast<size_t>(env->GetArrayLength(server_def)),
status);
env->ReleaseByteArrayElements(server_def, server_def_ptr, JNI_ABORT);
bool ok = throwExceptionIfNotOK(env, status);
TF_DeleteStatus(status);
return ok ? reinterpret_cast<jlong>(server) : 0;
}
JNIEXPORT void JNICALL Java_org_tensorflow_Server_start(JNIEnv* env,
jclass clazz,
jlong handle) {
TF_Server* server = requireHandle(env, handle);
if (server == nullptr) return;
TF_Status* status = TF_NewStatus();
TF_ServerStart(server, status);
throwExceptionIfNotOK(env, status);
TF_DeleteStatus(status);
}
JNIEXPORT void JNICALL Java_org_tensorflow_Server_stop(JNIEnv* env,
jclass clazz,
jlong handle) {
TF_Server* server = requireHandle(env, handle);
if (server == nullptr) return;
TF_Status* status = TF_NewStatus();
TF_ServerStop(server, status);
throwExceptionIfNotOK(env, status);
TF_DeleteStatus(status);
}
JNIEXPORT void JNICALL Java_org_tensorflow_Server_join(JNIEnv* env,
jclass clazz,
jlong handle) {
TF_Server* server = requireHandle(env, handle);
if (server == nullptr) return;
TF_Status* status = TF_NewStatus();
TF_ServerJoin(server, status);
throwExceptionIfNotOK(env, status);
TF_DeleteStatus(status);
}
JNIEXPORT void JNICALL Java_org_tensorflow_Server_delete(JNIEnv* env,
jclass clazz,
jlong handle) {
TF_Server* server = requireHandle(env, handle);
if (server == nullptr) return;
TF_DeleteServer(server);
}