2010-11-15 17:46:50 +08:00
|
|
|
#include <pthread.h>
|
2011-02-21 16:26:35 +08:00
|
|
|
#include "pthread_internal.h"
|
2010-11-12 18:26:36 +08:00
|
|
|
|
2010-11-15 08:25:37 +08:00
|
|
|
int pthread_condattr_destroy(pthread_condattr_t *attr)
|
|
|
|
{
|
|
|
|
if (!attr) return EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_condattr_destroy);
|
2010-11-15 08:25:37 +08:00
|
|
|
|
|
|
|
int pthread_condattr_init(pthread_condattr_t *attr)
|
|
|
|
{
|
|
|
|
if (!attr) return EINVAL;
|
|
|
|
*attr = PTHREAD_PROCESS_PRIVATE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_condattr_init);
|
2010-11-15 08:25:37 +08:00
|
|
|
|
|
|
|
int pthread_condattr_getclock(const pthread_condattr_t *attr,
|
|
|
|
clockid_t *clock_id)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_condattr_getclock);
|
2010-11-15 08:25:37 +08:00
|
|
|
|
|
|
|
int pthread_condattr_setclock(pthread_condattr_t *attr,
|
|
|
|
clockid_t clock_id)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_condattr_setclock);
|
2010-11-15 08:25:37 +08:00
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
|
|
|
|
{
|
|
|
|
if (!attr || !pshared) return EINVAL;
|
|
|
|
|
|
|
|
*pshared = PTHREAD_PROCESS_PRIVATE;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_condattr_getpshared);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
|
|
|
int pthread_condattr_setpshared(pthread_condattr_t*attr, int pshared)
|
|
|
|
{
|
|
|
|
if ((pshared != PTHREAD_PROCESS_PRIVATE) && (pshared != PTHREAD_PROCESS_SHARED))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (pshared != PTHREAD_PROCESS_PRIVATE)
|
|
|
|
return ENOSYS;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_condattr_setpshared);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
|
|
|
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
|
|
|
|
{
|
2010-11-15 08:25:37 +08:00
|
|
|
rt_err_t result;
|
2011-02-21 16:26:35 +08:00
|
|
|
char cond_name[RT_NAME_MAX];
|
2010-11-15 08:25:37 +08:00
|
|
|
static rt_uint16_t cond_num = 0;
|
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
/* parameter check */
|
|
|
|
if (cond == RT_NULL) return EINVAL;
|
|
|
|
if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE)) return EINVAL;
|
|
|
|
|
2010-11-15 08:25:37 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
/* detach the object from system object container */
|
|
|
|
rt_object_detach(&(cond->sem.parent.parent));
|
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_cond_init);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
|
|
|
int pthread_cond_destroy(pthread_cond_t *cond)
|
|
|
|
{
|
2010-11-15 08:25:37 +08:00
|
|
|
rt_err_t result;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* clean condition */
|
|
|
|
rt_memset(cond, 0, sizeof(pthread_cond_t));
|
|
|
|
cond->attr = -1;
|
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_cond_destroy);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
|
|
|
int pthread_cond_broadcast(pthread_cond_t *cond)
|
|
|
|
{
|
2010-11-15 08:25:37 +08:00
|
|
|
rt_err_t result;
|
|
|
|
if (cond->attr == -1)
|
|
|
|
pthread_cond_init(cond, RT_NULL);
|
|
|
|
|
|
|
|
rt_enter_critical();
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* try to take condition semaphore */
|
|
|
|
result = rt_sem_trytake(&(cond->sem));
|
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
{
|
|
|
|
/* it's timeout, release this semaphore */
|
|
|
|
rt_sem_release(&(cond->sem));
|
|
|
|
}
|
|
|
|
else if (result == RT_EOK)
|
|
|
|
{
|
|
|
|
/* has taken this semaphore, release it */
|
|
|
|
rt_sem_release(&(cond->sem));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_exit_critical();
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_exit_critical();
|
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_cond_broadcast);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
|
|
|
int pthread_cond_signal(pthread_cond_t *cond)
|
|
|
|
{
|
2010-11-15 08:25:37 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
if (cond->attr == -1)
|
|
|
|
pthread_cond_init(cond, RT_NULL);
|
|
|
|
|
|
|
|
result = rt_sem_release(&(cond->sem));
|
|
|
|
if (result == RT_EOK) return 0;
|
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_cond_signal);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
2010-11-15 08:25:37 +08:00
|
|
|
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;
|
|
|
|
/* check whether initialized */
|
|
|
|
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;
|
|
|
|
/* unlock a mutex failed */
|
|
|
|
if (pthread_mutex_unlock(mutex) != 0)
|
|
|
|
return -RT_ERROR;
|
|
|
|
|
|
|
|
result = rt_sem_take(&(cond->sem), timeout);
|
|
|
|
/* lock mutex again */
|
2010-11-18 07:35:13 +08:00
|
|
|
pthread_mutex_lock(mutex);
|
2010-11-15 08:25:37 +08:00
|
|
|
return result;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(_pthread_cond_timedwait);
|
2010-11-15 08:25:37 +08:00
|
|
|
|
2010-11-12 18:26:36 +08:00
|
|
|
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
|
|
|
{
|
2010-11-15 08:25:37 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
|
|
|
|
if (result == RT_EOK) return 0;
|
|
|
|
|
|
|
|
return EINVAL;
|
2010-11-12 18:26:36 +08:00
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_cond_wait);
|
2010-11-12 18:26:36 +08:00
|
|
|
|
|
|
|
int pthread_cond_timedwait(pthread_cond_t *cond,
|
|
|
|
pthread_mutex_t * mutex,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
2010-11-22 17:52:06 +08:00
|
|
|
int timeout;
|
2010-11-15 08:25:37 +08:00
|
|
|
rt_err_t result;
|
2010-11-12 18:26:36 +08:00
|
|
|
|
2011-02-21 16:26:35 +08:00
|
|
|
timeout = clock_time_to_tick(abstime);
|
2010-11-15 08:25:37 +08:00
|
|
|
result = _pthread_cond_timedwait(cond, mutex, timeout);
|
|
|
|
if (result == RT_EOK) return 0;
|
|
|
|
if (result == -RT_ETIMEOUT) return ETIMEDOUT;
|
|
|
|
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2012-11-23 10:40:32 +08:00
|
|
|
RTM_EXPORT(pthread_cond_timedwait);
|
|
|
|
|