summaryrefslogtreecommitdiff
path: root/components/libc
diff options
context:
space:
mode:
authorarmink <[email protected]>2019-05-17 16:36:34 +0800
committerarmink <[email protected]>2019-05-17 17:07:14 +0800
commitbf4a51b599b56cddb93f7f2ad576f6f0d022a64e (patch)
treee9c54e3d37a1d3342d1bf19c44384b6c38703fac /components/libc
parent94e3f77f25249b52c6f72921a2d2564f68bcd6ff (diff)
Update the kernel and components to master version.
Diffstat (limited to 'components/libc')
-rw-r--r--components/libc/Kconfig6
-rw-r--r--components/libc/compilers/armlibc/time.c17
-rw-r--r--components/libc/compilers/dlib/time.c17
-rw-r--r--components/libc/compilers/minilibc/sys/types.h1
-rw-r--r--components/libc/compilers/minilibc/time.c305
-rw-r--r--components/libc/compilers/newlib/syscalls.c8
-rw-r--r--components/libc/compilers/newlib/time.c17
-rw-r--r--components/libc/libdl/dlelf.c4
-rw-r--r--components/libc/libdl/dlmodule.c8
-rw-r--r--components/libc/pthreads/pthread.c301
-rw-r--r--components/libc/pthreads/pthread.h30
-rw-r--r--components/libc/pthreads/pthread_attr.c29
-rw-r--r--components/libc/pthreads/pthread_cond.c2
-rw-r--r--components/libc/pthreads/pthread_internal.h16
-rw-r--r--components/libc/pthreads/pthread_tls.c10
15 files changed, 451 insertions, 320 deletions
diff --git a/components/libc/Kconfig b/components/libc/Kconfig
index b2abe69850..43d9330102 100644
--- a/components/libc/Kconfig
+++ b/components/libc/Kconfig
@@ -8,6 +8,12 @@ config RT_USING_PTHREADS
bool "Enable pthreads APIs"
default n
+if RT_USING_PTHREADS
+ config PTHREAD_NUM_MAX
+ int "Maximum number of pthreads"
+ default 8
+endif
+
if RT_USING_LIBC && RT_USING_DFS
config RT_USING_POSIX
bool "Enable POSIX layer for poll/select, stdin etc"
diff --git a/components/libc/compilers/armlibc/time.c b/components/libc/compilers/armlibc/time.c
index 29c5c6cdd2..f099d51330 100644
--- a/components/libc/compilers/armlibc/time.c
+++ b/components/libc/compilers/armlibc/time.c
@@ -16,19 +16,16 @@ int gettimeofday(struct timeval *tp, void *ignore)
rt_device_t device;
device = rt_device_find("rtc");
- if (device != RT_NULL)
- {
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
- if (tp != RT_NULL)
- {
- tp->tv_sec = time;
- tp->tv_usec = 0;
- }
+ RT_ASSERT(device != RT_NULL);
- return time;
+ rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
+ if (tp != RT_NULL)
+ {
+ tp->tv_sec = time;
+ tp->tv_usec = 0;
}
- return 0;
+ return time;
}
#endif
diff --git a/components/libc/compilers/dlib/time.c b/components/libc/compilers/dlib/time.c
index 87574df851..df35313b0e 100644
--- a/components/libc/compilers/dlib/time.c
+++ b/components/libc/compilers/dlib/time.c
@@ -16,19 +16,16 @@ int gettimeofday(struct timeval *tp, void *ignore)
rt_device_t device;
device = rt_device_find("rtc");
- if (device != RT_NULL)
- {
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
- if (tp != RT_NULL)
- {
- tp->tv_sec = time;
- tp->tv_usec = 0;
- }
+ RT_ASSERT(device != RT_NULL);
- return time;
+ rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
+ if (tp != RT_NULL)
+ {
+ tp->tv_sec = time;
+ tp->tv_usec = 0;
}
- return 0;
+ return time;
}
#endif
diff --git a/components/libc/compilers/minilibc/sys/types.h b/components/libc/compilers/minilibc/sys/types.h
index 0c00a79d1e..460baab623 100644
--- a/components/libc/compilers/minilibc/sys/types.h
+++ b/components/libc/compilers/minilibc/sys/types.h
@@ -8,6 +8,7 @@
*/
#ifndef __TYPES_H__
#define __TYPES_H__
+#include <rtconfig.h>
typedef long off_t;
typedef unsigned long size_t;
diff --git a/components/libc/compilers/minilibc/time.c b/components/libc/compilers/minilibc/time.c
index 508558bafc..25e8f0e310 100644
--- a/components/libc/compilers/minilibc/time.c
+++ b/components/libc/compilers/minilibc/time.c
@@ -11,20 +11,20 @@
/* days per month -- nonleap! */
const short __spm[13] =
- { 0,
+{ 0,
(31),
- (31+28),
- (31+28+31),
- (31+28+31+30),
- (31+28+31+30+31),
- (31+28+31+30+31+30),
- (31+28+31+30+31+30+31),
- (31+28+31+30+31+30+31+31),
- (31+28+31+30+31+30+31+31+30),
- (31+28+31+30+31+30+31+31+30+31),
- (31+28+31+30+31+30+31+31+30+31+30),
- (31+28+31+30+31+30+31+31+30+31+30+31),
- };
+ (31 + 28),
+ (31 + 28 + 31),
+ (31 + 28 + 31 + 30),
+ (31 + 28 + 31 + 30 + 31),
+ (31 + 28 + 31 + 30 + 31 + 30),
+ (31 + 28 + 31 + 30 + 31 + 30 + 31),
+ (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31),
+ (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
+ (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
+ (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30),
+ (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31),
+};
static long int timezone;
static const char days[] = "Sun Mon Tue Wed Thu Fri Sat ";
static const char months[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
@@ -34,196 +34,193 @@ static const char months[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
int __isleap(int year)
{
- /* every fourth year is a leap year except for century years that are
- * not divisible by 400. */
- /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
- return (!(year % 4) && ((year % 100) || !(year % 400)));
+ /* every fourth year is a leap year except for century years that are
+ * not divisible by 400. */
+ /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
+ return (!(year % 4) && ((year % 100) || !(year % 400)));
}
struct tm *gmtime_r(const time_t *timep, struct tm *r)
{
- time_t i;
- register time_t work = *timep % (SPD);
- r->tm_sec = work % 60;
- work /= 60;
- r->tm_min = work % 60;
- r->tm_hour = work / 60;
- work = *timep / (SPD);
- r->tm_wday = (4 + work) % 7;
- for (i = 1970;; ++i)
- {
- register time_t k = __isleap(i) ? 366 : 365;
- if (work >= k)
- work -= k;
- else
- break;
- }
- r->tm_year = i - 1900;
- r->tm_yday = work;
+ time_t i;
+ register time_t work = *timep % (SPD);
+ r->tm_sec = work % 60;
+ work /= 60;
+ r->tm_min = work % 60;
+ r->tm_hour = work / 60;
+ work = *timep / (SPD);
+ r->tm_wday = (4 + work) % 7;
+ for (i = 1970;; ++i)
+ {
+ register time_t k = __isleap(i) ? 366 : 365;
+ if (work >= k)
+ work -= k;
+ else
+ break;
+ }
+ r->tm_year = i - 1900;
+ r->tm_yday = work;
- r->tm_mday = 1;
- if (__isleap(i) && (work > 58))
- {
- if (work == 59)
- r->tm_mday = 2; /* 29.2. */
- work -= 1;
- }
+ r->tm_mday = 1;
+ if (__isleap(i) && (work > 58))
+ {
+ if (work == 59)
+ r->tm_mday = 2; /* 29.2. */
+ work -= 1;
+ }
- for (i = 11; i && (__spm[i] > work); --i)
- ;
- r->tm_mon = i;
- r->tm_mday += work - __spm[i];
- return r;
+ for (i = 11; i && (__spm[i] > work); --i)
+ ;
+ r->tm_mon = i;
+ r->tm_mday += work - __spm[i];
+ return r;
}
struct tm* localtime_r(const time_t* t, struct tm* r)
{
- time_t tmp;
- struct timezone tz = {0};
- gettimeofday(0, &tz);
- timezone = tz.tz_minuteswest * 60L;
- tmp = *t + timezone;
- return gmtime_r(&tmp, r);
+ time_t tmp;
+ struct timezone tz = {0};
+ gettimeofday(0, &tz);
+ timezone = tz.tz_minuteswest * 60L;
+ tmp = *t + timezone;
+ return gmtime_r(&tmp, r);
}
struct tm* localtime(const time_t* t)
{
- static struct tm tmp;
- return localtime_r(t, &tmp);
+ static struct tm tmp;
+ return localtime_r(t, &tmp);
}
time_t mktime(struct tm * const t)
{
- register time_t day;
- register time_t i;
- register time_t years = t->tm_year - 70;
+ register time_t day;
+ register time_t i;
+ register time_t years = t->tm_year - 70;
- if (t->tm_sec > 60)
- {
- t->tm_min += t->tm_sec / 60;
- t->tm_sec %= 60;
- }
- if (t->tm_min > 60)
- {
- t->tm_hour += t->tm_min / 60;
- t->tm_min %= 60;
- }
- if (t->tm_hour > 24)
- {
- t->tm_mday += t->tm_hour / 24;
- t->tm_hour %= 24;
- }
- if (t->tm_mon > 12)
- {
- t->tm_year += t->tm_mon / 12;
- t->tm_mon %= 12;
- }
- while (t->tm_mday > __spm[1 + t->tm_mon])
- {
- if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
- {
- --t->tm_mday;
- }
- t->tm_mday -= __spm[t->tm_mon];
- ++t->tm_mon;
- if (t->tm_mon > 11)
- {
- t->tm_mon = 0;
- ++t->tm_year;
- }
- }
+ if (t->tm_sec > 60)
+ {
+ t->tm_min += t->tm_sec / 60;
+ t->tm_sec %= 60;
+ }
+ if (t->tm_min > 60)
+ {
+ t->tm_hour += t->tm_min / 60;
+ t->tm_min %= 60;
+ }
+ if (t->tm_hour > 24)
+ {
+ t->tm_mday += t->tm_hour / 24;
+ t->tm_hour %= 24;
+ }
+ if (t->tm_mon > 12)
+ {
+ t->tm_year += t->tm_mon / 12;
+ t->tm_mon %= 12;
+ }
+ while (t->tm_mday > __spm[1 + t->tm_mon])
+ {
+ if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
+ {
+ --t->tm_mday;
+ }
+ t->tm_mday -= __spm[t->tm_mon];
+ ++t->tm_mon;
+ if (t->tm_mon > 11)
+ {
+ t->tm_mon = 0;
+ ++t->tm_year;
+ }
+ }
- if (t->tm_year < 70)
- return (time_t) -1;
+ if (t->tm_year < 70)
+ return (time_t) - 1;
- /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
- day = years * 365 + (years + 1) / 4;
+ /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
+ day = years * 365 + (years + 1) / 4;
- /* After 2100 we have to substract 3 leap years for every 400 years
- This is not intuitive. Most mktime implementations do not support
- dates after 2059, anyway, so we might leave this out for it's
- bloat. */
- if (years >= 131)
- {
- years -= 131;
- years /= 100;
- day -= (years >> 2) * 3 + 1;
- if ((years &= 3) == 3)
- years--;
- day -= years;
- }
+ /* After 2100 we have to substract 3 leap years for every 400 years
+ This is not intuitive. Most mktime implementations do not support
+ dates after 2059, anyway, so we might leave this out for it's
+ bloat. */
+ if (years >= 131)
+ {
+ years -= 131;
+ years /= 100;
+ day -= (years >> 2) * 3 + 1;
+ if ((years &= 3) == 3)
+ years--;
+ day -= years;
+ }
- day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
- (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
+ day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
+ (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
- /* day is now the number of days since 'Jan 1 1970' */
- i = 7;
- t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
+ /* day is now the number of days since 'Jan 1 1970' */
+ i = 7;
+ t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
- i = 24;
- day *= i;
- i = 60;
- return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
+ i = 24;
+ day *= i;
+ i = 60;
+ return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
}
static void num2str(char *c, int i)
{
- c[0] = i / 10 + '0';
- c[1] = i % 10 + '0';
+ c[0] = i / 10 + '0';
+ c[1] = i % 10 + '0';
}
char *asctime_r(const struct tm *t, char *buf)
{
- /* "Wed Jun 30 21:49:08 1993\n" */
- *(int*) buf = *(int*) (days + (t->tm_wday << 2));
- *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
- num2str(buf + 8, t->tm_mday);
- if (buf[8] == '0')
- buf[8] = ' ';
- buf[10] = ' ';
- num2str(buf + 11, t->tm_hour);
- buf[13] = ':';
- num2str(buf + 14, t->tm_min);
- buf[16] = ':';
- num2str(buf + 17, t->tm_sec);
- buf[19] = ' ';
- num2str(buf + 20, (t->tm_year + 1900) / 100);
- num2str(buf + 22, (t->tm_year + 1900) % 100);
- buf[24] = '\n';
- return buf;
+ /* "Wed Jun 30 21:49:08 1993\n" */
+ *(int*) buf = *(int*) (days + (t->tm_wday << 2));
+ *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
+ num2str(buf + 8, t->tm_mday);
+ if (buf[8] == '0')
+ buf[8] = ' ';
+ buf[10] = ' ';
+ num2str(buf + 11, t->tm_hour);
+ buf[13] = ':';
+ num2str(buf + 14, t->tm_min);
+ buf[16] = ':';
+ num2str(buf + 17, t->tm_sec);
+ buf[19] = ' ';
+ num2str(buf + 20, (t->tm_year + 1900) / 100);
+ num2str(buf + 22, (t->tm_year + 1900) % 100);
+ buf[24] = '\n';
+ return buf;
}
char *asctime(const struct tm *timeptr)
{
- static char buf[25];
- return asctime_r(timeptr, buf);
+ static char buf[25];
+ return asctime_r(timeptr, buf);
}
char *ctime(const time_t *timep)
{
- return asctime(localtime(timep));
+ return asctime(localtime(timep));
}
#ifdef RT_USING_DEVICE
int gettimeofday(struct timeval *tp, void *ignore)
{
- time_t time;
- rt_device_t device;
+ time_t time;
+ rt_device_t device;
- device = rt_device_find("rtc");
- if (device != RT_NULL)
- {
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
- if (tp != RT_NULL)
- {
- tp->tv_sec = time;
- tp->tv_usec = 0;
- }
+ device = rt_device_find("rtc");
+ RT_ASSERT(device != RT_NULL);
- return time;
- }
+ rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
+ if (tp != RT_NULL)
+ {
+ tp->tv_sec = time;
+ tp->tv_usec = 0;
+ }
- return 0;
+ return time;
}
#endif
diff --git a/components/libc/compilers/newlib/syscalls.c b/components/libc/compilers/newlib/syscalls.c
index 759feafe67..9919e11727 100644
--- a/components/libc/compilers/newlib/syscalls.c
+++ b/components/libc/compilers/newlib/syscalls.c
@@ -27,6 +27,14 @@
/* Reentrant versions of system calls. */
+#ifndef _REENT_ONLY
+int *
+__errno ()
+{
+ return _rt_errno();
+}
+#endif
+
int
_close_r(struct _reent *ptr, int fd)
{
diff --git a/components/libc/compilers/newlib/time.c b/components/libc/compilers/newlib/time.c
index 29c5c6cdd2..f099d51330 100644
--- a/components/libc/compilers/newlib/time.c
+++ b/components/libc/compilers/newlib/time.c
@@ -16,19 +16,16 @@ int gettimeofday(struct timeval *tp, void *ignore)
rt_device_t device;
device = rt_device_find("rtc");
- if (device != RT_NULL)
- {
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
- if (tp != RT_NULL)
- {
- tp->tv_sec = time;
- tp->tv_usec = 0;
- }
+ RT_ASSERT(device != RT_NULL);
- return time;
+ rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
+ if (tp != RT_NULL)
+ {
+ tp->tv_sec = time;
+ tp->tv_usec = 0;
}
- return 0;
+ return time;
}
#endif
diff --git a/components/libc/libdl/dlelf.c b/components/libc/libdl/dlelf.c
index 4557ee3458..1265ffcb07 100644
--- a/components/libc/libdl/dlelf.c
+++ b/components/libc/libdl/dlelf.c
@@ -11,8 +11,8 @@
#include "dlmodule.h"
#include "dlelf.h"
-#define DBG_SECTION_NAME "DLMD"
-#define DBG_LEVEL DBG_INFO
+#define DBG_TAG "DLMD"
+#define DBG_LVL DBG_INFO
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_ptr)
diff --git a/components/libc/libdl/dlmodule.c b/components/libc/libdl/dlmodule.c
index 0029c01728..dbebfc8961 100644
--- a/components/libc/libdl/dlmodule.c
+++ b/components/libc/libdl/dlmodule.c
@@ -16,8 +16,8 @@
#include <dfs_posix.h>
-#define DBG_SECTION_NAME "DLMD"
-#define DBG_LEVEL DBG_INFO
+#define DBG_TAG "DLMD"
+#define DBG_LVL DBG_INFO
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
@@ -442,6 +442,10 @@ struct rt_dlmodule* dlmodule_load(const char* filename)
close(fd);
fd = -1;
}
+ else
+ {
+ goto __exit;
+ }
/* check ELF header */
if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c
index 55f46c0caa..7ecf304038 100644
--- a/components/libc/pthreads/pthread.c
+++ b/components/libc/pthreads/pthread.c
@@ -10,10 +10,121 @@
* 2019-02-07 Bernard Add _pthread_destroy to release pthread resource.
*/
+#include <rthw.h>
#include <pthread.h>
#include <sched.h>
#include "pthread_internal.h"
+RT_DEFINE_SPINLOCK(pth_lock);
+_pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
+
+_pthread_data_t *_pthread_get_data(pthread_t thread)
+{
+ RT_DECLARE_SPINLOCK(pth_lock);
+ _pthread_data_t *ptd;
+
+ if (thread >= PTHREAD_NUM_MAX) return NULL;
+
+ rt_hw_spin_lock(&pth_lock);
+ ptd = pth_table[thread];
+ rt_hw_spin_unlock(&pth_lock);
+
+ if (ptd && ptd->magic == PTHREAD_MAGIC) return ptd;
+
+ return NULL;
+}
+
+pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
+{
+ int index;
+ RT_DECLARE_SPINLOCK(pth_lock);
+
+ rt_hw_spin_lock(&pth_lock);
+ for (index = 0; index < PTHREAD_NUM_MAX; index ++)
+ {
+ if (pth_table[index] == ptd) break;
+ }
+ rt_hw_spin_unlock(&pth_lock);
+
+ return index;
+}
+
+pthread_t _pthread_data_create(void)
+{
+ int index;
+ _pthread_data_t *ptd = NULL;
+ RT_DECLARE_SPINLOCK(pth_lock);
+
+ ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
+ if (!ptd) return PTHREAD_NUM_MAX;
+
+ memset(ptd, 0x0, sizeof(_pthread_data_t));
+ ptd->canceled = 0;
+ ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
+ ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
+ ptd->magic = PTHREAD_MAGIC;
+
+ rt_hw_spin_lock(&pth_lock);
+ for (index = 0; index < PTHREAD_NUM_MAX; index ++)
+ {
+ if (pth_table[index] == NULL)
+ {
+ pth_table[index] = ptd;
+ break;
+ }
+ }
+ rt_hw_spin_unlock(&pth_lock);
+
+ /* full of pthreads, clean magic and release ptd */
+ if (index == PTHREAD_NUM_MAX)
+ {
+ ptd->magic = 0x0;
+ rt_free(ptd);
+ }
+
+ return index;
+}
+
+void _pthread_data_destroy(pthread_t pth)
+{
+ RT_DECLARE_SPINLOCK(pth_lock);
+
+ _pthread_data_t *ptd = _pthread_get_data(pth);
+ if (ptd)
+ {
+ /* remove from pthread table */
+ rt_hw_spin_lock(&pth_lock);
+ pth_table[pth] = NULL;
+ rt_hw_spin_unlock(&pth_lock);
+
+ /* delete joinable semaphore */
+ if (ptd->joinable_sem != RT_NULL)
+ rt_sem_delete(ptd->joinable_sem);
+
+ /* release thread resource */
+ if (ptd->attr.stackaddr == RT_NULL)
+ {
+ /* release thread allocated stack */
+ rt_free(ptd->tid->stack_addr);
+ }
+ /* clean stack addr pointer */
+ ptd->tid->stack_addr = RT_NULL;
+
+ /*
+ * if this thread create the local thread data,
+ * delete it
+ */
+ if (ptd->tls != RT_NULL) rt_free(ptd->tls);
+ rt_free(ptd->tid);
+
+ /* clean magic */
+ ptd->magic = 0x0;
+
+ /* free ptd */
+ rt_free(ptd);
+ }
+}
+
int pthread_system_init(void)
{
/* initialize key area */
@@ -29,26 +140,11 @@ INIT_COMPONENT_EXPORT(pthread_system_init);
static void _pthread_destroy(_pthread_data_t *ptd)
{
- /* delete joinable semaphore */
- if (ptd->joinable_sem != RT_NULL)
- rt_sem_delete(ptd->joinable_sem);
-
- /* release thread resource */
- if (ptd->attr.stack_base == RT_NULL)
+ pthread_t pth = _pthread_data_get_pth(ptd);
+ if (pth != PTHREAD_NUM_MAX)
{
- /* release thread allocated stack */
- rt_free(ptd->tid->stack_addr);
+ _pthread_data_destroy(pth);
}
- /* clean stack addr pointer */
- ptd->tid->stack_addr = RT_NULL;
-
- /*
- * if this thread create the local thread data,
- * delete it
- */
- if (ptd->tls != RT_NULL) rt_free(ptd->tls);
- rt_free(ptd->tid);
- rt_free(ptd);
return;
}
@@ -56,7 +152,10 @@ static void _pthread_destroy(_pthread_data_t *ptd)
static void _pthread_cleanup(rt_thread_t tid)
{
_pthread_data_t *ptd;
- ptd = _pthread_get_data(tid);
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)tid->user_data;
+ RT_ASSERT(ptd != RT_NULL);
/* clear cleanup function */
tid->cleanup = RT_NULL;
@@ -84,29 +183,30 @@ static void pthread_entry_stub(void *parameter)
ptd->return_value = value;
}
-int pthread_create(pthread_t *tid,
+int pthread_create(pthread_t *pid,
const pthread_attr_t *attr,
void *(*start)(void *), void *parameter)
{
- int result;
+ int ret = 0;
void *stack;
char name[RT_NAME_MAX];
static rt_uint16_t pthread_number = 0;
+
+ pthread_t pth_id;
_pthread_data_t *ptd;
- /* tid shall be provided */
- RT_ASSERT(tid != RT_NULL);
+ /* pid shall be provided */
+ RT_ASSERT(pid != RT_NULL);
/* allocate posix thread data */
- ptd = (_pthread_data_t *)rt_malloc(sizeof(_pthread_data_t));
- if (ptd == RT_NULL)
- return ENOMEM;
- /* clean posix thread data memory */
- rt_memset(ptd, 0, sizeof(_pthread_data_t));
- ptd->canceled = 0;
- ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
- ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
- ptd->magic = PTHREAD_MAGIC;
+ pth_id = _pthread_data_create();
+ if (pth_id == PTHREAD_NUM_MAX)
+ {
+ ret = ENOMEM;
+ goto __exit;
+ }
+ /* get pthread data */
+ ptd = _pthread_get_data(pth_id);
if (attr != RT_NULL)
{
@@ -119,31 +219,27 @@ int pthread_create(pthread_t *tid,
}
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
- if (ptd->attr.stack_base == 0)
+ if (ptd->attr.stackaddr == 0)
{
- stack = (void *)rt_malloc(ptd->attr.stack_size);
+ stack = (void *)rt_malloc(ptd->attr.stacksize);
}
else
{
- stack = (void *)(ptd->attr.stack_base);
+ stack = (void *)(ptd->attr.stackaddr);
}
if (stack == RT_NULL)
{
- rt_free(ptd);
-
- return ENOMEM;
+ ret = ENOMEM;
+ goto __exit;
}
/* pthread is a static thread object */
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
if (ptd->tid == RT_NULL)
{
- if (ptd->attr.stack_base == 0)
- rt_free(stack);
- rt_free(ptd);
-
- return ENOMEM;
+ ret = ENOMEM;
+ goto __exit;
}
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
@@ -151,11 +247,8 @@ int pthread_create(pthread_t *tid,
ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
if (ptd->joinable_sem == RT_NULL)
{
- if (ptd->attr.stack_base != 0)
- rt_free(stack);
- rt_free(ptd);
-
- return ENOMEM;
+ ret = ENOMEM;
+ goto __exit;
}
}
else
@@ -169,40 +262,32 @@ int pthread_create(pthread_t *tid,
/* initial this pthread to system */
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
- stack, ptd->attr.stack_size,
- ptd->attr.priority, 5) != RT_EOK)
+ stack, ptd->attr.stacksize,
+ ptd->attr.schedparam.sched_priority, 5) != RT_EOK)
{
- if (ptd->attr.stack_base == 0)
- rt_free(stack);
- if (ptd->joinable_sem != RT_NULL)
- rt_sem_delete(ptd->joinable_sem);
- rt_free(ptd);
-
- return EINVAL;
+ ret = EINVAL;
+ goto __exit;
}
/* set pthread id */
- *tid = ptd->tid;
+ *pid = pth_id;
/* set pthread cleanup function and ptd data */
- (*tid)->cleanup = _pthread_cleanup;
- (*tid)->user_data = (rt_uint32_t)ptd;
+ ptd->tid->cleanup = _pthread_cleanup;
+ ptd->tid->user_data = (rt_uint32_t)ptd;
/* start thread */
- result = rt_thread_startup(*tid);
- if (result == RT_EOK)
+ if (rt_thread_startup(ptd->tid) == RT_EOK)
return 0;
/* start thread failed */
rt_thread_detach(ptd->tid);
- if (ptd->attr.stack_base == 0)
- rt_free(stack);
- if (ptd->joinable_sem != RT_NULL)
- rt_sem_delete(ptd->joinable_sem);
-
- rt_free(ptd);
+ ret = EINVAL;
- return EINVAL;
+__exit:
+ if (pth_id != PTHREAD_NUM_MAX)
+ _pthread_data_destroy(pth_id);
+ return ret;
}
RTM_EXPORT(pthread_create);
@@ -221,7 +306,7 @@ int pthread_detach(pthread_t thread)
goto __exit;
}
- if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
+ if ((ptd->tid->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
{
/* this defunct pthread is not handled by idle */
if (rt_sem_trytake(ptd->joinable_sem) != RT_EOK)
@@ -270,13 +355,13 @@ int pthread_join(pthread_t thread, void **value_ptr)
_pthread_data_t *ptd;
rt_err_t result;
- if (thread == rt_thread_self())
+ ptd = _pthread_get_data(thread);
+ if (ptd && ptd->tid == rt_thread_self())
{
/* join self */
return EDEADLK;
}
- ptd = _pthread_get_data(thread);
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
return EINVAL; /* join on a detached pthread */
@@ -299,13 +384,32 @@ int pthread_join(pthread_t thread, void **value_ptr)
}
RTM_EXPORT(pthread_join);
+pthread_t pthread_self (void)
+{
+ rt_thread_t tid;
+ _pthread_data_t *ptd;
+
+ tid = rt_thread_self();
+ if (tid == NULL) return PTHREAD_NUM_MAX;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
+ RT_ASSERT(ptd != RT_NULL);
+
+ return _pthread_data_get_pth(ptd);
+}
+RTM_EXPORT(pthread_self);
+
void pthread_exit(void *value)
{
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
rt_enter_critical();
/* disable cancel */
@@ -382,7 +486,15 @@ RTM_EXPORT(pthread_atfork);
int pthread_kill(pthread_t thread, int sig)
{
#ifdef RT_USING_SIGNALS
- return rt_thread_kill(thread, sig);
+ _pthread_data_t *ptd;
+
+ ptd = _pthread_get_data(thread);
+ if (ptd)
+ {
+ return rt_thread_kill(ptd->tid, sig);
+ }
+
+ return EINVAL;
#else
return ENOSYS;
#endif
@@ -401,8 +513,10 @@ void pthread_cleanup_pop(int execute)
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if (execute)
@@ -428,8 +542,10 @@ void pthread_cleanup_push(void (*routine)(void *), void *arg)
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
@@ -478,8 +594,10 @@ int pthread_setcancelstate(int state, int *oldstate)
{
_pthread_data_t *ptd;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return EINVAL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
@@ -499,8 +617,10 @@ int pthread_setcanceltype(int type, int *oldtype)
{
_pthread_data_t *ptd;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return EINVAL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
@@ -519,8 +639,10 @@ void pthread_testcancel(void)
int cancel = 0;
_pthread_data_t *ptd;
- /* get posix thread data */
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != RT_NULL);
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
@@ -534,14 +656,14 @@ int pthread_cancel(pthread_t thread)
{
_pthread_data_t *ptd;
- /* cancel self */
- if (thread == rt_thread_self())
- return 0;
-
/* get posix thread data */
ptd = _pthread_get_data(thread);
RT_ASSERT(ptd != RT_NULL);
+ /* cancel self */
+ if (ptd->tid == rt_thread_self())
+ return 0;
+
/* set canceled */
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
{
@@ -555,10 +677,11 @@ int pthread_cancel(pthread_t thread)
* thread (pthread_cleanup), it will move to defunct
* thread list and wait for handling in idle thread.
*/
- rt_thread_detach(thread);
+ rt_thread_detach(ptd->tid);
}
}
return 0;
}
RTM_EXPORT(pthread_cancel);
+
diff --git a/components/libc/pthreads/pthread.h b/components/libc/pthreads/pthread.h
index f9a0a92b68..7bb72b726c 100644
--- a/components/libc/pthreads/pthread.h
+++ b/components/libc/pthreads/pthread.h
@@ -32,7 +32,7 @@ extern "C" {
#define PTHREAD_EXPLICIT_SCHED 0
#define PTHREAD_INHERIT_SCHED 1
-typedef rt_thread_t pthread_t;
+typedef long pthread_t;
typedef long pthread_condattr_t;
typedef long pthread_rwlockattr_t;
typedef long pthread_mutexattr_t;
@@ -76,15 +76,21 @@ enum
#define PTHREAD_SCOPE_PROCESS 0
#define PTHREAD_SCOPE_SYSTEM 1
+struct sched_param
+{
+ int sched_priority;
+};
+
struct pthread_attr
{
- void* stack_base;
- rt_uint32_t stack_size; /* stack size of thread */
+ void* stackaddr; /* stack address of thread */
+ int stacksize; /* stack size of thread */
+
+ int inheritsched; /* Inherit parent prio/policy */
+ int schedpolicy; /* scheduler policy */
+ struct sched_param schedparam; /* sched parameter */
- rt_uint8_t priority; /* priority of thread */
- rt_uint8_t detachstate; /* detach state */
- rt_uint8_t policy; /* scheduler policy */
- rt_uint8_t inheritsched; /* Inherit parent prio/policy */
+ int detachstate; /* detach state */
};
typedef struct pthread_attr pthread_attr_t;
@@ -131,11 +137,6 @@ struct pthread_barrier
};
typedef struct pthread_barrier pthread_barrier_t;
-struct sched_param
-{
- int sched_priority;
-};
-
/* pthread thread interface */
int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_init(pthread_attr_t *attr);
@@ -171,10 +172,7 @@ rt_inline int pthread_equal (pthread_t t1, pthread_t t2)
return t1 == t2;
}
-rt_inline pthread_t pthread_self (void)
-{
- return rt_thread_self();
-}
+pthread_t pthread_self (void);
void pthread_exit (void *value_ptr);
int pthread_once(pthread_once_t * once_control, void (*init_routine) (void));
diff --git a/components/libc/pthreads/pthread_attr.c b/components/libc/pthreads/pthread_attr.c
index 8eed35932f..2294599bf3 100644
--- a/components/libc/pthreads/pthread_attr.c
+++ b/components/libc/pthreads/pthread_attr.c
@@ -20,10 +20,13 @@ const pthread_attr_t pthread_default_attr =
{
0, /* stack base */
DEFAULT_STACK_SIZE, /* stack size */
- DEFAULT_PRIORITY, /* priority */
- PTHREAD_CREATE_JOINABLE, /* detach state */
+
+ PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
SCHED_FIFO, /* scheduler policy */
- PTHREAD_INHERIT_SCHED /* Inherit parent prio/policy */
+ {
+ DEFAULT_PRIORITY, /* scheduler priority */
+ },
+ PTHREAD_CREATE_JOINABLE, /* detach state */
};
int pthread_attr_init(pthread_attr_t *attr)
@@ -73,7 +76,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
{
RT_ASSERT(attr != RT_NULL);
- attr->policy = policy;
+ attr->schedpolicy = policy;
return 0;
}
@@ -83,7 +86,7 @@ int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
{
RT_ASSERT(attr != RT_NULL);
- *policy = (int)attr->policy;
+ *policy = (int)attr->schedpolicy;
return 0;
}
@@ -95,7 +98,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr,
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
- attr->priority = param->sched_priority;
+ attr->schedparam.sched_priority = param->sched_priority;
return 0;
}
@@ -107,7 +110,7 @@ int pthread_attr_getschedparam(pthread_attr_t const *attr,
RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL);
- param->sched_priority = attr->priority;
+ param->sched_priority = attr->schedparam.sched_priority;
return 0;
}
@@ -117,7 +120,7 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
{
RT_ASSERT(attr != RT_NULL);
- attr->stack_size = stack_size;
+ attr->stacksize = stack_size;
return 0;
}
@@ -127,7 +130,7 @@ int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
{
RT_ASSERT(attr != RT_NULL);
- *stack_size = attr->stack_size;
+ *stack_size = attr->stacksize;
return 0;
}
@@ -155,8 +158,8 @@ int pthread_attr_setstack(pthread_attr_t *attr,
{
RT_ASSERT(attr != RT_NULL);
- attr->stack_base = stack_base;
- attr->stack_size = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
+ attr->stackaddr = stack_base;
+ attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
return 0;
}
@@ -168,8 +171,8 @@ int pthread_attr_getstack(pthread_attr_t const *attr,
{
RT_ASSERT(attr != RT_NULL);
- *stack_base = attr->stack_base;
- *stack_size = attr->stack_size;
+ *stack_base = attr->stackaddr;
+ *stack_size = attr->stacksize;
return 0;
}
diff --git a/components/libc/pthreads/pthread_cond.c b/components/libc/pthreads/pthread_cond.c
index d3c4ac7637..38dc697469 100644
--- a/components/libc/pthreads/pthread_cond.c
+++ b/components/libc/pthreads/pthread_cond.c
@@ -189,7 +189,7 @@ rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
pthread_cond_init(cond, RT_NULL);
/* The mutex was not owned by the current thread at the time of the call. */
- if (mutex->lock.owner != pthread_self())
+ if (mutex->lock.owner != rt_thread_self())
return -RT_ERROR;
/* unlock a mutex failed */
if (pthread_mutex_unlock(mutex) != 0)
diff --git a/components/libc/pthreads/pthread_internal.h b/components/libc/pthreads/pthread_internal.h
index 3e94e867d7..a6706ca091 100644
--- a/components/libc/pthreads/pthread_internal.h
+++ b/components/libc/pthreads/pthread_internal.h
@@ -30,6 +30,10 @@ struct _pthread_key_data
};
typedef struct _pthread_key_data _pthread_key_data_t;
+#ifndef PTHREAD_NUM_MAX
+#define PTHREAD_NUM_MAX 32
+#endif
+
#define PTHREAD_MAGIC 0x70746873
struct _pthread_data
{
@@ -56,17 +60,7 @@ struct _pthread_data
};
typedef struct _pthread_data _pthread_data_t;
-rt_inline _pthread_data_t *_pthread_get_data(pthread_t thread)
-{
- _pthread_data_t *ptd;
- RT_ASSERT(thread != RT_NULL);
-
- ptd = (_pthread_data_t *)thread->user_data;
- RT_ASSERT(ptd != RT_NULL);
- RT_ASSERT(ptd->magic == PTHREAD_MAGIC);
-
- return ptd;
-}
+_pthread_data_t *_pthread_get_data(pthread_t thread);
int clock_time_to_tick(const struct timespec *time);
diff --git a/components/libc/pthreads/pthread_tls.c b/components/libc/pthreads/pthread_tls.c
index c6d0bca155..4b97d796d9 100644
--- a/components/libc/pthreads/pthread_tls.c
+++ b/components/libc/pthreads/pthread_tls.c
@@ -22,7 +22,10 @@ void *pthread_getspecific(pthread_key_t key)
{
struct _pthread_data* ptd;
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return NULL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != NULL);
if (ptd->tls == NULL)
@@ -39,7 +42,10 @@ int pthread_setspecific(pthread_key_t key, const void *value)
{
struct _pthread_data* ptd;
- ptd = _pthread_get_data(rt_thread_self());
+ if (rt_thread_self() == NULL) return EINVAL;
+
+ /* get pthread data from user data of thread */
+ ptd = (_pthread_data_t *)rt_thread_self()->user_data;
RT_ASSERT(ptd != NULL);
/* check tls area */