| | |
| | | #include "log.h" |
| | | #include <atomic> |
| | | #include <chrono> |
| | | #include <memory> |
| | | #include <mutex> |
| | | #include <string> |
| | | #include <sys/file.h> |
| | | #include <sys/ipc.h> |
| | | #include <sys/sem.h> |
| | | #include <sys/stat.h> |
| | | #include <sys/types.h> |
| | | #include <thread> |
| | | #include <unistd.h> |
| | | |
| | | namespace robust |
| | | { |
| | | |
| | | using namespace std::chrono; |
| | | using namespace std::chrono_literals; |
| | | |
| | | /* |
| | | template <unsigned PowerSize = 4, class Int = int64_t> |
| | | class AtomicQueue |
| | | { |
| | |
| | | std::atomic<size_type> tail_; |
| | | AData buf[capacity]; |
| | | }; |
| | | //*/ |
| | | |
| | | template <class Int> |
| | | class AtomicQueue<0, Int> |
| | | class AtomicQ63 |
| | | { |
| | | typedef Int Data; |
| | | typedef std::atomic<Data> AData; |
| | | static_assert(sizeof(Data) == sizeof(AData)); |
| | | |
| | | public: |
| | | AtomicQueue() { memset(this, 0, sizeof(*this)); } |
| | | typedef int64_t Data; |
| | | AtomicQ63() { memset(this, 0, sizeof(*this)); } |
| | | bool push(const Data d, bool try_more = false) |
| | | { |
| | | auto cur = buf.load(); |
| | |
| | | if (r) { d = Dec(cur); } |
| | | return r; |
| | | } |
| | | uint32_t head() const { return 0; } |
| | | uint32_t tail() const { return 0; } |
| | | |
| | | private: |
| | | static inline bool Empty(const Data d) { return (d & 1) == 0; } // lowest bit 1 means data ok. |
| | | static inline Data Enc(const Data d) { return (d << 1) | 1; } // lowest bit 1 means data ok. |
| | | static inline Data Dec(const Data d) { return d >> 1; } // lowest bit 1 means data ok. |
| | | |
| | | typedef std::atomic<Data> AData; |
| | | static_assert(sizeof(Data) == sizeof(AData)); |
| | | |
| | | AData buf; |
| | | }; |
| | | |
| | |
| | | static int GetState(Data d) { return d & MaskBits(3); } |
| | | static Data Encode(Data d, State st) { return (d << 3) | st; } |
| | | static Data Decode(Data d) { return d >> 3; } |
| | | static void yield() { std::this_thread::sleep_for(10us); } |
| | | typedef std::chrono::steady_clock steady_clock; |
| | | typedef steady_clock::duration Duration; |
| | | Duration now() { return steady_clock::now().time_since_epoch(); } |
| | | |