tmsim

A fast turing machine simulator with graphviz export functionality

git clone https://git.8pit.net/tmsim.git

 1/*
 2 * Copyright © 2016-2018 Sören Tempel
 3 *
 4 * This program is free software: you can redistribute it and/or
 5 * modify it under the terms of the GNU Affero General Public
 6 * License as published by the Free Software Foundation, either
 7 * 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 of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef TMSIM_QUEUE_H
20#define TMSIM_QUEUE_H
21
22#include <pthread.h>
23#include <semaphore.h>
24
25#include <sys/types.h>
26
27#include "token.h"
28
29enum {
30	/**
31	 * Maximum amount of tokens kept in the queue.
32	 */
33	NUMTOKENS = 5,
34};
35
36/**
37 * Two-lock concurrent queue algorithm. All operations wait for the queue to
38 * become non-empty when retrieving an element, and wait for space to become
39 * available in the queue when storing an element.
40 */
41typedef struct _queue queue;
42
43struct _queue {
44	token *tokens[NUMTOKENS]; /**< Array used to store tokens. */
45
46	size_t head; /**< Index of the queue head. */
47	size_t tail; /**< Index of the queue tail. */
48
49	sem_t fullsem;  /**< Represents number of elements in the queue. */
50	sem_t emptysem; /**< Represents number of empty places in the queue. */
51
52	pthread_mutex_t hmtx; /**< Prevents concurrent access of head. */
53	pthread_mutex_t tmtx; /**< Prevents concurrent access of tail. */
54};
55
56queue *newqueue(void);
57void freequeue(queue *);
58void enqueue(queue *, token *);
59token *dequeue(queue *);
60
61#endif