Skip to content
Snippets Groups Projects
Commit 08e82543 authored by iliya's avatar iliya
Browse files

feat: using provided wrappers, tasks are being launched in parallel

parent 4bf79a5e
No related branches found
No related tags found
No related merge requests found
#include "thread_wrapper.h" #include "thread_wrapper.h"
// #include <assert.h> #include <assert.h>
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
...@@ -20,7 +20,7 @@ typedef struct { ...@@ -20,7 +20,7 @@ typedef struct {
int *ptr; int *ptr;
int size; int size;
int capacity; int capacity;
pthread_mutex_t lock; pthread_mutex_t *lock;
} stack_t; } stack_t;
typedef struct { typedef struct {
...@@ -29,20 +29,14 @@ typedef struct { ...@@ -29,20 +29,14 @@ typedef struct {
} thr_params; } thr_params;
bool stack_create(stack_t *s, int max_size) { bool stack_create(stack_t *s, int max_size) {
if (max_size < 1) { assert(max_size >= 1);
fprintf(stderr, "Stack size has to be equal to or greater than 1");
return false;
}
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&s->lock, &attr); s->lock = mutex_create();
assert(s->lock != NULL);
// pthread_mutexattr_destroy(&attr);
s->ptr = malloc(max_size * sizeof(int)); s->ptr = malloc(max_size * sizeof(int));
assert(s->ptr != NULL);
s->capacity = max_size; s->capacity = max_size;
s->size = 0; s->size = 0;
...@@ -53,11 +47,10 @@ bool stack_is_empty(stack_t *s) { return s->size == 0; } ...@@ -53,11 +47,10 @@ bool stack_is_empty(stack_t *s) { return s->size == 0; }
void stack_push(stack_t *s, int val) { void stack_push(stack_t *s, int val) {
if (s->size < s->capacity) { if (s->size < s->capacity) {
pthread_mutex_lock(&s->lock); mutex_lock(s->lock);
s->ptr[(s->capacity - 1) - s->size] = val; s->ptr[(s->capacity - 1) - s->size] = val;
s->size++; s->size++;
pthread_mutex_unlock(&s->lock); mutex_unlock(s->lock);
// fprintf(stderr, "%d has been inserted\n", val);
} else { } else {
fprintf(stderr, "Stack is full\n"); fprintf(stderr, "Stack is full\n");
} }
...@@ -65,28 +58,28 @@ void stack_push(stack_t *s, int val) { ...@@ -65,28 +58,28 @@ void stack_push(stack_t *s, int val) {
// Returns -1 in case of an error // Returns -1 in case of an error
int stack_pop(stack_t *s) { int stack_pop(stack_t *s) {
if (stack_is_empty(s)) { assert(!stack_is_empty(s));
fprintf(stderr, "Stack is empty\n");
return -1;
}
pthread_mutex_lock(&s->lock); // pthread_mutex_lock(&s->lock);
mutex_lock(s->lock);
int ret = s->ptr[s->capacity - s->size]; int ret = s->ptr[s->capacity - s->size];
s->size--; s->size--;
pthread_mutex_unlock(&s->lock); mutex_unlock(s->lock);
return ret; return ret;
} }
void stack_destroy(stack_t *s) { void stack_destroy(stack_t *s) {
if (s->ptr != NULL) { mutex_destroy(s->lock);
pthread_mutex_destroy(&s->lock);
assert(s->ptr != NULL);
free(s->ptr); free(s->ptr);
} }
}
void *test_stack(void *data) { void *test_stack(void *data) {
fprintf(stderr, "thread n°%lu, waiting...\n", pthread_self());
bar_wait(b); bar_wait(b);
fprintf(stderr, "launching task for thread id n°%lu\n", pthread_self());
thr_params *p = (thr_params *)data; thr_params *p = (thr_params *)data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment