12 #define READ_NUM_MAX 256
14 static pthread_mutex_t mutex_ring = PTHREAD_MUTEX_INITIALIZER;
20 RING_T* ring_create(int read_num)
23 if (read_num <= 0) return NULL;
24 if (read_num > READ_NUM_MAX) return NULL;
26 t = malloc(sizeof(RING_T));
29 t->read = malloc(sizeof(int) * read_num);
30 if (t->read == NULL) {
34 t->read_num = read_num;
38 void ring_destroy(RING_T *t)
46 void ring_init(RING_T *t, int num)
50 pthread_mutex_lock(&mutex_ring);
52 for(i = 0; i < t->read_num; i++) {
57 pthread_mutex_unlock(&mutex_ring);
59 void ring_clear(RING_T *t, int no)
61 ring_read_set(t, no, ring_write_get(t));
68 int ring_read_get(RING_T *t, int no)
71 pthread_mutex_lock(&mutex_ring);
73 pthread_mutex_unlock(&mutex_ring);
76 void ring_read_set(RING_T *t, int no, int i)
78 pthread_mutex_lock(&mutex_ring);
80 pthread_mutex_unlock(&mutex_ring);
82 void ring_read_plus(RING_T *t, int no)
84 pthread_mutex_lock(&mutex_ring);
86 if (t->read[no] >= t->num) t->read[no] = 0;
87 pthread_mutex_unlock(&mutex_ring);
91 int ring_write_get(RING_T *t)
94 pthread_mutex_lock(&mutex_ring);
96 pthread_mutex_unlock(&mutex_ring);
99 void ring_write_plus(RING_T *t)
101 pthread_mutex_lock(&mutex_ring);
103 if (t->write >= t->num) t->write = 0;
104 pthread_mutex_unlock(&mutex_ring);
107 int ring_num_get(RING_T *t, int no)
111 pthread_mutex_lock(&mutex_ring);
112 i = t->write - t->read[no];
113 if (i < 0) i += t->num;
114 pthread_mutex_unlock(&mutex_ring);
118 int ring_latest_get(RING_T *t)
121 pthread_mutex_lock(&mutex_ring);
123 pthread_mutex_unlock(&mutex_ring);
126 void ring_latest_set(RING_T *t, int i)
128 pthread_mutex_lock(&mutex_ring);
130 pthread_mutex_unlock(&mutex_ring);
138 int ring_is_full(RING_T *t, int no)
140 if (ring_num_get(t, no) >= t->num - 1) return 1;