summaryrefslogtreecommitdiff
path: root/components/libc/pthreads
diff options
context:
space:
mode:
authorguo <[email protected]>2021-09-28 10:36:54 +0800
committerGitHub <[email protected]>2021-09-28 10:36:54 +0800
commit55a302cd092758a44b589d65470a69d9e8ae4ced (patch)
treee60fc53994c426007e9eb4877e5bd123f124ef39 /components/libc/pthreads
parent00727d6b6db92dc1cf98f028f26fd6168594b905 (diff)
parent00d42ef73cff61ecda7ff006b077a73e1a4ff669 (diff)
Merge branch 'master' into usbh_construct
Diffstat (limited to 'components/libc/pthreads')
-rw-r--r--components/libc/pthreads/mqueue.c5
-rw-r--r--components/libc/pthreads/mqueue.h2
-rw-r--r--components/libc/pthreads/posix_types.h13
-rw-r--r--components/libc/pthreads/pthread.c78
-rw-r--r--components/libc/pthreads/pthread.h9
-rw-r--r--components/libc/pthreads/pthread_attr.c4
-rw-r--r--components/libc/pthreads/pthread_barrier.c2
-rw-r--r--components/libc/pthreads/pthread_cond.c12
-rw-r--r--components/libc/pthreads/pthread_internal.h2
-rw-r--r--components/libc/pthreads/pthread_mutex.c4
-rw-r--r--components/libc/pthreads/pthread_rwlock.c10
-rw-r--r--components/libc/pthreads/pthread_spin.c2
-rw-r--r--components/libc/pthreads/pthread_tls.c2
-rw-r--r--components/libc/pthreads/sched.c2
-rw-r--r--components/libc/pthreads/sched.h2
-rw-r--r--components/libc/pthreads/semaphore.c6
-rw-r--r--components/libc/pthreads/semaphore.h2
17 files changed, 107 insertions, 50 deletions
diff --git a/components/libc/pthreads/mqueue.c b/components/libc/pthreads/mqueue.c
index 003b49b9a0..6840164a68 100644
--- a/components/libc/pthreads/mqueue.c
+++ b/components/libc/pthreads/mqueue.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -8,6 +8,7 @@
*/
#include <string.h>
+#include <sys/signal.h>
#include "mqueue.h"
#include "pthread_internal.h"
@@ -243,7 +244,7 @@ ssize_t mq_timedreceive(mqd_t mqdes,
return -1;
}
- tick = clock_time_to_tick(abs_timeout);
+ tick = rt_timespec_to_tick(abs_timeout);
result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, tick);
if (result == RT_EOK)
diff --git a/components/libc/pthreads/mqueue.h b/components/libc/pthreads/mqueue.h
index 5cadde3ae8..5152bfad29 100644
--- a/components/libc/pthreads/mqueue.h
+++ b/components/libc/pthreads/mqueue.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/posix_types.h b/components/libc/pthreads/posix_types.h
index ca20464bb5..5838e8b370 100644
--- a/components/libc/pthreads/posix_types.h
+++ b/components/libc/pthreads/posix_types.h
@@ -1,11 +1,11 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
- * 2013-12-23 Bernard Add the checking for ESHUTDOWN
+ * 2013-12-23 Bernard Add the checking for ESHUTDOWN
*/
#ifndef __POSIX_TYPES_H__
@@ -19,16 +19,9 @@
#include <sys/types.h>
#include <sys/time.h>
-
-/* errno for Keil MDK */
-#if defined(__CC_ARM) || defined(__IAR_SYSTEMS_ICC__)
-#include <sys/errno.h>
-#include <sys/unistd.h>
-#else
#include <unistd.h>
-#include <errno.h>
+#include <sys/errno.h>
#include <fcntl.h>
-#endif
#endif
diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c
index 7430510bf2..b0bf39dff6 100644
--- a/components/libc/pthreads/pthread.c
+++ b/components/libc/pthreads/pthread.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -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,
@@ -201,7 +226,7 @@ int pthread_create(pthread_t *pid,
/* allocate posix thread data */
pth_id = _pthread_data_create();
- if (pth_id == PTHREAD_NUM_MAX)
+ if (pth_id == PTHREAD_NUM_MAX)
{
ret = ENOMEM;
goto __exit;
@@ -209,6 +234,8 @@ int pthread_create(pthread_t *pid,
/* get pthread data */
ptd = _pthread_get_data(pth_id);
+ RT_ASSERT(ptd != RT_NULL);
+
if (attr != RT_NULL)
{
ptd->attr = *attr;
@@ -219,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 */
@@ -267,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;
@@ -299,6 +332,12 @@ int pthread_detach(pthread_t thread)
{
int ret = 0;
_pthread_data_t *ptd = _pthread_get_data(thread);
+ if (ptd == RT_NULL)
+ {
+ /* invalid pthread id */
+ ret = EINVAL;
+ goto __exit;
+ }
rt_enter_critical();
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
@@ -360,6 +399,12 @@ int pthread_join(pthread_t thread, void **value_ptr)
rt_err_t result;
ptd = _pthread_get_data(thread);
+
+ if (ptd == RT_NULL)
+ {
+ return EINVAL; /* invalid pthread id */
+ }
+
if (ptd && ptd->tid == rt_thread_self())
{
/* join self */
@@ -367,7 +412,9 @@ int pthread_join(pthread_t thread, void **value_ptr)
}
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
+ {
return EINVAL; /* join on a detached pthread */
+ }
result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
if (result == RT_EOK)
@@ -444,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);
}
}
@@ -491,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
@@ -662,7 +716,10 @@ int pthread_cancel(pthread_t thread)
/* get posix thread data */
ptd = _pthread_get_data(thread);
- RT_ASSERT(ptd != RT_NULL);
+ if (ptd == RT_NULL)
+ {
+ return EINVAL;
+ }
/* cancel self */
if (ptd->tid == rt_thread_self())
@@ -688,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 7bb72b726c..177b2539c5 100644
--- a/components/libc/pthreads/pthread.h
+++ b/components/libc/pthreads/pthread.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -161,7 +161,7 @@ int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size);
int pthread_attr_setscope(pthread_attr_t *attr, int scope);
int pthread_attr_getscope(pthread_attr_t const *attr);
int pthread_system_init(void);
-int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
+int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
void *(*start) (void *), void *arg);
int pthread_detach (pthread_t thread);
@@ -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
diff --git a/components/libc/pthreads/pthread_attr.c b/components/libc/pthreads/pthread_attr.c
index 2294599bf3..4aedf5a90d 100644
--- a/components/libc/pthreads/pthread_attr.c
+++ b/components/libc/pthreads/pthread_attr.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -16,7 +16,7 @@
#define DEFAULT_STACK_SIZE 2048
#define DEFAULT_PRIORITY (RT_THREAD_PRIORITY_MAX/2 + RT_THREAD_PRIORITY_MAX/4)
-const pthread_attr_t pthread_default_attr =
+const pthread_attr_t pthread_default_attr =
{
0, /* stack base */
DEFAULT_STACK_SIZE, /* stack size */
diff --git a/components/libc/pthreads/pthread_barrier.c b/components/libc/pthreads/pthread_barrier.c
index d76fe7f13f..1a5b7fa3f9 100644
--- a/components/libc/pthreads/pthread_barrier.c
+++ b/components/libc/pthreads/pthread_barrier.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/pthread_cond.c b/components/libc/pthreads/pthread_cond.c
index 38dc697469..9bf49704ed 100644
--- a/components/libc/pthreads/pthread_cond.c
+++ b/components/libc/pthreads/pthread_cond.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -84,10 +84,10 @@ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
- if (attr == RT_NULL) /* use default value */
- cond->attr = PTHREAD_PROCESS_PRIVATE;
- else
- cond->attr = *attr;
+ if (attr == RT_NULL) /* use default value */
+ cond->attr = PTHREAD_PROCESS_PRIVATE;
+ else
+ cond->attr = *attr;
result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
if (result != RT_EOK)
@@ -222,7 +222,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond,
int timeout;
rt_err_t result;
- timeout = clock_time_to_tick(abstime);
+ timeout = rt_timespec_to_tick(abstime);
result = _pthread_cond_timedwait(cond, mutex, timeout);
if (result == RT_EOK)
return 0;
diff --git a/components/libc/pthreads/pthread_internal.h b/components/libc/pthreads/pthread_internal.h
index 240741e21a..1fce29f636 100644
--- a/components/libc/pthreads/pthread_internal.h
+++ b/components/libc/pthreads/pthread_internal.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/pthread_mutex.c b/components/libc/pthreads/pthread_mutex.c
index 8f96531782..a681380b11 100644
--- a/components/libc/pthreads/pthread_mutex.c
+++ b/components/libc/pthreads/pthread_mutex.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -211,7 +211,7 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex)
result = rt_mutex_release(&(mutex->lock));
if (result == RT_EOK)
return 0;
-
+
return EINVAL;
}
RTM_EXPORT(pthread_mutex_unlock);
diff --git a/components/libc/pthreads/pthread_rwlock.c b/components/libc/pthreads/pthread_rwlock.c
index 64a0e221b4..1c2fe19f5f 100644
--- a/components/libc/pthreads/pthread_rwlock.c
+++ b/components/libc/pthreads/pthread_rwlock.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -60,7 +60,7 @@ int pthread_rwlock_init(pthread_rwlock_t *rwlock,
pthread_mutex_init(&(rwlock->rw_mutex), NULL);
pthread_cond_init(&(rwlock->rw_condreaders), NULL);
pthread_cond_init(&(rwlock->rw_condwriters), NULL);
-
+
rwlock->rw_nwaitwriters = 0;
rwlock->rw_nwaitreaders = 0;
rwlock->rw_refcount = 0;
@@ -117,7 +117,7 @@ int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
pthread_mutex_unlock(&rwlock->rw_mutex);
if (result == 0)
pthread_mutex_destroy(&rwlock->rw_mutex);
-
+
return result;
}
RTM_EXPORT(pthread_rwlock_destroy);
@@ -234,7 +234,7 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
result = pthread_cond_timedwait(&rwlock->rw_condwriters, &rwlock->rw_mutex, abstime);
/* rw_mutex should have been taken again when returned from waiting */
rwlock->rw_nwaitwriters--;
-
+
if (result != 0)
break;
}
@@ -324,7 +324,7 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
result = pthread_cond_wait(&rwlock->rw_condwriters, &rwlock->rw_mutex);
/* rw_mutex should have been taken again when returned from waiting */
rwlock->rw_nwaitwriters--;
-
+
if (result != 0)
break;
}
diff --git a/components/libc/pthreads/pthread_spin.c b/components/libc/pthreads/pthread_spin.c
index b31f4f6393..3f3dc1593c 100644
--- a/components/libc/pthreads/pthread_spin.c
+++ b/components/libc/pthreads/pthread_spin.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/pthread_tls.c b/components/libc/pthreads/pthread_tls.c
index 4b97d796d9..d48df7f2dd 100644
--- a/components/libc/pthreads/pthread_tls.c
+++ b/components/libc/pthreads/pthread_tls.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/sched.c b/components/libc/pthreads/sched.c
index a1bf50985e..5f2a53c01f 100644
--- a/components/libc/pthreads/sched.c
+++ b/components/libc/pthreads/sched.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/sched.h b/components/libc/pthreads/sched.h
index dbaf66edc2..07e2c0c6a8 100644
--- a/components/libc/pthreads/sched.h
+++ b/components/libc/pthreads/sched.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
diff --git a/components/libc/pthreads/semaphore.c b/components/libc/pthreads/semaphore.c
index 2ed93e99b6..5e59ae850f 100644
--- a/components/libc/pthreads/semaphore.c
+++ b/components/libc/pthreads/semaphore.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -186,7 +186,7 @@ int sem_init(sem_t *sem, int pshared, unsigned int value)
rt_snprintf(name, sizeof(name), "psem%02d", psem_number++);
sem->sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
- if (sem == RT_NULL)
+ if (sem->sem == RT_NULL)
{
rt_set_errno(ENOMEM);
@@ -319,7 +319,7 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
return EINVAL;
/* calculate os tick */
- tick = clock_time_to_tick(abs_timeout);
+ tick = rt_timespec_to_tick(abs_timeout);
result = rt_sem_take(sem->sem, tick);
if (result == -RT_ETIMEOUT)
diff --git a/components/libc/pthreads/semaphore.h b/components/libc/pthreads/semaphore.h
index d135ae9261..872226e385 100644
--- a/components/libc/pthreads/semaphore.h
+++ b/components/libc/pthreads/semaphore.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*