4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-01-30 06:00:27 +08:00

Embedded GPLv2 license in pthreads

This commit is contained in:
yiyue.fang 2013-06-26 23:18:30 +08:00
parent 773990abdb
commit 8ab2b287b8
18 changed files with 2239 additions and 1617 deletions

View File

@ -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>
@ -70,11 +81,12 @@ int clock_time_to_tick(const struct timespec *time)
}
RTM_EXPORT(clock_time_to_tick);
int clock_getres (clockid_t clockid, struct timespec *res)
int clock_getres(clockid_t clockid, struct timespec *res)
{
if ((clockid != CLOCK_REALTIME) || (res == RT_NULL))
{
rt_set_errno(EINVAL);
return -1;
}
@ -85,13 +97,14 @@ int clock_getres (clockid_t clockid, struct timespec *res)
}
RTM_EXPORT(clock_getres);
int clock_gettime (clockid_t clockid, struct timespec *tp)
int clock_gettime(clockid_t clockid, struct timespec *tp)
{
rt_tick_t tick;
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
{
rt_set_errno(EINVAL);
return -1;
}
@ -105,7 +118,7 @@ int clock_gettime (clockid_t clockid, struct timespec *tp)
}
RTM_EXPORT(clock_gettime);
int clock_settime (clockid_t clockid, const struct timespec *tp)
int clock_settime(clockid_t clockid, const struct timespec *tp)
{
int second;
rt_tick_t tick;
@ -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;
}

View File

@ -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,8 +128,9 @@ 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;
attr = (struct mq_attr *) va_arg(arg, struct mq_attr *);
mode = (mode_t)va_arg(arg, unsigned int);
mode = mode;
attr = (struct mq_attr *)va_arg(arg, struct mq_attr *);
va_end(arg);
if (oflag & O_EXCL)
@ -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);

View File

@ -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);

View File

@ -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 */
};

View File

@ -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"
@ -35,10 +58,10 @@ static void _pthread_cleanup(rt_thread_t tid)
}
}
static void pthread_entry_stub(void* parameter)
static void pthread_entry_stub(void *parameter)
{
_pthread_data_t *ptd;
void* value;
void *value;
ptd = (_pthread_data_t*)parameter;
@ -48,11 +71,12 @@ 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;
void* stack;
void *stack;
char name[RT_NAME_MAX];
static rt_uint16_t pthread_number = 0;
_pthread_data_t *ptd;
@ -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,21 +252,23 @@ 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;
}
RTM_EXPORT(pthread_join);
void pthread_exit (void* value)
void pthread_exit (void *value)
{
_pthread_data_t* ptd;
_pthread_cleanup_t* cleanup;
_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());
@ -251,7 +294,7 @@ void pthread_exit (void* value)
/* destruct thread local key */
if (ptd->tls != RT_NULL)
{
void* data;
void *data;
rt_uint32_t index;
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
@ -282,7 +325,7 @@ void pthread_exit (void* value)
}
RTM_EXPORT(pthread_exit);
int pthread_once(pthread_once_t * once_control, void (*init_routine) (void))
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
{
RT_ASSERT(once_control != RT_NULL);
RT_ASSERT(init_routine != RT_NULL);
@ -316,8 +359,8 @@ RTM_EXPORT(pthread_kill);
void pthread_cleanup_pop(int execute)
{
_pthread_data_t* ptd;
_pthread_cleanup_t* cleanup;
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
/* get posix thread data */
ptd = _pthread_get_data(rt_thread_self());
@ -343,14 +386,14 @@ RTM_EXPORT(pthread_cleanup_pop);
void pthread_cleanup_push(void (*routine)(void*), void *arg)
{
_pthread_data_t* ptd;
_pthread_cleanup_t* cleanup;
_pthread_data_t *ptd;
_pthread_cleanup_t *cleanup;
/* get posix thread data */
ptd = _pthread_get_data(rt_thread_self());
RT_ASSERT(ptd != RT_NULL);
cleanup = (_pthread_cleanup_t*)rt_malloc(sizeof(_pthread_cleanup_t));
cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
if (cleanup != RT_NULL)
{
cleanup->cleanup_func = routine;
@ -394,7 +437,7 @@ RTM_EXPORT(pthread_cleanup_push);
int pthread_setcancelstate(int state, int *oldstate)
{
_pthread_data_t* ptd;
_pthread_data_t *ptd;
/* get posix thread data */
ptd = _pthread_get_data(rt_thread_self());
@ -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;
@ -414,7 +458,7 @@ RTM_EXPORT(pthread_setcancelstate);
int pthread_setcanceltype(int type, int *oldtype)
{
_pthread_data_t* ptd;
_pthread_data_t *ptd;
/* get posix thread data */
ptd = _pthread_get_data(rt_thread_self());
@ -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,17 +484,20 @@ 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);
int pthread_cancel(pthread_t thread)
{
_pthread_data_t* ptd;
_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);

View File

@ -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,
@ -193,7 +207,7 @@ int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t * mutex,
pthread_mutex_t *mutex,
const struct timespec *abstime);
/* pthread rwlock interface */
@ -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 */

View File

@ -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,11 +55,12 @@ 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);
int pthread_attr_setdetachstate(pthread_attr_t * attr, int state)
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
{
RT_ASSERT(attr != RT_NULL);
@ -43,11 +68,12 @@ int pthread_attr_setdetachstate(pthread_attr_t * attr, int state)
return EINVAL;
attr->detachstate = state;
return 0;
}
RTM_EXPORT(pthread_attr_setdetachstate);
int pthread_attr_getdetachstate(pthread_attr_t const * attr, int *state)
int pthread_attr_getdetachstate(pthread_attr_t const *attr, int *state)
{
RT_ASSERT(attr != RT_NULL);
@ -57,11 +83,12 @@ int pthread_attr_getdetachstate(pthread_attr_t const * attr, int *state)
}
RTM_EXPORT(pthread_attr_getdetachstate);
int pthread_attr_setschedpolicy(pthread_attr_t * attr, int policy)
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,63 +98,74 @@ 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);
int pthread_attr_setstacksize(pthread_attr_t * attr, size_t stack_size)
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);
int pthread_attr_getstacksize(pthread_attr_t const * attr, size_t *stack_size)
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);
int pthread_attr_setstackaddr(pthread_attr_t * attr, void * stack_addr)
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack_addr)
{
RT_ASSERT(attr != RT_NULL);
return ENOTSUP;
}
RTM_EXPORT(pthread_attr_setstackaddr);
int pthread_attr_getstackaddr(pthread_attr_t const * attr, void ** stack_addr)
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);
@ -149,12 +189,12 @@ int pthread_attr_getstack(pthread_attr_t const * attr, void **stack_base, size_t
}
RTM_EXPORT(pthread_attr_getstack);
int pthread_attr_setguardsize(pthread_attr_t * attr, size_t guard_size)
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard_size)
{
return ENOTSUP;
}
int pthread_attr_getguardsize(pthread_attr_t const * attr, size_t *guard_size)
int pthread_attr_getguardsize(pthread_attr_t const *attr, size_t *guard_size)
{
return ENOTSUP;
}

View File

@ -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);

View File

@ -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,14 +212,15 @@ 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;
}
RTM_EXPORT(pthread_cond_wait);
int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t * mutex,
pthread_mutex_t *mutex,
const struct timespec *abstime)
{
int timeout;
@ -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;
}

View File

@ -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__
@ -6,17 +30,17 @@
struct _pthread_cleanup
{
void (*cleanup_func)(void* parameter);
void* parameter;
void (*cleanup_func)(void *parameter);
void *parameter;
struct _pthread_cleanup* next;
struct _pthread_cleanup *next;
};
typedef struct _pthread_cleanup _pthread_cleanup_t;
struct _pthread_key_data
{
int is_used;
void (*destructor)(void* parameter);
void (*destructor)(void *parameter);
};
typedef struct _pthread_key_data _pthread_key_data_t;
@ -27,11 +51,11 @@ struct _pthread_data
pthread_attr_t attr;
rt_thread_t tid;
void* (*thread_entry)(void* parameter);
void* thread_parameter;
void* (*thread_entry)(void *parameter);
void *thread_parameter;
/* return value */
void* return_value;
void *return_value;
/* semaphore for joinable thread */
rt_sem_t joinable_sem;
@ -41,17 +65,17 @@ struct _pthread_data
volatile rt_uint8_t canceltype;
volatile rt_uint8_t canceled;
_pthread_cleanup_t* cleanup;
_pthread_cleanup_t *cleanup;
void** tls; /* thread-local storage area */
};
typedef struct _pthread_data _pthread_data_t;
rt_inline _pthread_data_t* _pthread_get_data(pthread_t thread)
rt_inline _pthread_data_t *_pthread_get_data(pthread_t thread)
{
_pthread_data_t* ptd;
_pthread_data_t *ptd;
RT_ASSERT(thread != RT_NULL);
ptd = (_pthread_data_t*)thread->user_data;
ptd = (_pthread_data_t *)thread->user_data;
RT_ASSERT(ptd != RT_NULL);
RT_ASSERT(ptd->magic == PTHREAD_MAGIC);

View File

@ -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;
}

View File

@ -1,42 +1,74 @@
/*
* 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)
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
{
if (!attr) return EINVAL;
if (!attr)
return EINVAL;
*attr = PTHREAD_PROCESS_PRIVATE;
return 0;
}
RTM_EXPORT(pthread_rwlockattr_init);
int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr)
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)
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,24 +124,28 @@ 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;
}
RTM_EXPORT(pthread_rwlock_destroy);
int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
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)
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
/* give preference to waiting writers */
@ -121,19 +161,23 @@ 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);
int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
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,21 +219,26 @@ 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)
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
while (rwlock->rw_refcount != 0)
@ -196,24 +249,29 @@ 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);
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
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)
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
if (rwlock->rw_refcount != 0)
@ -222,16 +280,19 @@ 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);
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
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,18 +314,21 @@ int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
}
pthread_mutex_unlock(&rwlock->rw_mutex);
return(result);
}
RTM_EXPORT(pthread_rwlock_unlock);
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
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)
if ((result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
return(result);
while (rwlock->rw_refcount != 0)
@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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__

View File

@ -1,8 +1,32 @@
/*
* 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"
static sem_t* posix_sem_list = RT_NULL;
static sem_t *posix_sem_list = RT_NULL;
static struct rt_semaphore posix_sem_lock;
void posix_sem_system_init()
{
@ -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);

View File

@ -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__