-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.hpp
More file actions
74 lines (59 loc) · 2.04 KB
/
common.hpp
File metadata and controls
74 lines (59 loc) · 2.04 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef __GALAPAGOS_COMMON_HPP__ // if x.h hasn't been included yet...
#define __GALAPAGOS_COMMON_HPP__
#include <mutex>
#include <thread>
#include <condition_variable>
#if LOG_LEVEL > 0
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#endif
namespace galapagos{
typedef struct{
bool * done;
std::mutex * mutex;
}_done_struct; //<! pointer to done that is set outside the router, routing function will stop when done is asserted and no more packets left in router
typedef struct{
int * num;
std::mutex * mutex;
}_num_threadsafe; //<! pointer to done that is set outside the router, routing function will stop when done is asserted and no more packets left in router
class done_clean{
public:
#if LOG_LEVEL > 0
done_clean(bool * _done, std::mutex * _mutex, std::shared_ptr<spdlog::logger> _logger);
#endif
done_clean(bool * _done, std::mutex * _mutex);
_done_struct done_struct;
bool is_done();
void wait_for_clean();
void clean();
~done_clean(){;}
private:
std::mutex mutex_clean_up;
bool clean_status;
std::condition_variable cv_clean_up;
#if LOG_LEVEL > 0
std::shared_ptr<spdlog::logger> logger;
#endif
};
#define POWER_2(x) (1ULL << (x))
template <class T>
T range(short msb, short lsb, T source, size_t value){
short size = msb - lsb + 1;
T max_value = POWER_2(size) - 1;
T value_bitmask = ~(max_value << lsb);
T source_masked = source & value_bitmask;
return source_masked | ((value & max_value) << lsb);
}
// this conflicts with the one below
// template <class T>
// T range(short bit, T source, size_t value){
// return range(bit, bit, source, value);
// }
template <class T>
T range(short msb, short lsb, T source){
short size = msb - lsb + 1;
T max_value = POWER_2(size) - 1;
return (source >> lsb) & max_value;
}
}
#endif