-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomicResultList.h
More file actions
122 lines (98 loc) · 3.1 KB
/
AtomicResultList.h
File metadata and controls
122 lines (98 loc) · 3.1 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <Python.h>
#include <utility>
#include <iostream>
#include <atomic>
namespace Interpret{
class ReturnValue{
public:
PyObject* m_Value;
public:
enum ReturnValueType{
STRING = 0,
NUMBER = 1,
DICTIONARY = 2,
LIST = 3,
NULL_OBJECT = 4
} ;
private:
ReturnValueType m_Type;
public:
ReturnValue(PyObject* theValue, const ReturnValueType& t)
: m_Value(theValue), m_Type(t)
{
//std::cout << "Added" << PyInt_AsLong(m_Value) << "=>" << m_Value << std::endl;
}
~ReturnValue()
{
if(m_Value) {
//std::cout << "Going to delete => " << m_Value << std::endl;
Py_DECREF(m_Value);
}
}
ReturnValueType getType(){return m_Type;}
PyObject* getValue(){return m_Value;}
};
typedef std::pair<int, ReturnValue*> ReturnValueEntry;
class AtomicResultList
{
struct Node {
Node* m_Next;
ReturnValueEntry m_Value;
Node():m_Next(nullptr){}
Node(const Node& another):m_Value(another.m_Value){
std::cout << "cpy ctor" << std::endl;
}
Node(const ReturnValueEntry& another){
m_Value = std::make_pair<int, ReturnValue*> (0, nullptr);
m_Value.first = another.first;
m_Value.second = another.second;
std::cout << "Node->" << m_Value.second->m_Value << std::endl;
}
~Node(){
std::cout << "Delete the node" << std::endl;
assert(m_Value.second);
delete m_Value.second;
}
};
std::atomic<Node*> m_Head{nullptr};
public:
~AtomicResultList()
{
//std::cout << "AtomicResultList~" << std::endl;
clear();
}
void clear()
{
Node* newHead = nullptr;
newHead = m_Head.load();
while(!m_Head.compare_exchange_weak(newHead,nullptr ));
Node* current = nullptr;
int i = 0;
while(1){
current = newHead;
if(!current->m_Next) break;
newHead = current->m_Next;
std::cout << i++ << std::endl;
delete current;
}
}
void dump()
{
Node* current = m_Head.load();
int size = 0;
while(!m_Head.compare_exchange_weak(current,m_Head));
while(current) {
std::cout << size++ << ". => " << current->m_Value.second->m_Value << std::endl;
current = current->m_Next;
}
}
void push(ReturnValueEntry& entry){
Node* n = new Node(entry);
n->m_Next = m_Head.load();
while(!m_Head.compare_exchange_weak(n->m_Next,n )){
//if head == n->next, then head = n, return true finally
std::cout<<n->m_Next<<" "<<m_Head<<std::endl;
};
}
};
};