Ringbuffer
This commit is contained in:
parent
142e054efc
commit
828d55e708
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "g++",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/bin/test",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
],
|
||||
"preLaunchTask": "all",
|
||||
"miDebuggerPath": "/usr/bin/gdb"
|
||||
}
|
||||
]
|
||||
}
|
15
Makefile
15
Makefile
@ -2,23 +2,14 @@ CROSS_COMPILE ?=
|
||||
|
||||
TARGET_FILE ?= test
|
||||
|
||||
C_FLAGS += -Wall -Werror -DGNU -D__GNU__ -D__GCC__
|
||||
C_FLAGS += -Os
|
||||
C_FLAGS += -ffunction-sections
|
||||
C_FLAGS += -fdata-sections
|
||||
C_FLAGS += -std=c18
|
||||
C_FLAGS += -pedantic
|
||||
C_FLAGS += -pedantic-errors
|
||||
C_FLAGS += -ggdb3
|
||||
C_FLAGS += -Iinclude
|
||||
|
||||
CXXFLAGS += -Os
|
||||
CXXFLAGS += -O0
|
||||
CXXFLAGS += -ggdb3
|
||||
CXXFLAGS += -std=c++17
|
||||
CXXFLAGS += -pedantic
|
||||
CXXFLAGS += -pedantic-errors
|
||||
CXXFLAGS += -ffunction-sections -fdata-sections
|
||||
CXXFLAGS += -fno-implicit-inline-templates
|
||||
CXXFLAGS += -Iinc
|
||||
|
||||
|
||||
LD_LIBS := -lc -lgcc -lpthread
|
||||
@ -62,5 +53,5 @@ define makedep
|
||||
endef
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $($(subst .o,.d,$(OBJS))
|
||||
-include $(subst .o,.d,$(OBJS))
|
||||
endif
|
||||
|
24
inc/ringbuffer.h
Normal file
24
inc/ringbuffer.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef __INC_RINGBUFFER_H__
|
||||
#define __INC_RINGBUFFER_H__
|
||||
|
||||
#include <array>
|
||||
|
||||
class ringbuffer
|
||||
{
|
||||
public:
|
||||
ringbuffer();
|
||||
|
||||
int write(char);
|
||||
int read(char&);
|
||||
|
||||
private:
|
||||
enum {
|
||||
MAX_BUFFER_SIZE = 2
|
||||
};
|
||||
|
||||
std::array<char, MAX_BUFFER_SIZE> buffer;
|
||||
std::array<char, MAX_BUFFER_SIZE>::iterator head;
|
||||
std::array<char, MAX_BUFFER_SIZE>::iterator tail;
|
||||
};
|
||||
|
||||
#endif
|
35
src/main.cc
35
src/main.cc
@ -1,28 +1,23 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <pthread.h>
|
||||
#include "ringbuffer.h"
|
||||
|
||||
void *fake_it(void *param)
|
||||
{
|
||||
std::cout << "Hello " << __FUNCTION__ << "\r\n";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *poller(void *param)
|
||||
{
|
||||
std::cout << "Hello " << __FUNCTION__ << "\r\n";
|
||||
return NULL;
|
||||
}
|
||||
ringbuffer buf;
|
||||
|
||||
int main(void) {
|
||||
pthread_t it_thread, poll_thread;
|
||||
|
||||
pthread_create(&it_thread, NULL, fake_it, NULL);
|
||||
pthread_create(&poll_thread, NULL, poller, NULL);
|
||||
std::cout << "Hello World!!!" << "\r\n";
|
||||
|
||||
pthread_join(it_thread, NULL);
|
||||
pthread_join(poll_thread, NULL);
|
||||
buf.write('a');
|
||||
buf.write('b');
|
||||
buf.write('c');
|
||||
|
||||
char c;
|
||||
if(buf.read(c)) {
|
||||
std::cout << c;
|
||||
}
|
||||
if(buf.read(c)) {
|
||||
std::cout << c;
|
||||
}
|
||||
if(buf.read(c)) {
|
||||
std::cout << c;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
39
src/ringbuffer.cc
Normal file
39
src/ringbuffer.cc
Normal file
@ -0,0 +1,39 @@
|
||||
#include "ringbuffer.h"
|
||||
|
||||
ringbuffer::ringbuffer()
|
||||
{
|
||||
head = buffer.begin();
|
||||
tail = buffer.begin();
|
||||
}
|
||||
|
||||
int ringbuffer::write(char c)
|
||||
{
|
||||
if(head - tail == MAX_BUFFER_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(head == buffer.end()) {
|
||||
head = buffer.begin();
|
||||
}
|
||||
|
||||
*head = c;
|
||||
head++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ringbuffer::read(char& c)
|
||||
{
|
||||
if(head - tail == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(tail == buffer.end()) {
|
||||
tail = buffer.begin();
|
||||
}
|
||||
|
||||
c = *tail;
|
||||
tail++;
|
||||
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user