Browse Source

locked queue and threads

JNI-44
Volker Birk 7 years ago
parent
commit
60b5eedca1
4 changed files with 146 additions and 4 deletions
  1. +2
    -2
      src/basic_api.cc
  2. +79
    -0
      src/jniutils.hh
  3. +5
    -0
      src/org/pEp/jniadapter/AbstractEngine.java
  4. +60
    -2
      src/org_pEp_jniadapter_AbstractEngine.cc

+ 2
- 2
src/basic_api.cc View File

@ -3,9 +3,9 @@
#include "throw_pEp_exception.hh"
#include "jniutils.hh"
using namespace pEp::JNIAdapter;
extern "C" {
using namespace pEp::JNIAdapter;
JNIEXPORT jobject JNICALL Java_org_pEp_jniadapter_Engine_trustwords(
JNIEnv *env,
jobject obj,


+ 79
- 0
src/jniutils.hh View File

@ -1,11 +1,90 @@
#pragma once
#include <list>
#include <pthread.h>
#include <jni.h>
#include <pEp/stringpair.h>
#include <pEp/identity_list.h>
#include <pEp/bloblist.h>
namespace pEp {
namespace utility {
using namespace std;
class mutex {
pthread_mutex_t _mutex;
public:
mutex() {
pthread_mutex_init(&_mutex, NULL);
}
~mutex() {
pthread_mutex_destroy(&_mutex);
}
void lock() {
pthread_mutex_lock(&_mutex);
}
void unlock() {
pthread_mutex_unlock(&_mutex);
}
};
template<class T> class lock_guard {
T& _mtx;
public:
lock_guard(T& mtx) : _mtx(mtx) {
_mtx.lock();
}
~lock_guard() {
_mtx.unlock();
}
};
template<class T> class locked_queue
{
mutex _mtx;
list<T> _q;
public:
T& back()
{
lock_guard<mutex> lg(_mtx);
return _q.back();
}
T& front()
{
lock_guard<mutex> lg(_mtx);
return _q.front();
}
void pop_back()
{
lock_guard<mutex> lg(_mtx);
_q.pop_back();
}
void pop_front()
{
lock_guard<mutex> lg(_mtx);
_q.pop_front();
}
void push_back(const T& data)
{
lock_guard<mutex> lg(_mtx);
_q.push_back(data);
}
void push_front(const T& data)
{
lock_guard<mutex> lg(_mtx);
_q.push_front(data);
}
size_t size()
{
lock_guard<mutex> lg(_mtx);
return _q.size();
}
};
}
namespace JNIAdapter {
jclass findClass(JNIEnv *env, const char *classname);


+ 5
- 0
src/org/pEp/jniadapter/AbstractEngine.java View File

@ -27,6 +27,11 @@ abstract class AbstractEngine implements AutoCloseable {
release();
}
private long keyserverThread;
public native void startKeyserverLookup();
public native void stopKeyserverLookup();
public static byte[] toUTF8(String str) {
try {
String _str = Normalizer.normalize(str, Normalizer.Form.NFC);


+ 60
- 2
src/org_pEp_jniadapter_AbstractEngine.cc View File

@ -2,7 +2,8 @@
#include <stdexcept>
#include <assert.h>
#include <pEp/pEpEngine.h>
#include <pthread.h>
#include <pEp/keymanagement.h>
#include "throw_pEp_exception.hh"
#include "jniutils.hh"
@ -62,5 +63,62 @@ extern "C" {
else
env->SetLongField(me, handle, jlong(0));
}
}
static void *start_routine(void *arg)
{
return NULL;
}
JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_startKeyserverLookup(
JNIEnv *env,
jobject obj
)
{
pthread_t *thread = NULL;
jfieldID handle;
try {
handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverThread", "J");
}
catch (std::exception& ex) {
assert(0);
return;
}
thread = (pthread_t *) env->GetLongField(obj, handle);
if (thread)
return;
thread = (pthread_t *) calloc(1, sizeof(pthread_t));
assert(thread);
env->SetLongField(obj, handle, (jlong) thread);
pthread_create(thread, NULL, start_routine, NULL);
}
JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_stopKeyserverLookup(
JNIEnv *env,
jobject obj
)
{
pthread_t *thread = NULL;
jfieldID handle;
try {
handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverThread", "J");
}
catch (std::exception& ex) {
assert(0);
return;
}
thread = (pthread_t *) env->GetLongField(obj, handle);
if (!thread)
return;
// stop thread
}
} // extern "C"

Loading…
Cancel
Save