-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·172 lines (121 loc) · 3.73 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @date 2016.11.17
* @author [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <string.h>
pthread_mutex_t m_lock = PTHREAD_MUTEX_INITIALIZER;
constexpr int total_count = 15;
constexpr int buffer_data_size = 640*480;
constexpr int buffer_total_count = total_count + 1;
static uint8_t m_buffer[buffer_total_count][buffer_data_size];
static int m_front = 0;
static int m_rear = 0;
static int m_buffer_count = 0;
void push(uint8_t *data, int size)
{
printf("Push >>>> \n");
pthread_mutex_lock(&m_lock);
if (size < 1 || size > buffer_data_size)
size = buffer_data_size;
memset(m_buffer + m_rear, 0x00, size);
memcpy(m_buffer + m_rear, data, size);
++m_rear;
if (m_rear >= buffer_total_count)
m_rear = 0;
if (m_rear == m_front) {
++m_front;
if (m_front >= buffer_total_count)
m_front = 0;
}
if (m_buffer_count < (buffer_total_count - 1))
++m_buffer_count;
// printf("front[%d] rear[%d] \n", m_front, m_rear);
// printf("m_buffer_count[%d] \n", m_buffer_count);
// printf("push value[%d] \n", data[0]);
// printf("========================= \n");
pthread_mutex_unlock(&m_lock);
}
int pop(uint8_t *data, int size)
{
printf("<<<< Pop ... \n");
pthread_mutex_lock(&m_lock);
if (m_rear == m_front) {
pthread_mutex_unlock(&m_lock);
return 0;
}
if (size < 1 || size > buffer_data_size)
size = buffer_data_size;
memcpy(data, m_buffer + m_front, size);
++m_front;
if (m_front >= buffer_total_count)
m_front = 0;
if (m_buffer_count > 0)
--m_buffer_count;
// printf("front[%d] rear[%d] \n", m_front, m_rear);
// printf("m_buffer_count[%d] \n", m_buffer_count);
printf("pop value[%d] \n", data[0]);
// printf("========================= \n");
pthread_mutex_unlock(&m_lock);
return size;
}
int front(uint8_t *data, int size)
{
printf("<<<< Front ... \n");
pthread_mutex_lock(&m_lock);
if (m_rear == m_front) {
pthread_mutex_unlock(&m_lock);
return 0;
}
if (size < 1 || size > buffer_data_size)
size = buffer_data_size;
memcpy(data, m_buffer + m_front, size);
printf("front value[%d] \n", data[0]);
pthread_mutex_unlock(&m_lock);
return size;
}
int back(uint8_t *data, int size)
{
printf("<<<< Back ... \n");
pthread_mutex_lock(&m_lock);
if (m_rear == m_front) {
pthread_mutex_unlock(&m_lock);
return 0;
}
if (size < 1 || size > buffer_data_size)
size = buffer_data_size;
memcpy(data, m_buffer + m_rear, size);
printf("Back value[%d] \n", data[0]);
pthread_mutex_unlock(&m_lock);
return size;
}
int getCount()
{
return m_buffer_count;
}
uint8_t image1[640*480] = {1,};
int main()
{
printf("<<< test ... >>> \n");
constexpr int count = 20;
for (int i = 0; i < count; ++i) {
image1[0] = i;
push(image1, buffer_data_size);
}
for (int i = 0; i < buffer_total_count; ++i) {
printf("m_buffer[%d] \n", m_buffer[i][0]);
}
printf("Buffer Count[%d] \n\n", getCount());
front(image1, buffer_data_size);
back(image1, buffer_data_size);
for (int i = 0; i < buffer_total_count; ++i) {
pop(image1, buffer_data_size);
}
printf("Buffer Count[%d] \n\n", getCount());
int status = pthread_mutex_destroy(&m_lock);
printf("code = %d \n", status);
return 0;
}