修正一些问题

This commit is contained in:
zhangzheng
2023-11-21 01:34:47 +08:00
parent f58d4fe537
commit 13fd264adf
15 changed files with 330 additions and 67 deletions

View File

@@ -248,7 +248,10 @@
"ctime": "c",
"pthread_impl.h": "c",
"time.h": "c",
"u_thread_util.h": "c"
"u_thread_util.h": "c",
"__mutex_base": "c",
"mutex": "c",
"__threading_support": "c"
},
"cortex-debug.showRTOS": false,
"cortex-debug.variableUseNaturalFormat": false,

View File

@@ -43,7 +43,15 @@ int pthread_get(void)
{
return pthread_cnt;
}
struct start_args
{
void *(*start_func)(void *);
void *start_arg;
volatile int control;
unsigned long sig_mask[_NSIG / 8 / sizeof(long)];
void *tp;
int start_flag;
};
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)
{
@@ -54,6 +62,7 @@ int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *pti
msg_tag_t tag;
obj_handler_t th1_hd;
struct pthread *ph;
struct start_args *st_args = args;
ph = (struct pthread *)((char *)tls - sizeof(struct pthread));
@@ -100,7 +109,10 @@ int be_clone(int (*func)(void *), void *stack, int flags, void *args, pid_t *pti
ph->ctid = (umword_t)ctid;
*ptid = th1_hd;
pthread_cnt_inc();
thread_run(th1_hd, 2); // 优先级默认为2
if (!(st_args->start_flag & PTHREAD_DONT_RUN))
{
thread_run(th1_hd, 2); // 优先级默认为2
}
return 0;
}

View File

@@ -75,8 +75,11 @@ extern "C" {
#define PTHREAD_NULL ((pthread_t)0)
#define PTHREAD_DONT_RUN 0x1
#include <u_types.h>
obj_handler_t pthread_hd_get(pthread_t th);
int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
int pthread_detach(pthread_t);
_Noreturn void pthread_exit(void *);
@@ -149,6 +152,7 @@ int pthread_setspecific(pthread_key_t, const void *);
int pthread_attr_init(pthread_attr_t *);
int pthread_attr_destroy(pthread_attr_t *);
int pthread_attr_set_flag(pthread_attr_t *a, int flag);
int pthread_attr_getguardsize(const pthread_attr_t *__restrict, size_t *__restrict);
int pthread_attr_setguardsize(pthread_attr_t *, size_t);
int pthread_attr_getstacksize(const pthread_attr_t *__restrict, size_t *__restrict);

View File

@@ -29,9 +29,12 @@ typedef double double_t;
#define __DEFINED_double_t
#endif
#if defined(__NEED_max_align_t) && !defined(__DEFINED_max_align_t)
typedef struct { long long __ll; long double __ld; } max_align_t;
typedef struct
{
long long __ll;
long double __ld;
} max_align_t;
#define __DEFINED_max_align_t
#endif
@@ -84,44 +87,43 @@ typedef _Int64 suseconds_t;
#define __DEFINED_suseconds_t
#endif
#if defined(__NEED_int8_t) && !defined(__DEFINED_int8_t)
typedef signed char int8_t;
typedef signed char int8_t;
#define __DEFINED_int8_t
#endif
#if defined(__NEED_int16_t) && !defined(__DEFINED_int16_t)
typedef signed short int16_t;
typedef signed short int16_t;
#define __DEFINED_int16_t
#endif
#if defined(__NEED_int32_t) && !defined(__DEFINED_int32_t)
typedef signed int int32_t;
typedef signed int int32_t;
#define __DEFINED_int32_t
#endif
#if defined(__NEED_int64_t) && !defined(__DEFINED_int64_t)
typedef signed _Int64 int64_t;
typedef signed _Int64 int64_t;
#define __DEFINED_int64_t
#endif
#if defined(__NEED_intmax_t) && !defined(__DEFINED_intmax_t)
typedef signed _Int64 intmax_t;
typedef signed _Int64 intmax_t;
#define __DEFINED_intmax_t
#endif
#if defined(__NEED_uint8_t) && !defined(__DEFINED_uint8_t)
typedef unsigned char uint8_t;
typedef unsigned char uint8_t;
#define __DEFINED_uint8_t
#endif
#if defined(__NEED_uint16_t) && !defined(__DEFINED_uint16_t)
typedef unsigned short uint16_t;
typedef unsigned short uint16_t;
#define __DEFINED_uint16_t
#endif
#if defined(__NEED_uint32_t) && !defined(__DEFINED_uint32_t)
typedef unsigned int uint32_t;
typedef unsigned int uint32_t;
#define __DEFINED_uint32_t
#endif
@@ -140,7 +142,6 @@ typedef unsigned _Int64 uintmax_t;
#define __DEFINED_uintmax_t
#endif
#if defined(__NEED_mode_t) && !defined(__DEFINED_mode_t)
typedef unsigned mode_t;
#define __DEFINED_mode_t
@@ -186,7 +187,6 @@ typedef unsigned _Int64 fsfilcnt_t;
#define __DEFINED_fsfilcnt_t
#endif
#if defined(__NEED_wint_t) && !defined(__DEFINED_wint_t)
typedef unsigned wint_t;
#define __DEFINED_wint_t
@@ -197,9 +197,8 @@ typedef unsigned long wctype_t;
#define __DEFINED_wctype_t
#endif
#if defined(__NEED_timer_t) && !defined(__DEFINED_timer_t)
typedef void * timer_t;
typedef void *timer_t;
#define __DEFINED_timer_t
#endif
@@ -214,16 +213,25 @@ typedef long clock_t;
#endif
#if defined(__NEED_struct_timeval) && !defined(__DEFINED_struct_timeval)
struct timeval { time_t tv_sec; suseconds_t tv_usec; };
struct timeval
{
time_t tv_sec;
suseconds_t tv_usec;
};
#define __DEFINED_struct_timeval
#endif
#if defined(__NEED_struct_timespec) && !defined(__DEFINED_struct_timespec)
struct timespec { time_t tv_sec; int :8*(sizeof(time_t)-sizeof(long))*(__BYTE_ORDER==4321); long tv_nsec; int :8*(sizeof(time_t)-sizeof(long))*(__BYTE_ORDER!=4321); };
struct timespec
{
time_t tv_sec;
int : 8 * (sizeof(time_t) - sizeof(long)) * (__BYTE_ORDER == 4321);
long tv_nsec;
int : 8 * (sizeof(time_t) - sizeof(long)) * (__BYTE_ORDER != 4321);
};
#define __DEFINED_struct_timespec
#endif
#if defined(__NEED_pid_t) && !defined(__DEFINED_pid_t)
typedef int pid_t;
#define __DEFINED_pid_t
@@ -254,7 +262,6 @@ typedef unsigned useconds_t;
#define __DEFINED_useconds_t
#endif
#ifdef __cplusplus
#if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t)
typedef unsigned long pthread_t;
@@ -263,7 +270,7 @@ typedef unsigned long pthread_t;
#else
#if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t)
typedef struct __pthread * pthread_t;
typedef struct __pthread *pthread_t;
#define __DEFINED_pthread_t
#endif
@@ -284,28 +291,42 @@ typedef int pthread_spinlock_t;
#endif
#if defined(__NEED_pthread_mutexattr_t) && !defined(__DEFINED_pthread_mutexattr_t)
typedef struct { unsigned __attr; } pthread_mutexattr_t;
typedef struct
{
unsigned __attr;
} pthread_mutexattr_t;
#define __DEFINED_pthread_mutexattr_t
#endif
#if defined(__NEED_pthread_condattr_t) && !defined(__DEFINED_pthread_condattr_t)
typedef struct { unsigned __attr; } pthread_condattr_t;
typedef struct
{
unsigned __attr;
} pthread_condattr_t;
#define __DEFINED_pthread_condattr_t
#endif
#if defined(__NEED_pthread_barrierattr_t) && !defined(__DEFINED_pthread_barrierattr_t)
typedef struct { unsigned __attr; } pthread_barrierattr_t;
typedef struct
{
unsigned __attr;
} pthread_barrierattr_t;
#define __DEFINED_pthread_barrierattr_t
#endif
#if defined(__NEED_pthread_rwlockattr_t) && !defined(__DEFINED_pthread_rwlockattr_t)
typedef struct { unsigned __attr[2]; } pthread_rwlockattr_t;
typedef struct
{
unsigned __attr[2];
} pthread_rwlockattr_t;
#define __DEFINED_pthread_rwlockattr_t
#endif
#if defined(__NEED_struct__IO_FILE) && !defined(__DEFINED_struct__IO_FILE)
struct _IO_FILE { char __x; };
struct _IO_FILE
{
char __x;
};
#define __DEFINED_struct__IO_FILE
#endif
@@ -314,7 +335,6 @@ typedef struct _IO_FILE FILE;
#define __DEFINED_FILE
#endif
#if defined(__NEED_va_list) && !defined(__DEFINED_va_list)
typedef __builtin_va_list va_list;
#define __DEFINED_va_list
@@ -325,37 +345,44 @@ typedef __builtin_va_list __isoc_va_list;
#define __DEFINED___isoc_va_list
#endif
#if defined(__NEED_mbstate_t) && !defined(__DEFINED_mbstate_t)
typedef struct __mbstate_t { unsigned __opaque1, __opaque2; } mbstate_t;
typedef struct __mbstate_t
{
unsigned __opaque1, __opaque2;
} mbstate_t;
#define __DEFINED_mbstate_t
#endif
#if defined(__NEED_locale_t) && !defined(__DEFINED_locale_t)
typedef struct __locale_struct * locale_t;
typedef struct __locale_struct *locale_t;
#define __DEFINED_locale_t
#endif
#if defined(__NEED_sigset_t) && !defined(__DEFINED_sigset_t)
typedef struct __sigset_t { unsigned long __bits[128/sizeof(long)]; } sigset_t;
typedef struct __sigset_t
{
unsigned long __bits[128 / sizeof(long)];
} sigset_t;
#define __DEFINED_sigset_t
#endif
#if defined(__NEED_struct_iovec) && !defined(__DEFINED_struct_iovec)
struct iovec { void *iov_base; size_t iov_len; };
struct iovec
{
void *iov_base;
size_t iov_len;
};
#define __DEFINED_struct_iovec
#endif
#if defined(__NEED_struct_winsize) && !defined(__DEFINED_struct_winsize)
struct winsize { unsigned short ws_row, ws_col, ws_xpixel, ws_ypixel; };
struct winsize
{
unsigned short ws_row, ws_col, ws_xpixel, ws_ypixel;
};
#define __DEFINED_struct_winsize
#endif
#if defined(__NEED_socklen_t) && !defined(__DEFINED_socklen_t)
typedef unsigned socklen_t;
#define __DEFINED_socklen_t
@@ -366,43 +393,97 @@ typedef unsigned short sa_family_t;
#define __DEFINED_sa_family_t
#endif
#if defined(__NEED_pthread_attr_t) && !defined(__DEFINED_pthread_attr_t)
typedef struct { union { int __i[sizeof(long)==8?14:9]; volatile int __vi[sizeof(long)==8?14:9]; unsigned long __s[sizeof(long)==8?7:9]; } __u; } pthread_attr_t;
typedef struct
{
union
{
int __i[sizeof(long) == 8 ? 15 : 10];
volatile int __vi[sizeof(long) == 8 ? 15 : 10];
unsigned long __s[sizeof(long) == 8 ? 8 : 10];
} __u;
} pthread_attr_t;
#define __DEFINED_pthread_attr_t
#endif
#if defined(__NEED_pthread_mutex_t) && !defined(__DEFINED_pthread_mutex_t)
typedef struct { union { int __i[sizeof(long)==8?10:6]; volatile int __vi[sizeof(long)==8?10:6]; volatile void *volatile __p[sizeof(long)==8?5:6]; } __u; } pthread_mutex_t;
typedef struct
{
union
{
int __i[sizeof(long) == 8 ? 10 : 6];
volatile int __vi[sizeof(long) == 8 ? 10 : 6];
volatile void *volatile __p[sizeof(long) == 8 ? 5 : 6];
} __u;
} pthread_mutex_t;
#define __DEFINED_pthread_mutex_t
#endif
#if defined(__NEED_mtx_t) && !defined(__DEFINED_mtx_t)
typedef struct { union { int __i[sizeof(long)==8?10:6]; volatile int __vi[sizeof(long)==8?10:6]; volatile void *volatile __p[sizeof(long)==8?5:6]; } __u; } mtx_t;
typedef struct
{
union
{
int __i[sizeof(long) == 8 ? 10 : 6];
volatile int __vi[sizeof(long) == 8 ? 10 : 6];
volatile void *volatile __p[sizeof(long) == 8 ? 5 : 6];
} __u;
} mtx_t;
#define __DEFINED_mtx_t
#endif
#if defined(__NEED_pthread_cond_t) && !defined(__DEFINED_pthread_cond_t)
typedef struct { union { int __i[12]; volatile int __vi[12]; void *__p[12*sizeof(int)/sizeof(void*)]; } __u; } pthread_cond_t;
typedef struct
{
union
{
int __i[12];
volatile int __vi[12];
void *__p[12 * sizeof(int) / sizeof(void *)];
} __u;
} pthread_cond_t;
#define __DEFINED_pthread_cond_t
#endif
#if defined(__NEED_cnd_t) && !defined(__DEFINED_cnd_t)
typedef struct { union { int __i[12]; volatile int __vi[12]; void *__p[12*sizeof(int)/sizeof(void*)]; } __u; } cnd_t;
typedef struct
{
union
{
int __i[12];
volatile int __vi[12];
void *__p[12 * sizeof(int) / sizeof(void *)];
} __u;
} cnd_t;
#define __DEFINED_cnd_t
#endif
#if defined(__NEED_pthread_rwlock_t) && !defined(__DEFINED_pthread_rwlock_t)
typedef struct { union { int __i[sizeof(long)==8?14:8]; volatile int __vi[sizeof(long)==8?14:8]; void *__p[sizeof(long)==8?7:8]; } __u; } pthread_rwlock_t;
typedef struct
{
union
{
int __i[sizeof(long) == 8 ? 14 : 8];
volatile int __vi[sizeof(long) == 8 ? 14 : 8];
void *__p[sizeof(long) == 8 ? 7 : 8];
} __u;
} pthread_rwlock_t;
#define __DEFINED_pthread_rwlock_t
#endif
#if defined(__NEED_pthread_barrier_t) && !defined(__DEFINED_pthread_barrier_t)
typedef struct { union { int __i[sizeof(long)==8?8:5]; volatile int __vi[sizeof(long)==8?8:5]; void *__p[sizeof(long)==8?4:5]; } __u; } pthread_barrier_t;
typedef struct
{
union
{
int __i[sizeof(long) == 8 ? 8 : 5];
volatile int __vi[sizeof(long) == 8 ? 8 : 5];
void *__p[sizeof(long) == 8 ? 4 : 5];
} __u;
} pthread_barrier_t;
#define __DEFINED_pthread_barrier_t
#endif
#undef _Addr
#undef _Int64
#undef _Reg

View File

@@ -88,6 +88,7 @@ enum
#define _a_sched __u.__i[3 * __SU + 1]
#define _a_policy __u.__i[3 * __SU + 2]
#define _a_prio __u.__i[3 * __SU + 3]
#define _a_flag __u.__i[9]
#define _m_type __u.__i[0]
#define _m_lock __u.__vi[1]
#define _m_waiters __u.__vi[2]

View File

@@ -0,0 +1,9 @@
#include "pthread_impl.h"
int pthread_attr_set_flag(pthread_attr_t *a, int flag)
{
__acquire_ptc();
a->_a_flag = PTHREAD_DONT_RUN;
__release_ptc();
return 0;
}

View File

@@ -209,7 +209,7 @@ struct start_args
volatile int control;
unsigned long sig_mask[_NSIG / 8 / sizeof(long)];
void *tp;
// void *none;
int start_flag;
};
#ifndef NO_LITTLE_MODE
#include "syscall_backend.h"
@@ -393,6 +393,7 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
args->start_arg = arg;
args->tp = TP_ADJ(new);
args->control = attr._a_sched ? 1 : 0;
args->start_flag = attr._a_flag;
/* Application signals (but not the synccall signal) must be
* blocked before the thread list lock can be taken, to ensure

View File

@@ -0,0 +1,7 @@
#include <pthread_impl.h>
#include <u_types.h>
obj_handler_t pthread_hd_get(pthread_t th)
{
return th->hd;
}

View File

@@ -35,7 +35,7 @@ int u_thread_create(obj_handler_t *th_hd, void *stack, umword_t stack_size, void
handler_free_umap(th1_hd);
return msg_tag_get_prot(tag);
}
tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size - sizeof(void *)*2, RAM_BASE(), 0);
tag = thread_exec_regs(th1_hd, (umword_t)thread_func, (umword_t)stack + stack_size - sizeof(void *), RAM_BASE(), 0);
if (msg_tag_get_prot(tag) < 0)
{
handler_free_umap(th1_hd);

View File

@@ -914,7 +914,7 @@ struct rt_spinlock
*/
struct rt_thread
{
// pthread_t th; /**< use pthread*/
pthread_t th; /**< use pthread*/
rt_list_t tlist; /**< the thread list */
int stat;

View File

@@ -1,3 +1,4 @@
#pragma once
int completion_sample(void);
void rtthread_drv_test(void);

View File

@@ -0,0 +1,66 @@
/*
* 程序清单:完成量例程
*
* 程序会初始化 2 个线程及初始化一个完成量对象
* 一个线程等待另一个线程发送完成量
*/
#include <rtthread.h>
#include <rtdevice.h>
#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5
/* 完成量控制块 */
static struct rt_completion completion;
__attribute__((aligned(RT_ALIGN_SIZE)))
static char thread1_stack[2048];
static struct rt_thread thread1;
/* 线程 1 入口函数 */
static void thread1_completion_wait(void *param)
{
/* 等待完成 */
rt_kprintf("thread1: completion is waitting\n");
rt_completion_wait(&completion, RT_WAITING_FOREVER);
rt_kprintf("thread1: completion waitting done\n");
rt_kprintf("thread1 leave.\n");
}
__attribute__((aligned(RT_ALIGN_SIZE)))
static char thread2_stack[2048];
static struct rt_thread thread2;
/* 线程 2 入口 */
static void thread2_completion_done(void *param)
{
rt_kprintf("thread2: completion done\n");
rt_completion_done(&completion);
rt_kprintf("thread2 leave.\n");
}
int completion_sample(void)
{
/* 初始化完成量对象 */
rt_completion_init(&completion);
rt_thread_init(&thread1,
"thread1",
thread1_completion_wait,
RT_NULL,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
rt_thread_startup(&thread1);
rt_thread_init(&thread2,
"thread2",
thread2_completion_done,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
rt_thread_startup(&thread2);
return 0;
}

View File

@@ -14,13 +14,16 @@ static struct rt_thread main_rtt;
int main(void)
{
printf("test\n");
main_rtt.th = pthread_self();
rt_thread_bind_mkrtos(&main_rtt);
/* init board */
rt_hw_board_init();
dfs_init();
#if 0
rtthread_drv_test();
#endif
completion_sample();
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);

View File

@@ -57,6 +57,8 @@ rt_err_t rt_thread_yield(void)
return 0;
}
static umword_t intr_status = 0;
static bool_t to_suspend;
static bool_t to_run;
rt_base_t rt_hw_interrupt_disable(void)
{
rt_base_t old = intr_status;
@@ -72,7 +74,31 @@ rt_base_t rt_hw_interrupt_disable(void)
}
void rt_hw_interrupt_enable(rt_base_t level)
{
rt_thread_t thread;
thread = rt_thread_self();
if (level == 0)
{
if (to_run)
{
to_run = 0;
pthread_mutex_unlock(&thread->suspend_lock);
u_sleep_ms(1);
pthread_mutex_lock(&thread->suspend_lock);
}
}
intr_status = level;
if (level == 0)
{
if (to_suspend)
{
thread->stat = RT_THREAD_SUSPEND;
to_suspend = 0;
pthread_mutex_lock(&thread->suspend_lock);
thread->stat = 0;
}
}
}
rt_bool_t rt_hw_interrupt_is_disabled(void)
{
@@ -113,32 +139,81 @@ rt_err_t rt_thread_bind_mkrtos(rt_thread_t th)
th->tlist.next = th->tlist.prev = &th->tlist;
th->stat = 0;
thread_msg_buf_get(-1, (umword_t *)(&i_msg), NULL);
thread_msg_buf_get(pthread_hd_get(th->th), (umword_t *)(&i_msg), NULL);
i_msg->user[1] = (umword_t)th;
}
rt_err_t rt_thread_init(struct rt_thread *thread,
const char *name,
void (*entry)(void *parameter),
void *parameter,
void *stack_start,
rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstack(&attr, stack_start, stack_size);
pthread_attr_set_flag(&attr, PTHREAD_DONT_RUN);
int ret = pthread_create(&thread->th, &attr, (void *(*)(void *))entry, NULL);
if (ret >= 0)
{
rt_thread_bind_mkrtos(thread);
}
return ret;
}
rt_err_t rt_thread_startup(rt_thread_t thread)
{
printf("[drv]%s:%d\n", __func__, __LINE__);
thread_run(pthread_hd_get(thread->th), 2);
return 0;
}
rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
{
//! 这里锁两次,第二次加锁将会导致挂起
printf("[drv]%s:%d\n", __func__, __LINE__);
thread->stat = RT_THREAD_SUSPEND;
pthread_mutex_lock(&thread->suspend_lock);
thread->stat = 0;
if (!rt_hw_interrupt_is_disabled())
{
printf("[drv]%s:%d\n", __func__, __LINE__);
thread->stat = RT_THREAD_SUSPEND;
pthread_mutex_lock(&thread->suspend_lock);
thread->stat = 0;
}
else
{
to_suspend = 1;
}
return 0;
}
rt_err_t rt_thread_suspend(rt_thread_t thread)
{
//! 这里锁两次,第二次加锁将会导致挂起
printf("[drv]%s:%d\n", __func__, __LINE__);
thread->stat = RT_THREAD_SUSPEND;
pthread_mutex_lock(&thread->suspend_lock);
thread->stat = 0;
if (!rt_hw_interrupt_is_disabled())
{
printf("[drv]%s:%d\n", __func__, __LINE__);
thread->stat = RT_THREAD_SUSPEND;
pthread_mutex_lock(&thread->suspend_lock);
thread->stat = 0;
}
else
{
to_suspend = 1;
}
return 0;
}
rt_err_t rt_thread_resume(rt_thread_t thread)
{
pthread_mutex_unlock(&thread->suspend_lock);
pthread_mutex_lock(&thread->suspend_lock);
printf("[drv]%s:%d\n", __func__, __LINE__);
if (!rt_hw_interrupt_is_disabled())
{
pthread_mutex_unlock(&thread->suspend_lock);
pthread_mutex_lock(&thread->suspend_lock);
printf("[drv]%s:%d\n", __func__, __LINE__);
}
else
{
to_run = 1;
}
return 0;
}

View File

@@ -36,8 +36,8 @@ int main(int argc, char *args[])
thread_press_test();
kobj_create_press_test();
ipc_test();
pthread_cond_lock_test();
pthread_lock_test();
pthread_cond_lock_test();
#endif
uenv_t env = *u_get_global_env();
obj_handler_t ipc_hd;