aboutsummaryrefslogtreecommitdiff
path: root/aufgabe4/queue.c
blob: 373bcd3d5a55d98b6465ad26e57d3393f886e7f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * vim:ts=4:sw=4:expandtab
 * 
 * © 2010 Michael Stapelberg
 *
 */
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>

#include "queue.h"

static const int queue_size = sizeof(struct queueheader) + (sizeof(struct msg) * 255);

static uint8_t *shm;
static struct queueheader *shmheader;
static struct msg *shmdata;

/*
 * Sperrt den Semaphor: Nun kann im Shared Memory-Bereich geschrieben werden.
 *
 */
static void lock() {
    while (1) {
        if (sem_wait(&(shmheader->sem)) == -1) {
            if (errno == EINTR)
                continue;
            perror("sem_wait");
            exit(EXIT_FAILURE);
        }
        break;
    }
}

/*
 * Entsperrt den Semaphor.
 *
 */
static void unlock() {
    sem_post(&(shmheader->sem));
}

/*
 * Initialisiert das Shared Memory-Segment, bildet es in den Speicher ab und
 * initialisiert den Semaphor.
 *
 */
void queue_init() {
    int c;
    int fd;
    int flags = O_RDWR | O_CREAT | O_TRUNC;
    if ((fd = shm_open("/bts-sem", flags, S_IREAD | S_IWRITE)) == -1) {
        perror("shm_open");
        exit(EXIT_FAILURE);
    }

    ftruncate(fd, queue_size);

    if ((shm = mmap(0, queue_size, (PROT_READ | PROT_WRITE),
                    MAP_SHARED, fd, 0)) == MAP_FAILED) {
        perror("mmap");
        exit(EXIT_FAILURE);
    }

    close(fd);

    shmheader = (struct queueheader*)shm;
    shmdata = (struct msg*)(shm + sizeof(struct queueheader));

    /* Speicher mit 0 initialisieren */
    memset(shmheader, 0, sizeof(struct queueheader));
    struct msg *curmsg = shmdata;
    for (c = 0; c < 255; c++) {
        curmsg->dir = D_INVALID;
        curmsg++;
    }

    /* Semaphor initialisieren */
    if (sem_init(&(shmheader->sem), 1, 1) != 0) {
        perror("sem_init");
        exit(EXIT_FAILURE);
    }
}

/*
 * Hängt den übergebenen Wert in die Queue.
 *
 */
struct msg *queue_write(uint8_t dir, uint8_t data) {
    struct msg msg;
    msg.dir = dir;
    msg.data = data;

    uint8_t next;
    if (shmheader->cur == 255)
        next = 0;
    else next = shmheader->cur + 1;

    lock();
    struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg));
    struct msg *nextmsg = shmdata + (next * sizeof(struct msg));
    memcpy(nextmsg, &msg, sizeof(struct msg));
    if (curmsg->dir == D_INVALID)
        shmheader->cur = next;
    unlock();

    return nextmsg;
}

/*
 * Gibt die Quelle der derzeit zu verarbeitenden Nachricht zurück.
 *
 */
uint8_t queue_get_dir() {
    struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg));
    return curmsg->dir;
}

/*
 * Gibt den Wert der derzeit zu verarbeitenden Nachricht zurück und invalidiert
 * diese.
 *
 */
uint8_t queue_get_data() {
    uint8_t data;

    struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg));
    data = curmsg->data;

    lock();
    curmsg->dir = D_INVALID;
    if (shmheader->cur == 255)
        shmheader->cur = 0;
    else shmheader->cur++;
    unlock();

    return data;
}