-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDispatch.cpp
110 lines (88 loc) · 3.14 KB
/
Dispatch.cpp
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
105
106
107
108
//
// Dispatch.cpp
// Jako
//
// Created by John Zakharov on 06.08.16.
// Copyright © 2016 Outlaw Studio. All rights reserved.
//
#include "Dispatch.hpp"
#ifdef __ANDROID__
using namespace java::lang;
using namespace android::os;
Mitsoko::Dispatch::Callbacks Mitsoko::Dispatch::callbacks;
std::mutex Mitsoko::Dispatch::callbacksMutex;
std::mutex Mitsoko::Dispatch::mainThreadHandlersMutex;
int Mitsoko::Dispatch::callbackId=0;
Handler Mitsoko::Dispatch::mainThreadHandler;
void Mitsoko::Dispatch::postCallback(int callbackId,bool isMainThread){
(void)isMainThread;
std::function<void()> f;
callbacksMutex.lock();
auto it=callbacks.find(callbackId);
if(it!=callbacks.end()){
f=it->second;
callbacks.erase(it);
}
callbacksMutex.unlock();
if(f){
f();
}else{
std::cerr<<"callback not found with id "<<callbackId<<std::endl;
}
}
void Mitsoko::Dispatch::initMainThreadHandler(){
if(auto handler=Handler::create()){
auto globalHandler=java::lang::JNI::Env()->NewGlobalRef((jobject)handler.handle);
mainThreadHandler=globalHandler;
}else{
std::cerr<<"handler is null"<<std::endl;
}
}
#endif
void Mitsoko::Dispatch::onBackground(std::function<void()> functionPointer){
#ifdef __APPLE__
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
functionPointer();
});
#else
if(auto java_env=java::lang::JNI::Env()){
callbacksMutex.lock();
const auto cId=callbackId++;
callbacks.insert({cId,functionPointer});
callbacksMutex.unlock();
auto backgroundRunnableClass=java_env->FindClass("kz/outlawstudio/viper/BackgroundRunnable");
auto ctor=java_env->GetMethodID(backgroundRunnableClass,"<init>",Object::generateMethodSignature<void,int>().c_str());
java::lang::Runnable backgroundRunnable = java_env->NewObject(backgroundRunnableClass, ctor,jint(cId));
auto thread=Thread::create(backgroundRunnable);
thread.start();
}else{
std::cerr<<"java::lang::JNI::Env is null"<<std::endl;
}
#endif
}
void Mitsoko::Dispatch::onMainThread(std::function<void()> functionPointer){
#ifdef __APPLE__
dispatch_async(dispatch_get_main_queue(), ^{
functionPointer();
});
#else
if(auto java_env=java::lang::JNI::Env()){
callbacksMutex.lock();
const auto cId=callbackId++;
callbacks.insert({cId,functionPointer});
callbacksMutex.unlock();
auto backgroundRunnableClass=java_env->FindClass("kz/outlawstudio/viper/BackgroundRunnable");
auto ctor=java_env->GetMethodID(backgroundRunnableClass,"<init>",Object::generateMethodSignature<void,int>().c_str());
Runnable backgroundRunnable = java_env->NewObject(backgroundRunnableClass, ctor,jint(cId));
mainThreadHandler.post(backgroundRunnable);
}else{
std::cout<<"java::lang::JNI::Env is null"<<std::endl;
}
#endif
}
void Mitsoko::operator--(std::function<void()> f){
Mitsoko::Dispatch::onMainThread(f);
}
void Mitsoko::operator++(std::function<void()> f){
Mitsoko::Dispatch::onBackground(f);
}