线程退出支持

This commit is contained in:
zhangzheng
2023-11-20 16:46:50 +08:00
parent faa0e2134f
commit 2c410d4000
6 changed files with 64 additions and 16 deletions

View File

@@ -48,7 +48,6 @@ long be_writev(long fd, const struct iovec *iov, long iovcnt);
long be_ioctl(long fd, long req, void *args);
long be_set_tid_address(int *val);
long be_set_thread_area(void *p);
void be_exit(int code);
umword_t be_munmap(void *start, size_t len);
umword_t be_mmap2(void *start,
size_t len,
@@ -64,4 +63,7 @@ void sys_exit(va_list ap);
umword_t sys_munmap(va_list ap);
int be_futex(uint32_t *uaddr, int futex_op, uint32_t val,
const struct timespec *timeout, uint32_t uaddr2, uint32_t val3);
// int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *ptid, void *tls, pid_t *ctid);
void pthread_cnt_inc(void);
int pthread_cnt_dec(void);
int pthread_get(void);

View File

@@ -59,13 +59,4 @@ unsigned long get_thread_area(void)
return i_msg->user[0];
}
void be_exit(int code)
{
/*TODO:暂时先这样*/
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, TASK_THIS));
ulog_write_str(u_get_global_env()->log_hd, "It shouldn't go here.\n");
}
void sys_exit(va_list ap)
{
be_exit(0);
}

View File

@@ -28,6 +28,21 @@
#include <assert.h>
#include <errno.h>
#include <pthread_impl.h>
#include <atomic.h>
static int pthread_cnt = 1;
void pthread_cnt_inc(void)
{
a_inc(&pthread_cnt);
}
int pthread_cnt_dec(void)
{
return a_fetch_add(&pthread_cnt, -1);
}
int pthread_get(void)
{
return pthread_cnt;
}
extern void __pthread_new_thread_entry__(void);
int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *ptid, void *tls, pid_t *ctid)
@@ -83,7 +98,47 @@ int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *pti
ph->hd = th1_hd;
ph->ctid = (umword_t)ctid;
*ptid = th1_hd;
pthread_cnt_inc();
thread_run(th1_hd, 2); // 优先级默认为2
return 0;
}
#define FUTEX_WAKE_CLEAR 10
void be_exit(long exit_code)
{
struct pthread *pt = __pthread_self();
int th_hd = pt->hd;
int *old_ctid = (int *)(pt->ctid);
if (th_hd != THREAD_MAIN)
{
if (old_ctid)
{
be_futex(old_ctid, FUTEX_WAKE_CLEAR, 1, 0, 0, 0);
}
if (pthread_cnt_dec() == 1)
{
/*删除整个进程*/
goto del_task;
}
else
{
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, th_hd));
}
}
else
{
del_task:
/*TODO:删除其它东西*/
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, TASK_THIS)); //!< 删除当前task以及申请得所有对象
a_crash(); //!< 强制退出
}
}
void sys_exit(va_list ap)
{
long exit_code;
ARG_1_BE(ap, exit_code, long);
be_exit(exit_code);
}

View File

@@ -29,7 +29,7 @@ static void hard_sleep(void)
}
static void *thread_test_func(void* arg)
{
while (1)
// while (1)
{
pthread_mutex_lock(&lock);
printf("thread 1 ..\n");
@@ -39,7 +39,7 @@ static void *thread_test_func(void* arg)
}
static void *thread_test_func2(void* arg)
{
while (1)
// while (1)
{
pthread_mutex_lock(&lock);
printf("thread 2 ..\n");