使用进程间共享的信号量,标记消费者进程是否正在运行
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
#define MESA_SHM_RING_QUEUE_HALF_IDLE 2
|
||||
#define MESA_SHM_RING_QUEUE_USED 3
|
||||
|
||||
#define MESA_SEM_KEY 35719
|
||||
|
||||
|
||||
struct MESA_shm_overview *MESA_shm_alloc_overview();
|
||||
struct MESA_shm_queue_head *MESA_shm_get_ring_queue();
|
||||
void MESA_shm_init_mutex();
|
||||
@@ -25,6 +28,10 @@ int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue
|
||||
int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head);
|
||||
int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head);
|
||||
void MESA_shm_ring_queue_set_empty(struct MESA_shm_queue_head *head);
|
||||
int MESA_shm_alloc_sem();
|
||||
int MESA_shm_consumer_is_running();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/sem.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
@@ -43,6 +44,8 @@ struct log_file_list g_log_file_list;
|
||||
struct care_pid_list g_care_pid_list;
|
||||
int g_output_mode = CONSUMER_OUTPUT_MODE_FILE;
|
||||
int g_care_pid = CONSUMER_CARE_PID_ALL;
|
||||
int g_semid = -1;
|
||||
|
||||
void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head)
|
||||
{
|
||||
int i = 0;
|
||||
@@ -52,7 +55,10 @@ void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_
|
||||
if(tmp_ovw->shmid == -1){
|
||||
break;
|
||||
}
|
||||
ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0);
|
||||
ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0);
|
||||
if(ring_queue_head[i] == (struct MESA_shm_queue_head *)-1){
|
||||
ring_queue_head[i] = NULL;
|
||||
}
|
||||
}
|
||||
return ;
|
||||
}
|
||||
@@ -507,6 +513,30 @@ error:
|
||||
pclose(fp);
|
||||
return -1;
|
||||
}
|
||||
void MESA_shm_sem_p(int semid)
|
||||
{
|
||||
if(g_semid == -1){
|
||||
return ;
|
||||
}
|
||||
struct sembuf sem_b;
|
||||
sem_b.sem_num = 0;
|
||||
sem_b.sem_op = -1;
|
||||
sem_b.sem_flg = SEM_UNDO;
|
||||
semop(semid, &sem_b, 1);
|
||||
return ;
|
||||
}
|
||||
void MESA_shm_sem_v(int semid)
|
||||
{
|
||||
if(g_semid == -1){
|
||||
return ;
|
||||
}
|
||||
struct sembuf sem_b;
|
||||
sem_b.sem_num = 0;
|
||||
sem_b.sem_op = 1;
|
||||
sem_b.sem_flg = SEM_UNDO;
|
||||
semop(g_semid, &sem_b, 1);
|
||||
return ;
|
||||
}
|
||||
|
||||
void print_help(char *exe_name)
|
||||
{
|
||||
@@ -537,6 +567,8 @@ int main(int argc, char **argv)
|
||||
}else{
|
||||
g_cur_tty_fd = get_cur_tty_fd();
|
||||
}
|
||||
g_semid = MESA_shm_alloc_sem();
|
||||
MESA_shm_sem_p(g_semid);
|
||||
int i = 0;
|
||||
struct MESA_shm_overview *shm_overview = NULL;
|
||||
struct MESA_shm_overview *tmp_ovw = NULL;
|
||||
|
||||
@@ -26,6 +26,11 @@ static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = "";
|
||||
pthread_key_t MESA_pthread_key;
|
||||
pthread_once_t MESA_pthread_key_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
#define MESA_CONSUMER_RUNNING 1
|
||||
#define MESA_CONSUMER_NOT_RUNNING 0
|
||||
|
||||
int MESA_consumer_status = MESA_CONSUMER_NOT_RUNNING;
|
||||
|
||||
struct MESA_pthread_private{
|
||||
char *fmt_buf;
|
||||
char *cache_buf;
|
||||
@@ -260,11 +265,21 @@ void MESA_free_pthread_private(void *arg)
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
void *MESA_check_consumer_status(void *arg)
|
||||
{
|
||||
while(1){
|
||||
MESA_consumer_status = MESA_shm_consumer_is_running();
|
||||
sleep(1);
|
||||
};
|
||||
return NULL;
|
||||
}
|
||||
void MESA_alloc_pthread_key()
|
||||
{
|
||||
pthread_key_create(&MESA_pthread_key, MESA_free_pthread_private);
|
||||
MESA_shm_init_overview();
|
||||
pthread_t tid;
|
||||
pthread_create(&tid, NULL, MESA_check_consumer_status, NULL);
|
||||
pthread_detach(tid);
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -329,7 +344,9 @@ void MESA_destroy_runtime_log_handle(void *handle)
|
||||
|
||||
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
||||
{
|
||||
|
||||
if(MESA_consumer_status == MESA_CONSUMER_NOT_RUNNING){
|
||||
return ;
|
||||
}
|
||||
log_handle_t *p_handle = (log_handle_t *)handle;
|
||||
|
||||
if(p_handle == NULL || p_handle->runtime_log_file == NULL)return;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/sem.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@@ -13,6 +14,14 @@
|
||||
|
||||
|
||||
struct MESA_shm_overview *MESA_shm_ovw = NULL;
|
||||
int MESA_shm_semid = -1;
|
||||
union semun {
|
||||
int val;
|
||||
struct semid_ds *buf;
|
||||
unsigned short *array;
|
||||
struct seminfo *__buf;
|
||||
};
|
||||
|
||||
|
||||
void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw)
|
||||
{
|
||||
@@ -24,6 +33,31 @@ void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw)
|
||||
return ;
|
||||
}
|
||||
|
||||
int MESA_shm_alloc_sem()
|
||||
{
|
||||
union semun semopt;
|
||||
int semid = semget(MESA_SEM_KEY, 0 , 0);
|
||||
if(semid == -1){
|
||||
semid = semget(MESA_SEM_KEY, 1 ,IPC_CREAT | 0666);
|
||||
if(semid == -1){
|
||||
return -1;
|
||||
}
|
||||
semopt.val = 1;
|
||||
semctl(semid, 0, SETVAL, semopt);
|
||||
}
|
||||
return semid;
|
||||
}
|
||||
|
||||
int MESA_shm_consumer_is_running()
|
||||
{
|
||||
if(MESA_shm_semid == -1){
|
||||
return 0;
|
||||
}
|
||||
int semval = semctl(MESA_shm_semid, 0 ,GETVAL, 0);
|
||||
return !semval ;
|
||||
}
|
||||
|
||||
|
||||
struct MESA_shm_overview *MESA_shm_alloc_overview()
|
||||
{
|
||||
int shmsize = sizeof(struct MESA_shm_overview) * MESA_SHM_RING_QUEUE_NUM;
|
||||
@@ -62,6 +96,7 @@ void MESA_shm_init_overview()
|
||||
if(MESA_shm_ovw == NULL){
|
||||
MESA_shm_ovw = MESA_shm_alloc_overview();
|
||||
}
|
||||
MESA_shm_semid = MESA_shm_alloc_sem();
|
||||
return ;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
global: MESA*runtime_log*;GIT_VERSION_*;MESA_shm_alloc_overview;MESA_handle_fmt_rule_register;MESA_shm_ring_queue_is_empty;MESA_shm_ring_queue_is_full;MESA_shm_ring_queue_set_empty;
|
||||
global: MESA*runtime_log*;GIT_VERSION_*;MESA_shm_alloc*;MESA_handle_fmt_rule_register;MESA_shm_ring_queue*;
|
||||
local: *;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user