2013-06-26 23:18:30 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-06-26 23:18:30 +08:00
|
|
|
*
|
2018-10-14 19:28:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-06-26 23:18:30 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2010-10-26 Bernard the first version
|
|
|
|
*/
|
|
|
|
|
2010-11-18 07:35:13 +08:00
|
|
|
#include <rtthread.h>
|
2013-08-12 22:21:29 +08:00
|
|
|
#include <string.h>
|
2010-11-18 07:35:13 +08:00
|
|
|
#include "semaphore.h"
|
2012-11-23 10:40:32 +08:00
|
|
|
#include "pthread_internal.h"
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
static sem_t *posix_sem_list = RT_NULL;
|
2010-11-24 08:04:16 +08:00
|
|
|
static struct rt_semaphore posix_sem_lock;
|
|
|
|
void posix_sem_system_init()
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_sem_init(&posix_sem_lock, "psem", 1, RT_IPC_FLAG_FIFO);
|
2010-11-24 08:04:16 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 19:04:08 +08:00
|
|
|
rt_inline void posix_sem_insert(sem_t *psem)
|
2010-11-24 08:04:16 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
psem->next = posix_sem_list;
|
|
|
|
posix_sem_list = psem;
|
2010-11-24 08:04:16 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 19:04:08 +08:00
|
|
|
static void posix_sem_delete(sem_t *psem)
|
2010-11-24 08:04:16 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
sem_t *iter;
|
|
|
|
if (posix_sem_list == psem)
|
|
|
|
{
|
|
|
|
posix_sem_list = psem->next;
|
|
|
|
|
|
|
|
rt_sem_delete(psem->sem);
|
|
|
|
rt_free(psem);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (iter = posix_sem_list; iter->next != RT_NULL; iter = iter->next)
|
|
|
|
{
|
|
|
|
if (iter->next == psem)
|
|
|
|
{
|
|
|
|
/* delete this mq */
|
|
|
|
if (psem->next != RT_NULL)
|
|
|
|
iter->next = psem->next;
|
|
|
|
else
|
|
|
|
iter->next = RT_NULL;
|
|
|
|
|
|
|
|
/* delete RT-Thread mqueue */
|
|
|
|
rt_sem_delete(psem->sem);
|
|
|
|
rt_free(psem);
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
}
|
2010-11-24 08:04:16 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 19:04:08 +08:00
|
|
|
static sem_t *posix_sem_find(const char* name)
|
2010-11-24 08:04:16 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
sem_t *iter;
|
|
|
|
rt_object_t object;
|
|
|
|
|
|
|
|
for (iter = posix_sem_list; iter != RT_NULL; iter = iter->next)
|
|
|
|
{
|
2020-11-24 11:18:54 +08:00
|
|
|
object = (rt_object_t)iter->sem;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
if (strncmp(object->name, name, RT_NAME_MAX) == 0)
|
|
|
|
{
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
2016-11-08 09:21:32 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return RT_NULL;
|
2010-11-24 08:04:16 +08:00
|
|
|
}
|
|
|
|
|
2010-11-18 07:35:13 +08:00
|
|
|
int sem_close(sem_t *sem)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
if (sem == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2010-11-24 08:04:16 +08:00
|
|
|
/* lock posix semaphore list */
|
|
|
|
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
|
2010-11-24 19:04:08 +08:00
|
|
|
sem->refcount --;
|
|
|
|
if (sem->refcount == 0)
|
2010-11-24 08:04:16 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
/* delete from posix semaphore list */
|
|
|
|
if (sem->unlinked)
|
|
|
|
posix_sem_delete(sem);
|
|
|
|
sem = RT_NULL;
|
2010-11-24 08:04:16 +08:00
|
|
|
}
|
|
|
|
rt_sem_release(&posix_sem_lock);
|
|
|
|
|
|
|
|
return 0;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_close);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_destroy(sem_t *sem)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
if ((!sem) || !(sem->unamed))
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2010-11-24 08:04:16 +08:00
|
|
|
/* lock posix semaphore list */
|
|
|
|
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
|
2010-11-24 19:04:08 +08:00
|
|
|
result = rt_sem_trytake(sem->sem);
|
2010-11-24 08:04:16 +08:00
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_sem_release(&posix_sem_lock);
|
|
|
|
rt_set_errno(EBUSY);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
return -1;
|
2010-11-24 08:04:16 +08:00
|
|
|
}
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2010-11-24 08:04:16 +08:00
|
|
|
/* destroy an unamed posix semaphore */
|
2013-06-26 23:18:30 +08:00
|
|
|
posix_sem_delete(sem);
|
2010-11-24 08:04:16 +08:00
|
|
|
rt_sem_release(&posix_sem_lock);
|
|
|
|
|
|
|
|
return 0;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_destroy);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_unlink(const char *name)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
sem_t *psem;
|
2010-11-24 08:04:16 +08:00
|
|
|
|
|
|
|
/* lock posix semaphore list */
|
|
|
|
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
|
|
|
|
psem = posix_sem_find(name);
|
|
|
|
if (psem != RT_NULL)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
psem->unlinked = 1;
|
|
|
|
if (psem->refcount == 0)
|
|
|
|
{
|
|
|
|
/* remove this semaphore */
|
|
|
|
posix_sem_delete(psem);
|
|
|
|
}
|
2010-11-24 08:04:16 +08:00
|
|
|
rt_sem_release(&posix_sem_lock);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2010-11-24 08:04:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rt_sem_release(&posix_sem_lock);
|
|
|
|
|
|
|
|
/* no this entry */
|
|
|
|
rt_set_errno(ENOENT);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2010-11-24 08:04:16 +08:00
|
|
|
return -1;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_unlink);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_getvalue(sem_t *sem, int *sval)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
if (!sem || !sval)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*sval = sem->sem->value;
|
|
|
|
|
|
|
|
return 0;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_getvalue);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_init(sem_t *sem, int pshared, unsigned int value)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
char name[RT_NAME_MAX];
|
|
|
|
static rt_uint16_t psem_number = 0;
|
|
|
|
|
|
|
|
if (sem == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_snprintf(name, sizeof(name), "psem%02d", psem_number++);
|
|
|
|
sem->sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
|
|
|
|
if (sem == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(ENOMEM);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize posix semaphore */
|
|
|
|
sem->refcount = 1;
|
|
|
|
sem->unlinked = 0;
|
|
|
|
sem->unamed = 1;
|
2010-11-24 08:04:16 +08:00
|
|
|
/* lock posix semaphore list */
|
|
|
|
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
|
2010-11-24 19:04:08 +08:00
|
|
|
posix_sem_insert(sem);
|
2010-11-24 08:04:16 +08:00
|
|
|
rt_sem_release(&posix_sem_lock);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return 0;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_init);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
sem_t *sem_open(const char *name, int oflag, ...)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
sem_t* sem;
|
|
|
|
va_list arg;
|
2014-07-19 06:52:35 +08:00
|
|
|
mode_t mode;
|
2013-06-26 23:18:30 +08:00
|
|
|
unsigned int value;
|
2010-11-24 08:04:16 +08:00
|
|
|
|
|
|
|
sem = RT_NULL;
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2010-11-24 08:04:16 +08:00
|
|
|
/* lock posix semaphore list */
|
|
|
|
rt_sem_take(&posix_sem_lock, RT_WAITING_FOREVER);
|
2013-06-26 23:18:30 +08:00
|
|
|
if (oflag & O_CREAT)
|
|
|
|
{
|
|
|
|
va_start(arg, oflag);
|
2014-07-19 06:52:35 +08:00
|
|
|
mode = (mode_t) va_arg( arg, unsigned int); mode = mode;
|
2013-06-26 23:18:30 +08:00
|
|
|
value = va_arg( arg, unsigned int);
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
if (oflag & O_EXCL)
|
|
|
|
{
|
|
|
|
if (posix_sem_find(name) != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(EEXIST);
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sem = (sem_t*) rt_malloc (sizeof(struct posix_sem));
|
|
|
|
if (sem == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(ENFILE);
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create RT-Thread semaphore */
|
|
|
|
sem->sem = rt_sem_create(name, value, RT_IPC_FLAG_FIFO);
|
|
|
|
if (sem->sem == RT_NULL) /* create failed */
|
|
|
|
{
|
|
|
|
rt_set_errno(ENFILE);
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
/* initialize reference count */
|
|
|
|
sem->refcount = 1;
|
|
|
|
sem->unlinked = 0;
|
|
|
|
sem->unamed = 0;
|
|
|
|
|
|
|
|
/* insert semaphore to posix semaphore list */
|
|
|
|
posix_sem_insert(sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* find semaphore */
|
|
|
|
sem = posix_sem_find(name);
|
|
|
|
if (sem != RT_NULL)
|
|
|
|
{
|
|
|
|
sem->refcount ++; /* increase reference count */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_set_errno(ENOENT);
|
|
|
|
goto __return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_sem_release(&posix_sem_lock);
|
|
|
|
|
|
|
|
return sem;
|
2010-11-24 08:04:16 +08:00
|
|
|
|
|
|
|
__return:
|
2013-06-26 23:18:30 +08:00
|
|
|
/* release lock */
|
|
|
|
rt_sem_release(&posix_sem_lock);
|
|
|
|
|
|
|
|
/* release allocated memory */
|
|
|
|
if (sem != RT_NULL)
|
|
|
|
{
|
|
|
|
/* delete RT-Thread semaphore */
|
|
|
|
if (sem->sem != RT_NULL)
|
|
|
|
rt_sem_delete(sem->sem);
|
|
|
|
rt_free(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_NULL;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_open);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_post(sem_t *sem)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
if (!sem)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-24 08:04:16 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
result = rt_sem_release(sem->sem);
|
|
|
|
if (result == RT_EOK)
|
|
|
|
return 0;
|
2010-11-24 08:04:16 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_set_errno(EINVAL);
|
2010-11-24 08:04:16 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_post);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
rt_int32_t tick;
|
|
|
|
|
|
|
|
if (!sem || !abs_timeout)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* calculate os tick */
|
|
|
|
tick = clock_time_to_tick(abs_timeout);
|
2016-11-08 09:21:32 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
result = rt_sem_take(sem->sem, tick);
|
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
{
|
|
|
|
rt_set_errno(ETIMEDOUT);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (result == RT_EOK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rt_set_errno(EINTR);
|
|
|
|
|
|
|
|
return -1;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_timedwait);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_trywait(sem_t *sem)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
if (!sem)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:21:32 +08:00
|
|
|
result = rt_sem_take(sem->sem, 0);
|
2013-06-26 23:18:30 +08:00
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
{
|
|
|
|
rt_set_errno(EAGAIN);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (result == RT_EOK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rt_set_errno(EINTR);
|
|
|
|
|
|
|
|
return -1;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_trywait);
|
2010-11-18 07:35:13 +08:00
|
|
|
|
|
|
|
int sem_wait(sem_t *sem)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if (!sem)
|
|
|
|
{
|
|
|
|
rt_set_errno(EINVAL);
|
2010-11-24 08:04:16 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2010-11-18 07:35:13 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
result = rt_sem_take(sem->sem, RT_WAITING_FOREVER);
|
|
|
|
if (result == RT_EOK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rt_set_errno(EINTR);
|
|
|
|
|
|
|
|
return -1;
|
2010-11-18 07:35:13 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(sem_wait);
|
2010-11-18 07:35:13 +08:00
|
|
|
|