-
This commit is contained in:
@@ -24,7 +24,7 @@ static result_t do_dispatch(rkt_webview_t *, item_t item);
|
||||
static DWORD webviewThread(LPVOID args)
|
||||
#endif
|
||||
#ifdef USE_PTHREADS
|
||||
static int webviewThread(void *args)
|
||||
static void *webviewThread(void *args)
|
||||
#endif
|
||||
{
|
||||
rkt_webview_t *wv = (rkt_webview_t *) args;
|
||||
@@ -41,14 +41,19 @@ static int webviewThread(void *args)
|
||||
wv->webview_handle = NULL;
|
||||
wv->handle_destroyed = true;
|
||||
mutex_unlock(wv);
|
||||
#ifdef USE_WIN_THREADS
|
||||
return 0;
|
||||
#endif
|
||||
#ifdef USE_PTHREADS
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_WIN_THREADS
|
||||
static DWORD queueGuardThread(LPVOID args)
|
||||
#endif
|
||||
#ifdef USE_PTHREADS
|
||||
static int queueGuardThread(void *args)
|
||||
static void *queueGuardThread(void *args)
|
||||
#endif
|
||||
{
|
||||
rkt_webview_t *wv = (rkt_webview_t *) args;
|
||||
@@ -73,7 +78,12 @@ static DWORD queueGuardThread(LPVOID args)
|
||||
thread_sleep_ms(wv, 10);
|
||||
}
|
||||
|
||||
#ifdef USE_WIN_THREADS
|
||||
return 0;
|
||||
#endif
|
||||
#ifdef USE_PTHREADS
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -82,9 +92,12 @@ RKTWEBVIEW_EXPORT rkt_webview_t *rkt_create_webview()
|
||||
rkt_webview_t *wv = (rkt_webview_t *) malloc(sizeof(rkt_webview_t));
|
||||
if (wv == NULL) { return NULL; }
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef USE_WIN_THREADS
|
||||
wv->mutex = CreateMutex(NULL, FALSE, NULL);
|
||||
#endif
|
||||
#ifdef USE_PTHREADS
|
||||
wv->mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
wv->handle_set = false;
|
||||
wv->handle_destroyed = false;
|
||||
queue_init(&wv->to_webview);
|
||||
@@ -112,7 +125,9 @@ RKTWEBVIEW_EXPORT rkt_webview_t *rkt_create_webview()
|
||||
&wv->queue_guard_thread_id
|
||||
);
|
||||
#endif
|
||||
#ifdef USE_PTHREAD
|
||||
#ifdef USE_PTHREADS
|
||||
wv->webview_thread_id = pthread_create(&wv->webview_thread, NULL, webviewThread, wv);
|
||||
wv->queue_guard_thread_id = pthread_create(&wv->queue_guard_thread, NULL, queueGuardThread, wv);
|
||||
#endif
|
||||
bool go_on = true;
|
||||
while(go_on) {
|
||||
@@ -123,6 +138,7 @@ RKTWEBVIEW_EXPORT rkt_webview_t *rkt_create_webview()
|
||||
thread_sleep_ms(wv, 10);
|
||||
}
|
||||
}
|
||||
printf("handle_set = %d\n", wv->handle_set);
|
||||
return wv;
|
||||
}
|
||||
|
||||
@@ -152,6 +168,7 @@ RKTWEBVIEW_EXPORT item_t rkt_webview_call_js(rkt_webview_t *wv, const char *js)
|
||||
mutex_unlock(wv);
|
||||
char buf[30];
|
||||
sprintf(buf, "%d", call_nr);
|
||||
|
||||
std::string _js = std::string("{ let f = function() { ") + js + " };" +
|
||||
" let call_nr = " + buf + ";" +
|
||||
" try { let r = { result: f() };" +
|
||||
@@ -159,7 +176,7 @@ RKTWEBVIEW_EXPORT item_t rkt_webview_call_js(rkt_webview_t *wv, const char *js)
|
||||
" } catch(e) {" +
|
||||
" rkt_webview_call_js_result(call_nr, false, e.message); " +
|
||||
" }" +
|
||||
"}"; "";
|
||||
"}";
|
||||
|
||||
|
||||
item_t item = { CONTEXT_CALL_JS, strdup(_js.c_str()) };
|
||||
@@ -232,7 +249,9 @@ RKTWEBVIEW_EXPORT result_t rkt_destroy_webview(rkt_webview_t *wv)
|
||||
CloseHandle(wv->queue_guard_thread);
|
||||
CloseHandle(wv->mutex);
|
||||
#endif
|
||||
#ifdef USE_PTHREAD
|
||||
#ifdef USE_PTHREADS
|
||||
pthread_join(wv->webview_thread, NULL);
|
||||
pthread_join(wv->queue_guard_thread, NULL);
|
||||
#endif
|
||||
delete wv->js_evaluated;
|
||||
free(wv);
|
||||
@@ -405,11 +424,24 @@ result_t do_dispatch(rkt_webview_t *wv, item_t item)
|
||||
}
|
||||
return r;
|
||||
} else {
|
||||
lr = reason_set_html_failed;
|
||||
switch(e) {
|
||||
case WEBVIEW_ERROR_MISSING_DEPENDENCY: lr = reason_webview_missing_dependency;
|
||||
break;
|
||||
case WEBVIEW_ERROR_CANCELED: lr = reason_webview_canceled;
|
||||
break;
|
||||
case WEBVIEW_ERROR_INVALID_STATE: lr = reason_webview_invalid_state;
|
||||
break;
|
||||
case WEBVIEW_ERROR_INVALID_ARGUMENT: lr = reason_webview_invalid_argument;
|
||||
break;
|
||||
case WEBVIEW_ERROR_UNSPECIFIED: lr = reason_webview_unspecified;
|
||||
break;
|
||||
default:
|
||||
lr = reason_webview_dispatch_failed;
|
||||
}
|
||||
r = error;
|
||||
}
|
||||
|
||||
printf("error = %d, reason = %d", r, lr);
|
||||
printf("error = %d, reason = %d\n", r, lr);
|
||||
|
||||
return r;
|
||||
}
|
||||
@@ -490,7 +522,8 @@ void mutex_lock(rkt_webview_t *wv)
|
||||
#ifdef USE_WIN_THREADS
|
||||
WaitForSingleObject(wv->mutex, INFINITE);
|
||||
#endif
|
||||
#ifdef USE_PTHREAD
|
||||
#ifdef USE_PTHREADS
|
||||
pthread_mutex_lock(&wv->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -500,7 +533,8 @@ void mutex_unlock(rkt_webview_t *wv)
|
||||
#ifdef USE_WIN_THREADS
|
||||
ReleaseMutex(wv->mutex);
|
||||
#endif
|
||||
#ifdef USE_PTHREAD
|
||||
#ifdef USE_PTHREADS
|
||||
pthread_mutex_unlock(&wv->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -509,6 +543,7 @@ static void thread_sleep_ms(rkt_webview_t *wv, int ms)
|
||||
#ifdef USE_WIN_THREADS
|
||||
Sleep(ms);
|
||||
#endif
|
||||
#ifdef USE_PTHREAD
|
||||
#ifdef USE_PTHREADS
|
||||
usleep(ms * 1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user