about summary refs log tree commit diff stats
path: root/src/box64context.c
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-02-12 16:38:12 +0100
committerptitSeb <sebastien.chev@gmail.com>2023-02-12 16:38:12 +0100
commit96a7d1e7ec0fa1266304779389359804e6108795 (patch)
tree889bec6cc539ff6a6ea655b56d293c874480700e /src/box64context.c
parent2d2a65b616b1bb4250cfdc1d300f7bbc14685a3a (diff)
downloadbox64-96a7d1e7ec0fa1266304779389359804e6108795.tar.gz
box64-96a7d1e7ec0fa1266304779389359804e6108795.zip
[DYNAREC] Use custom mutex, improved Signal while FillBlocks64 and atomic handling
Diffstat (limited to 'src/box64context.c')
-rwxr-xr-xsrc/box64context.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/box64context.c b/src/box64context.c
index 467accfa..67f7f584 100755
--- a/src/box64context.c
+++ b/src/box64context.c
@@ -75,11 +75,20 @@ int unlockMutex()
 {
     int ret = unlockCustommemMutex();
     int i;
+    #ifdef DYNAREC
+    uint32_t tid = (uint32_t)GetTID();
     #define GO(A, B)                    \
+        i = (native_lock_storeifref2_d(&A, 0, tid)==tid); \
+        if(i) {                         \
+            ret|=(1<<B);                \
+        }
+    #else
+        #define GO(A, B)                \
         i = checkUnlockMutex(&A);       \
         if(i) {                         \
             ret|=(1<<B);                \
         }
+    #endif
 
     GO(my_context->mutex_trace, 7)
     #ifdef DYNAREC
@@ -100,7 +109,7 @@ void relockMutex(int locks)
     relockCustommemMutex(locks);
     #define GO(A, B)                    \
         if(locks&(1<<B))                \
-            pthread_mutex_trylock(&A);  \
+            mutex_trylock(&A);          \
 
     GO(my_context->mutex_trace, 7)
     #ifdef DYNAREC
@@ -116,20 +125,24 @@ void relockMutex(int locks)
 
 static void init_mutexes(box64context_t* context)
 {
+#ifndef DYNAREC
     pthread_mutexattr_t attr;
     pthread_mutexattr_init(&attr);
     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
-    pthread_mutex_init(&context->mutex_trace, &attr);
-#ifndef DYNAREC
     pthread_mutex_init(&context->mutex_lock, &attr);
-#else
-    pthread_mutex_init(&context->mutex_dyndump, &attr);
-#endif
+    pthread_mutex_init(&context->mutex_trace, &attr);
     pthread_mutex_init(&context->mutex_tls, &attr);
     pthread_mutex_init(&context->mutex_thread, &attr);
     pthread_mutex_init(&context->mutex_bridge, &attr);
 
     pthread_mutexattr_destroy(&attr);
+#else
+    native_lock_store(&context->mutex_trace, 0);
+    native_lock_store(&context->mutex_tls, 0);
+    native_lock_store(&context->mutex_thread, 0);
+    native_lock_store(&context->mutex_bridge, 0);
+    native_lock_store(&context->mutex_dyndump, 0);
+#endif
 }
 
 static void atfork_child_box64context(void)
@@ -310,15 +323,13 @@ void FreeBox64Context(box64context_t** context)
 
     finiAllHelpers(ctx);
 
-    pthread_mutex_destroy(&ctx->mutex_trace);
 #ifndef DYNAREC
+    pthread_mutex_destroy(&ctx->mutex_trace);
     pthread_mutex_destroy(&ctx->mutex_lock);
-#else
-    pthread_mutex_destroy(&ctx->mutex_dyndump);
-#endif
     pthread_mutex_destroy(&ctx->mutex_tls);
     pthread_mutex_destroy(&ctx->mutex_thread);
     pthread_mutex_destroy(&ctx->mutex_bridge);
+#endif
 
     freeCycleLog(ctx);