From a3541b2a243bdf1cc49029b696b217ce752199c7 Mon Sep 17 00:00:00 2001 From: liukangcc Date: Fri, 3 Sep 2021 11:38:48 +0800 Subject: [update] support armclang and c++11. --- .../libc/compilers/common/none-gcc/sys/types.h | 1 + components/libc/compilers/common/sys/time.h | 7 +-- components/libc/pthreads/pthread.c | 55 ++++++++++++++++++---- components/libc/pthreads/pthread.h | 5 ++ 4 files changed, 57 insertions(+), 11 deletions(-) (limited to 'components/libc') diff --git a/components/libc/compilers/common/none-gcc/sys/types.h b/components/libc/compilers/common/none-gcc/sys/types.h index e9f44f7762..8dfffcef97 100644 --- a/components/libc/compilers/common/none-gcc/sys/types.h +++ b/components/libc/compilers/common/none-gcc/sys/types.h @@ -25,6 +25,7 @@ typedef signed int ssize_t; /* Used for a count of bytes or an error #else typedef long signed int ssize_t; /* Used for a count of bytes or an error indication. */ #endif +typedef long suseconds_t; /* microseconds. */ typedef unsigned long useconds_t; /* microseconds (unsigned) */ typedef unsigned long dev_t; diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index c2320429e0..95605d47c7 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -48,9 +48,10 @@ rt_int8_t rt_tz_is_dst(void); #ifndef _TIMEVAL_DEFINED #define _TIMEVAL_DEFINED #if !(defined(_WIN32)) -struct timeval { - long tv_sec; /* seconds */ - long tv_usec; /* and microseconds */ +struct timeval +{ + time_t tv_sec; /* seconds */ + suseconds_t tv_usec; /* and microseconds */ }; #endif #endif /* _TIMEVAL_DEFINED */ diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c index 79104d1fd5..3df51caca6 100644 --- a/components/libc/pthreads/pthread.c +++ b/components/libc/pthreads/pthread.c @@ -61,7 +61,7 @@ pthread_t _pthread_data_create(void) memset(ptd, 0x0, sizeof(_pthread_data_t)); ptd->canceled = 0; - ptd->cancelstate = PTHREAD_CANCEL_DISABLE; + ptd->cancelstate = PTHREAD_CANCEL_ENABLE; ptd->canceltype = PTHREAD_CANCEL_DEFERRED; ptd->magic = PTHREAD_MAGIC; @@ -90,9 +90,30 @@ void _pthread_data_destroy(pthread_t pth) { RT_DECLARE_SPINLOCK(pth_lock); + extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX]; _pthread_data_t *ptd = _pthread_get_data(pth); if (ptd) { + /* destruct thread local key */ + if (ptd->tls != RT_NULL) + { + void *data; + rt_uint32_t index; + for (index = 0; index < PTHREAD_KEY_MAX; index ++) + { + if (_thread_keys[index].is_used) + { + data = ptd->tls[index]; + if (data && _thread_keys[index].destructor) + _thread_keys[index].destructor(data); + } + } + + /* release tls area */ + rt_free(ptd->tls); + ptd->tls = RT_NULL; + } + /* remove from pthread table */ rt_hw_spin_lock(&pth_lock); pth_table[pth] = NULL; @@ -103,13 +124,17 @@ void _pthread_data_destroy(pthread_t pth) rt_sem_delete(ptd->joinable_sem); /* release thread resource */ - if (ptd->attr.stackaddr == RT_NULL && ptd->tid->stack_addr != RT_NULL) + if (ptd->attr.stackaddr == RT_NULL) { /* release thread allocated stack */ - rt_free(ptd->tid->stack_addr); + if (ptd->tid) + { + rt_free(ptd->tid->stack_addr); + } } /* clean stack addr pointer */ - ptd->tid->stack_addr = RT_NULL; + if (ptd->tid) + ptd->tid->stack_addr = RT_NULL; /* * if this thread create the local thread data, @@ -221,6 +246,12 @@ int pthread_create(pthread_t *pid, pthread_attr_init(&ptd->attr); } + if (ptd->attr.stacksize == 0) + { + ret = EINVAL; + goto __exit; + } + rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++); /* pthread is a static thread object */ @@ -269,7 +300,7 @@ int pthread_create(pthread_t *pid, /* initial this pthread to system */ if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, stack, ptd->attr.stacksize, - ptd->attr.schedparam.sched_priority, 5) != RT_EOK) + ptd->attr.schedparam.sched_priority, 20) != RT_EOK) { ret = EINVAL; goto __exit; @@ -460,7 +491,7 @@ void pthread_exit(void *value) if (_thread_keys[index].is_used) { data = ptd->tls[index]; - if (data) + if (data && _thread_keys[index].destructor) _thread_keys[index].destructor(data); } } @@ -507,14 +538,21 @@ int pthread_kill(pthread_t thread, int sig) { #ifdef RT_USING_SIGNALS _pthread_data_t *ptd; + int ret; ptd = _pthread_get_data(thread); if (ptd) { - return rt_thread_kill(ptd->tid, sig); + ret = rt_thread_kill(ptd->tid, sig); + if (ret == -RT_EINVAL) + { + return EINVAL; + } + + return ret; } - return EINVAL; + return ESRCH; #else return ENOSYS; #endif @@ -707,3 +745,4 @@ int pthread_cancel(pthread_t thread) return 0; } RTM_EXPORT(pthread_cancel); + diff --git a/components/libc/pthreads/pthread.h b/components/libc/pthreads/pthread.h index 8639c1604b..177b2539c5 100644 --- a/components/libc/pthreads/pthread.h +++ b/components/libc/pthreads/pthread.h @@ -265,6 +265,11 @@ int pthread_barrier_init(pthread_barrier_t *barrier, int pthread_barrier_wait(pthread_barrier_t *barrier); +int pthread_setspecific(pthread_key_t key, const void *value); +void *pthread_getspecific(pthread_key_t key); +int pthread_key_create(pthread_key_t *key, void (*destructor)(void *)); +int pthread_key_delete(pthread_key_t key); + #ifdef __cplusplus } #endif -- cgit v1.3.1