-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtsqueue.c
More file actions
184 lines (156 loc) · 3.51 KB
/
Copy pathtsqueue.c
File metadata and controls
184 lines (156 loc) · 3.51 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
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* =====================================================================================
*
* Filename: tsqueue.c
*
* Description: it's a thread safe queue
*
* Version: 1.0
* Created: 05/08/2012 09:53:42 AM
* Revision: none
* Compiler: gcc
*
* Author: boyce
* Organization: gw
*
* =====================================================================================
*/
#include <stdlib.h>
#include <pthread.h>
#include "tsqueue.h"
static void ts_queue_init(TSQueue *cq);
//static TSQItem *ts_queue_item_new(TSQueue *cq);
//static void ts_queue_item_free(TSQueue *cq, TSQItem *item);
static TSQItem *ts_queue_head(TSQueue *cq);
static TSQItem *ts_queue_tail(TSQueue *cq);
static TSQItem *ts_queue_peek(TSQueue *cq);
static TSQItem *ts_queue_deq(TSQueue *cq);
static void ts_queue_enq(TSQueue *cq, TSQItem *item);
TSQueue *ts_queue_create(){
TSQueue *cq = (TSQueue *) malloc(sizeof(TSQueue));
ts_queue_init(cq);
return cq;
}
void ts_queue_destroy(TSQueue *cq){
if(!cq)
return;
while (!ts_queue_is_empty(cq)) ts_queue_deq_data(cq);
pthread_mutex_destroy(&cq->lock);
free(cq);
}
void *ts_queue_deq_data(TSQueue *cq){
void *data;
TSQItem *item;
if(!cq)
return NULL;
item = ts_queue_deq(cq);
if(!item){
//
return NULL;
}
data = item->data;
free(item);
return data;
}
int ts_queue_enq_data(TSQueue *cq, void *data){
TSQItem *item;
if(!cq || !data)
return -1;
item = (TSQItem *) malloc(sizeof(TSQItem));
if(!item){
//perror("ts_queue_push_data");
return -1;
}
item->data = data;
ts_queue_enq(cq, item);
return 0;
}
void *ts_queue_rm_data(TSQueue *cq, void *data){
TSQItem **p;
TSQItem *item = NULL;
TSQItem *prev = NULL;
if(!cq || !data)
return NULL;
pthread_mutex_lock(&cq->lock);
p = &cq->head;
while (*p) {
if ((*p)->data == data) {
//data found
item = *p;
*p = item->next; //remove item from queue
item->next = NULL;
if (item == cq->tail) cq->tail = prev;
cq->count--;
break;
}
prev = *p;
p = &(*p)->next;
}
pthread_mutex_unlock(&cq->lock);
if (item) {
free(item);
return data;
}
return NULL;
}
unsigned ts_queue_count(TSQueue *cq){
unsigned count = 0;
pthread_mutex_lock(&cq->lock);
count = cq->count;
pthread_mutex_unlock(&cq->lock);
return count;
}
BOOL ts_queue_is_empty(TSQueue *cq){
return ts_queue_count(cq)? FALSE : TRUE;
}
static void ts_queue_init(TSQueue *cq){
if(!cq)
return;
pthread_mutex_init(&cq->lock, NULL);
cq->head = NULL;
cq->tail = NULL;
cq->count = 0;
}
static TSQItem *ts_queue_head(TSQueue *cq){
//TSQItem *item;
if(!cq)
return NULL;
return cq->head;
}
static TSQItem *ts_queue_tail(TSQueue *cq){
//TSQItem *item;
if(!cq)
return NULL;
return cq->tail;
}
static TSQItem *ts_queue_peek(TSQueue *cq){
return ts_queue_head(cq);
}
static TSQItem *ts_queue_deq(TSQueue *cq){
TSQItem *item;
if(!cq)
return NULL;
pthread_mutex_lock(&cq->lock);
item = cq->head;
if(NULL != item){
cq->head = item->next;
if(NULL == cq->head)
cq->tail = NULL;
cq->count--;
}
pthread_mutex_unlock(&cq->lock);
return item;
}
static void ts_queue_enq(TSQueue *cq, TSQItem *item) {
if(!cq || !item)
return;
item->next = NULL;
pthread_mutex_lock(&cq->lock);
if (NULL == cq->tail)
cq->head = item;
else
cq->tail->next = item;
cq->tail = item;
cq->count++;
pthread_mutex_unlock(&cq->lock);
}