This commit is contained in:
Nikolaj
2021-10-26 16:12:26 +02:00
parent a988405fd3
commit 456adc7611
11 changed files with 418 additions and 327 deletions

View File

@ -1,5 +1,6 @@
CC=gcc
CFLAGS=-g -Wall -Wextra -pedantic -std=gnu99 -pthread
#CFLAGS=-g -Wall -Wextra -pedantic -std=gnu99 -pthread
CFLAGS=-g -pthread
EXAMPLES=fibs fauxgrep fauxgrep-mt fhistogram fhistogram-mt
.PHONY: all test clean ../src.zip

View File

@ -4,18 +4,90 @@
#include "job_queue.h"
pthread_mutex_t queue_operation = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t queue_push = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t queue_pop = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t queue_destroy = PTHREAD_MUTEX_INITIALIZER;
int job_queue_init(struct job_queue *job_queue, int capacity) {
assert(0);
pthread_mutex_lock(&queue_operation);
pthread_mutex_lock(&queue_push);
pthread_mutex_lock(&queue_pop);
pthread_mutex_lock(&queue_destroy);
job_queue->capacity = capacity;
job_queue->size = 0;
job_queue->jobs = malloc(capacity);
pthread_mutex_unlock(&queue_operation);
pthread_mutex_unlock(&queue_push);
pthread_mutex_unlock(&queue_destroy);
return 0;
}
int job_queue_destroy(struct job_queue *job_queue) {
assert(0);
printf("esauc\n");
pthread_mutex_lock(&queue_destroy);
pthread_mutex_lock(&queue_operation);
printf("esauc\n");
//free(job_queue->jobs);
printf("esauc\n");
//free(job_queue);
printf("esauc\n");
pthread_mutex_unlock(&queue_push);
pthread_mutex_unlock(&queue_pop);
pthread_mutex_unlock(&queue_destroy);
pthread_mutex_unlock(&queue_operation);
return 0;
}
int job_queue_push(struct job_queue *job_queue, void *data) {
assert(0);
pthread_mutex_lock(&queue_push);
pthread_mutex_lock(&queue_operation);
(&job_queue->jobs)[job_queue->size] = data;
job_queue->size = job_queue->size + 1;
if (job_queue->size != job_queue->capacity) {
pthread_mutex_unlock(&queue_push);
}
if (job_queue->size == 1) {
pthread_mutex_unlock(&queue_pop);
pthread_mutex_lock(&queue_destroy);
}
pthread_mutex_unlock(&queue_operation);
return 0;
}
int job_queue_pop(struct job_queue *job_queue, void **data) {
assert(0);
pthread_mutex_lock(&queue_pop);
pthread_mutex_lock(&queue_operation);
if (job_queue == NULL) {
pthread_mutex_unlock(&queue_pop);
pthread_mutex_unlock(&queue_operation);
return -1;
}
job_queue->size = job_queue->size - 1;
*data = (&job_queue->jobs)[job_queue->size];
if (job_queue->size == 0) {
pthread_mutex_unlock(&queue_destroy);
}
if (job_queue->size != 0) {
pthread_mutex_unlock(&queue_pop);
}
if (job_queue->size == job_queue->capacity - 1) {
pthread_mutex_unlock(&queue_pop);
}
pthread_mutex_unlock(&queue_operation);
return 0;
}

View File

@ -4,7 +4,9 @@
#include <pthread.h>
struct job_queue {
int dummy;
int capacity;
int size;
void* jobs;
};
// Initialise a job queue with the given capacity. The queue starts out

BIN
A2/testfile Executable file

Binary file not shown.

16
A2/testfile.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "job_queue.h"
int main() {
struct job_queue q;
int b = 5;
job_queue_init(&q, 64);
job_queue_push(&q, &b);
void* a = malloc(1);
job_queue_pop(&q, &a);
printf("%i\n",*((int *) a));
printf("esauc\n");
job_queue_destroy(&q);
}