-
Notifications
You must be signed in to change notification settings - Fork 5
/
list.cpp
165 lines (124 loc) · 2.19 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "stratum.h"
//#define _LIST_DEBUG_
void CommonLock(pthread_mutex_t *mutex)
{
if (g_debuglog_list) {
int i=0;
for(; i<10; i++)
{
int res = pthread_mutex_trylock(mutex);
if(res == 0) break;
usleep(100*YAAMP_MS);
}
if(i == 10)
debuglog("failed mutex2 %x <<----------------\n", mutex);
} else {
pthread_mutex_lock(mutex);
}
}
void CommonUnlock(pthread_mutex_t *mutex)
{
pthread_mutex_unlock(mutex);
}
CommonList::CommonList()
{
count = 0;
yaamp_create_mutex(&mutex);
first = NULL;
last = NULL;
}
CommonList::~CommonList()
{
// DeleteAll(NULL);
}
void CommonList::Enter()
{
if (g_debuglog_list) {
int i=0;
for(; i<10; i++)
{
int res = pthread_mutex_trylock(&mutex);
if(res == 0) break;
usleep(100*YAAMP_MS);
}
if(i == 10)
debuglog("failed mutex1 %x <<----------------\n", &mutex);
} else {
pthread_mutex_lock(&mutex);
}
}
void CommonList::Leave()
{
pthread_mutex_unlock(&mutex);
}
CLI CommonList::AddTail(void *data)
{
Enter();
CLI item = new COMMONLISTITEM;
item->data = data;
count++;
item->prev = last;
item->next = NULL;
last = item;
if(!first) first = item;
if(item->prev) item->prev->next = item;
Leave();
return item;
}
void CommonList::Delete(CLI item)
{
Enter();
if(first == item)
first = item->next;
if(last == item)
last = item->prev;
if(item->prev) item->prev->next = item->next;
if(item->next) item->next->prev = item->prev;
count--;
delete item;
Leave();
}
void CommonList::Delete(void *data)
{
CLI item = Find(data);
if(item) Delete(item);
}
void CommonList::DeleteAll(LISTFREEPARAM freeparam)
{
Enter();
for(CLI li1 = first; li1; )
{
CLI tmp = li1;
li1 = li1->next;
if(freeparam)
freeparam(tmp->data);
delete tmp;
}
count = 0;
Leave();
}
CLI CommonList::Find(void *data)
{
Enter();
for(CLI item = first; item; item = item->next)
if(data == item->data)
{
Leave();
return item;
}
Leave();
return NULL;
}
void CommonList::Swap(CLI i1, CLI i2)
{
// Enter();
if(i1->prev) i1->prev->next = i2;
if(i2->next) i2->next->prev = i1;
i1->next = i2->next;
i2->prev = i1->prev;
i1->prev = i2;
i2->next = i1;
if(!i2->prev) first = i2;
if(!i1->next) last = i1;
// Leave();
}