-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlqueue.c
More file actions
88 lines (78 loc) · 2.1 KB
/
lqueue.c
File metadata and controls
88 lines (78 loc) · 2.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
/*
* lqueue.c by Tao Wang (Peter)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "lqueue.h"
#include "queue.h"
pthread_mutex_t lock;
lqueue_t *
lqopen(void) {
struct lockedQ *lq = calloc(1, sizeof(struct lockedQ));
pthread_mutex_init(&lq->lock, NULL);
lq->queue = qopen();
return lq;
}
void
lqclose(lqueue_t *qp) {
struct lockedQ *lq = (struct lockedQ *)qp;
pthread_mutex_lock(&lq->lock);
qclose(lq->queue);
pthread_mutex_unlock(&lq->lock);
}
int
lqput(lqueue_t *qp, void *elementp) {
struct lockedQ *lq = (struct lockedQ *)qp;
int ret;
pthread_mutex_lock(&lq->lock);
ret = qput(lq->queue, elementp);
pthread_mutex_unlock(&lq->lock);
return ret;
}
void *
lqget(lqueue_t *qp) {
struct lockedQ *lq = (struct lockedQ *)qp;
void *ret;
pthread_mutex_lock(&lq->lock);
ret = qget(lq->queue);
pthread_mutex_unlock(&lq->lock);
return ret;
}
void
lqapply(lqueue_t *qp, void (*fn)(void *elementp)) {
struct lockedQ *lq = (struct lockedQ *)qp;
pthread_mutex_lock(&lq->lock);
qapply(lq->queue, fn);
pthread_mutex_unlock(&lq->lock);
}
void *
lqsearch(lqueue_t *qp, int (*searchfn)(void* elementp, const void* keyp), const void* skeyp) {
struct lockedQ *lq = (struct lockedQ *)qp;
void *ret;
pthread_mutex_lock(&lq->lock);
ret = qsearch(lq->queue, searchfn, skeyp);
pthread_mutex_unlock(&lq->lock);
return ret;
}
void *
lqremove(lqueue_t *qp, int (*searchfn)(void* elementp,const void* keyp), const void* skeyp) {
struct lockedQ *lq = (struct lockedQ *)qp;
void *ret;
pthread_mutex_lock(&lq->lock);
ret = qremove(lq->queue, searchfn, skeyp);
pthread_mutex_unlock(&lq->lock);
return ret;
}
/* concatenatenates elements of q2 into q1, q2 is dealocated upon completion */
void
lqconcat(lqueue_t *q1p, queue_t *q2p) {
struct lockedQ *lq1 = (struct lockedQ *)q1p;
struct lockedQ *lq2 = (struct lockedQ *)q2p;
pthread_mutex_lock(&lq1->lock);
pthread_mutex_lock(&lq1->lock);
qconcat(lq1->queue, lq2->queue);
pthread_mutex_unlock(&lq1->lock);
pthread_mutex_unlock(&lq1->lock);
}