1/**
2 * \file
3 * \brief Thread synchronization definitions.
4 */
5
6/*
7 * Copyright (c) 2011, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef LIBBARRELFISH_THREAD_SYNC_H
16#define LIBBARRELFISH_THREAD_SYNC_H
17
18#include <stdint.h>
19#include <limits.h> // for INT_MAX
20
21#include <barrelfish_kpi/spinlocks_arch.h>
22
23#include <sys/cdefs.h>
24
25__BEGIN_DECLS
26
27/// A thread of execution
28struct thread;
29
30struct thread_mutex {
31    volatile int        locked;
32    struct thread       *queue;
33    spinlock_t          lock;
34    struct thread       *holder;
35};
36#ifndef __cplusplus
37#       define THREAD_MUTEX_INITIALIZER \
38    { .locked = 0, .queue = NULL, .lock = 0, .holder = NULL }
39#else
40#       define THREAD_MUTEX_INITIALIZER                                \
41    { 0, (struct thread *)NULL, 0, (struct thread *)NULL }
42#endif
43
44struct thread_cond {
45    struct thread       *queue;
46    spinlock_t          lock;
47};
48#ifndef __cplusplus
49#       define THREAD_COND_INITIALIZER \
50    { .queue = NULL, .lock = 0 }
51#else
52#       define THREAD_COND_INITIALIZER \
53    { (struct thread *)NULL, 0 }
54#endif
55
56struct thread_sem {
57    volatile unsigned int       value;
58    struct thread               *queue;
59    spinlock_t                  lock;
60};
61#ifndef __cplusplus
62#       define THREAD_SEM_INITIALIZER \
63    { .value = 0, .queue = NULL, .lock = 0 }
64#else
65#       define THREAD_SEM_INITIALIZER \
66    { 0, (struct thread *)NULL, 0 }
67#endif
68
69typedef int thread_once_t;
70#define THREAD_ONCE_INIT INT_MAX
71
72struct thread_barrier {
73	uint64_t count;
74	uint64_t max_count;
75	struct thread_sem mutex;
76	struct thread_sem barrier;
77	struct thread_sem reset;
78};
79#ifndef __cplusplus
80#		define THREAD_BARRIER_INITIALIZER \
81	{ .count = 0, .max_count 0, .mutex = NULL, \
82	  .barrier = NULL, .reset = NULL }
83#else
84#		define THREAD_BARRIER_INITIALIZER \
85	{ 0, 0, (struct thread_sem *) NULL, \
86	  (struct thread_sem *) NULL, (struct thread_sem *) NULL }
87#endif
88
89__END_DECLS
90
91#endif
92