嵌入式 线程退出pthread_setcanceltype以及pthread_self以及pthread_cancel
2014-05-20 09:59
411 查看
/******************************************************************************
* function : Create the thread to get stream from file and send to adec
******************************************************************************/
HI_S32 SAMPLE_COMM_AUDIO_Playing_Timer(void *argv)
{
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta = 1;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 0;
int timer;
int tmp_time,now_time;
int ret;
timer = get_current_time_second() + AUDIO_PLAY_MAX_TIME;
do
{
if(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta == 0)
{
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 1;
return 0;
}
now_time = get_current_time_second();
tmp_time = now_time;
if(tmp_time >= timer)
{
pthread_cancel(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_pthread_id);
printf("\nIT'S THE TIME\n\n");
ret = HI_MPI_ADEC_ClearChnBuf(0);
if(ret != 0)
{
printf("%s: HI_MPI_ADEC_ClearChnBuf failed with %#x!\n",__FUNCTION__, ret);
}
ret = HI_MPI_AO_ClearChnBuf(0 , 0);
if(ret != 0)
{
printf("%s: HI_MPI_AO_ClearChnBuf failed with %#x!\n", __FUNCTION__, ret);
}
SAMPLE_ADEC_S * pstAdec = &gs_stSampleAdec[0];
pstAdec->bStart = HI_FALSE;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_sta = 0;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 1;
return -1;
}
}while(1);
}
/******************************************************************************
* function : get stream from file, and send it to Adec
******************************************************************************/
void *SAMPLE_COMM_AUDIO_AdecProc(void *parg)
{
HI_S32 s32Ret;
AUDIO_STREAM_S stAudioStream;
HI_S32 s32AdecChn;
HI_S32 myseek = 0;
SAMPLE_ADEC_S *pstAdecCtl = (SAMPLE_ADEC_S *)parg;
s32AdecChn = pstAdecCtl->AdChn;
int ret;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_pthread_id = pthread_self();
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_sta = 1;
printf("BEGIN PLAY\n\n");
while (HI_TRUE == pstAdecCtl->bStart)
{
if(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_grabs_sta == 1)
{
printf("TO BE GRABS\n");
ret = HI_MPI_ADEC_ClearChnBuf(0);
if(ret != 0)
{
printf("%s: HI_MPI_ADEC_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
ret = HI_MPI_AO_ClearChnBuf(0 , 0);
if(ret != 0)
{
printf("%s: HI_MPI_AO_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_grabs_sta = 0;
break;
}
/* here only demo adec streaming sending mode, but pack sending mode is commended */
stAudioStream.pStream =(void *)(pstAdecCtl->AudioNode->audio_buf_pointer) + myseek;
stAudioStream.u32Len = ((pstAdecCtl->AudioNode->audio_data_len - myseek) > 640) ? 640 : (pstAdecCtl->AudioNode->audio_data_len - myseek);
myseek += stAudioStream.u32Len;
if(stAudioStream.u32Len <= 0)
{
ret = HI_MPI_ADEC_ClearChnBuf(0);
if(ret != 0)
{
printf("%s: HI_MPI_ADEC_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
ret = HI_MPI_AO_ClearChnBuf(0 , 0);
if(ret != 0)
{
printf("%s: HI_MPI_AO_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
break;
}
s32Ret = HI_MPI_ADEC_SendStream(s32AdecChn, &stAudioStream, HI_TRUE);
if (s32Ret != 0)
{
printf("%s: HI_MPI_ADEC_SendStream(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, s32Ret);
break;
}
}
if(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta == 0)
{
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 1;
}
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta = 0;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_sta = 0;
pstAdecCtl->bStart = HI_FALSE;
return NULL;
}
* function : Create the thread to get stream from file and send to adec
******************************************************************************/
HI_S32 SAMPLE_COMM_AUDIO_Playing_Timer(void *argv)
{
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta = 1;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 0;
int timer;
int tmp_time,now_time;
int ret;
timer = get_current_time_second() + AUDIO_PLAY_MAX_TIME;
do
{
if(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta == 0)
{
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 1;
return 0;
}
now_time = get_current_time_second();
tmp_time = now_time;
if(tmp_time >= timer)
{
pthread_cancel(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_pthread_id);
printf("\nIT'S THE TIME\n\n");
ret = HI_MPI_ADEC_ClearChnBuf(0);
if(ret != 0)
{
printf("%s: HI_MPI_ADEC_ClearChnBuf failed with %#x!\n",__FUNCTION__, ret);
}
ret = HI_MPI_AO_ClearChnBuf(0 , 0);
if(ret != 0)
{
printf("%s: HI_MPI_AO_ClearChnBuf failed with %#x!\n", __FUNCTION__, ret);
}
SAMPLE_ADEC_S * pstAdec = &gs_stSampleAdec[0];
pstAdec->bStart = HI_FALSE;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_sta = 0;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 1;
return -1;
}
}while(1);
}
/******************************************************************************
* function : get stream from file, and send it to Adec
******************************************************************************/
void *SAMPLE_COMM_AUDIO_AdecProc(void *parg)
{
HI_S32 s32Ret;
AUDIO_STREAM_S stAudioStream;
HI_S32 s32AdecChn;
HI_S32 myseek = 0;
SAMPLE_ADEC_S *pstAdecCtl = (SAMPLE_ADEC_S *)parg;
s32AdecChn = pstAdecCtl->AdChn;
int ret;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_pthread_id = pthread_self();
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_sta = 1;
printf("BEGIN PLAY\n\n");
while (HI_TRUE == pstAdecCtl->bStart)
{
if(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_grabs_sta == 1)
{
printf("TO BE GRABS\n");
ret = HI_MPI_ADEC_ClearChnBuf(0);
if(ret != 0)
{
printf("%s: HI_MPI_ADEC_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
ret = HI_MPI_AO_ClearChnBuf(0 , 0);
if(ret != 0)
{
printf("%s: HI_MPI_AO_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_grabs_sta = 0;
break;
}
/* here only demo adec streaming sending mode, but pack sending mode is commended */
stAudioStream.pStream =(void *)(pstAdecCtl->AudioNode->audio_buf_pointer) + myseek;
stAudioStream.u32Len = ((pstAdecCtl->AudioNode->audio_data_len - myseek) > 640) ? 640 : (pstAdecCtl->AudioNode->audio_data_len - myseek);
myseek += stAudioStream.u32Len;
if(stAudioStream.u32Len <= 0)
{
ret = HI_MPI_ADEC_ClearChnBuf(0);
if(ret != 0)
{
printf("%s: HI_MPI_ADEC_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
ret = HI_MPI_AO_ClearChnBuf(0 , 0);
if(ret != 0)
{
printf("%s: HI_MPI_AO_ClearChnBuf(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, ret);
}
break;
}
s32Ret = HI_MPI_ADEC_SendStream(s32AdecChn, &stAudioStream, HI_TRUE);
if (s32Ret != 0)
{
printf("%s: HI_MPI_ADEC_SendStream(%d) failed with %#x!\n",\
__FUNCTION__, s32AdecChn, s32Ret);
break;
}
}
if(joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta == 0)
{
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_close_sta = 1;
}
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_timer_sta = 0;
joseph_ipnc_param.joseph_ipnc_audio_attr.joseph_ipnc_a_playing_sta = 0;
pstAdecCtl->bStart = HI_FALSE;
return NULL;
}
相关文章推荐
- pthread_setcanceltype 线程取消
- 使用 pthread_testcancel 作为pthread_cancel 时 线程的退出点函数
- pthread_cancel 线程取消以及锁的释放
- 2线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_setcancelstate() pthread_setcanceltype() pthread_testcancel()
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_setcancelstate()和pthread_setcanceltype()
- pthread_cancel 线程取消以及锁的释放
- POSIX定时器timer_create()以及线程中的gettid() 和pthread_self()
- pthread_cancel 完美退出线程
- 线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_testcancel函数以及pthread_setcancelstate
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- 线程退出处理函数 pthread_cleanup_push pthread_cleanup_pop
- 如何不使用pthread_cancel而杀死线程
- linux多线程之pthread_cancel结束线程
- pthread_cancel 进程和线程的讲解
- 6、一个 pthread_cancel 引起的线程死锁(转)
- 线程堆栈大小 pthread_attr_setstacksize 的使用【转】