Example #1
0
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 ( (result = pthread_mutex_lock(&rwlock->rw_mutex)) != 0)
        return(result);

    if (rwlock->rw_refcount != 0 ||
        rwlock->rw_nwaitreaders != 0 ||
        rwlock->rw_nwaitwriters != 0)
    {
        result = EBUSY;

        return result;
    }
    else
    {
        /* check whether busy */
        result = rt_sem_trytake(&(rwlock->rw_condreaders.sem));
        if (result == RT_EOK)
        {
            result = rt_sem_trytake(&(rwlock->rw_condwriters.sem));
            if (result == RT_EOK)
            {
                rt_sem_release(&(rwlock->rw_condreaders.sem));
                rt_sem_release(&(rwlock->rw_condwriters.sem));

                pthread_cond_destroy(&rwlock->rw_condreaders);
                pthread_cond_destroy(&rwlock->rw_condwriters);
            }
            else
            {
                rt_sem_release(&(rwlock->rw_condreaders.sem));
                result = EBUSY;
            }
        }
        else
            result = EBUSY;
    }

    pthread_mutex_unlock(&rwlock->rw_mutex);
    if (result == 0)
        pthread_mutex_destroy(&rwlock->rw_mutex);
    
    return result;
}
int pthread_cond_broadcast(pthread_cond_t *cond)
{
    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();

    return 0;
}
Example #3
0
void tc_thread_entry(void* parameter)
{
	struct finsh_syscall* index;

	/* create tc semaphore */
	rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO);

	while (_tc_stat & TC_STAT_RUNNING)
	{
		for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
		{
			/* search testcase */
			if (rt_strstr(index->name, _tc_prefix) == index->name)
			{
				long tick;

				_tc_current = index->name + 4;
				rt_kprintf("Run TestCase: %s\n", _tc_current);
				_tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
				tick = index->func();
				if (tick > 0)
				{
					rt_sem_take(&_tc_sem, tick * _tc_scale);

					if (_tc_cleanup != RT_NULL)
					{
						/* perform testcase cleanup */
						_tc_cleanup();
						_tc_cleanup = RT_NULL;
					}

					rt_sem_trytake(&_tc_sem);/* by nl1031 */

					if (_tc_stat & TC_STAT_FAILED)
						rt_kprintf("TestCase[%s] failed\n", _tc_current);
					else
						rt_kprintf("TestCase[%s] passed\n", _tc_current);
				}
				else
				{
					if (_tc_cleanup != RT_NULL)
					{
						/* perform testcase cleanup */
						_tc_cleanup();
						_tc_cleanup = RT_NULL;
					}
				}
			}
		}
	}

	rt_kprintf("RT-Thread TestCase Running Done!\n");
	/* detach tc semaphore */
	rt_sem_detach(&_tc_sem);
}
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 */

    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;

    return 0;
}