-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpq.c
More file actions
170 lines (125 loc) · 2.79 KB
/
pq.c
File metadata and controls
170 lines (125 loc) · 2.79 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
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
162
163
164
165
166
167
168
169
170
/**
* pq.c -- thread safety producer and consumer priority queue
*
* @author tony <tunghai.huo@gmail.com>
*
*/
#include "pq.h"
pq_handle *pq_init(int pq_size, int entry_size, int (*cb) (void *, void *))
{
int ret;
if (pq_size <= 0)
return NULL;
if (entry_size <= 0)
return NULL;
if (cb == NULL)
return NULL;
pq_handle *hand = (pq_handle *) malloc(sizeof(pq_handle));
if (hand == NULL)
return NULL;
hand->buffer = (void **)malloc(sizeof(void *) * pq_size);
hand->pq_size = pq_size;
hand->count = 0;
if (hand->buffer == NULL)
return NULL;
hand->magic = sizeof(pq_handle);
hand->entry_size = entry_size;
hand->compare = cb;
ret = sem_init(&hand->mutex, 0, 1);
if (ret != 0)
return NULL;
ret = sem_init(&hand->empty, 0, pq_size); // maybe bug
if (ret != 0)
return NULL;
ret = sem_init(&hand->full, 0, 0);
if (ret != 0)
return NULL;
return hand;
}
void swap(void **buffer, int a, int b)
{
int temp = a;
buffer[a] = buffer[b];
buffer[b] = buffer[temp];
}
/**
* keep heap features
*
*/
void heapify(int (*compare) (void *, void *), void **buffer, int count,
int index)
{
int left = 2 * index + 1;
int right = 2 * index + 1;
if (left >= count)
left = 0;
if (right >= count)
right = 0;
int largest = index;
if (left != 0 && compare(buffer[left], buffer[index]))
largest = left;
if (right != 0 && compare(buffer[right], buffer[index]))
largest = right;
if (largest != index)
{
swap(buffer, index, largest);
heapify(compare, buffer, count, largest);
}
}
int pq_put(pq_handle * hand, void *entry)
{
if (hand == NULL)
return -1;
if (entry == NULL)
return -1;
if (hand->magic != sizeof(pq_handle))
return -1;
// if(sizeof(*entry) != hand->entry_size)
// return -1;
sem_wait(&hand->empty);
sem_wait(&hand->mutex);
hand->buffer[hand->count] = entry;
int index = hand->count;
int parent = (index-1)/2;
while(index>0 && hand->compare(hand->buffer[index], hand->buffer[parent]))
{
swap(hand->buffer, index, parent);
index = parent;
parent = (index - 1) / 2;
}
hand->count++;
sem_post(&hand->mutex);
sem_post(&hand->full);
return 0;
}
void *pq_get(pq_handle * hand)
{
if (hand == NULL)
return NULL;
void *entry = NULL;
sem_wait(&hand->full); //down
sem_wait(&hand->mutex); //down
entry = hand->buffer[0];
hand->buffer[0] = hand->buffer[hand->count];
hand->count--;
heapify(hand->compare, hand->buffer, hand->count, 0);
sem_post(&hand->mutex); //up
sem_post(&hand->empty); //up
return entry;
}
int pq_uninit(pq_handle * hand)
{
if (hand == NULL)
return -1;
if (hand->magic != sizeof(pq_handle))
return -1;
// if(hand->mutex != 0)
sem_destroy(&hand->mutex);
// if(hand->empty != 0)
sem_destroy(&hand->empty);
// if(hand->full != 0)
sem_destroy(&hand->full);
if (hand != NULL)
free(hand);
return 0;
}