pthread で itron ライクな sng_msg と rcv_msg
pthread で itron ライクなメッセージ機能の実装。とりあえずヘッダファイル。メールボックスの数は NUM_OF_MBOX 、メールキューの大きさは NUM_OF_MSG で決め打ちにした。
#ifndef _MBOX_H_ #define _MBOX_H_ #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include <time.h> #include <pthread.h> #include <errno.h> #define TRUE 1 #define FALSE 0 #define E_OK 0 /* success */ #define E_NOEXS (-1) /* no objcet */ #define E_QOVR (-2) /* queue overflow */ #define E_TMOUT (-3) /* timeout */ #define E_SYS (-4) /* system error */ #define E_PAR (-5) /* parameter error */ #define NUM_OF_MBOX 10 #define NUM_OF_MSG 5 /* macro */ #define NELEMS(array) (sizeof(array) / sizeof(array[0])) typedef struct Msg { void *data; u_int len; } Msg; /* ring buffer to store msg. this buffer wastes extra 1 msg */ typedef struct MsgRingBuf { int read_pos; int write_pos; Msg msg[NUM_OF_MSG + 1]; } MsgRingBuf; typedef struct Mbox { int use; pthread_mutex_t mutex; pthread_cond_t cond; MsgRingBuf rbuf; } Mbox; int write_rbuf(MsgRingBuf *rbuf, void const *data, u_int len); int read_rbuf(MsgRingBuf *rbuf, void **data, u_int *len); void dump_rbuf(MsgRingBuf *rbuf, FILE *fp); void init_mutex(pthread_mutex_t *mutex); int init_mbox_table(); int create_mbox(u_int *mbox_id); int delete_mbox(u_int mbox_id); int snd_msg(u_int mbox_id, void const *msg, u_int len); int prcv_msg(u_int mbox_id, void **msg, u_int *len); int rcv_msg(u_int mbox_id, void **msg, u_int *len); int add_msec(struct timespec *ts, int msec); int diff_msec(struct timeval *tv_before, struct timeval *tv_after); int trcv_msg(u_int mbox_id, void **msg, u_int *len, int timeout); #endif /* _MBOX_H_ */