/
list.cpp
64 lines (57 loc) · 1.36 KB
/
list.cpp
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
#include "msg_lib.hpp"
#include "link.hpp"
#include "list.hpp"
#include <new>
//constructor
List::List() { //creates datapath for thread
head = nullptr;
tail = nullptr;
length = 0;
}
int list_push(List* dest, message_t* msg) { // pushes msg to another thread
try {
if(dest->length == 0) {
dest->lock.lock();
dest->head = new Link(msg);
dest->tail = dest->head;
dest->length = 1;
dest->lock.unlock();
return 0;
}
else {
dest->lock.lock();
dest->tail->next = new Link(msg);
dest->tail = dest->tail->next;
dest->length++;
dest->lock.unlock();
return 0;
}
}
catch(const std::bad_alloc& e) {
dest->lock.unlock();
return 1;
}
}
message_t* List::list_pop() { //pops from own list
try {
lock.lock();
message_t* temp_msg;
if(head != nullptr) {
temp_msg = head->msg;
Link* deleted_link = head;
head->msg = nullptr; //removes msg before deleting the Link
head = head->next;
delete deleted_link;
length--;
}
else {
throw 0;
}
lock.unlock();
return temp_msg;
}
catch(int num) {
lock.unlock();
return nullptr;
}
}