UDPConnection/cpp_impl/src/TSQueue.hpp

45 lines
1,006 B
C++
Raw Normal View History

2019-06-06 07:40:04 +00:00
#ifndef UDPC_THREADSAFE_QUEUE_HPP
#define UDPC_THREADSAFE_QUEUE_HPP
#define UDPC_TSQUEUE_DEFAULT_CAPACITY 32
#include <atomic>
#include <cstdlib>
#include <memory>
class TSQueue {
public:
2019-06-07 03:17:35 +00:00
typedef std::unique_ptr<unsigned char[]> TopType;
2019-06-06 07:40:04 +00:00
TSQueue(unsigned int elemSize,
unsigned int capacity = UDPC_TSQUEUE_DEFAULT_CAPACITY);
~TSQueue();
// disable copy
TSQueue(const TSQueue &other) = delete;
TSQueue &operator=(const TSQueue &other) = delete;
// disable move
TSQueue(TSQueue &&other) = delete;
TSQueue &operator=(TSQueue &&other) = delete;
bool push(void *data);
2019-06-07 03:17:35 +00:00
TopType top();
2019-06-06 07:40:04 +00:00
bool pop();
void clear();
void changeCapacity(unsigned int newCapacity);
unsigned int size();
2019-06-06 07:40:04 +00:00
private:
unsigned int elemSize;
2019-06-07 02:33:44 +00:00
unsigned int capacityBytes;
2019-06-06 07:40:04 +00:00
unsigned int head;
unsigned int tail;
bool isEmpty;
std::unique_ptr<unsigned char[]> buffer;
std::atomic_bool spinLock;
2019-06-07 02:33:44 +00:00
unsigned int sizeBytes();
2019-06-06 07:40:04 +00:00
};
#endif