user14595256
user14595256

Reputation:

Thread safe functions using semaphore in C

#include "queue.h"
#include <semaphore.h>

sem_t sem;

Queue queueCreate(unsigned capacity){
    sem_init(&sem, 0, 1);
    Queue q = malloc(sizeof(queue_t)); 
    q->capacity = capacity;
    q->front = q->size = 0;
    q->rear = capacity - 1;
    q->array = malloc(q->capacity * sizeof(*q->array));
    return q;
}

void queueDestroy(Queue q){
    free(q->array);
    free(q);
    sem_destroy(&sem);
}

int queueIsFull(Queue q){
    sem_wait(&sem);
    return (q->size == q->capacity);
    sem_post(&sem);
}

int queueIsEmpty(Queue q){
    sem_wait(&sem);
    return (q->size == 0);
    sem_post(&sem);
}

int queueEnqueue(Queue q, int* value){ 
    sem_wait(&sem);
    if (queueIsFull(q)) return -1;
    q->rear = (q->rear + 1) % q->capacity;
    q->array[q->rear] = *value;
    q->size = q->size + 1;
    sem_post(&sem);
    return 0;
}

int queueDequeueFront(Queue queue, int* container){
    sem_wait(&sem);
    if (queueIsEmpty(queue)) return -1; 
    *container = queue->array[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    sem_post(&sem);
    return 0;
}

int queueDequeueRear(Queue queue, int* container){
    sem_wait(&sem);
    if (queueIsEmpty(queue)) return -1; 
    *container = queue->array[queue->rear];
    queue->rear = (queue->capacity + queue->rear - 1) % queue->capacity;
    queue->size = queue->size - 1; 
    sem_post(&sem);
    return 0;
}

int* queueFront(Queue queue){
    sem_wait(&sem);
    if (queueIsEmpty(queue)) return NULL;
    return &queue->array[queue->front]; 
    sem_post(&sem);
}

int* queueRear(Queue queue){ 
    sem_wait(&sem);
    if (queueIsEmpty(queue)) return NULL;
    return &queue->array[queue->rear];
    sem_post(&sem);
}

void queuePrint(Queue q){
    printf("Queue(^: front, *: rear): [");
    int i;
    for(i = 0; i < q->capacity; i++){
        printf("%d", q->array[i]);
        if(i == q->front) printf("^");
        if(i == q->rear) printf("*");
        i == q->capacity - 1 ? printf("] ") : printf(", ");
    }
    printf("size=%d, capacity=%d\n", q->size, q->capacity);
    fflush(stdout);
} 

I am a beginner, and I am working on a homework needed to make these functions "thread-safe". It is supposed to be a library that will be used by other programs to test it. As you can see, I have declared sem globally (this does not feel like I can declare in the functions), calling sem_init in the function queueCreate, calling sem_destroy in function queueDetroy, and basically enclosed all the other functions with sem_wait and sem_destroy. But when I am trying to test it, the testing program (these are templates and should be error-free) basically stuck in the very beginning, like in an infinite loop, and need to be interrupted manually. I don't know why.

Upvotes: 1

Views: 313

Answers (1)

Omry
Omry

Reputation: 316

Some of your methods are waiting on the semaphore, returning, then attempting to post to it. You can't post to the semaphore inside a function that returned, that function has finished its execution.

For example:

int queueIsFull(Queue q){
sem_wait(&sem);
return (q->size == q->capacity);
sem_post(&sem);
}

Should be:

int queueIsFull(Queue q){
int ret_val;
sem_wait(&sem);
ret_val = (q->size == q->capacity);
sem_post(&sem);
return ret_val
}

Upvotes: 1

Related Questions