OSDN Git Service

1sec and high smpling file record add.
[scilog/scilog.git] / ring.c
1 /*
2         Ring Buffer用基本機能
3         write位置
4         read位置
5 */
6 #include <stdlib.h>
7 #include <pthread.h>
8 #include <semaphore.h> 
9
10 #include "ring.h"
11
12 #define READ_NUM_MAX    256
13 // ミューテックス
14 static pthread_mutex_t  mutex_ring = PTHREAD_MUTEX_INITIALIZER;
15
16 /*
17         int     read_num
18                 読み込みポインタの個数
19 */
20 RING_T* ring_create(int read_num)
21 {
22         RING_T  *t;
23         if (read_num <= 0) return NULL;
24         if (read_num > READ_NUM_MAX) return NULL;
25         
26         t = malloc(sizeof(RING_T));
27         if (t != NULL) {
28                 // readポインタ確保
29                 t->read = malloc(sizeof(int) * read_num);
30                 if (t->read == NULL) {
31                         free(t);
32                         return NULL;
33                 }
34                 t->read_num = read_num;
35         }
36         return t;
37 }
38 void ring_destroy(RING_T *t)
39 {
40         free(t->read);
41         free(t);
42 }
43 /*
44         int num:        バッファの個数
45 */
46 void ring_init(RING_T *t, int num)
47 {
48         int     i;
49         
50 pthread_mutex_lock(&mutex_ring);
51         t->write = 0;
52         for(i = 0; i < t->read_num; i++) {
53                 t->read[i] = 0;
54         }
55         t->latest = -1;
56         t->num = num;
57 pthread_mutex_unlock(&mutex_ring);
58 }
59 void ring_clear(RING_T *t, int no)
60 {
61         ring_read_set(t, no, ring_write_get(t));
62 }
63 /*
64         読み出し位置
65         int no
66                 readポインタ番号
67 */
68 int ring_read_get(RING_T *t, int no)
69 {
70         int     i;
71 pthread_mutex_lock(&mutex_ring);
72         i = t->read[no];
73 pthread_mutex_unlock(&mutex_ring);
74         return i;
75 }
76 void ring_read_set(RING_T *t, int no, int i)
77 {
78 pthread_mutex_lock(&mutex_ring);
79         t->read[no] = i;
80 pthread_mutex_unlock(&mutex_ring);
81 }
82 void ring_read_plus(RING_T *t, int no)
83 {
84 pthread_mutex_lock(&mutex_ring);
85         t->read[no]++;
86         if (t->read[no] >= t->num) t->read[no] = 0;
87 pthread_mutex_unlock(&mutex_ring);
88 }
89
90 // 書き込み位置
91 int ring_write_get(RING_T *t)
92 {
93         int     i;
94 pthread_mutex_lock(&mutex_ring);
95         i = t->write;
96 pthread_mutex_unlock(&mutex_ring);
97         return i;
98 }
99 void ring_write_plus(RING_T *t)
100 {
101 pthread_mutex_lock(&mutex_ring);
102         t->write++;
103         if (t->write >= t->num) t->write = 0;
104 pthread_mutex_unlock(&mutex_ring);
105 }
106 // 読み込んでいないデータ数
107 int ring_num_get(RING_T *t, int no)
108 {
109         int     i;
110         
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);
115         return i;
116 }
117 // 最新データ位置
118 int ring_latest_get(RING_T *t)
119 {
120         int     i;
121 pthread_mutex_lock(&mutex_ring);
122         i = t->latest;
123 pthread_mutex_unlock(&mutex_ring);
124         return i;
125 }
126 void ring_latest_set(RING_T *t, int i)
127 {
128 pthread_mutex_lock(&mutex_ring);
129         t->latest = i;
130 pthread_mutex_unlock(&mutex_ring);
131 }
132
133 /*
134         パケットバッファフル?
135         1=Full
136         0=not Full
137 */
138 int ring_is_full(RING_T *t, int no)
139 {
140         if (ring_num_get(t, no) >= t->num - 1) return 1;
141         return 0;
142 }