Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions pkgs/jni/src/dartjni.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,31 +171,17 @@ Java_com_github_dart_1lang_jni_JniPlugin_setJniActivity(JNIEnv* env,
// on Android NDK. So IFDEF is required.
#else
#ifdef _WIN32
// Pre-initialization of critical section on windows - this is required because
// there's no coordination between multiple isolates calling Spawn.
//
// Taken from https://stackoverflow.com/a/12858955
CRITICAL_SECTION spawnLock = {0};
BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpReserved) { // reserved
SRWLOCK spawnLock = SRWLOCK_INIT;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
InitializeCriticalSection(&spawnLock);
break;
case DLL_THREAD_DETACH:
if (jniEnv != NULL) {
detach_thread(jniEnv);
}
break;
case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
DeleteCriticalSection(&spawnLock);
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
return TRUE;
}
#else
pthread_mutex_t spawnLock = PTHREAD_MUTEX_INITIALIZER;
Expand Down Expand Up @@ -477,7 +463,8 @@ Java_com_github_dart_1lang_jni_PortProxyBuilder__1invoke(
if (mustEnterIsolate) {
Dart_EnterIsolate_DL((Dart_Isolate)isolateId);
}
result->object = ((jobject(*)(uint64_t, jobject, jobject))functionPtr)(
typedef jobject (*DartCallback)(uint64_t, jobject, jobject);
result->object = ((DartCallback)functionPtr)(
port, (*env)->NewGlobalRef(env, methodDescriptor),
(*env)->NewGlobalRef(env, args));
if (mustEnterIsolate) {
Expand Down
14 changes: 7 additions & 7 deletions pkgs/jni/src/dartjni.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@
#ifdef _WIN32
#include <windows.h>

typedef CRITICAL_SECTION MutexLock;
typedef SRWLOCK MutexLock;
typedef CONDITION_VARIABLE ConditionVariable;

static inline void init_lock(MutexLock* lock) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two files really need cleanup they are very messy and don't follow Google C++ Style Guide. Ignore this for now though.

(e.g. why is it static inline in the header? Should be just inline. The name should be InitLock. It should be in the namespace. dartjni.c should be dartjni.cc, etc).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #2701

InitializeCriticalSection(lock);
InitializeSRWLock(lock);
}

static inline void acquire_lock(MutexLock* lock) {
EnterCriticalSection(lock);
AcquireSRWLockExclusive(lock);
}

static inline void release_lock(MutexLock* lock) {
LeaveCriticalSection(lock);
ReleaseSRWLockExclusive(lock);
}

static inline void destroy_lock(MutexLock* lock) {
DeleteCriticalSection(lock);
// Not available on Windows.
}

static inline void init_cond(ConditionVariable* cond) {
Expand All @@ -75,11 +75,11 @@ static inline void signal_cond(ConditionVariable* cond) {
}

static inline void wait_for(ConditionVariable* cond, MutexLock* lock) {
SleepConditionVariableCS(cond, lock, INFINITE);
SleepConditionVariableSRW(cond, lock, INFINITE, 0);
}

static inline void destroy_cond(ConditionVariable* cond) {
// Not available.
// Not available on Windows.
}

static inline void free_mem(void* mem) {
Expand Down
Loading