mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-01-18 16:53:30 +08:00
Embedded GPLv2 license in pthreads
This commit is contained in:
parent
773990abdb
commit
8ab2b287b8
@ -3,15 +3,26 @@
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-12-08 Bernard fix the issue of _timevalue.tv_usec initialization,
|
||||
* which found by Rob <rdent@iinet.net.au>
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <pthread.h>
|
||||
|
||||
@ -75,6 +86,7 @@ int clock_getres (clockid_t clockid, struct timespec *res)
|
||||
if ((clockid != CLOCK_REALTIME) || (res == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -92,6 +104,7 @@ int clock_gettime (clockid_t clockid, struct timespec *tp)
|
||||
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -114,6 +127,7 @@ int clock_settime (clockid_t clockid, const struct timespec *tp)
|
||||
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -133,7 +147,8 @@ int clock_settime (clockid_t clockid, const struct timespec *tp)
|
||||
/* set realtime seconds */
|
||||
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
|
||||
}
|
||||
else return -1;
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,3 +1,26 @@
|
||||
/*
|
||||
* File : mqueue.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#include "mqueue.h"
|
||||
#include "pthread_internal.h"
|
||||
|
||||
@ -39,6 +62,7 @@ static void posix_mq_delete(mqd_t pmq)
|
||||
/* delete RT-Thread mqueue */
|
||||
rt_mq_delete(pmq->mq);
|
||||
rt_free(pmq);
|
||||
|
||||
return ;
|
||||
}
|
||||
}
|
||||
@ -62,10 +86,12 @@ static mqd_t posix_mq_find(const char* name)
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat,
|
||||
int mq_setattr(mqd_t mqdes,
|
||||
const struct mq_attr *mqstat,
|
||||
struct mq_attr *omqstat)
|
||||
{
|
||||
rt_set_errno(-RT_ERROR);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(mq_setattr);
|
||||
@ -75,6 +101,7 @@ int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat)
|
||||
if ((mqdes == RT_NULL) || mqstat == RT_NULL)
|
||||
{
|
||||
rt_set_errno(EBADF);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -101,7 +128,8 @@ mqd_t mq_open(const char *name, int oflag, ...)
|
||||
if (oflag & O_CREAT)
|
||||
{
|
||||
va_start(arg, oflag);
|
||||
mode = (mode_t) va_arg(arg, unsigned int); mode = mode;
|
||||
mode = (mode_t)va_arg(arg, unsigned int);
|
||||
mode = mode;
|
||||
attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
|
||||
va_end(arg);
|
||||
|
||||
@ -149,6 +177,7 @@ mqd_t mq_open(const char *name, int oflag, ...)
|
||||
}
|
||||
}
|
||||
rt_sem_release(&posix_mq_lock);
|
||||
|
||||
return mqdes;
|
||||
|
||||
__return:
|
||||
@ -176,6 +205,7 @@ ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_pri
|
||||
if ((mqdes == RT_NULL) || (msg_ptr == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -195,6 +225,7 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
|
||||
if ((mqdes == RT_NULL) || (msg_ptr == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -203,12 +234,16 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
|
||||
return 0;
|
||||
|
||||
rt_set_errno(EBADF);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(mq_send);
|
||||
|
||||
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
|
||||
unsigned *msg_prio, const struct timespec *abs_timeout)
|
||||
ssize_t mq_timedreceive(mqd_t mqdes,
|
||||
char *msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned *msg_prio,
|
||||
const struct timespec *abs_timeout)
|
||||
{
|
||||
int tick;
|
||||
rt_err_t result;
|
||||
@ -217,13 +252,15 @@ ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
|
||||
if ((mqdes == RT_NULL) || (msg_ptr == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
tick = clock_time_to_tick(abs_timeout);
|
||||
|
||||
result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, tick);
|
||||
if (result == RT_EOK) return msg_len;
|
||||
if (result == RT_EOK)
|
||||
return msg_len;
|
||||
|
||||
if (result == -RT_ETIMEOUT)
|
||||
rt_set_errno(ETIMEDOUT);
|
||||
@ -234,7 +271,10 @@ ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
|
||||
}
|
||||
RTM_EXPORT(mq_timedreceive);
|
||||
|
||||
int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio,
|
||||
int mq_timedsend(mqd_t mqdes,
|
||||
const char *msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned msg_prio,
|
||||
const struct timespec *abs_timeout)
|
||||
{
|
||||
/* RT-Thread does not support timed send */
|
||||
@ -245,6 +285,7 @@ RTM_EXPORT(mq_timedsend);
|
||||
int mq_notify(mqd_t mqdes, const struct sigevent *notification)
|
||||
{
|
||||
rt_set_errno(-RT_ERROR);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(mq_notify);
|
||||
@ -254,6 +295,7 @@ int mq_close(mqd_t mqdes)
|
||||
if (mqdes == RT_NULL)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -288,12 +330,14 @@ int mq_unlink(const char *name)
|
||||
posix_mq_delete(pmq);
|
||||
}
|
||||
rt_sem_release(&posix_mq_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
rt_sem_release(&posix_mq_lock);
|
||||
|
||||
/* no this entry */
|
||||
rt_set_errno(ENOENT);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(mq_unlink);
|
||||
|
@ -1,3 +1,26 @@
|
||||
/*
|
||||
* File : mqueue.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#ifndef __MQUEUE_H__
|
||||
#define __MQUEUE_H__
|
||||
|
||||
@ -31,11 +54,18 @@ int mq_notify(mqd_t mqdes, const struct sigevent *notification);
|
||||
mqd_t mq_open(const char *name, int oflag, ...);
|
||||
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio);
|
||||
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio);
|
||||
int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat,
|
||||
int mq_setattr(mqd_t mqdes,
|
||||
const struct mq_attr *mqstat,
|
||||
struct mq_attr *omqstat);
|
||||
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
|
||||
unsigned *msg_prio, const struct timespec *abs_timeout);
|
||||
int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio,
|
||||
ssize_t mq_timedreceive(mqd_t mqdes,
|
||||
char *msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned *msg_prio,
|
||||
const struct timespec *abs_timeout);
|
||||
int mq_timedsend(mqd_t mqdes,
|
||||
const char *msg_ptr,
|
||||
size_t msg_len,
|
||||
unsigned msg_prio,
|
||||
const struct timespec *abs_timeout);
|
||||
|
||||
int mq_unlink(const char *name);
|
||||
|
@ -1,3 +1,26 @@
|
||||
/*
|
||||
* File : posix_types.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#ifndef __POSIX_TYPES_H__
|
||||
#define __POSIX_TYPES_H__
|
||||
|
||||
@ -30,12 +53,14 @@ typedef rt_int32_t pid_t; /* Used for process IDs and process group IDs. */
|
||||
typedef signed long ssize_t; /* Used for a count of bytes or an error indication. */
|
||||
typedef signed long time_t; /* Used for time in seconds. */
|
||||
|
||||
struct timespec {
|
||||
struct timespec
|
||||
{
|
||||
time_t tv_sec; /* seconds */
|
||||
long tv_nsec; /* nanoseconds */
|
||||
};
|
||||
|
||||
struct timeval {
|
||||
struct timeval
|
||||
{
|
||||
long tv_sec; /* seconds */
|
||||
long tv_usec; /* microseconds */
|
||||
};
|
||||
|
@ -1,3 +1,26 @@
|
||||
/*
|
||||
* File : pthread.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include "pthread_internal.h"
|
||||
@ -48,7 +71,8 @@ static void pthread_entry_stub(void* parameter)
|
||||
ptd->return_value = value;
|
||||
}
|
||||
|
||||
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 *parameter)
|
||||
{
|
||||
int result;
|
||||
@ -62,7 +86,8 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
|
||||
/* allocate posix thread data */
|
||||
ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
|
||||
if (ptd == RT_NULL) return ENOMEM;
|
||||
if (ptd == RT_NULL)
|
||||
return ENOMEM;
|
||||
/* clean posix thread data memory */
|
||||
rt_memset(ptd, 0, sizeof(_pthread_data_t));
|
||||
ptd->canceled = 0;
|
||||
@ -70,7 +95,8 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
|
||||
ptd->magic = PTHREAD_MAGIC;
|
||||
|
||||
if (attr != RT_NULL) ptd->attr = *attr;
|
||||
if (attr != RT_NULL)
|
||||
ptd->attr = *attr;
|
||||
else
|
||||
{
|
||||
/* use default attribute */
|
||||
@ -82,11 +108,13 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
{
|
||||
stack = (void*)rt_malloc(ptd->attr.stack_size);
|
||||
}
|
||||
else stack = (void*)(ptd->attr.stack_base);
|
||||
else
|
||||
stack = (void*)(ptd->attr.stack_base);
|
||||
|
||||
if (stack == RT_NULL)
|
||||
{
|
||||
rt_free(ptd);
|
||||
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
@ -94,8 +122,10 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
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);
|
||||
if (ptd->attr.stack_base == 0)
|
||||
rt_free(stack);
|
||||
rt_free(ptd);
|
||||
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
@ -104,12 +134,15 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
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);
|
||||
if (ptd->attr.stack_base != 0)
|
||||
rt_free(stack);
|
||||
rt_free(ptd);
|
||||
|
||||
return ENOMEM;
|
||||
}
|
||||
}
|
||||
else ptd->joinable_sem = RT_NULL;
|
||||
else
|
||||
ptd->joinable_sem = RT_NULL;
|
||||
|
||||
/* set parameter */
|
||||
ptd->thread_entry = start;
|
||||
@ -120,9 +153,12 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
stack, ptd->attr.stack_size,
|
||||
ptd->attr.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);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -135,14 +171,18 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
|
||||
|
||||
/* start thread */
|
||||
result = rt_thread_startup(*tid);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == 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);
|
||||
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;
|
||||
}
|
||||
RTM_EXPORT(pthread_create);
|
||||
@ -172,7 +212,8 @@ int pthread_detach(pthread_t thread)
|
||||
* if this thread create the local thread data,
|
||||
* delete it
|
||||
*/
|
||||
if (ptd->tls != RT_NULL) rt_free(ptd->tls);
|
||||
if (ptd->tls != RT_NULL)
|
||||
rt_free(ptd->tls);
|
||||
rt_free(ptd->tid);
|
||||
rt_free(ptd);
|
||||
}
|
||||
@ -211,12 +252,14 @@ int pthread_join (pthread_t thread, void **value_ptr)
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
/* get return value */
|
||||
if (value_ptr != RT_NULL) *value_ptr = ptd->return_value;
|
||||
if (value_ptr != RT_NULL)
|
||||
*value_ptr = ptd->return_value;
|
||||
|
||||
/* release resource */
|
||||
pthread_detach(thread);
|
||||
}
|
||||
else return ESRCH;
|
||||
else
|
||||
return ESRCH;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -402,7 +445,8 @@ int pthread_setcancelstate(int state, int *oldstate)
|
||||
|
||||
if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
|
||||
{
|
||||
if (oldstate) *oldstate = ptd->cancelstate;
|
||||
if (oldstate)
|
||||
*oldstate = ptd->cancelstate;
|
||||
ptd->cancelstate = state;
|
||||
|
||||
return 0;
|
||||
@ -423,7 +467,8 @@ int pthread_setcanceltype(int type, int *oldtype)
|
||||
if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
|
||||
return EINVAL;
|
||||
|
||||
if (oldtype) *oldtype = ptd->canceltype;
|
||||
if (oldtype)
|
||||
*oldtype = ptd->canceltype;
|
||||
ptd->canceltype = type;
|
||||
|
||||
return 0;
|
||||
@ -439,8 +484,10 @@ void pthread_testcancel(void)
|
||||
ptd = _pthread_get_data(rt_thread_self());
|
||||
RT_ASSERT(ptd != RT_NULL);
|
||||
|
||||
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE) cancel = ptd->canceled;
|
||||
if (cancel) pthread_exit((void*)PTHREAD_CANCELED);
|
||||
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
|
||||
cancel = ptd->canceled;
|
||||
if (cancel)
|
||||
pthread_exit((void*)PTHREAD_CANCELED);
|
||||
}
|
||||
RTM_EXPORT(pthread_testcancel);
|
||||
|
||||
@ -449,7 +496,8 @@ int pthread_cancel(pthread_t thread)
|
||||
_pthread_data_t *ptd;
|
||||
|
||||
/* cancel self */
|
||||
if (thread == rt_thread_self()) return 0;
|
||||
if (thread == rt_thread_self())
|
||||
return 0;
|
||||
|
||||
/* get posix thread data */
|
||||
ptd = _pthread_get_data(thread);
|
||||
|
@ -3,14 +3,25 @@
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rt-thread.org/license/LICENSE
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#ifndef __PTHREAD_H__
|
||||
#define __PTHREAD_H__
|
||||
|
||||
@ -38,7 +49,8 @@ typedef long pthread_barrierattr_t;
|
||||
typedef int pthread_key_t;
|
||||
typedef int pthread_once_t;
|
||||
|
||||
enum {
|
||||
enum
|
||||
{
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS = 0,
|
||||
PTHREAD_CANCEL_ENABLE,
|
||||
PTHREAD_CANCEL_DEFERRED,
|
||||
@ -46,7 +58,8 @@ enum {
|
||||
PTHREAD_CANCELED
|
||||
};
|
||||
|
||||
enum {
|
||||
enum
|
||||
{
|
||||
PTHREAD_MUTEX_NORMAL = 0,
|
||||
PTHREAD_MUTEX_RECURSIVE = 1,
|
||||
PTHREAD_MUTEX_ERRORCHECK = 2,
|
||||
@ -58,7 +71,8 @@ enum {
|
||||
/* init value for pthread_once_t */
|
||||
#define PTHREAD_ONCE_INIT 0
|
||||
|
||||
enum {
|
||||
enum
|
||||
{
|
||||
PTHREAD_PRIO_INHERIT =0,
|
||||
PTHREAD_PRIO_NONE,
|
||||
PTHREAD_PRIO_PROTECT,
|
||||
@ -232,19 +246,22 @@ int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared);
|
||||
|
||||
int pthread_barrier_destroy(pthread_barrier_t *barrier);
|
||||
int pthread_barrier_init(pthread_barrier_t *barrier,
|
||||
const pthread_barrierattr_t *attr, unsigned count);
|
||||
const pthread_barrierattr_t *attr,
|
||||
unsigned count);
|
||||
|
||||
int pthread_barrier_wait(pthread_barrier_t *barrier);
|
||||
|
||||
/* Signal Generation and Delivery, P1003.1b-1993, p. 63
|
||||
NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and
|
||||
sigev_notify_attributes to the sigevent structure. */
|
||||
union sigval {
|
||||
union sigval
|
||||
{
|
||||
int sival_int; /* Integer signal value */
|
||||
void *sival_ptr; /* Pointer signal value */
|
||||
};
|
||||
|
||||
struct sigevent {
|
||||
struct sigevent
|
||||
{
|
||||
int sigev_notify; /* Notification type */
|
||||
int sigev_signo; /* Signal number */
|
||||
union sigval sigev_value; /* Signal value */
|
||||
|
@ -1,3 +1,27 @@
|
||||
/*
|
||||
* File : pthread_attr.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "pthread.h"
|
||||
#include "sched.h"
|
||||
@ -31,6 +55,7 @@ int pthread_attr_destroy(pthread_attr_t *attr)
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
memset(attr, 0, sizeof(pthread_attr_t));
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_destroy);
|
||||
@ -43,6 +68,7 @@ int pthread_attr_setdetachstate(pthread_attr_t * attr, int state)
|
||||
return EINVAL;
|
||||
|
||||
attr->detachstate = state;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setdetachstate);
|
||||
@ -62,6 +88,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t * attr, int policy)
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
attr->policy = policy;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setschedpolicy);
|
||||
@ -71,26 +98,31 @@ int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*policy = (int)attr->policy;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getschedpolicy);
|
||||
|
||||
int pthread_attr_setschedparam(pthread_attr_t *attr, struct sched_param const *param)
|
||||
int pthread_attr_setschedparam(pthread_attr_t *attr,
|
||||
struct sched_param const *param)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
RT_ASSERT(param != RT_NULL);
|
||||
|
||||
attr->priority = param->sched_priority;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setschedparam);
|
||||
|
||||
int pthread_attr_getschedparam(pthread_attr_t const *attr, struct sched_param *param)
|
||||
int pthread_attr_getschedparam(pthread_attr_t const *attr,
|
||||
struct sched_param *param)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
RT_ASSERT(param != RT_NULL);
|
||||
|
||||
param->sched_priority = attr->priority;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getschedparam);
|
||||
@ -100,6 +132,7 @@ int pthread_attr_setstacksize(pthread_attr_t * attr, size_t stack_size)
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
attr->stack_size = stack_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setstacksize);
|
||||
@ -109,6 +142,7 @@ int pthread_attr_getstacksize(pthread_attr_t const * attr, size_t *stack_size)
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
*stack_size = attr->stack_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getstacksize);
|
||||
@ -116,6 +150,7 @@ RTM_EXPORT(pthread_attr_getstacksize);
|
||||
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
return ENOTSUP;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setstackaddr);
|
||||
@ -123,11 +158,14 @@ RTM_EXPORT(pthread_attr_setstackaddr);
|
||||
int pthread_attr_getstackaddr(pthread_attr_t const *attr, void **stack_addr)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
return ENOTSUP;
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_getstackaddr);
|
||||
|
||||
int pthread_attr_setstack(pthread_attr_t * attr, void *stack_base, size_t stack_size)
|
||||
int pthread_attr_setstack(pthread_attr_t *attr,
|
||||
void *stack_base,
|
||||
size_t stack_size)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
@ -138,7 +176,9 @@ int pthread_attr_setstack(pthread_attr_t * attr, void *stack_base, size_t stack_
|
||||
}
|
||||
RTM_EXPORT(pthread_attr_setstack);
|
||||
|
||||
int pthread_attr_getstack(pthread_attr_t const * attr, void **stack_base, size_t *stack_size)
|
||||
int pthread_attr_getstack(pthread_attr_t const *attr,
|
||||
void **stack_base,
|
||||
size_t *stack_size)
|
||||
{
|
||||
RT_ASSERT(attr != RT_NULL);
|
||||
|
||||
|
@ -1,24 +1,53 @@
|
||||
/*
|
||||
* File : pthread_barrier.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrierattr_destroy);
|
||||
|
||||
int pthread_barrierattr_init(pthread_barrierattr_t *attr)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrierattr_init);
|
||||
|
||||
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared)
|
||||
int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
|
||||
int *pshared)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*pshared = (int)*attr;
|
||||
|
||||
return 0;
|
||||
@ -27,8 +56,10 @@ RTM_EXPORT(pthread_barrierattr_getpshared);
|
||||
|
||||
int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (pshared == PTHREAD_PROCESS_PRIVATE) attr = PTHREAD_PROCESS_PRIVATE;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
if (pshared == PTHREAD_PROCESS_PRIVATE)
|
||||
attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
@ -38,7 +69,8 @@ int pthread_barrier_destroy(pthread_barrier_t *barrier)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (!barrier) return EINVAL;
|
||||
if (!barrier)
|
||||
return EINVAL;
|
||||
|
||||
result = pthread_cond_destroy(&(barrier->cond));
|
||||
|
||||
@ -47,10 +79,13 @@ int pthread_barrier_destroy(pthread_barrier_t *barrier)
|
||||
RTM_EXPORT(pthread_barrier_destroy);
|
||||
|
||||
int pthread_barrier_init(pthread_barrier_t *barrier,
|
||||
const pthread_barrierattr_t *attr, unsigned count)
|
||||
const pthread_barrierattr_t *attr,
|
||||
unsigned count)
|
||||
{
|
||||
if (!barrier) return EINVAL;
|
||||
if (attr &&(*attr != PTHREAD_PROCESS_PRIVATE)) return EINVAL;
|
||||
if (!barrier)
|
||||
return EINVAL;
|
||||
if (attr && (*attr != PTHREAD_PROCESS_PRIVATE))
|
||||
return EINVAL;
|
||||
|
||||
barrier->count = count;
|
||||
pthread_cond_init(&(barrier->cond), NULL);
|
||||
@ -63,12 +98,15 @@ RTM_EXPORT(pthread_barrier_init);
|
||||
int pthread_barrier_wait(pthread_barrier_t *barrier)
|
||||
{
|
||||
rt_err_t result;
|
||||
if (!barrier) return EINVAL;
|
||||
if (!barrier)
|
||||
return EINVAL;
|
||||
|
||||
result = pthread_mutex_lock(&(barrier->mutex));
|
||||
if (result != 0) return EINVAL;
|
||||
if (result != 0)
|
||||
return EINVAL;
|
||||
|
||||
if (barrier->count == 0) result = EINVAL;
|
||||
if (barrier->count == 0)
|
||||
result = EINVAL;
|
||||
else
|
||||
{
|
||||
barrier->count -= 1;
|
||||
@ -79,6 +117,7 @@ int pthread_barrier_wait(pthread_barrier_t *barrier)
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&(barrier->mutex));
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(pthread_barrier_wait);
|
||||
|
@ -1,9 +1,34 @@
|
||||
/*
|
||||
* File : pthread_cond.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include "pthread_internal.h"
|
||||
|
||||
int pthread_condattr_destroy(pthread_condattr_t *attr)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -11,7 +36,8 @@ RTM_EXPORT(pthread_condattr_destroy);
|
||||
|
||||
int pthread_condattr_init(pthread_condattr_t *attr)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
@ -34,17 +60,22 @@ RTM_EXPORT(pthread_condattr_setclock);
|
||||
|
||||
int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
|
||||
{
|
||||
if (!attr || !pshared) return EINVAL;
|
||||
if (!attr || !pshared)
|
||||
return EINVAL;
|
||||
|
||||
*pshared = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_condattr_getpshared);
|
||||
|
||||
int pthread_condattr_setpshared(pthread_condattr_t*attr, int pshared)
|
||||
{
|
||||
if ((pshared != PTHREAD_PROCESS_PRIVATE) && (pshared != PTHREAD_PROCESS_SHARED))
|
||||
if ((pshared != PTHREAD_PROCESS_PRIVATE) &&
|
||||
(pshared != PTHREAD_PROCESS_SHARED))
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (pshared != PTHREAD_PROCESS_PRIVATE)
|
||||
return ENOSYS;
|
||||
@ -60,15 +91,17 @@ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
|
||||
static rt_uint16_t cond_num = 0;
|
||||
|
||||
/* parameter check */
|
||||
if (cond == RT_NULL) return EINVAL;
|
||||
if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE)) return EINVAL;
|
||||
if (cond == RT_NULL)
|
||||
return EINVAL;
|
||||
if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE))
|
||||
return EINVAL;
|
||||
|
||||
rt_snprintf(cond_name, sizeof(cond_name),
|
||||
"cond%02d", cond_num++);
|
||||
rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
|
||||
|
||||
cond->attr = *attr;
|
||||
result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
|
||||
if (result != RT_EOK) return EINVAL;
|
||||
if (result != RT_EOK)
|
||||
return EINVAL;
|
||||
|
||||
/* detach the object from system object container */
|
||||
rt_object_detach(&(cond->sem.parent.parent));
|
||||
@ -80,11 +113,14 @@ RTM_EXPORT(pthread_cond_init);
|
||||
int pthread_cond_destroy(pthread_cond_t *cond)
|
||||
{
|
||||
rt_err_t result;
|
||||
if (cond == RT_NULL) return EINVAL;
|
||||
if (cond->attr == -1) return 0; /* which is not initialized */
|
||||
if (cond == RT_NULL)
|
||||
return EINVAL;
|
||||
if (cond->attr == -1)
|
||||
return 0; /* which is not initialized */
|
||||
|
||||
result = rt_sem_trytake(&(cond->sem));
|
||||
if (result != RT_EOK) return EBUSY;
|
||||
if (result != RT_EOK)
|
||||
return EBUSY;
|
||||
|
||||
/* clean condition */
|
||||
rt_memset(cond, 0, sizeof(pthread_cond_t));
|
||||
@ -119,6 +155,7 @@ int pthread_cond_broadcast(pthread_cond_t *cond)
|
||||
else
|
||||
{
|
||||
rt_exit_critical();
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
@ -136,23 +173,28 @@ int pthread_cond_signal(pthread_cond_t *cond)
|
||||
pthread_cond_init(cond, RT_NULL);
|
||||
|
||||
result = rt_sem_release(&(cond->sem));
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_cond_signal);
|
||||
|
||||
rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
|
||||
pthread_mutex_t *mutex,
|
||||
rt_int32_t timeout)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (!cond || !mutex) return -RT_ERROR;
|
||||
if (!cond || !mutex)
|
||||
return -RT_ERROR;
|
||||
/* check whether initialized */
|
||||
if (cond->attr == -1) pthread_cond_init(cond, RT_NULL);
|
||||
if (cond->attr == -1)
|
||||
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()) return -RT_ERROR;
|
||||
if (mutex->lock.owner != pthread_self())
|
||||
return -RT_ERROR;
|
||||
/* unlock a mutex failed */
|
||||
if (pthread_mutex_unlock(mutex) != 0)
|
||||
return -RT_ERROR;
|
||||
@ -160,6 +202,7 @@ rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
result = rt_sem_take(&(cond->sem), timeout);
|
||||
/* lock mutex again */
|
||||
pthread_mutex_lock(mutex);
|
||||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(_pthread_cond_timedwait);
|
||||
@ -169,7 +212,8 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
||||
rt_err_t result;
|
||||
|
||||
result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
@ -184,8 +228,10 @@ int pthread_cond_timedwait(pthread_cond_t *cond,
|
||||
|
||||
timeout = clock_time_to_tick(abstime);
|
||||
result = _pthread_cond_timedwait(cond, mutex, timeout);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == -RT_ETIMEOUT) return ETIMEDOUT;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
if (result == -RT_ETIMEOUT)
|
||||
return ETIMEDOUT;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
|
@ -1,3 +1,27 @@
|
||||
/*
|
||||
* File : pthread_internal.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#ifndef __PTHREAD_INTERNAL_H__
|
||||
#define __PTHREAD_INTERNAL_H__
|
||||
|
||||
|
@ -1,3 +1,27 @@
|
||||
/*
|
||||
* File : pthread_mutex.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "pthread.h"
|
||||
|
||||
@ -11,6 +35,7 @@ int pthread_mutexattr_init(pthread_mutexattr_t *attr)
|
||||
if (attr)
|
||||
{
|
||||
*attr = pthread_default_mutexattr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -23,6 +48,7 @@ int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
|
||||
if (attr)
|
||||
{
|
||||
*attr = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -39,6 +65,7 @@ int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
|
||||
if (atype >= PTHREAD_MUTEX_NORMAL && atype <= PTHREAD_MUTEX_ERRORCHECK)
|
||||
{
|
||||
*type = atype;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -49,10 +76,10 @@ RTM_EXPORT(pthread_mutexattr_gettype);
|
||||
|
||||
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
|
||||
{
|
||||
if (attr && type >= PTHREAD_MUTEX_NORMAL &&
|
||||
type <= PTHREAD_MUTEX_ERRORCHECK )
|
||||
if (attr && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_ERRORCHECK)
|
||||
{
|
||||
*attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -62,7 +89,8 @@ RTM_EXPORT(pthread_mutexattr_settype);
|
||||
|
||||
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
switch (pshared)
|
||||
{
|
||||
@ -81,7 +109,8 @@ RTM_EXPORT(pthread_mutexattr_setpshared);
|
||||
|
||||
int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
|
||||
{
|
||||
if (!attr || !pshared) return EINVAL;
|
||||
if (!attr || !pshared)
|
||||
return EINVAL;
|
||||
|
||||
*pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
|
||||
: PTHREAD_PROCESS_PRIVATE;
|
||||
@ -95,16 +124,20 @@ int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
|
||||
char name[RT_NAME_MAX];
|
||||
static rt_uint16_t pthread_mutex_number = 0;
|
||||
|
||||
if (!mutex) return EINVAL;
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
|
||||
/* build mutex name */
|
||||
rt_snprintf(name, sizeof(name), "pmtx%02d", pthread_mutex_number ++);
|
||||
if (attr == RT_NULL) mutex->attr = pthread_default_mutexattr;
|
||||
else mutex->attr = *attr;
|
||||
if (attr == RT_NULL)
|
||||
mutex->attr = pthread_default_mutexattr;
|
||||
else
|
||||
mutex->attr = *attr;
|
||||
|
||||
/* init mutex lock */
|
||||
result = rt_mutex_init(&(mutex->lock), name, RT_IPC_FLAG_FIFO);
|
||||
if (result != RT_EOK) return EINVAL;
|
||||
if (result != RT_EOK)
|
||||
return EINVAL;
|
||||
|
||||
/* detach the object from system object container */
|
||||
rt_object_detach(&(mutex->lock.parent.parent));
|
||||
@ -115,10 +148,12 @@ RTM_EXPORT(pthread_mutex_init);
|
||||
|
||||
int pthread_mutex_destroy(pthread_mutex_t *mutex)
|
||||
{
|
||||
if (!mutex || mutex->attr == -1) return EINVAL;
|
||||
if (!mutex || mutex->attr == -1)
|
||||
return EINVAL;
|
||||
|
||||
/* it's busy */
|
||||
if (mutex->lock.owner != RT_NULL) return EBUSY;
|
||||
if (mutex->lock.owner != RT_NULL)
|
||||
return EBUSY;
|
||||
|
||||
rt_memset(mutex, 0, sizeof(pthread_mutex_t));
|
||||
mutex->attr = -1;
|
||||
@ -132,7 +167,8 @@ int pthread_mutex_lock(pthread_mutex_t *mutex)
|
||||
int mtype;
|
||||
rt_err_t result;
|
||||
|
||||
if (!mutex) return EINVAL;
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
|
||||
if (mutex->attr == -1)
|
||||
{
|
||||
@ -142,15 +178,18 @@ int pthread_mutex_lock(pthread_mutex_t *mutex)
|
||||
|
||||
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
|
||||
rt_enter_critical();
|
||||
if (mutex->lock.owner == rt_thread_self() && mtype != PTHREAD_MUTEX_RECURSIVE)
|
||||
if (mutex->lock.owner == rt_thread_self() &&
|
||||
mtype != PTHREAD_MUTEX_RECURSIVE)
|
||||
{
|
||||
rt_exit_critical();
|
||||
|
||||
return EDEADLK;
|
||||
}
|
||||
rt_exit_critical();
|
||||
|
||||
result = rt_mutex_take(&(mutex->lock), RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
@ -160,7 +199,8 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (!mutex) return EINVAL;
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
if (mutex->attr == -1)
|
||||
{
|
||||
/* init mutex */
|
||||
@ -173,14 +213,17 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex)
|
||||
mtype = mutex->attr & MUTEXATTR_TYPE_MASK;
|
||||
|
||||
/* error check, return EPERM */
|
||||
if (mtype == PTHREAD_MUTEX_ERRORCHECK) return EPERM;
|
||||
if (mtype == PTHREAD_MUTEX_ERRORCHECK)
|
||||
return EPERM;
|
||||
|
||||
/* no thread waiting on this mutex */
|
||||
if (mutex->lock.owner == RT_NULL) return 0;
|
||||
if (mutex->lock.owner == RT_NULL)
|
||||
return 0;
|
||||
}
|
||||
|
||||
result = rt_mutex_release(&(mutex->lock));
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return EINVAL;
|
||||
}
|
||||
@ -190,7 +233,8 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex)
|
||||
{
|
||||
rt_err_t result;
|
||||
|
||||
if (!mutex) return EINVAL;
|
||||
if (!mutex)
|
||||
return EINVAL;
|
||||
if (mutex->attr == -1)
|
||||
{
|
||||
/* init mutex */
|
||||
@ -198,7 +242,8 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex)
|
||||
}
|
||||
|
||||
result = rt_mutex_take(&(mutex->lock), 0);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
return EBUSY;
|
||||
}
|
||||
|
@ -1,8 +1,33 @@
|
||||
/*
|
||||
* File : pthread_rwlock.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
*attr = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
@ -11,32 +36,39 @@ RTM_EXPORT(pthread_rwlockattr_init);
|
||||
|
||||
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
|
||||
{
|
||||
if (!attr) return EINVAL;
|
||||
if (!attr)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_destroy);
|
||||
|
||||
int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr, int *pshared)
|
||||
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
|
||||
int *pshared)
|
||||
{
|
||||
if (!attr || !pshared) return EINVAL;
|
||||
if (!attr || !pshared)
|
||||
return EINVAL;
|
||||
|
||||
*pshared = PTHREAD_PROCESS_PRIVATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_getpshared);
|
||||
|
||||
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
|
||||
{
|
||||
if (!attr || pshared != PTHREAD_PROCESS_PRIVATE) return EINVAL;
|
||||
if (!attr || pshared != PTHREAD_PROCESS_PRIVATE)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlockattr_setpshared);
|
||||
|
||||
int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t * attr)
|
||||
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
|
||||
const pthread_rwlockattr_t *attr)
|
||||
{
|
||||
if (!rwlock) return EINVAL;
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
|
||||
rwlock->attr = PTHREAD_PROCESS_PRIVATE;
|
||||
pthread_mutex_init(&(rwlock->rw_mutex), NULL);
|
||||
@ -55,16 +87,20 @@ int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) return 0; /* rwlock is not initialized */
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
return 0; /* rwlock is not initialized */
|
||||
|
||||
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
|
||||
if (rwlock->rw_refcount != 0 ||
|
||||
rwlock->rw_nwaitreaders != 0 || rwlock->rw_nwaitwriters != 0)
|
||||
rwlock->rw_nwaitreaders != 0 ||
|
||||
rwlock->rw_nwaitwriters != 0)
|
||||
{
|
||||
result = EBUSY;
|
||||
|
||||
return(EBUSY);
|
||||
}
|
||||
else
|
||||
@ -88,11 +124,13 @@ int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
|
||||
result = EBUSY;
|
||||
}
|
||||
}
|
||||
else result = EBUSY;
|
||||
else
|
||||
result = EBUSY;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
if (result == 0) pthread_mutex_destroy(&rwlock->rw_mutex);
|
||||
if (result == 0)
|
||||
pthread_mutex_destroy(&rwlock->rw_mutex);
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -102,8 +140,10 @@ int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -121,9 +161,11 @@ int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
|
||||
}
|
||||
|
||||
/* another reader has a read lock */
|
||||
if (result == 0) rwlock->rw_refcount++;
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount++;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return (result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_rdlock);
|
||||
@ -132,8 +174,10 @@ int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -144,16 +188,20 @@ int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
|
||||
rwlock->rw_refcount++; /* increment count of reader locks */
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_tryrdlock);
|
||||
|
||||
int pthread_rwlock_timedrdlock (pthread_rwlock_t * rwlock, const struct timespec *abstime)
|
||||
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
|
||||
const struct timespec *abstime)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -171,19 +219,24 @@ int pthread_rwlock_timedrdlock (pthread_rwlock_t * rwlock, const struct timespec
|
||||
}
|
||||
|
||||
/* another reader has a read lock */
|
||||
if (result == 0) rwlock->rw_refcount++;
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount++;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return (result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_timedrdlock);
|
||||
|
||||
int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime)
|
||||
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
|
||||
const struct timespec *abstime)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -196,12 +249,15 @@ int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec
|
||||
/* rw_mutex should have been taken again when returned from waiting */
|
||||
rwlock->rw_nwaitwriters--;
|
||||
|
||||
if (result != 0) break;
|
||||
if (result != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == 0) rwlock->rw_refcount = -1;
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount = -1;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_timedwrlock);
|
||||
@ -210,8 +266,10 @@ int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -222,6 +280,7 @@ int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
|
||||
rwlock->rw_refcount = -1; /* available, indicate a writer has it */
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_trywrlock);
|
||||
@ -230,8 +289,10 @@ int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -253,6 +314,7 @@ int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_unlock);
|
||||
@ -261,8 +323,10 @@ int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (!rwlock) return EINVAL;
|
||||
if (rwlock->attr == -1) pthread_rwlock_init(rwlock, NULL);
|
||||
if (!rwlock)
|
||||
return EINVAL;
|
||||
if (rwlock->attr == -1)
|
||||
pthread_rwlock_init(rwlock, NULL);
|
||||
|
||||
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
|
||||
return(result);
|
||||
@ -275,12 +339,15 @@ int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
|
||||
/* rw_mutex should have been taken again when returned from waiting */
|
||||
rwlock->rw_nwaitwriters--;
|
||||
|
||||
if (result != 0) break;
|
||||
if (result != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == 0) rwlock->rw_refcount = -1;
|
||||
if (result == 0)
|
||||
rwlock->rw_refcount = -1;
|
||||
|
||||
pthread_mutex_unlock(&rwlock->rw_mutex);
|
||||
|
||||
return(result);
|
||||
}
|
||||
RTM_EXPORT(pthread_rwlock_wrlock);
|
||||
|
@ -1,23 +1,51 @@
|
||||
/*
|
||||
* File : pthread_spin.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int pthread_spin_init (pthread_spinlock_t *lock, int pshared)
|
||||
{
|
||||
if (!lock) return EINVAL;
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
lock->lock = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_destroy (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock) return EINVAL;
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_spin_lock (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock) return EINVAL;
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
while (!(lock->lock))
|
||||
{
|
||||
@ -29,11 +57,13 @@ int pthread_spin_lock (pthread_spinlock_t *lock)
|
||||
|
||||
int pthread_spin_trylock (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock) return EINVAL;
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
|
||||
if (!(lock->lock))
|
||||
{
|
||||
lock->lock = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -42,8 +72,10 @@ int pthread_spin_trylock (pthread_spinlock_t *lock)
|
||||
|
||||
int pthread_spin_unlock (pthread_spinlock_t *lock)
|
||||
{
|
||||
if (!lock) return EINVAL;
|
||||
if (!(lock->lock)) return EPERM;
|
||||
if (!lock)
|
||||
return EINVAL;
|
||||
if (!(lock->lock))
|
||||
return EPERM;
|
||||
|
||||
lock->lock = 0;
|
||||
|
||||
|
@ -1,3 +1,27 @@
|
||||
/*
|
||||
* File : pthread_tls.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include "pthread_internal.h"
|
||||
|
||||
@ -15,7 +39,8 @@ void *pthread_getspecific(pthread_key_t key)
|
||||
ptd = _pthread_get_data(rt_thread_self());
|
||||
RT_ASSERT(ptd != NULL);
|
||||
|
||||
if (ptd->tls == NULL) return NULL;
|
||||
if (ptd->tls == NULL)
|
||||
return NULL;
|
||||
|
||||
if ((key < PTHREAD_KEY_MAX) && (_thread_keys[key].is_used))
|
||||
return ptd->tls[key];
|
||||
@ -40,6 +65,7 @@ int pthread_setspecific(pthread_key_t key, const void *value)
|
||||
if ((key < PTHREAD_KEY_MAX) && _thread_keys[key].is_used)
|
||||
{
|
||||
ptd->tls[key] = (void *)value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -62,18 +88,21 @@ int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
|
||||
*key = index;
|
||||
|
||||
rt_exit_critical();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
rt_exit_critical();
|
||||
|
||||
return EAGAIN;
|
||||
}
|
||||
RTM_EXPORT(pthread_key_create);
|
||||
|
||||
int pthread_key_delete(pthread_key_t key)
|
||||
{
|
||||
if (key >= PTHREAD_KEY_MAX) return EINVAL;
|
||||
if (key >= PTHREAD_KEY_MAX)
|
||||
return EINVAL;
|
||||
|
||||
rt_enter_critical();
|
||||
_thread_keys[key].is_used = 0;
|
||||
|
@ -1,8 +1,32 @@
|
||||
/*
|
||||
* File : sched.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#include <sched.h>
|
||||
|
||||
int sched_yield(void)
|
||||
{
|
||||
rt_thread_yield();
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(sched_yield);
|
||||
|
@ -1,3 +1,26 @@
|
||||
/*
|
||||
* File : sched.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
*/
|
||||
|
||||
#ifndef __SCHED_H__
|
||||
#define __SCHED_H__
|
||||
|
||||
|
@ -1,3 +1,27 @@
|
||||
/*
|
||||
* File : semaphore.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "semaphore.h"
|
||||
#include "pthread_internal.h"
|
||||
@ -40,6 +64,7 @@ static void posix_sem_delete(sem_t *psem)
|
||||
/* delete RT-Thread mqueue */
|
||||
rt_sem_delete(psem->sem);
|
||||
rt_free(psem);
|
||||
|
||||
return ;
|
||||
}
|
||||
}
|
||||
@ -68,6 +93,7 @@ int sem_close(sem_t *sem)
|
||||
if (sem == RT_NULL)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -94,6 +120,7 @@ int sem_destroy(sem_t *sem)
|
||||
if ((!sem) || !(sem->unamed))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -104,6 +131,7 @@ int sem_destroy(sem_t *sem)
|
||||
{
|
||||
rt_sem_release(&posix_sem_lock);
|
||||
rt_set_errno(EBUSY);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -131,12 +159,14 @@ int sem_unlink(const char *name)
|
||||
posix_sem_delete(psem);
|
||||
}
|
||||
rt_sem_release(&posix_sem_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
rt_sem_release(&posix_sem_lock);
|
||||
|
||||
/* no this entry */
|
||||
rt_set_errno(ENOENT);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(sem_unlink);
|
||||
@ -146,9 +176,11 @@ int sem_getvalue(sem_t *sem, int *sval)
|
||||
if (!sem || !sval)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
*sval = sem->sem->value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(sem_getvalue);
|
||||
@ -161,6 +193,7 @@ int sem_init(sem_t *sem, int pshared, unsigned int value)
|
||||
if (sem == RT_NULL)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -169,6 +202,7 @@ int sem_init(sem_t *sem, int pshared, unsigned int value)
|
||||
if (sem == RT_NULL)
|
||||
{
|
||||
rt_set_errno(ENOMEM);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -248,6 +282,7 @@ sem_t *sem_open(const char *name, int oflag, ...)
|
||||
}
|
||||
}
|
||||
rt_sem_release(&posix_sem_lock);
|
||||
|
||||
return sem;
|
||||
|
||||
__return:
|
||||
@ -262,6 +297,7 @@ __return:
|
||||
rt_sem_delete(sem->sem);
|
||||
rt_free(sem);
|
||||
}
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(sem_open);
|
||||
@ -273,13 +309,16 @@ int sem_post(sem_t *sem)
|
||||
if (!sem)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = rt_sem_release(sem->sem);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(sem_post);
|
||||
@ -289,7 +328,8 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
|
||||
rt_err_t result;
|
||||
rt_int32_t tick;
|
||||
|
||||
if (!sem || !abs_timeout) return EINVAL;
|
||||
if (!sem || !abs_timeout)
|
||||
return EINVAL;
|
||||
|
||||
/* calculate os tick */
|
||||
tick = clock_time_to_tick(abs_timeout);
|
||||
@ -298,11 +338,14 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
rt_set_errno(ETIMEDOUT);
|
||||
|
||||
return -1;
|
||||
}
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
rt_set_errno(EINTR);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(sem_timedwait);
|
||||
@ -314,6 +357,7 @@ int sem_trywait(sem_t *sem)
|
||||
if (!sem)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -321,11 +365,14 @@ int sem_trywait(sem_t *sem)
|
||||
if (result == -RT_ETIMEOUT)
|
||||
{
|
||||
rt_set_errno(EAGAIN);
|
||||
|
||||
return -1;
|
||||
}
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
rt_set_errno(EINTR);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(sem_trywait);
|
||||
@ -337,13 +384,16 @@ int sem_wait(sem_t *sem)
|
||||
if (!sem)
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = rt_sem_take(sem->sem, RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK) return 0;
|
||||
if (result == RT_EOK)
|
||||
return 0;
|
||||
|
||||
rt_set_errno(EINTR);
|
||||
|
||||
return -1;
|
||||
}
|
||||
RTM_EXPORT(sem_wait);
|
||||
|
@ -1,3 +1,27 @@
|
||||
/*
|
||||
* File : semaphore.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-10-26 Bernard the first version
|
||||
*/
|
||||
|
||||
#ifndef __POSIX_SEMAPHORE_H__
|
||||
#define __POSIX_SEMAPHORE_H__
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user