aboutsummaryrefslogtreecommitdiff
path: root/pa6_threaded_resolver/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'pa6_threaded_resolver/array.c')
-rw-r--r--pa6_threaded_resolver/array.c65
1 files changed, 28 insertions, 37 deletions
diff --git a/pa6_threaded_resolver/array.c b/pa6_threaded_resolver/array.c
index e43dbdf..2976bda 100644
--- a/pa6_threaded_resolver/array.c
+++ b/pa6_threaded_resolver/array.c
@@ -1,48 +1,39 @@
// Jack Sangdahl
// CSCI3753 SP24
-// PA6 Threaded Resolver
+// PA4 Shared Array
#include "array.h"
-int queue_init(queue *q, int queue_size) {
- q->data = malloc(queue_size * sizeof(node));
- q->maxsize = queue_size;
- q->front = 0;
- q->back = -1;
- q->size = 0;
- return EXIT_SUCCESS;
+int array_init(array *q) {
+ q->head = 0;
+ q->tail = 0;
+ pthread_mutex_init(&q->lock, NULL);
+ sem_init(&q->avail, 0, ARRAY_SIZE);
+ sem_init(&q->empty, 0, 0);
+ return 0;
}
-int queue_size(queue *q) { return q->size; }
-
-bool queue_full(queue *q) { return (q->size == q->maxsize); }
-
-int queue_push(queue *q, void *element) {
- if (queue_full(q)) {
- fprintf(stderr, "push error: queue full\n");
- return EXIT_FAILURE;
- }
- q->back = (q->back + 1) % q->maxsize;
- q->data[q->back].value = element;
- q->size++;
- return EXIT_SUCCESS;
+int array_put(array *q, char *hostname) {
+ sem_wait(&q->avail);
+ pthread_mutex_lock(&q->lock);
+ q->data[q->tail++] = hostname;
+ q->tail %= ARRAY_SIZE;
+ pthread_mutex_unlock(&q->lock);
+ sem_post(&q->empty);
+ return 0;
}
-void *queue_pop(queue *q) {
- if (!queue_size(q)) {
- fprintf(stderr, "pop error: queue empty\n");
- return NULL;
- }
- void *value = q->data[q->front].value;
- q->data[q->front].value = NULL;
- q->front = (q->front + 1) % q->maxsize;
- q->size--;
- return value;
+int array_get(array *q, char **hostname) {
+ sem_wait(&q->empty);
+ pthread_mutex_lock(&q->lock);
+ *hostname = q->data[q->head++];
+ q->head %= ARRAY_SIZE;
+ pthread_mutex_unlock(&q->lock);
+ sem_post(&q->avail);
+ return 0;
}
-void queue_free(queue *q) {
- while (q->size > 0) {
- void *val = queue_pop(q);
- free(val);
- }
- free(q->data);
+void array_free(array *q) {
+ pthread_mutex_destroy(&q->lock);
+ sem_destroy(&q->avail);
+ sem_destroy(&q->empty);
}