Repository navigation
Expand file tree
/
Copy paththread_safe_bounded_queue.c
More file actions
executable file
·119 lines (103 loc) · 3.37 KB
/
Copy paththread_safe_bounded_queue.c
File metadata and controls
executable file
·119 lines (103 loc) · 3.37 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
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 project2 Thread_safe_bounded_queue
This is is from starter code
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "bounded_queue.h"
#include "thread_safe_bounded_queue.h"
struct thread_safe_bounded_queue
{
BoundedQueue *queue;
pthread_mutex_t lock;
};
TSBoundedQueue *TS_BB_MallocBoundedQueue(long size)
{
struct thread_safe_bounded_queue *returnValue = NULL;
returnValue = (struct thread_safe_bounded_queue*)malloc(sizeof(struct thread_safe_bounded_queue));
pthread_mutex_init(&(returnValue->lock), NULL);
returnValue->queue = BB_MallocBoundedQueue(size);
return (TSBoundedQueue *)returnValue;
}
long long TS_BB_TryEnqueue(struct thread_safe_bounded_queue *queue,void *item)
{
long long returnValue = -1;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_TryEnqueue(queue->queue,item);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
int TS_BB_TryDequeue(struct thread_safe_bounded_queue *queue,long long id)
{
int returnValue = 0;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_TryDequeue(queue->queue,id);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
long long TS_BB_GetFront(struct thread_safe_bounded_queue *queue)
{
long long returnValue = -1;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_GetFront(queue->queue);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
long long TS_BB_GetBack(struct thread_safe_bounded_queue *queue)
{
long long returnValue = -1;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_GetBack(queue->queue);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
int TS_BB_GetCount(struct thread_safe_bounded_queue *queue)
{
long long returnValue = 0;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_GetCount(queue->queue);
pthread_mutex_unlock(&(queue->lock));
return (int)returnValue;
}
int TS_BB_IsIdValid(struct thread_safe_bounded_queue *queue,long long id)
{
int returnValue = 0;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_IsIdValid(queue->queue,id);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
void *TS_BB_GetItem(struct thread_safe_bounded_queue *queue,long long id)
{
void *returnValue = NULL;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_GetItem(queue->queue,id);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
int TS_BB_IsFull(struct thread_safe_bounded_queue *queue)
{
int returnValue = 0;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_IsFull(queue->queue);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
int TS_BB_IsEmpty(struct thread_safe_bounded_queue *queue)
{
int returnValue = 0;
pthread_mutex_lock(&(queue->lock));
returnValue = BB_IsEmpty(queue->queue);
pthread_mutex_unlock(&(queue->lock));
return returnValue;
}
void TS_BB_FreeBoundedQueue(struct thread_safe_bounded_queue *queue)
{
BB_FreeBoundedQueue(queue->queue);
queue->queue = NULL;
pthread_mutex_destroy(&(queue->lock));
free(queue);
}