فهرست منبع

fix ASCII issue for threading module

lyon 1 سال پیش
والد
کامیت
cfc3ddc8ad

+ 65 - 79
port/linux/package/pikascript/pikascript-lib/threading/PikaPlatformEx.c

@@ -1,47 +1,45 @@
 #include "PikaPlatformEx.h"
 
 //----------------------------- mutex -------------------------------
-// 带超时的互斥锁加锁
+// Mutex lock with timeout
 int pika_platform_thread_mutex_timedlock(pika_platform_thread_mutex_t* m,
                                          pika_bool block,
                                          Arg* timeout) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
-    ArgType timout_type = arg_getType(timeout);
+    ArgType timeout_type = arg_getType(timeout);
     pika_float timeout_f;
     int result;
-    if (!(timout_type == ARG_TYPE_FLOAT || timout_type == ARG_TYPE_INT ||
-          timout_type == ARG_TYPE_NONE)) {
+    if (!(timeout_type == ARG_TYPE_FLOAT || timeout_type == ARG_TYPE_INT ||
+          timeout_type == ARG_TYPE_NONE)) {
         return PIKA_RES_ERR_INVALID_PARAM;
     }
 
-    if (timout_type == ARG_TYPE_FLOAT || timout_type == ARG_TYPE_INT) {
-        // printf("==== #01\n");
-        if (timout_type == ARG_TYPE_FLOAT) {
+    if (timeout_type == ARG_TYPE_FLOAT || timeout_type == ARG_TYPE_INT) {
+        if (timeout_type == ARG_TYPE_FLOAT) {
             timeout_f = arg_getFloat(timeout);
         }
-        if (timout_type == ARG_TYPE_INT) {
+        if (timeout_type == ARG_TYPE_INT) {
             int timeout_d = arg_getInt(timeout);
             timeout_f = (pika_float)timeout_d;
-            // printf("==== #04  %lf\n", timeout_f);
         }
         if (timeout_f < 0.0f) {
             return PIKA_RES_ERR_INVALID_PARAM;
         }
 
         struct timespec ts;
-        clock_gettime(CLOCK_REALTIME, &ts);  // 获取当前时间
+        clock_gettime(CLOCK_REALTIME, &ts);  // Get current time
 
-        // 将浮点数秒转换为秒和纳秒
+        // Convert floating seconds to seconds and nanoseconds
         long sec = (long)timeout_f;
         long nsec = (long)((timeout_f - (pika_float)sec) * 1000000000.0);
 
         ts.tv_sec += sec;
         ts.tv_nsec += nsec;
 
-        // 如果纳秒数超过 1 秒,则需要调整秒数和纳秒数
+        // Adjust seconds and nanoseconds if nanoseconds exceed 1 second
         if (ts.tv_nsec >= 1000000000) {
-            ts.tv_nsec -= 1000000000;  // 减去 1 秒的纳秒数
-            ts.tv_sec += 1;            // 增加 1 秒
+            ts.tv_nsec -= 1000000000;  // Subtract 1 second in nanoseconds
+            ts.tv_sec += 1;            // Add 1 second
         }
 
         pika_GIL_EXIT();
@@ -49,15 +47,13 @@ int pika_platform_thread_mutex_timedlock(pika_platform_thread_mutex_t* m,
         pika_GIL_ENTER();
         return result == 0 ? 0 : -1;
 
-    } else if (timout_type == ARG_TYPE_NONE) {
+    } else if (timeout_type == ARG_TYPE_NONE) {
         if (block) {
-            // printf("==== #02\n");
             pika_GIL_EXIT();
             result = pthread_mutex_lock(&m->mutex);
             pika_GIL_ENTER();
             return result == 0 ? 0 : -1;
         } else {
-            // printf("==== #03\n");
             pika_GIL_EXIT();
             result = pthread_mutex_trylock(&m->mutex);
             pika_GIL_ENTER();
@@ -86,7 +82,7 @@ int pika_platform_thread_mutex_timedlock(pika_platform_thread_mutex_t* m,
 }
 
 //----------------------------- rtmutex -------------------------------
-// 初始化递归互斥锁
+// Initialize recursive mutex
 void pika_platform_thread_rtmutex_init(pika_platform_thread_rtmutex_t* rtm) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     pthread_mutexattr_t attr;
@@ -94,7 +90,7 @@ void pika_platform_thread_rtmutex_init(pika_platform_thread_rtmutex_t* rtm) {
         perror("pthread_mutexattr_init");
         exit(EXIT_FAILURE);
     }
-    // 设置互斥锁类型为递归互斥锁
+    // Set the mutex type to recursive
     if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) {
         perror("pthread_mutexattr_settype");
         pthread_mutexattr_destroy(&attr);
@@ -114,7 +110,7 @@ void pika_platform_thread_rtmutex_init(pika_platform_thread_rtmutex_t* rtm) {
 #endif
 }
 
-// 销毁递归互斥锁
+// Destroy recursive mutex
 void pika_platform_thread_rtmutex_destroy(pika_platform_thread_rtmutex_t* rtm) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     pthread_cond_destroy(&rtm->cond);
@@ -127,17 +123,17 @@ void pika_platform_thread_rtmutex_destroy(pika_platform_thread_rtmutex_t* rtm) {
 #endif
 }
 
-// 带超时的递归互斥锁加锁
+// Lock recursive mutex with timeout
 int pika_platform_thread_rtmutex_lock(pika_platform_thread_rtmutex_t* rtm,
                                       pika_bool block,
                                       Arg* timeout) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
-    ArgType timout_type = arg_getType(timeout);
+    ArgType timeout_type = arg_getType(timeout);
     pika_float timeout_f;
     int result;
 
-    if (!(timout_type == ARG_TYPE_FLOAT || timout_type == ARG_TYPE_INT ||
-          timout_type == ARG_TYPE_NONE)) {
+    if (!(timeout_type == ARG_TYPE_FLOAT || timeout_type == ARG_TYPE_INT ||
+          timeout_type == ARG_TYPE_NONE)) {
         return PIKA_RES_ERR_INVALID_PARAM;
     }
 
@@ -146,45 +142,42 @@ int pika_platform_thread_rtmutex_lock(pika_platform_thread_rtmutex_t* rtm,
     pika_GIL_ENTER();
 
     if (rtm->owner == pthread_self()) {
-        // 如果当前线程已经持有锁,则递归深度加1
+        // If the current thread already owns the lock, increment recursion
+        // depth
         rtm->count++;
-        // printf("rtm->count = %d\n", rtm->count);
         pthread_mutex_unlock(&rtm->mutex);
-        // printf("succ\n");
         return 0;
     }
 
-    if (timout_type == ARG_TYPE_FLOAT || timout_type == ARG_TYPE_INT) {
-        // printf("==== #01\n");
-        if (timout_type == ARG_TYPE_FLOAT) {
+    if (timeout_type == ARG_TYPE_FLOAT || timeout_type == ARG_TYPE_INT) {
+        if (timeout_type == ARG_TYPE_FLOAT) {
             timeout_f = arg_getFloat(timeout);
         }
-        if (timout_type == ARG_TYPE_INT) {
+        if (timeout_type == ARG_TYPE_INT) {
             int timeout_d = arg_getInt(timeout);
             timeout_f = (pika_float)timeout_d;
-            // printf("==== #04  %lf\n", timeout_f);
         }
         if (timeout_f < 0.0f) {
             return PIKA_RES_ERR_INVALID_PARAM;
         }
 
         struct timespec ts;
-        clock_gettime(CLOCK_REALTIME, &ts);  // 获取当前时间
+        clock_gettime(CLOCK_REALTIME, &ts);  // Get current time
 
-        // 将浮点数秒转换为秒和纳秒
+        // Convert floating seconds to seconds and nanoseconds
         long sec = (long)timeout_f;
         long nsec = (long)((timeout_f - (pika_float)sec) * 1000000000.0);
 
         ts.tv_sec += sec;
         ts.tv_nsec += nsec;
 
-        // 如果纳秒数超过 1 秒,则需要调整秒数和纳秒数
+        // Adjust seconds and nanoseconds if nanoseconds exceed 1 second
         if (ts.tv_nsec >= 1000000000) {
-            ts.tv_nsec -= 1000000000;  // 减去 1 秒的纳秒数
-            ts.tv_sec += 1;            // 增加 1 秒
+            ts.tv_nsec -= 1000000000;  // Subtract 1 second in nanoseconds
+            ts.tv_sec += 1;            // Add 1 second
         }
 
-        // 等待直到获得锁或超时
+        // Wait until the lock is acquired or timeout occurs
         while (rtm->owner != (pthread_t)0) {
             pika_GIL_EXIT();
             result = pthread_cond_timedwait(&rtm->cond, &rtm->mutex, &ts);
@@ -195,15 +188,15 @@ int pika_platform_thread_rtmutex_lock(pika_platform_thread_rtmutex_t* rtm,
                 return -1;
             }
         }
-        // 设置当前线程为锁的持有者
+        // Set the current thread as the owner of the lock
         rtm->owner = pthread_self();
         rtm->count = 1;
         pthread_mutex_unlock(&rtm->mutex);
         return 0;
 
-    } else if (timout_type == ARG_TYPE_NONE) {
+    } else if (timeout_type == ARG_TYPE_NONE) {
         if (block) {
-            // 永久等待
+            // Wait indefinitely
             while (rtm->owner != (pthread_t)0) {
                 pika_GIL_EXIT();
                 result = pthread_cond_wait(&rtm->cond, &rtm->mutex);
@@ -214,22 +207,22 @@ int pika_platform_thread_rtmutex_lock(pika_platform_thread_rtmutex_t* rtm,
                     return -1;
                 }
             }
-            // 设置当前线程为锁的持有者
+            // Set the current thread as the owner of the lock
             rtm->owner = pthread_self();
             rtm->count = 1;
             pthread_mutex_unlock(&rtm->mutex);
             return 0;
 
         } else {
-            // 非阻塞模式
+            // Non-blocking mode
             if (rtm->owner == (pthread_t)0) {
-                // 如果没有其他线程持有锁,获取锁
+                // Acquire the lock if no other thread owns it
                 rtm->owner = pthread_self();
                 rtm->count = 1;
                 pthread_mutex_unlock(&rtm->mutex);
                 return 0;
             } else {
-                // 如果已经有其他线程持有锁,立即返回 -1
+                // If another thread owns the lock, return -1 immediately
                 pthread_mutex_unlock(&rtm->mutex);
                 return -1;
             }
@@ -246,8 +239,7 @@ int pika_platform_thread_rtmutex_lock(pika_platform_thread_rtmutex_t* rtm,
 #endif
 }
 
-#if 1
-// 释放递归互斥锁
+// Release recursive mutex
 int pika_platform_thread_rtmutex_unlock(pika_platform_thread_rtmutex_t* rtm) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     pthread_t self = pthread_self();
@@ -256,7 +248,6 @@ int pika_platform_thread_rtmutex_unlock(pika_platform_thread_rtmutex_t* rtm) {
     pthread_mutex_lock(&rtm->mutex);
     pika_GIL_ENTER();
 
-    // printf("rtm->owner = %lu\n", rtm->owner);
     if (rtm->owner != self) {
         perror("Attempt to unlock a mutex not owned by the current thread");
         pthread_mutex_unlock(&rtm->mutex);
@@ -267,7 +258,6 @@ int pika_platform_thread_rtmutex_unlock(pika_platform_thread_rtmutex_t* rtm) {
     if (rtm->count == 0) {
         rtm->owner = (pthread_t)0;
         pthread_cond_signal(&rtm->cond);
-        // printf("rtm->owner = %lu\n", rtm->owner);
     }
 
     pthread_mutex_unlock(&rtm->mutex);
@@ -279,9 +269,8 @@ int pika_platform_thread_rtmutex_unlock(pika_platform_thread_rtmutex_t* rtm) {
     WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
 #endif
 }
-#endif
 
-// 检查递归互斥锁是否已被当前线程获取
+// Check if recursive mutex is held by the current thread
 int pika_platform_thread_rtmutex_locked(pika_platform_thread_rtmutex_t* rtm) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     pthread_t self = pthread_self();
@@ -322,7 +311,7 @@ void pika_platform_thread_cond_destroy(pika_platform_thread_cond_t* cond) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     pthread_cond_destroy(&cond->cond);
     pika_platform_thread_rtmutex_destroy(&cond->rtmutex);
-    cond->owner = (pthread_t)0;  // 释放资源后重置 owner
+    cond->owner = (pthread_t)0;  // Reset owner after releasing resources
 #elif PIKA_FREERTOS_ENABLE
 #elif PIKA_RTTHREAD_ENABLE
 #elif PIKA_ZEUSOS_ENABLE
@@ -331,41 +320,37 @@ void pika_platform_thread_cond_destroy(pika_platform_thread_cond_t* cond) {
 #endif
 }
 
-// 检查当前线程是否持有互斥锁
+// Check if the current thread owns the mutex
 static int is_mutex_owned(pika_platform_thread_cond_t* cond) {
-    // pthread_t current_thread = pthread_self();
-
-    // 使用 pthread_mutex_trylock 来检查是否已经持有锁
     if (pthread_mutex_trylock(&cond->rtmutex.mutex) == EBUSY) {
-        // 如果锁已经被持有,尝试解锁并检查是否是当前线程持有的
         if (pthread_mutex_unlock(&cond->rtmutex.mutex) == 0) {
-            return 1;  // 当前线程持有锁
+            return 1;  // Current thread owns the lock
         }
     }
 
     return 0;
 }
 
-// 带阻塞和超时功能的条件变量等待
+// Condition variable wait with blocking and timeout functionality
 int pika_platform_thread_cond_timedwait(pika_platform_thread_cond_t* cond,
                                         Arg* timeout) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
-    ArgType timout_type = arg_getType(timeout);
+    ArgType timeout_type = arg_getType(timeout);
     pika_float timeout_f;
     int result;
 
-    // 检查是否已经获得了互斥锁
+    // Check if the mutex is already acquired
     if (!is_mutex_owned(cond)) {
         return -1;
     }
 
-    if (!(timout_type == ARG_TYPE_FLOAT || timout_type == ARG_TYPE_INT ||
-          timout_type == ARG_TYPE_NONE)) {
+    if (!(timeout_type == ARG_TYPE_FLOAT || timeout_type == ARG_TYPE_INT ||
+          timeout_type == ARG_TYPE_NONE)) {
         return PIKA_RES_ERR_INVALID_PARAM;
     }
 
-    if (timout_type == ARG_TYPE_FLOAT || timout_type == ARG_TYPE_INT) {
-        if (timout_type == ARG_TYPE_FLOAT) {
+    if (timeout_type == ARG_TYPE_FLOAT || timeout_type == ARG_TYPE_INT) {
+        if (timeout_type == ARG_TYPE_FLOAT) {
             timeout_f = arg_getFloat(timeout);
         } else {
             int timeout_d = arg_getInt(timeout);
@@ -377,35 +362,35 @@ int pika_platform_thread_cond_timedwait(pika_platform_thread_cond_t* cond,
         }
 
         struct timespec ts;
-        clock_gettime(CLOCK_REALTIME, &ts);  // 获取当前时间
+        clock_gettime(CLOCK_REALTIME, &ts);  // Get current time
 
-        // 将浮点数秒转换为秒和纳秒
+        // Convert floating seconds to seconds and nanoseconds
         long sec = (long)timeout_f;
         long nsec = (long)((timeout_f - (pika_float)sec) * 1000000000.0);
 
         ts.tv_sec += sec;
         ts.tv_nsec += nsec;
 
-        // 如果纳秒数超过 1 秒,则需要调整秒数和纳秒数
+        // Adjust seconds and nanoseconds if nanoseconds exceed 1 second
         if (ts.tv_nsec >= 1000000000) {
-            ts.tv_nsec -= 1000000000;  // 减去 1 秒的纳秒数
-            ts.tv_sec += 1;            // 增加 1 秒
+            ts.tv_nsec -= 1000000000;  // Subtract 1 second in nanoseconds
+            ts.tv_sec += 1;            // Add 1 second
         }
 
-        // 等待直到被通知或超时
+        // Wait until notified or timeout occurs
         pika_GIL_EXIT();
         result = pthread_cond_timedwait(&cond->cond, &cond->rtmutex.mutex, &ts);
         pika_GIL_ENTER();
 
         if (result != 0) {
             if (result == ETIMEDOUT) {
-                return -1;  // 超时
+                return -1;  // Timeout
             }
             perror("pthread_cond_timedwait");
-            return -1;  // 其他错误
+            return -1;  // Other errors
         }
-    } else if (timout_type == ARG_TYPE_NONE) {
-        // 永久等待
+    } else if (timeout_type == ARG_TYPE_NONE) {
+        // Wait indefinitely
         pika_GIL_EXIT();
         result = pthread_cond_wait(&cond->cond, &cond->rtmutex.mutex);
         pika_GIL_ENTER();
@@ -427,12 +412,13 @@ int pika_platform_thread_cond_timedwait(pika_platform_thread_cond_t* cond,
     WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_);
 #endif
 }
-// 信号量通知
+
+// Signal condition variable
 int pika_platform_thread_cond_signal(pika_platform_thread_cond_t* cond) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     int result;
     result = pthread_cond_signal(&cond->cond);
-    cond->owner = (pthread_t)0;  // 通知后重置 owner
+    cond->owner = (pthread_t)0;  // Reset owner after notifying
     return result == 0 ? 0 : -1;
 #elif PIKA_FREERTOS_ENABLE
 #elif PIKA_RTTHREAD_ENABLE
@@ -442,12 +428,12 @@ int pika_platform_thread_cond_signal(pika_platform_thread_cond_t* cond) {
 #endif
 }
 
-// 信号量广播
+// Broadcast condition variable
 int pika_platform_thread_cond_broadcast(pika_platform_thread_cond_t* cond) {
 #if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
     int result;
     result = pthread_cond_broadcast(&cond->cond);
-    cond->owner = (pthread_t)0;  // 广播后重置 owner
+    cond->owner = (pthread_t)0;  // Reset owner after broadcasting
     return result == 0 ? 0 : -1;
 #elif PIKA_FREERTOS_ENABLE
 #elif PIKA_RTTHREAD_ENABLE

+ 15 - 13
port/linux/package/pikascript/pikascript-lib/threading/PikaPlatformEx.h

@@ -1,39 +1,40 @@
 #ifndef ___PikaPlatformEx__H
 #define ___PikaPlatformEx__H
-#include "PikaPlatform.h"
-#include "PikaObj.h"
-#include <time.h>
-#include <pthread.h>
 #include <errno.h>
+#include <pthread.h>
+#include <time.h>
+#include "PikaObj.h"
+#include "PikaPlatform.h"
+
 int pika_platform_thread_mutex_timedlock(pika_platform_thread_mutex_t* m,
                                          pika_bool block,
                                          Arg* timeout);
 
 //-------------------------------
 
-// 递归带超时互斥锁结构体
+// Recursive mutex with timeout structure
 typedef struct {
     pthread_mutex_t mutex;
     pthread_cond_t cond;
-    pthread_t owner;  // 当前持有锁的线程 ID
-    int count;        // 递归深度
+    pthread_t owner;  // Thread ID of the current lock owner
+    int count;        // Recursion depth
 } pika_platform_thread_rtmutex_t;
 
-// 初始化递归互斥锁
+// Initialize recursive mutex
 void pika_platform_thread_rtmutex_init(pika_platform_thread_rtmutex_t* rtm);
 
-// 销毁递归互斥锁
+// Destroy recursive mutex
 void pika_platform_thread_rtmutex_destroy(pika_platform_thread_rtmutex_t* rtm);
 
-// 带超时的递归互斥锁加锁
+// Lock recursive mutex with timeout
 int pika_platform_thread_rtmutex_lock(pika_platform_thread_rtmutex_t* rtm,
                                       pika_bool block,
                                       Arg* timeout);
 
-// 递归互斥锁解锁
+// Unlock recursive mutex
 int pika_platform_thread_rtmutex_unlock(pika_platform_thread_rtmutex_t* rtm);
 
-// 是否已获得锁
+// Check if the lock is held
 int pika_platform_thread_rtmutex_locked(pika_platform_thread_rtmutex_t* rtm);
 
 //-------------------------------------
@@ -41,7 +42,7 @@ int pika_platform_thread_rtmutex_locked(pika_platform_thread_rtmutex_t* rtm);
 typedef struct {
     pika_platform_thread_rtmutex_t rtmutex;
     pthread_cond_t cond;
-    pthread_t owner;  // 当前持有锁的线程 ID
+    pthread_t owner;  // Thread ID of the current lock owner
 } pika_platform_thread_cond_t;
 
 void pika_platform_thread_cond_init(pika_platform_thread_cond_t* cond);
@@ -50,4 +51,5 @@ int pika_platform_thread_cond_signal(pika_platform_thread_cond_t* cond);
 int pika_platform_thread_cond_broadcast(pika_platform_thread_cond_t* cond);
 int pika_platform_thread_cond_timedwait(pika_platform_thread_cond_t* cond,
                                         Arg* timeout);
+
 #endif

+ 10 - 9
port/linux/package/pikascript/pikascript-lib/threading/pika_hal_ex.c

@@ -1,4 +1,3 @@
-
 #include "pika_hal_ex.h"
 
 pika_hal_CircularPtrQueue* pika_hal_circularPtrQueue_create(size_t capacity) {
@@ -14,8 +13,9 @@ pika_hal_CircularPtrQueue* pika_hal_circularPtrQueue_create(size_t capacity) {
     pika_platform_thread_mutex_init(&cb->mutex);
 #endif
     cb->capacity = capacity;
-    cb->buffer = (void**)pikaMalloc(capacity *
-                                    sizeof(void*));  // 分配足够的空间来存储指针
+    cb->buffer = (void**)pikaMalloc(
+        capacity *
+        sizeof(void*));  // Allocate sufficient space to store pointers
     if (NULL == cb->buffer) {
         pikaFree(cb, sizeof(pika_hal_CircularPtrQueue));
         return NULL;
@@ -69,7 +69,8 @@ int pika_hal_circularPtrQueue_deinit(pika_hal_CircularPtrQueue* cb) {
 #if PIKA_HAL_CIRCULAR_QUEUE_MUTEX_ENABLE
     pika_platform_thread_mutex_lock(&cb->mutex);
 #endif
-    pikaFree(cb->buffer, cb->capacity * sizeof(void*));  // 释放指针数组
+    pikaFree(cb->buffer,
+             cb->capacity * sizeof(void*));  // Free the pointer array
     cb->buffer = NULL;
     cb->head = 0;
     cb->tail = 0;
@@ -118,7 +119,7 @@ int pika_hal_circularPtrQueue_peek(pika_hal_CircularPtrQueue* cb,
     return 0;
 }
 
-// 以下待测试
+// Below functions need testing
 int pika_hal_circularPtrQueue_enqueueHead(pika_hal_CircularPtrQueue* cb,
                                           void* data) {
     int ret = 0;
@@ -126,11 +127,11 @@ int pika_hal_circularPtrQueue_enqueueHead(pika_hal_CircularPtrQueue* cb,
     pika_platform_thread_mutex_lock(&cb->mutex);
 #endif
     if (cb->count == cb->capacity) {
-        ret = -1;  // 队列已满
+        ret = -1;  // Queue is full
         goto __exit;
     }
 
-    // 更新 head 指针前的位置,然后更新 head
+    // Update head position before modifying head pointer
     cb->head = (cb->head - 1 + cb->capacity) % cb->capacity;
     cb->buffer[cb->head] = data;
     cb->count++;
@@ -148,7 +149,7 @@ int pika_hal_circularPtrQueue_dequeueTail(pika_hal_CircularPtrQueue* cb,
     pika_platform_thread_mutex_lock(&cb->mutex);
 #endif
     if (cb->count == 0) {
-        ret = -1;  // 队列为空
+        ret = -1;  // Queue is empty
         goto __exit;
     }
 
@@ -165,7 +166,7 @@ __exit:
 int pika_hal_circularPtrQueue_peekTail(pika_hal_CircularPtrQueue* cb,
                                        void** value) {
     if (cb->count == 0) {
-        return -1;  // 队列为空
+        return -1;  // Queue is empty
     }
 
 #if PIKA_HAL_CIRCULAR_QUEUE_MUTEX_ENABLE

+ 16 - 16
port/linux/package/pikascript/pikascript-lib/threading/pika_hal_ex.h

@@ -1,44 +1,44 @@
 #ifndef PIKA_HAL_CIRCULAR_PTR_QUEUE_H
 #define PIKA_HAL_CIRCULAR_PTR_QUEUE_H
-#include "pika_hal.h"
 #include "PikaObj.h"
+#include "pika_hal.h"
 
-// 定义循环指针队列的结构体
+// Define the structure for the circular pointer queue
 typedef struct pika_hal_CircularPtrQueue {
-    void** buffer;    // 存储元素的缓冲区
-    size_t head;      // 队头指针
-    size_t tail;      // 队尾指针
-    size_t count;     // 当前元素数量
-    size_t capacity;  // 缓冲区容量
+    void** buffer;    // Buffer to store elements
+    size_t head;      // Head pointer
+    size_t tail;      // Tail pointer
+    size_t count;     // Current element count
+    size_t capacity;  // Buffer capacity
 #if PIKA_HAL_CIRCULAR_QUEUE_MUTEX_ENABLE
-    pika_platform_thread_mutex_t mutex;  // 互斥锁
+    pika_platform_thread_mutex_t mutex;  // Mutex
 #endif
 } pika_hal_CircularPtrQueue;
 
-// 创建一个新的循环指针队列
+// Create a new circular pointer queue
 pika_hal_CircularPtrQueue* pika_hal_circularPtrQueue_create(size_t capacity);
 
-// 向队列中添加一个元素
+// Add an element to the queue
 int pika_hal_circularPtrQueue_enqueue(pika_hal_CircularPtrQueue* cb,
                                       void* data);
 
-// 从队列中移除一个元素
+// Remove an element from the queue
 int pika_hal_circularPtrQueue_dequeue(pika_hal_CircularPtrQueue* cb,
                                       void** value);
 
-// 销毁队列并释放相关资源
+// Destroy the queue and release related resources
 int pika_hal_circularPtrQueue_deinit(pika_hal_CircularPtrQueue* cb);
 
-// 获取队列中的元素数量
+// Get the number of elements in the queue
 size_t pika_hal_circularPtrQueue_getCount(pika_hal_CircularPtrQueue* cb);
 
-// 检查队列是否为空
+// Check if the queue is empty
 int pika_hal_circularPtrQueue_isEmpty(pika_hal_CircularPtrQueue* cb);
 
-// 检查队列是否已满
+// Check if the queue is full
 int pika_hal_circularPtrQueue_isFull(pika_hal_CircularPtrQueue* cb);
 
-// 查看队列头部的元素,但不移除它
+// Peek at the front element of the queue without removing it
 int pika_hal_circularPtrQueue_peek(pika_hal_CircularPtrQueue* cb, void** value);
 
 #endif  // PIKA_HAL_CIRCULAR_PTR_QUEUE_H

+ 5 - 8
port/linux/package/pikascript/pikascript-lib/threading/threading_Lock.c

@@ -14,6 +14,7 @@ void threading_Lock___init__(PikaObj* self) {
     pika_platform_thread_mutex_init(m);
     obj_setPtr(self, "_mutex_", m);
 }
+
 pika_bool threading_Lock_acquire(PikaObj* self, pika_bool block, Arg* timeout) {
     pika_platform_thread_mutex_t* m = obj_getPtr(self, "_mutex_");
     int result = pika_platform_thread_mutex_timedlock(m, block, timeout);
@@ -23,27 +24,23 @@ pika_bool threading_Lock_acquire(PikaObj* self, pika_bool block, Arg* timeout) {
     }
     return result == 0 ? pika_true : pika_false;
 }
+
 pika_bool threading_Lock_locked(PikaObj* self) {
     pika_platform_thread_mutex_t* m = obj_getPtr(self, "_mutex_");
     pika_GIL_EXIT();
     int result = pika_platform_thread_mutex_trylock(m);
     pika_GIL_ENTER();
     if (result == 0) {
-        // 成功获得了锁,需要解锁
-        // pika_GIL_EXIT();
+        // Successfully acquired the lock, need to unlock it
         pika_platform_thread_mutex_unlock(m);
-        // pika_GIL_ENTER();/*  */
-        return pika_false;  // 锁未被占用/*  */
+        return pika_false;  // Lock is not held
     } else {
-        // 锁已被占用或发生了其他错误
-        // perror("pthread_mutex_trylock");
+        // Lock is held or an error occurred
         return pika_true;
     }
 }
 
 void threading_Lock_release(PikaObj* self) {
     pika_platform_thread_mutex_t* m = obj_getPtr(self, "_mutex_");
-    // pika_GIL_EXIT();
     pika_platform_thread_mutex_unlock(m);
-    // pika_GIL_ENTER();
 }

+ 14 - 1
port/linux/test/module-test.cpp

@@ -1373,6 +1373,19 @@ TEST(jrpc, exec_concat_str_space) {
     free(response);
 }
 
-TEST_RUN_SINGLE_FILE(threading, lock_rlock, "test/python/threading/lock_rlock.py")
+#if !PIKA_NANO_ENABLE
+TEST(threading, lock_rlock) {
+    g_PikaMemInfo.heapUsedMax = 0;
+    PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
+    extern unsigned char pikaModules_py_a[];
+    obj_linkLibrary(pikaMain, pikaModules_py_a); /* run */
+    __platform_printf("BEGIN\r\n");
+    pikaVM_runSingleFile(pikaMain, "test/python/threading/lock_rlock.py");
+    /* assert */ /* deinit */
+    pika_vm_exit_await();
+    obj_deinit(pikaMain);
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+#endif
 
 TEST_END

+ 1 - 0
port/linux/test/python/threading/lock_rlock.py

@@ -78,6 +78,7 @@ def main():
     # time.sleep(60)
     while finished < 2:
         time.sleep(1)
+    time.sleep(1)
 
 
 main()

+ 1 - 1
src/PikaVersion.h

@@ -2,4 +2,4 @@
 #define PIKA_VERSION_MINOR 13
 #define PIKA_VERSION_MICRO 4
 
-#define PIKA_EDIT_TIME "2024/10/12 16:14:35"
+#define PIKA_EDIT_TIME "2024/10/13 21:00:39"