File: message_buffer.c

package info (click to toggle)
dssi 0.9.1-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,852 kB
  • ctags: 499
  • sloc: sh: 8,531; ansic: 3,241; cpp: 1,042; makefile: 211; perl: 191
file content (56 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download | duplicates (6)
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
/* -*- c-basic-offset: 4 -*-  vi:set ts=8 sts=4 sw=4: */

/* message_buffer.c

   This file is in the public domain.
*/

#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>

#define BUFFERS      16		/* must be 2^n */
#define BUFFER_SIZE 256

static char buffer[BUFFERS][BUFFER_SIZE];
static const char *mb_prefix;
static unsigned int initialised = 0;
static unsigned int in_buffer = 0;
static unsigned int out_buffer = 0;
static pthread_t writer_thread;

void *mb_thread_func(void *arg);

void add_message(const char *msg)
{
    strncpy(buffer[in_buffer], msg, BUFFER_SIZE - 1);
    in_buffer = (in_buffer + 1) & (BUFFERS - 1);
}

void mb_init(const char *prefix)
{
    if (initialised) {
	return;
    }
    mb_prefix = prefix;

    pthread_create(&writer_thread, NULL, &mb_thread_func, NULL);

    initialised = 1;
}

void *mb_thread_func(void *arg)
{
    while (1) {
	while (out_buffer != in_buffer) {
	    printf("%s%s", mb_prefix, buffer[out_buffer]);
	    out_buffer = (out_buffer + 1) & (BUFFERS - 1);
	}
	usleep(1000);
    }

    return NULL;
}

/* vi:set ts=8 sts=4 sw=4: */