📄 main_api_thread.c
字号:
/* set tabstop=4 *//******************************************************************************** * * * Copyright(C) 2004 Penta-Micro * * * * ALL RIGHT RESERVED * * * * This software is the property of Penta-Micro and is furnished under * * license by Penta-Micro. This software may be used only in accordance * * with the terms of said license. This copyright notice may not be * * removed, modified or obliterated without the prior written permission * * of Penta-Micro. * * * * This software may not be copyed, transmitted, provided to or otherwise * * made available to any other person, company, corporation or other entity * * except as specified in the terms of said license. * * * * No right, title, ownership or other interest in the software is hereby * * granted or transferred. * * * * The information contained herein is subject to change without notice and * * should not be construed as a commitment by Penta-Micro. * * * ******************************************************************************** MODULE NAME: MAIN_THREAD.C REVISION HISTORY: Date Ver Name Description ---------- --- --------------------- ----------------------------------------- 06/28/2004 2.0 CheulBeck(whitefe) Created ............................................................................... DESCRIPTION: This Module contains functions for thread syncronization. ...............................................................................*/ /** ************************************************************************* ** ** includes ** ************************************************************************* **/#include <ctype.h>#include <dirent.h>#include <fcntl.h>#include <pthread.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/ipc.h>#include <sys/stat.h>#include <sys/types.h>#include "main.h"/** ************************************************************************* ** ** defines ** ************************************************************************* **///#define m_DEBUG(format, args...) printf(format, ## args) #define m_DEBUG(format, args...) #define m_ERROR(format, args...) printf(format, ## args);fflush(stdout); /** ************************************************************************* ** ** typedefs ** ************************************************************************* **/ /** ************************************************************************* ** ** globals ** ************************************************************************* **/ /** ************************************************************************* ** ** locals ** ************************************************************************* **/pthread_t g_pthread[PTHREAD_MAX];pthread_mutex_t g_pthread_mutex[PTHREAD_MAX];pthread_cond_t g_pthread_cond[PTHREAD_MAX];PTHREAD_SIG_COND g_pthread_info[PTHREAD_MAX];PTHREAD_BUF g_pthread_buf[PTHREAD_MAX];/** ************************************************************************* ** ** forward declarations ** ************************************************************************* **/extern void *pthread_manager(void *args);extern void *pthread_input(void *args);extern void *pthread_live(void *args);extern void *pthread_diskm(void *args);extern void *pthread_setup(void *args);extern void *pthread_search(void *args);extern void *pthread_enc(void *args);extern void *pthread_dec(void *args);extern void *pthread_schedule(void *args);extern void *pthread_trans(void *args);extern void *pthread_gpio(void *args);extern void *pthread_ptzctrl(void *args);extern void *pthread_mouse(void *args);BOOL pthread_read_signal(PTHREAD_BUF *rbuf, PTHREAD_ID thread_num, BOOL wait){ BOOL b_valid = FALSE; pthread_mutex_lock(&g_pthread_mutex[thread_num]); if (wait == TRUE) pthread_cond_wait(&g_pthread_cond[thread_num], &g_pthread_mutex[thread_num]); if (g_pthread_info[thread_num] == WRITE_FLAG) { g_pthread_info[thread_num] = READ_FLAG; memcpy((PTHREAD_BUF *)rbuf, (PTHREAD_BUF *)&g_pthread_buf[thread_num], sizeof(PTHREAD_BUF)); b_valid = TRUE; } pthread_mutex_unlock(&g_pthread_mutex[thread_num]); return b_valid;}BOOL pthread_send_signal(PTHREAD_BUF *sbuf, PTHREAD_ID thread_num){ BOOL b_valid = FALSE; pthread_mutex_lock(&g_pthread_mutex[thread_num]); g_pthread_info[thread_num] = WRITE_FLAG; memcpy((PTHREAD_BUF *)&g_pthread_buf[thread_num], (PTHREAD_BUF *)sbuf, sizeof(PTHREAD_BUF)); pthread_mutex_unlock(&g_pthread_mutex[thread_num]); pthread_cond_signal(&g_pthread_cond[thread_num]); b_valid = TRUE; return b_valid;}BOOL pthread_broadcast_signal(PTHREAD_BUF *sbuf, PTHREAD_ID thread_num){ BOOL b_valid = FALSE; pthread_mutex_lock(&g_pthread_mutex[thread_num]); g_pthread_info[thread_num] = WRITE_FLAG; memcpy((PTHREAD_BUF *)&g_pthread_buf[thread_num], (PTHREAD_BUF *)sbuf, sizeof(PTHREAD_BUF)); pthread_mutex_unlock(&g_pthread_mutex[thread_num]); pthread_cond_broadcast(&g_pthread_cond[thread_num]); b_valid = TRUE; return b_valid;}RETURN pthread_initialize_mutex(void){ int ii; for(ii; ii < PTHREAD_MAX; ii++) { if ( (pthread_mutex_init(&g_pthread_mutex[ii], NULL)) != 0 ) { m_ERROR("\t!!! ERROR : pthread_mutex_init !!!\n"); return FAILURE; } } return SUCCESS;}RETURN pthread_destory_mutex(void){ int ii; for(ii; ii < PTHREAD_MAX; ii++) { if ( (pthread_mutex_destroy(&g_pthread_mutex[ii])) != 0 ) { m_ERROR("\t!!! ERROR : pthread_mutex_destroy !!!\n"); return FAILURE; } } return SUCCESS;}RETURN pthread_initialize_cond(void){ int ii; for(ii; ii < PTHREAD_MAX; ii++) { if ( (pthread_cond_init(&g_pthread_cond[ii], NULL)) != 0 ) { m_ERROR("\t!!! ERROR : pthread_cond_init !!!\n"); return FAILURE; } } return SUCCESS;}RETURN pthread_destroy_cond(void){ int ii; for(ii; ii < PTHREAD_MAX; ii++) { if ( (pthread_cond_destroy(&g_pthread_cond[ii])) != 0 ) { m_ERROR("\t!!! ERROR : pthread_cond_destroy !!!\n"); return FAILURE; } } return SUCCESS;}RETURN pthread_create_manager(void){ if (pthread_create(&g_pthread[PTHREAD_MANAGER] , NULL, (void * (*)(void *)) pthread_manager, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_input(void){ if (pthread_create(&g_pthread[PTHREAD_INPUT] , NULL, (void * (*)(void *)) pthread_input, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_live(void){ if (pthread_create(&g_pthread[PTHREAD_LIVE] , NULL, (void * (*)(void *)) pthread_live, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_diskm(void){ if (pthread_create(&g_pthread[PTHREAD_DISKM] , NULL, (void * (*)(void *)) pthread_diskm, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_setup(void){ if (pthread_create(&g_pthread[PTHREAD_SETUP] , NULL, (void * (*)(void *)) pthread_setup, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_search(void){ if (pthread_create(&g_pthread[PTHREAD_SEARCH] , NULL, (void * (*)(void *)) pthread_search, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_enc(void){ if (pthread_create(&g_pthread[PTHREAD_ENC] , NULL, (void * (*)(void *)) pthread_enc, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_dec(void){ if (pthread_create(&g_pthread[PTHREAD_DEC] , NULL, (void * (*)(void *)) pthread_dec, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_schedule(void){ if (pthread_create(&g_pthread[PTHREAD_SCHEDULE] , NULL, (void * (*)(void *)) pthread_schedule, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_trans(void){ if (pthread_create(&g_pthread[PTHREAD_TRANS] , NULL, (void * (*)(void *)) pthread_trans, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_gpio(void){ if (pthread_create(&g_pthread[PTHREAD_GPIO] , NULL, (void * (*)(void *)) pthread_gpio, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_ptz(void){ if (pthread_create(&g_pthread[PTHREAD_PTZ] , NULL, (void * (*)(void *)) pthread_ptzctrl, NULL) !=0) { return FAILURE; } return SUCCESS; }RETURN pthread_create_mouse(void){ if (pthread_create(&g_pthread[PTHREAD_MOUSE] , NULL, (void * (*)(void *)) pthread_mouse, NULL) !=0) { return FAILURE; } return SUCCESS; }/* end of main_thread.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -