File: thread.c

package info (click to toggle)
xwax 1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 576 kB
  • sloc: ansic: 6,687; sh: 170; makefile: 142
file content (84 lines) | stat: -rw-r--r-- 1,818 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2014 Mark Hills <mark@xwax.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 */

#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "thread.h"

static pthread_key_t key;

/*
 * Put in place checks for realtime and non-realtime threads
 *
 * Return: 0 on success, otherwise -1
 */

int thread_global_init(void)
{
    int r;

    r = pthread_key_create(&key, NULL);
    if (r != 0) {
        errno = r;
        perror("pthread_key_create");
        return -1;
    }

    if (pthread_setspecific(key, (void*)false) != 0)
        abort();

    return 0;
}

void thread_global_clear(void)
{
    if (pthread_key_delete(key) != 0)
        abort();
}

/*
 * Inform that this thread is a realtime thread, for assertions later
 */

void thread_to_realtime(void)
{
    if (pthread_setspecific(key, (void*)true) != 0)
        abort();
}

/*
 * Check for programmer error
 *
 * Pre: the current thread is non realtime
 */

void rt_not_allowed()
{
    bool rt;

    rt = (bool)pthread_getspecific(key);
    if (rt) {
        fprintf(stderr, "Realtime thread called a blocking function\n");
        abort();
    }
}