|
@@ -16,23 +16,20 @@
|
16
|
16
|
#include "queue.h"
|
17
|
17
|
|
18
|
18
|
QUEUE *queue_new(void) {
|
19
|
|
- pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
|
20
|
|
- if (pthread_mutex_init(mutex,NULL) != 0) {
|
|
19
|
+ QUEUE *q = calloc(1, sizeof(QUEUE));
|
|
20
|
+ if (!q)
|
|
21
|
+ return NULL;
|
|
22
|
+ if (pthread_mutex_init(&q->mutex,NULL) != 0) {
|
21
|
23
|
perror("queue_new: mutex_init");
|
|
24
|
+ free(q);
|
22
|
25
|
return NULL;
|
23
|
26
|
}
|
24
|
|
- pthread_cond_t *cond = malloc(sizeof(pthread_cond_t));
|
25
|
|
- if (pthread_cond_init(cond,NULL)!=0){
|
|
27
|
+ if (pthread_cond_init(&q->cond,NULL)!=0){
|
26
|
28
|
perror("queue_new: cond_init");
|
|
29
|
+ free(q);
|
27
|
30
|
return NULL;
|
28
|
31
|
}
|
29
|
|
- QUEUE *q = calloc(1, sizeof(QUEUE));
|
30
|
|
- if (!q)
|
31
|
|
- return NULL;
|
32
|
|
- // initialize queue
|
33
|
|
- q->mutex = mutex;
|
34
|
|
- q->cond = cond;
|
35
|
|
- return q;
|
|
32
|
+ return q;
|
36
|
33
|
}
|
37
|
34
|
|
38
|
35
|
void queue_free(QUEUE **pq) {
|
|
@@ -42,24 +39,22 @@ void queue_free(QUEUE **pq) {
|
42
|
39
|
while (q->items > 0) {
|
43
|
40
|
queue_get(q);
|
44
|
41
|
}
|
45
|
|
- pthread_mutex_destroy(q->mutex);
|
46
|
|
- FREE(q->mutex);
|
47
|
|
- pthread_cond_destroy(q->cond);
|
48
|
|
- FREE(q->cond);
|
|
42
|
+ pthread_mutex_destroy(&q->mutex);
|
|
43
|
+ pthread_cond_destroy(&q->cond);
|
49
|
44
|
FREE(*pq);
|
50
|
45
|
}
|
51
|
46
|
|
52
|
47
|
void queue_add(QUEUE *q, void *data) {
|
53
|
48
|
if (!q)
|
54
|
49
|
return;
|
55
|
|
- QNODE *a_msg = malloc(sizeof(QNODE));
|
|
50
|
+ QNODE *a_msg = calloc(1, sizeof(QNODE));
|
56
|
51
|
if (!a_msg) {
|
57
|
52
|
perror("queue_enqueue, malloc:");
|
58
|
53
|
return;
|
59
|
54
|
}
|
60
|
55
|
a_msg->data = data;
|
61
|
56
|
a_msg->next = NULL;
|
62
|
|
- pthread_mutex_lock(q->mutex);
|
|
57
|
+ pthread_mutex_lock(&q->mutex);
|
63
|
58
|
if (q->items == 0) { // special case - queue is empty
|
64
|
59
|
q->head = a_msg;
|
65
|
60
|
q->tail = a_msg;
|
|
@@ -68,16 +63,16 @@ void queue_add(QUEUE *q, void *data) {
|
68
|
63
|
q->tail = a_msg;
|
69
|
64
|
}
|
70
|
65
|
q->items++;
|
71
|
|
- pthread_cond_signal(q->cond);
|
72
|
|
- pthread_mutex_unlock(q->mutex);
|
|
66
|
+ pthread_cond_signal(&q->cond);
|
|
67
|
+ pthread_mutex_unlock(&q->mutex);
|
73
|
68
|
}
|
74
|
69
|
|
75
|
70
|
void *queue_get(QUEUE *q) {
|
76
|
71
|
if (!q)
|
77
|
72
|
return NULL;
|
78
|
|
- pthread_mutex_lock(q->mutex);
|
|
73
|
+ pthread_mutex_lock(&q->mutex);
|
79
|
74
|
while (q->items==0) {
|
80
|
|
- pthread_cond_wait(q->cond, q->mutex);
|
|
75
|
+ pthread_cond_wait(&q->cond, &q->mutex);
|
81
|
76
|
if (q->items == 0)
|
82
|
77
|
return NULL;
|
83
|
78
|
}
|
|
@@ -89,8 +84,8 @@ void *queue_get(QUEUE *q) {
|
89
|
84
|
q->tail = NULL;
|
90
|
85
|
}
|
91
|
86
|
q->items--;
|
92
|
|
- pthread_cond_signal(q->cond);
|
93
|
|
- pthread_mutex_unlock(q->mutex);
|
|
87
|
+ pthread_cond_signal(&q->cond);
|
|
88
|
+ pthread_mutex_unlock(&q->mutex);
|
94
|
89
|
void *data = a_msg->data;
|
95
|
90
|
FREE(a_msg);
|
96
|
91
|
return data;
|
|
@@ -104,7 +99,7 @@ void *queue_get_nowait(QUEUE* q) {
|
104
|
99
|
|
105
|
100
|
void queue_wakeup(QUEUE *q) {
|
106
|
101
|
if (q) {
|
107
|
|
- pthread_cond_signal(q->cond);
|
|
102
|
+ pthread_cond_signal(&q->cond);
|
108
|
103
|
}
|
109
|
104
|
}
|
110
|
105
|
|