blob: 14027a96f78b6ee32021ecd2a39e9c86dfbe9ceb [file] [log] [blame]
Marek Gradzki66ea26b2016-07-26 15:28:22 +02001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef __included_jvpp_common_h__
16#define __included_jvpp_common_h__
17//
18#include <vppinfra/types.h>
19#include <vlibapi/api.h>
20#include <vlibmemory/api.h>
21#include <jni.h>
22
23typedef struct {
24 /* Unique identifier used for matching replays with requests */
25 volatile u32 context_id;
26
27 /* Spinlock */
28 volatile u32 lock;
29 u32 tag;
30
31 /* JNI Native Method Interface pointer for message handlers */
32 JNIEnv *jenv;
33
34 /* JNI Invoke Interface pointer for attachment of rx thread to java thread */
35 JavaVM *jvm;
36
37 /* Convenience */
Florin Corase86a8ed2018-01-05 03:20:25 -080038 svm_queue_t * vl_input_queue;
Marek Gradzki66ea26b2016-07-26 15:28:22 +020039 u32 my_client_index;
Marek Gradzki4746a5d2017-01-27 08:57:40 +010040 uword *messages_hash;
Marek Gradzki66ea26b2016-07-26 15:28:22 +020041} jvpp_main_t;
42
43extern jvpp_main_t jvpp_main __attribute__((aligned (64)));
44
45static_always_inline u32 vppjni_get_context_id(jvpp_main_t * jm) {
46 return __sync_add_and_fetch(&jm->context_id, 1);
47}
48
49static_always_inline void vppjni_lock(jvpp_main_t * jm, u32 tag) {
50 while (__sync_lock_test_and_set(&jm->lock, 1))
51 ;
52 jm->tag = tag;
53}
54
55static_always_inline void vppjni_unlock(jvpp_main_t * jm) {
56 jm->tag = 0;
57 CLIB_MEMORY_BARRIER();
58 jm->lock = 0;
59}
60
61/**
62 * Calls onError callback on callbackObject reference. Passes instance of callbackExceptionClass as parameter.
63 */
64void call_on_error(const char* callName, int contextId, int retval,
65 jclass callbackClass, jobject callbackObject,
66 jclass callbackExceptionClass);
67
Marek Gradzki4746a5d2017-01-27 08:57:40 +010068/**
69 * Retrieves message id based on message name and crc (key format: name_crc).
70 * Throws java/lang/IllegalStateException on failure.
71 */
72u32 get_message_id(JNIEnv *env, const char* key);
73
Marek Gradzki66ea26b2016-07-26 15:28:22 +020074#endif /* __included_jvpp_common_h__ */