| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* @file exchange_manager_producer.cpp |
| 3 |
|
|
* @author Vincent Berenz |
| 4 |
|
|
* @license License BSD-3-Clause |
| 5 |
|
|
* @copyright Copyright (c) 2019, New York University and Max Planck |
| 6 |
|
|
* Gesellschaft. |
| 7 |
|
|
* @date 2019-05-22 |
| 8 |
|
|
* |
| 9 |
|
|
* @brief Demonstrate the use of the exhange manager producer |
| 10 |
|
|
*/ |
| 11 |
|
|
#include "shared_memory/exchange_manager_producer.hpp" |
| 12 |
|
|
#include <signal.h> |
| 13 |
|
|
#include <stdlib.h> |
| 14 |
|
|
#include <time.h> |
| 15 |
|
|
#include <unistd.h> |
| 16 |
|
|
#include <iostream> |
| 17 |
|
|
#include "shared_memory/demos/four_int_values.hpp" |
| 18 |
|
|
#include "shared_memory/exchange_manager_consumer.hpp" |
| 19 |
|
|
|
| 20 |
|
|
#define SEGMENT_ID "exchange_demo_segment" |
| 21 |
|
|
#define OBJECT_ID "exchange_demo_object" |
| 22 |
|
|
#define QUEUE_SIZE 2000 * 4 |
| 23 |
|
|
|
| 24 |
|
|
static bool RUNNING = true; |
| 25 |
|
|
|
| 26 |
|
✗ |
void stop(int) |
| 27 |
|
|
{ |
| 28 |
|
✗ |
RUNNING = false; |
| 29 |
|
|
} |
| 30 |
|
|
|
| 31 |
|
✗ |
static int _get_int(int max) |
| 32 |
|
|
{ |
| 33 |
|
✗ |
return rand() % max; |
| 34 |
|
|
} |
| 35 |
|
|
|
| 36 |
|
✗ |
void execute() |
| 37 |
|
|
{ |
| 38 |
|
✗ |
srand(time(NULL)); |
| 39 |
|
|
|
| 40 |
|
|
// Four_int_values is a subclass of shared_memory/serializable, |
| 41 |
|
|
// i.e an object which can be serialized as an array of double |
| 42 |
|
✗ |
bool autolock = false; // we need to "manually" call lock, in order to |
| 43 |
|
|
// write several item in a single shot |
| 44 |
|
✗ |
bool leading = true; // producer expected to start first, and to survive |
| 45 |
|
|
// several consumers |
| 46 |
|
|
shared_memory::Exchange_manager_producer<shared_memory::Four_int_values, |
| 47 |
|
|
QUEUE_SIZE> |
| 48 |
|
✗ |
exchange(SEGMENT_ID, OBJECT_ID, leading, autolock); |
| 49 |
|
|
|
| 50 |
|
|
// will be used to track ids that has been consumed |
| 51 |
|
|
// by the consummer |
| 52 |
|
✗ |
std::deque<int> consumed_ids; |
| 53 |
|
|
|
| 54 |
|
✗ |
int c = 1; |
| 55 |
|
|
|
| 56 |
|
|
// to be used to inform user producer is waiting because |
| 57 |
|
|
// memory is full |
| 58 |
|
|
|
| 59 |
|
✗ |
bool waiting_warning_printed = false; |
| 60 |
|
|
|
| 61 |
|
✗ |
while (RUNNING) |
| 62 |
|
|
{ |
| 63 |
|
|
// pushing to memory a random number of instances of Four_int_values |
| 64 |
|
|
|
| 65 |
|
✗ |
int nb_items = _get_int(5); |
| 66 |
|
|
|
| 67 |
|
✗ |
if (exchange.ready_to_produce()) |
| 68 |
|
|
{ |
| 69 |
|
|
// necessary because autolock is false |
| 70 |
|
✗ |
exchange.lock(); |
| 71 |
|
|
|
| 72 |
|
✗ |
for (int item = 0; item < nb_items; item++) |
| 73 |
|
|
{ |
| 74 |
|
✗ |
shared_memory::Four_int_values fiv(c, c, c, c); |
| 75 |
|
|
|
| 76 |
|
|
// serializing fiv and writing it to shared memory |
| 77 |
|
|
try |
| 78 |
|
|
{ |
| 79 |
|
✗ |
exchange.set(fiv); |
| 80 |
|
✗ |
waiting_warning_printed = false; |
| 81 |
|
✗ |
std::cout << "produced: " << fiv.get_id() << " | " << c |
| 82 |
|
✗ |
<< "\n"; |
| 83 |
|
✗ |
c++; |
| 84 |
|
|
} |
| 85 |
|
✗ |
catch (const shared_memory::Memory_overflow_exception &) |
| 86 |
|
|
{ |
| 87 |
|
✗ |
if (!waiting_warning_printed) |
| 88 |
|
|
{ |
| 89 |
|
✗ |
waiting_warning_printed = true; |
| 90 |
|
|
std::cout |
| 91 |
|
✗ |
<< "shared memory full, waiting for consumer ...\n"; |
| 92 |
|
|
} |
| 93 |
|
|
} |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
✗ |
exchange.unlock(); |
| 97 |
|
|
} |
| 98 |
|
|
else |
| 99 |
|
|
{ |
| 100 |
|
✗ |
if (!waiting_warning_printed) |
| 101 |
|
|
{ |
| 102 |
|
✗ |
waiting_warning_printed = true; |
| 103 |
|
✗ |
std::cout << "waiting for consumer to start ...\n"; |
| 104 |
|
|
} |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
// reading from shared_memory which |
| 108 |
|
|
// items have been consumed |
| 109 |
|
|
|
| 110 |
|
✗ |
exchange.lock(); |
| 111 |
|
✗ |
exchange.get(consumed_ids); |
| 112 |
|
✗ |
exchange.unlock(); |
| 113 |
|
|
|
| 114 |
|
|
// printing consumed item ids to console |
| 115 |
|
|
|
| 116 |
|
✗ |
while (!consumed_ids.empty()) |
| 117 |
|
|
{ |
| 118 |
|
✗ |
int id = consumed_ids.front(); |
| 119 |
|
✗ |
consumed_ids.pop_front(); |
| 120 |
|
✗ |
std::cout << "\t\tconsumed: " << id << "\n"; |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
// note : slower than consumer, |
| 124 |
|
|
// as otherwise the buffer |
| 125 |
|
|
// would end up overflowing |
| 126 |
|
|
|
| 127 |
|
✗ |
usleep(2000); |
| 128 |
|
|
} |
| 129 |
|
|
} |
| 130 |
|
|
|
| 131 |
|
✗ |
int main() |
| 132 |
|
|
{ |
| 133 |
|
✗ |
RUNNING = true; |
| 134 |
|
|
|
| 135 |
|
|
// cleaning and exit on ctrl+c |
| 136 |
|
|
struct sigaction cleaning; |
| 137 |
|
✗ |
cleaning.sa_handler = stop; |
| 138 |
|
✗ |
sigemptyset(&cleaning.sa_mask); |
| 139 |
|
✗ |
cleaning.sa_flags = 0; |
| 140 |
|
✗ |
sigaction(SIGINT, &cleaning, nullptr); |
| 141 |
|
|
|
| 142 |
|
✗ |
execute(); |
| 143 |
|
|
} |
| 144 |
|
|
|