1/*2 * Copyright © 2016-2018 Sören Tempel3 *4 * This program is free software: you can redistribute it and/or5 * modify it under the terms of the GNU Affero General Public6 * License as published by the Free Software Foundation, either7 * version 3 of the License, or (at your option) any later version.8 *9 * This program is distributed in the hope that it will be useful,10 * but WITHOUT ANY WARRANTY; without even the implied warranty of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12 * Affero General Public License for more details.13 *14 * You should have received a copy of the GNU Affero General Public15 * License along with this program. If not, see16 * <http://www.gnu.org/licenses/>.17 */1819#ifndef TMSIM_QUEUE_H20#define TMSIM_QUEUE_H2122#include <pthread.h>23#include <semaphore.h>2425#include <sys/types.h>2627#include "token.h"2829enum {30 /**31 * Maximum amount of tokens kept in the queue.32 */33 NUMTOKENS = 5,34};3536/**37 * Two-lock concurrent queue algorithm. All operations wait for the queue to38 * become non-empty when retrieving an element, and wait for space to become39 * available in the queue when storing an element.40 */41typedef struct _queue queue;4243struct _queue {44 token *tokens[NUMTOKENS]; /**< Array used to store tokens. */4546 size_t head; /**< Index of the queue head. */47 size_t tail; /**< Index of the queue tail. */4849 sem_t fullsem; /**< Represents number of elements in the queue. */50 sem_t emptysem; /**< Represents number of empty places in the queue. */5152 pthread_mutex_t hmtx; /**< Prevents concurrent access of head. */53 pthread_mutex_t tmtx; /**< Prevents concurrent access of tail. */54};5556queue *newqueue(void);57void freequeue(queue *);58void enqueue(queue *, token *);59token *dequeue(queue *);6061#endif