Skip to content

Commit c8f04bc

Browse files
committed
refactoring
1 parent 89811ba commit c8f04bc

15 files changed

+511
-744
lines changed

atomic_print.hpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,20 @@
2323
#include <iostream>
2424
#include <mutex>
2525

26-
using namespace std;
2726
///////////////////////////////////////////////////////////////////////////////
2827
class AtomicPrint
2928
{
3029
public:
31-
AtomicPrint()
32-
{
30+
AtomicPrint() {
3331
lock_mutex_.lock();
3432
}
3533

36-
AtomicPrint(string strMsg)
37-
{
34+
AtomicPrint(std::string msg) {
3835
lock_mutex_.lock();
39-
cout << strMsg <<"\n";
36+
std::cout << msg <<"\n";
4037
}
4138

42-
~AtomicPrint()
43-
{
39+
~AtomicPrint() {
4440
lock_mutex_.unlock();
4541
}
4642

common_def.hpp

+62-19
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,84 @@
1+
/****************************************************************************
2+
Copyright (c) 2015, ko jung hyun
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
****************************************************************************/
122

2-
#ifndef __COMMON_DEF_HPP__
3-
#define __COMMON_DEF_HPP__
23+
#ifndef COMMON_DEF_HPP
24+
#define COMMON_DEF_HPP
425

526
#include <atomic>
6-
//#include <mutex>
27+
#include <iostream>
728
#include <condition_variable>
8-
929
#include <pthread.h> //blocking strategy : mutex, condition_var on shared memory
1030
#include <sys/stat.h>
1131
#include <sys/mman.h>
1232
#include <fcntl.h>
1333
#include <sys/ipc.h>
1434
#include <sys/shm.h>
1535
#define CACHE_LINE_SIZE 64
36+
#define MAX_CONSUMER 200
37+
#define DEFAULT_RING_BUFFER_SIZE 1024
38+
39+
///////////////////////////////////////////////////////////////////////////////
40+
//color printf
41+
#define COLOR_RED "\x1B[31m"
42+
#define COLOR_GREEN "\x1B[32m"
43+
#define COLOR_BLUE "\x1B[34m"
44+
#define COLOR_RESET "\x1B[0m"
45+
#define LOG_WHERE "("<<__FILE__<<"-"<<__func__<<"-"<<__LINE__<<") "
46+
#define WHERE_DEF __FILE__,__func__,__LINE__
1647

48+
#ifdef DEBUG_PRINTF
49+
#define DEBUG_LOG(x) std::cout<<LOG_WHERE << x << "\n"
50+
#define DEBUG_RED_LOG(x) std::cout<<LOG_WHERE << COLOR_RED<< x << COLOR_RESET << "\n"
51+
#define DEBUG_GREEN_LOG(x) std::cout<<LOG_WHERE << COLOR_GREEN<< x << COLOR_RESET << "\n"
52+
#define DEBUG_ELOG(x) std::cerr<<LOG_WHERE << COLOR_RED<< x << COLOR_RESET << "\n"
53+
#else
54+
#define DEBUG_LOG(x)
55+
#define DEBUG_ELOG(x)
56+
#define DEBUG_RED_LOG(x)
57+
#define DEBUG_GREEN_LOG(x)
58+
#endif
59+
#define PRINT_ELOG(x) std::cerr<<LOG_WHERE << COLOR_RED<< x << COLOR_RESET << "\n"
60+
61+
///////////////////////////////////////////////////////////////////////////////
1762
typedef struct _OneBufferData_
1863
{
19-
int64_t nData;
20-
int producerId;
21-
64+
int64_t data;
65+
size_t producer_id;
2266
} OneBufferData ;
67+
const size_t LEN_ONE_BUFFER_DATA = sizeof(OneBufferData);
2368

24-
#define MAX_CONSUMER 200
2569
typedef struct _RingBufferStatusOnSharedMem_
2670
{
27-
int nBufferSize ;
28-
int nTotalMemSize ;
29-
std::atomic<int> registered_producer_count ;
30-
std::atomic<int> registered_consumer_count;
71+
size_t buffer_size ;
72+
size_t total_mem_size ;
73+
std::atomic<size_t> registered_producer_count ;
74+
std::atomic<size_t> registered_consumer_count;
3175
std::atomic<int64_t> cursor alignas(CACHE_LINE_SIZE);
3276
std::atomic<int64_t> next alignas(CACHE_LINE_SIZE);
33-
int64_t arrayOfConsumerIndexes [MAX_CONSUMER] __attribute__ ((aligned (CACHE_LINE_SIZE)));
34-
35-
pthread_cond_t condVar;
36-
pthread_mutex_t mtxLock;
37-
38-
77+
int64_t array_of_consumer_indexes [MAX_CONSUMER] __attribute__ ((aligned (CACHE_LINE_SIZE)));
78+
//TODO use uint64_t
79+
pthread_cond_t cond_var;
80+
pthread_mutex_t mtx_lock;
3981
} RingBufferStatusOnSharedMem ;
4082

41-
#endif
83+
#endif //COMMON_DEF_HPP
84+

elapsed_time.hpp

+7-18
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,27 @@ typedef enum _ENUM_TIME_RESOLUTION_
3636

3737
} ENUM_TIME_RESOLUTION;
3838

39+
////////////////////////////////////////////////////////////////////////////////
3940
class ElapsedTime
4041
{
4142
public:
42-
ElapsedTime()
43-
{
43+
ElapsedTime() {
4444
SetStartTime();
4545
}
46+
~ElapsedTime() {}
4647

47-
~ElapsedTime()
48-
{
49-
}
50-
51-
void SetStartTime()
52-
{
48+
void SetStartTime() {
5349
start_time_ = std::chrono::steady_clock::now();
5450
}
55-
56-
long long SetEndTime( ENUM_TIME_RESOLUTION resolution)
57-
{
51+
long long SetEndTime( ENUM_TIME_RESOLUTION resolution) {
5852
end_time_ = std::chrono::steady_clock::now();
59-
60-
if(resolution == MILLI_SEC_RESOLUTION)
61-
{
53+
if(resolution == MILLI_SEC_RESOLUTION) {
6254
millisecs_t duration(std::chrono::duration_cast<millisecs_t>(end_time_ - start_time_));
6355
return duration.count();
64-
}
65-
else if (resolution == MICRO_SEC_RESOLUTION)
66-
{
56+
} else if (resolution == MICRO_SEC_RESOLUTION) {
6757
microsecs_t duration(std::chrono::duration_cast<microsecs_t>(end_time_ - start_time_));
6858
return duration.count();
6959
}
70-
7160
return -1; //error
7261
}
7362

ring_buffer.hpp

+21-35
Original file line numberDiff line numberDiff line change
@@ -20,73 +20,59 @@
2020
THE SOFTWARE.
2121
****************************************************************************/
2222

23-
#ifndef __DISRUPTORCPP_RING_BUFFER_HPP__
24-
#define __DISRUPTORCPP_RING_BUFFER_HPP__
25-
//20150721 kojh create
23+
#ifndef DISRUPTORCPP_RING_BUFFER_HPP
24+
#define DISRUPTORCPP_RING_BUFFER_HPP
2625

2726
#include <iostream>
2827
#include <memory>
2928
#include <vector>
29+
#include "common_def.hpp"
3030

31-
#define DEFAULT_RING_BUFFER_SIZE 1024
32-
31+
////////////////////////////////////////////////////////////////////////////////
3332
template<typename T>
3433
class RingBuffer
3534
{
3635
public:
37-
38-
RingBuffer()
39-
{
40-
nCapacity_ = DEFAULT_RING_BUFFER_SIZE;
36+
RingBuffer() {
37+
capacity_ = DEFAULT_RING_BUFFER_SIZE;
4138
buffer_.reserve(DEFAULT_RING_BUFFER_SIZE);
4239
}
43-
4440
RingBuffer(const std::vector<T>& buffer) : buffer_(buffer) {}
4541

46-
T& operator[](const int64_t & sequence)
47-
{
48-
return buffer_[sequence & (nCapacity_ - 1)]; //only when multiple of 2
42+
T& operator[](const int64_t & sequence) {
43+
return buffer_[sequence & (capacity_ - 1)]; //only when multiple of 2
4944
}
5045

51-
int64_t GetTranslatedIndex( int64_t sequence)
52-
{
53-
int64_t translated_index = (sequence & (nCapacity_ - 1)) ;
46+
int64_t GetTranslatedIndex( int64_t sequence) {
47+
int64_t translated_index = (sequence & (capacity_ - 1)) ;
5448
return translated_index ;
5549
}
5650

57-
bool SetCapacity(int nCapacity)
58-
{
59-
bool bIsPower2 = nCapacity && !( (nCapacity-1) & nCapacity ) ;
60-
61-
if( bIsPower2 ==0 )
62-
{
63-
std::cerr << "Buffer capacity error: power of 2 required!" << '\n';
51+
bool SetCapacity(size_t capacity) {
52+
bool is_power2 = capacity && !( (capacity-1) & capacity ) ;
53+
if( is_power2 ==0 ) {
54+
DEBUG_ELOG("Buffer capacity error: power of 2 required!");
6455
return false;
6556
}
6657

67-
try
68-
{
69-
buffer_.reserve(nCapacity);
70-
}
71-
catch (const std::length_error& le)
72-
{
73-
std::cerr << "Length error: " << le.what() << '\n';
58+
try {
59+
buffer_.reserve(capacity);
60+
} catch (const std::length_error& le) {
61+
DEBUG_ELOG("Length error: " << le.what() );
7462
return false;
7563
}
76-
77-
nCapacity_ = nCapacity;
64+
capacity_ = capacity;
7865
return true;
7966
}
8067

8168
private:
82-
int nCapacity_ ;
69+
size_t capacity_ ;
8370
std::vector<T> buffer_;
84-
8571
RingBuffer(const RingBuffer&);
8672
void operator=(const RingBuffer&);
8773
RingBuffer(RingBuffer&&);
8874
void operator=(const RingBuffer&&);
8975
};
9076

91-
#endif
77+
#endif //DISRUPTORCPP_RING_BUFFER_HPP
9278

0 commit comments

Comments
 (0)