|
| 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 | + ****************************************************************************/ |
1 | 22 |
|
2 |
| -#ifndef __COMMON_DEF_HPP__ |
3 |
| -#define __COMMON_DEF_HPP__ |
| 23 | +#ifndef COMMON_DEF_HPP |
| 24 | +#define COMMON_DEF_HPP |
4 | 25 |
|
5 | 26 | #include <atomic>
|
6 |
| -//#include <mutex> |
| 27 | +#include <iostream> |
7 | 28 | #include <condition_variable>
|
8 |
| - |
9 | 29 | #include <pthread.h> //blocking strategy : mutex, condition_var on shared memory
|
10 | 30 | #include <sys/stat.h>
|
11 | 31 | #include <sys/mman.h>
|
12 | 32 | #include <fcntl.h>
|
13 | 33 | #include <sys/ipc.h>
|
14 | 34 | #include <sys/shm.h>
|
15 | 35 | #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__ |
16 | 47 |
|
| 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 | +/////////////////////////////////////////////////////////////////////////////// |
17 | 62 | typedef struct _OneBufferData_
|
18 | 63 | {
|
19 |
| - int64_t nData; |
20 |
| - int producerId; |
21 |
| - |
| 64 | + int64_t data; |
| 65 | + size_t producer_id; |
22 | 66 | } OneBufferData ;
|
| 67 | +const size_t LEN_ONE_BUFFER_DATA = sizeof(OneBufferData); |
23 | 68 |
|
24 |
| -#define MAX_CONSUMER 200 |
25 | 69 | typedef struct _RingBufferStatusOnSharedMem_
|
26 | 70 | {
|
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; |
31 | 75 | std::atomic<int64_t> cursor alignas(CACHE_LINE_SIZE);
|
32 | 76 | 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; |
39 | 81 | } RingBufferStatusOnSharedMem ;
|
40 | 82 |
|
41 |
| -#endif |
| 83 | +#endif //COMMON_DEF_HPP |
| 84 | + |
0 commit comments