Skip to content

Commit

Permalink
fix ASCII issue for threading module
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed Oct 13, 2024
1 parent b3782c8 commit cfc3ddc
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 127 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
#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);

//-------------------------------------

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);
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "pika_hal_ex.h"

pika_hal_CircularPtrQueue* pika_hal_circularPtrQueue_create(size_t capacity) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -118,19 +119,19 @@ 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;
#if PIKA_HAL_CIRCULAR_QUEUE_MUTEX_ENABLE
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++;
Expand All @@ -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;
}

Expand All @@ -165,7 +166,7 @@ int pika_hal_circularPtrQueue_dequeueTail(pika_hal_CircularPtrQueue* cb,
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
15 changes: 14 additions & 1 deletion port/linux/test/module-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions port/linux/test/python/threading/lock_rlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def main():
# time.sleep(60)
while finished < 2:
time.sleep(1)
time.sleep(1)


main()
2 changes: 1 addition & 1 deletion src/PikaVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit cfc3ddc

Please sign in to comment.