4 #include "ConcurrentLinkedListQueue.hpp"
9 Node::Node(char* val, int len) {
30 char* Node::getValue() {
36 int Node::getLength() {
42 Node* Node::getNext() {
48 void Node::setNext(Node* nxt) {
54 ConcurrentLinkedListQueue::ConcurrentLinkedListQueue() {
61 ConcurrentLinkedListQueue::~ConcurrentLinkedListQueue() {
64 do { // Dequeue and free everything up
70 void ConcurrentLinkedListQueue::enqueue(char* value, int length) {
72 lock_guard<mutex> guard(queueMutex);
73 if (tail == NULL && head == NULL) { // first element
74 tail = new Node(value, length);
75 head = tail; // Both tail and head point to the first element
76 } else { // Next elements
77 Node* newEl = new Node(value, length);
84 // Return element and remove from list
85 char* ConcurrentLinkedListQueue::dequeue() {
87 lock_guard<mutex> guard(queueMutex);
88 if (tail == NULL && head == NULL) { // empty
92 if (head->getNext() == NULL) {
96 head = head->getNext();
97 char* retVal = retEl->getValue();
98 // Prepare retEl for deletion
101 // Return just the value
106 // Return element, length, and remove it from list
107 char* ConcurrentLinkedListQueue::deQAndGetLength(int* length) {
109 lock_guard<mutex> guard(queueMutex);
110 if (tail == NULL && head == NULL) { // empty
115 if (head->getNext() == NULL) {
119 head = head->getNext();
120 char* retVal = retEl->getValue();
121 *length = retEl->getLength();
122 // Prepare retEl for deletion
123 retEl->setNext(NULL);
125 // Return just the value
126 //cout << "Print bytes inside dequeue: ";
127 //IoTRMIUtil::printBytes(*((char**) retVal), *length, false);
128 //cout << "Dequeuing: " << *((char**) retVal) << endl;
129 //cout << "Dequeuing address: " << std::ref(retVal) << endl;