futex支持超时

This commit is contained in:
zhangzheng
2023-11-20 16:17:58 +08:00
parent c7ee3e2fe7
commit faa0e2134f
18 changed files with 679 additions and 86 deletions

View File

@@ -30,20 +30,20 @@ int be_futex(uint32_t *uaddr, int futex_op, uint32_t val,
struct pthread *pt = __pthread_self();
ipc_timeout_t to = ipc_timeout_create2(0, 0);
umword_t total = 0;
umword_t st_val;
sys_info_t sys_info;
msg_tag_t tag;
if (timeout && !(futex_op & FUTEX_REQUEUE))
{
to = ipc_timeout_create2(timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000, 0);
total = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000;
}
umword_t st_val;
_try_again:
sys_read_info(SYS_PROT, &sys_info);
st_val = sys_info.sys_tick;
msg_tag_t tag = futex_ctrl(FUTEX_PROT, uaddr, futex_op, val, (void *)timeout, uaddr2, val3, pt->tid);
tag = futex_ctrl(FUTEX_PROT, uaddr, futex_op, val, total, uaddr2, val3, pt->tid);
if (msg_tag_get_val(tag) == -EWTIMEDOUT)
{
umword_t en_val;

View File

@@ -42,7 +42,6 @@ int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *pti
ph = (struct pthread *)((char *)tls - sizeof(struct pthread));
th1_hd = handler_alloc();
if (th1_hd == HANDLER_INVALID)
{
@@ -64,8 +63,9 @@ int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *pti
stack = (char *)stack - MSG_BUG_LEN;
umword_t *stack_tmp = (umword_t *)stack;
// 设置调用参数等
*(--stack_tmp) = (umword_t)args;
*(--stack_tmp) = (umword_t)0;
*(--stack_tmp) = (umword_t)0; // 保留
*(--stack_tmp) = (umword_t)func;
tag = thread_exec_regs(th1_hd, (umword_t)__pthread_new_thread_entry__, (umword_t)stack_tmp, RAM_BASE(), 0);
@@ -80,7 +80,10 @@ int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *pti
handler_free_umap(th1_hd);
return msg_tag_get_prot(tag);
}
thread_run(th1_hd, 2);
ph->hd = th1_hd;
ph->ctid = (umword_t)ctid;
*ptid = th1_hd;
thread_run(th1_hd, 2); // 优先级默认为2
return 0;
}

View File

@@ -3,4 +3,4 @@
#include <u_types.h>
msg_tag_t futex_ctrl(obj_handler_t obj, uint32_t *uaddr, int futex_op, uint32_t val,
void *timeout, uint32_t uaddr2, uint32_t val3, int tid);
umword_t timeout, uint32_t uaddr2, uint32_t val3, int tid);

View File

@@ -10,7 +10,7 @@ enum futex_op
FUTEX_CTRL,
};
msg_tag_t futex_ctrl(obj_handler_t obj, uint32_t *uaddr, int futex_op, uint32_t val,
void *timeout, uint32_t uaddr2, uint32_t val3, int tid)
umword_t timeout, uint32_t uaddr2, uint32_t val3, int tid)
{
ipc_msg_t *msg;

View File

@@ -14,68 +14,53 @@
#include <string.h>
#define DEBUG_IPC_CALL 1
static umword_t th1_hd = 0;
static umword_t th2_hd = 0;
static umword_t ipc_hd = 0;
static pthread_mutex_t lock;
static pthread_t pth;
static pthread_t pth2;
static char msg_buf0[MSG_BUG_LEN];
static char msg_buf1[MSG_BUG_LEN];
#define STACK_SIZE 2048
static __attribute__((aligned(8))) uint8_t stack0[STACK_SIZE];
static __attribute__((aligned(8))) uint8_t stack1[STACK_SIZE];
static void hard_sleep(void)
{
for (volatile int i; i < 10000000; i++)
;
}
static void thread_test_func(void)
static void *thread_test_func(void* arg)
{
char *buf;
umword_t len;
thread_msg_buf_get(th1_hd, (umword_t *)(&buf), NULL);
while (1)
{
pthread_mutex_lock(&lock);
printf("thread 1 ..\n");
pthread_mutex_unlock(&lock);
}
printf("thread_test_func.\n");
task_unmap(TASK_PROT, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, th1_hd));
printf("Error\n");
return NULL;
}
static void thread_test_func2(void)
static void *thread_test_func2(void* arg)
{
char *buf;
umword_t len;
thread_msg_buf_get(th2_hd, (umword_t *)(&buf), NULL);
while (1)
{
pthread_mutex_lock(&lock);
printf("thread 2 ..\n");
pthread_mutex_unlock(&lock);
}
printf("thread_test_func2.\n");
task_unmap(TASK_PROT, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, th2_hd));
printf("Error\n");
}
static pthread_t pth;
static void *func_test(void *arg)
{
return NULL;
}
/**
*
*/
void pthread_lock_test(void)
{
pthread_mutex_init(&lock, NULL);
pthread_attr_t attr;
pthread_mutex_init(&lock, NULL);
pthread_attr_init(&attr);
pthread_attr_setstack(&attr, stack0, STACK_SIZE);
pthread_create(&pth, &attr, func_test, NULL);
pthread_create(&pth, &attr, thread_test_func, NULL);
pthread_attr_setstack(&attr, stack1, STACK_SIZE);
pthread_create(&pth2, &attr, thread_test_func2, NULL);
// th1_hd = handler_alloc();
// assert(th1_hd != HANDLER_INVALID);