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, Haldeneggsteig 4, 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/// A thread of execution
24struct thread;
25
26struct thread_mutex {
27    volatile int        locked;
28    struct thread       *queue;
29    spinlock_t          lock;
30    struct thread       *holder;
31};
32#ifndef __cplusplus
33#       define THREAD_MUTEX_INITIALIZER \
34    { .locked = 0, .queue = NULL, .lock = 0, .holder = NULL }
35#else
36#       define THREAD_MUTEX_INITIALIZER                                \
37    { 0, (struct thread *)NULL, 0, (struct thread *)NULL }
38#endif
39
40struct thread_cond {
41    struct thread       *queue;
42    spinlock_t          lock;
43};
44#ifndef __cplusplus
45#       define THREAD_COND_INITIALIZER \
46    { .queue = NULL, .lock = 0 }
47#else
48#       define THREAD_COND_INITIALIZER \
49    { (struct thread *)NULL, 0 }
50#endif
51
52struct thread_sem {
53    volatile unsigned int       value;
54    struct thread               *queue;
55    spinlock_t                  lock;
56};
57#ifndef __cplusplus
58#       define THREAD_SEM_INITIALIZER \
59    { .value = 0, .queue = NULL, .lock = 0 }
60#else
61#       define THREAD_SEM_INITIALIZER \
62    { 0, (struct thread *)NULL, 0 }
63#endif
64
65typedef int thread_once_t;
66#define THREAD_ONCE_INIT INT_MAX
67
68struct thread_barrier {
69	uint64_t count;
70	uint64_t max_count;
71	struct thread_sem mutex;
72	struct thread_sem barrier;
73	struct thread_sem reset;
74};
75#ifndef __cplusplus
76#		define THREAD_BARRIER_INITIALIZER \
77	{ .count = 0, .max_count 0, .mutex = NULL, \
78	  .barrier = NULL, .reset = NULL }
79#else
80#		define THREAD_BARRIER_INITIALIZER \
81	{ 0, 0, (struct thread_sem *) NULL, \
82	  (struct thread_sem *) NULL, (struct thread_sem *) NULL }
83#endif
84
85#endif
86