Minimal refactoring of queue sources

replace/8e04fec8a9ceea047647e89cd734b4136eab0795
Silvano Seva 2021-03-10 17:00:29 +01:00
rodzic fb3447b0c6
commit e5316aa8be
2 zmienionych plików z 66 dodań i 14 usunięć

Wyświetl plik

@ -1,3 +1,26 @@
/***************************************************************************
* Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, *
* Niccolò Izzo IU2KIN *
* Frederik Saraci IU2NRO *
* Silvano Seva IU2KWO *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#ifndef QUEUE_H
#define QUEUE_H
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@ -17,10 +40,24 @@ typedef struct queue_t
uint32_t buffer[MSG_QTY]; uint32_t buffer[MSG_QTY];
} queue_t; } queue_t;
/**
*
*/
void queue_init(queue_t *q); void queue_init(queue_t *q);
/**
*
*/
void queue_terminate(queue_t *q); void queue_terminate(queue_t *q);
/**
*
*/
bool queue_pend(queue_t *q, uint32_t *msg, bool blocking); bool queue_pend(queue_t *q, uint32_t *msg, bool blocking);
/**
*
*/
bool queue_post(queue_t *q, uint32_t msg); bool queue_post(queue_t *q, uint32_t msg);
#endif

Wyświetl plik

@ -1,9 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include "queue.h" #include "queue.h"
void queue_init(queue_t *q) { void queue_init(queue_t *q)
if(q == NULL) {
return; if(q == NULL) return;
pthread_mutex_init(&q->mutex, NULL); pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->not_empty, NULL); pthread_cond_init(&q->not_empty, NULL);
q->read_pos = 0; q->read_pos = 0;
@ -11,51 +11,65 @@ void queue_init(queue_t *q) {
q->msg_num = 0; q->msg_num = 0;
} }
void queue_terminate(queue_t *q) { void queue_terminate(queue_t *q)
if(q == NULL) {
return; if(q == NULL) return;
pthread_mutex_destroy(&q->mutex); pthread_mutex_destroy(&q->mutex);
pthread_cond_destroy(&q->not_empty); pthread_cond_destroy(&q->not_empty);
} }
bool queue_pend(queue_t *q, uint32_t *msg, bool blocking) { bool queue_pend(queue_t *q, uint32_t *msg, bool blocking)
if((q == NULL) || (msg == NULL)) {
return false; if((q == NULL) || (msg == NULL)) return false;
pthread_mutex_lock(&q->mutex); pthread_mutex_lock(&q->mutex);
// The queue is empty // The queue is empty
if(q->msg_num == 0) if(q->msg_num == 0)
{ {
if(blocking) if(blocking)
{ {
while(q->msg_num == 0) while(q->msg_num == 0)
{
pthread_cond_wait(&q->not_empty, &q->mutex); pthread_cond_wait(&q->not_empty, &q->mutex);
} }
}
else else
{ {
pthread_mutex_unlock(&q->mutex); pthread_mutex_unlock(&q->mutex);
return false; return false;
} }
} }
*msg = q->buffer[q->read_pos]; *msg = q->buffer[q->read_pos];
// Wrap around pointer to make a circular buffer // Wrap around pointer to make a circular buffer
q->read_pos = (q->read_pos + 1) % MSG_QTY; q->read_pos = (q->read_pos + 1) % MSG_QTY;
q->msg_num -= 1; q->msg_num -= 1;
pthread_mutex_unlock(&q->mutex); pthread_mutex_unlock(&q->mutex);
return true; return true;
} }
bool queue_post(queue_t *q, uint32_t msg) { bool queue_post(queue_t *q, uint32_t msg)
if(q == NULL) {
return false; if(q == NULL) return false;
pthread_mutex_lock(&q->mutex); pthread_mutex_lock(&q->mutex);
if(q->msg_num < MSG_QTY) if(q->msg_num < MSG_QTY)
{ {
q->buffer[q->write_pos] = msg; q->buffer[q->write_pos] = msg;
// Wrap around pointer to make a circular buffer // Wrap around pointer to make a circular buffer
q->write_pos = (q->write_pos + 1) % MSG_QTY; q->write_pos = (q->write_pos + 1) % MSG_QTY;
// Signal that the queue is not empty // Signal that the queue is not empty
if(q->msg_num == 0) if(q->msg_num == 0)
{
pthread_cond_signal(&q->not_empty); pthread_cond_signal(&q->not_empty);
}
q->msg_num += 1; q->msg_num += 1;
} }
else else
@ -63,6 +77,7 @@ bool queue_post(queue_t *q, uint32_t msg) {
pthread_mutex_unlock(&q->mutex); pthread_mutex_unlock(&q->mutex);
return false; return false;
} }
pthread_mutex_unlock(&q->mutex); pthread_mutex_unlock(&q->mutex);
return true; return true;
} }