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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* vim:ts=4:sw=4:expandtab
*
* © 2010 Michael Stapelberg
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.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);
}
}
#ifdef DEBUG
static void vlog(char *fmt, va_list args) {
char timebuf[64];
pid_t p = getpid();
/* Get current time */
time_t t = time(NULL);
/* Convert time to local time (determined by the locale) */
struct tm *tmp = localtime(&t);
/* Generate time prefix */
strftime(timebuf, sizeof(timebuf), "%X - ", tmp);
printf("[%d] %s", p, timebuf);
vprintf(fmt, args);
fflush(stdout);
}
static void log(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vlog(fmt, args);
va_end(args);
}
#endif
/*
* 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;
lock();
uint8_t next = shmheader->cur;
struct msg *nextmsg = shmdata + (shmheader->cur * sizeof(struct msg));
while (nextmsg->dir != D_INVALID) {
if (next == 255)
next = 0;
else next++;
nextmsg = shmdata + (next * sizeof(struct msg));
}
struct msg *curmsg = shmdata + (shmheader->cur * sizeof(struct msg));
#ifdef DEBUG
log("queue_write(), next = %d, nextmsg = %p, cur = %d, cur->dir = %d\n",
next, nextmsg, shmheader->cur, curmsg->dir);
#endif
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));
#ifdef DEBUG
log("queue_get_dir(), cur = %d, cur->dir = %d\n",
shmheader->cur, curmsg->dir);
#endif
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));
#ifdef DEBUG
log("queue_get_data(), cur = %d, cur->dir = %d\n",
shmheader->cur, curmsg->dir);
#endif
data = curmsg->data;
lock();
curmsg->dir = D_INVALID;
if (shmheader->cur == 255)
shmheader->cur = 0;
else shmheader->cur++;
unlock();
return data;
}
|