-
Notifications
You must be signed in to change notification settings - Fork 0
/
CASBuffer.h
55 lines (44 loc) · 1.05 KB
/
CASBuffer.h
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
#pragma once
#include "windows.h"
#define SAFE_LONGS 2
#define BANK_IS_FREE 0
#define BANK_IS_READ 1
#define EOWS 3
#define CTRL_VALS 3
class CASBuffer{
public:
//run from main thread
CASBuffer(unsigned int size):buf(new long[size+SAFE_LONGS]), buf_size(size+SAFE_LONGS),read_pos(0), write_pos(0)
{
memset((void*)buf, 0, buf_size*sizeof(long));
}
~CASBuffer(){
delete[] buf;
}
//run from producer thread
void put(const char* in_buf, unsigned int bytes);
//run from consumer thread
unsigned int CASBuffer::get(char* in_buf);
//run from producer thread
void close(){
put(EOWS);
};
//run from main thread
void reset(){
read_pos = write_pos = 0;
memset((void*)buf, 0, buf_size*sizeof(long));
}
private:
long volatile * const buf;
const unsigned int buf_size;
unsigned int read_pos;
unsigned int write_pos;
void skip_write(){
write_pos=(write_pos+1>=buf_size)?0:write_pos+1;
}
void skip_read(){
read_pos=(read_pos+1>=buf_size)?0:read_pos+1;
}
long CASBuffer::get(bool isCtrl=false, long xch = BANK_IS_FREE);
void put(long val);
};