📄 pthread.c
字号:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_cond_signal(pthread_cond_t * cond)
{
int tid, status;
if (!cond || cond->defunct)
return PTHR_FAILURE;
if (msgQNumMsgs(cond->waite_msg_q) > 0) {
if (msgQReceive(cond->waite_msg_q, (char *)&tid, sizeof(int),
WAIT_FOREVER) == ERROR) {
printf("ERROR: pthread_cond_signal can't read waiter tid\n");
return PTHR_FAILURE;
}
if (taskResume(tid) == ERROR) {
printf("ERROR: pthread_cond_signal can't restart task %d\n", tid);
return PTHR_FAILURE;
}
}
return PTHR_SUCCESS;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_mutex_init
*
* DESCRIPTION:
* This function initializes a mutex. The new mutex may be used
* immediately for serializing critical resources.
*
* INPUTS:
* 1. pthread_mutex_t * mutex -- pointer to the mutex semaphore
* 2. NULL
*
* OUTPUTS:
* N/A
*
* RETURN:
* PTHR_SUCCESS -- success
* PTHR_FAILURE -- failure
*
* NOTES:
* The second parameter should be NULL.
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_mutex_init(pthread_mutex_t *mutex, char *dummy)
{
*mutex = (pthread_mutex_t)semBCreate(SEM_Q_PRIORITY, SEM_FULL);
if(mutex == NULL) return PTHR_FAILURE;
return PTHR_SUCCESS;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_mutex_init
*
* DESCRIPTION:
* The pthread_mutex_lock() function acquires ownership of the mutex
* specified. If the mutex is currently locked by another thread, the
* call to pthread_mutex_lock() blocks until that thread relinquishes
* ownership via a call to pthread_mutex_unlock().
*
* INPUTS:
* 1. pthread_mutex_t * mutex -- pointer to the mutex semaphore
*
* OUTPUTS:
* N/A
*
* RETURN:
* PTHR_SUCCESS -- success
* PTHR_FAILURE -- failure
*
* NOTES:
*
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_mutex_lock( pthread_mutex_t *mutex)
{
if(semTake((SEM_ID)*mutex, WAIT_FOREVER) == ERROR) return PTHR_FAILURE;
return PTHR_SUCCESS;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_mutex_unlock
*
* DESCRIPTION:
* This function unlocks the mutex specified.
*
* INPUTS:
* 1. pthread_mutex_t * mutex -- pointer to the mutex semaphore
*
* OUTPUTS:
* N/A
*
* RETURN:
* PTHR_SUCCESS -- success
* PTHR_FAILURE -- failure
*
* NOTES:
*
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
if(semGive((SEM_ID)*mutex) == ERROR) return PTHR_FAILURE;
return PTHR_SUCCESS;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_mutex_unlock
*
* DESCRIPTION:
* This routine terminates and deallocates any memory associated with
* a specified semaphore. Any pended thread will unblock and return
* PTHR_FAILURE.
*
* INPUTS:
* 1. pthread_mutex_t * mutex -- pointer to the mutex semaphore
*
* OUTPUTS:
* N/A
*
* RETURN:
* PTHR_SUCCESS -- success
* PTHR_FAILURE -- failure
*
* NOTES:
*
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_mutex_destory(pthread_mutex_t *mutex)
{
if(semDelete((SEM_ID)*mutex) == ERROR) return PTHR_FAILURE;
return PTHR_SUCCESS;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_get_num
*
* DESCRIPTION:
* This routine returns a free thread sequence number
*
* INPUTS:
* N/A
*
* OUTPUTS:
* N/A
*
* RETURN:
* sequency number
* -1 -- if failed
*
* NOTES:
*
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_get_num( void )
{
int i;
for(i = 0; i < PTHR_MAX_THREAD_NUM; i ++)
{
if(thread_seq_num[i] != 1)
{
thread_seq_num[i] = 1;
return i;
}
}
if(i == PTHR_MAX_THREAD_NUM) return -1;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_get_num
*
* DESCRIPTION:
* This routine init the pthread module. It is the first fucntion call
* before any other pthread function to be called.
* It inits the sequence number array with 0.
*
* INPUTS:
* N/A
*
* OUTPUTS:
* N/A
*
* RETURN:
* N/A
*
* NOTES:
*
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int ptherad_module_init( void )
{
int i;
if(inited == 1)
{
printf("ERROR: pthread inited already.\n");
return PTHR_FAILURE;
}
for( i = 0; i < PTHR_MAX_THREAD_NUM; i++)
{
thread_seq_num[i] = 0;
}
inited = 1;
return PTHR_SUCCESS;
}
/**************************************************************************
* FUNCTION NAME:
* pthread_create
*
* DESCRIPTION:
* This routine create athread and runs the C function start_routine in thead
* with the single pointer argument specified. If pthread_create() completes
* successfully, the pthread handle is stored in the contents of the location
* referred to by thread_id
*
* INPUTS:
* 1. pthread_mutex_t * mutex -- pointer to the mutex semaphore
*
* OUTPUTS:
* N/A
*
* RETURN:
* PTHR_SUCCESS -- success
* PTHR_FAILURE -- failure
*
* NOTES:
*
* AUTHOR:
* Rex Yan, <rexyan@lucent.com>
*
**************************************************************************
*/
int pthread_create(pthread_t *thread_id, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg, int arg_len)
{
int i;
char threadName[20];
int thread_num;
pthread_attr_t attr_tmp;
char temp_buff[PTHR_MAX_ARG_LEN];
if(inited == 0)
{
printf("ERROR: pthread module has not been inited yet.\n");
return PTHR_FAILURE;
}
if( arg_len != 0 )
{
memcpy(temp_buff, (char *)arg, arg_len);
}
thread_num = pthread_get_num();
if(thread_num < 0) return PTHR_FAILURE;
sprintf(threadName, "%s%d", "th", thread_num);
if(attr == NULL)
{
attr_tmp.priority = DEFAULT_PRI;
attr_tmp.stack_size = DEFAULT_STACK;
}
else
{
attr_tmp.priority = attr->priority;
attr_tmp.stack_size = attr->stack_size;
}
if( arg_len != 0 )
{
if(taskSpawn(threadName, attr_tmp.priority,
0,
attr_tmp.stack_size,
(FUNCPTR)start_routine,
(int)temp_buff,0,0,0,0,0,0,0,0,0) == ERROR)
{
printf("ERROR: create thread failed.\n");
return PTHR_FAILURE;
};
}
else
{
if(taskSpawn(threadName, attr_tmp.priority,
0,
attr_tmp.stack_size,
(FUNCPTR)start_routine,
0,0,0,0,0,0,0,0,0,0) == ERROR)
{
printf("ERROR: create thread failed.\n");
return PTHR_FAILURE;
};
}
return PTHR_SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -