2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
27 #define MUTEX_INVALID 0
28 #define MUTEX_UNLOCKED 1
29 #define MUTEX_LOCKED 2
30 #define MUTEX_LOCKED_WRITE -1
32 #define do_mutex_init(m) \
34 (m)->state = MUTEX_UNLOCKED; \
37 #define do_mutex_done(m) \
39 if (unlikely((m)->state != MUTEX_UNLOCKED)) \
40 internal(position_string(position_arg), "mutex_done: invalid mutex state %d", (m)->state);\
41 (m)->state = MUTEX_INVALID; \
44 #define do_mutex_lock(m) \
46 if (unlikely((m)->state != MUTEX_UNLOCKED)) \
47 internal(position_string(position_arg), "mutex_lock: invalid mutex state %d", (m)->state);\
48 (m)->state = MUTEX_LOCKED; \
51 #define do_mutex_trylock(m) \
53 if (likely((m)->state == MUTEX_UNLOCKED)) { \
54 (m)->state = MUTEX_LOCKED; \
57 if (unlikely((m)->state != MUTEX_LOCKED)) \
58 internal(position_string(position_arg), "mutex_trylock: invalid mutex state %d", (m)->state);\
62 #define do_mutex_unlock(m) \
64 if (unlikely((m)->state != MUTEX_LOCKED)) \
65 internal(position_string(position_arg), "mutex_unlock: invalid mutex state %d", (m)->state);\
66 (m)->state = MUTEX_UNLOCKED; \
70 #define do_rwmutex_init(m) \
72 (m)->state = MUTEX_UNLOCKED; \
75 #define do_rwmutex_done(m) \
77 if (unlikely((m)->state != MUTEX_UNLOCKED)) \
78 internal(position_string(position_arg), "rwmutex_done: invalid mutex state %d", (m)->state);\
79 (m)->state = MUTEX_INVALID; \
82 #define do_rwmutex_lock_read(m) \
84 if (unlikely((m)->state < MUTEX_UNLOCKED)) \
85 internal(position_string(position_arg), "rwmutex_lock_read: invalid mutex state %d", (m)->state);\
89 #define do_rwmutex_unlock_read(m) \
91 if (unlikely((m)->state < MUTEX_LOCKED)) \
92 internal(position_string(position_arg), "rwmutex_unlock_read: invalid mutex state %d", (m)->state);\
96 #define do_rwmutex_lock_write(m) \
98 if (unlikely((m)->state != MUTEX_UNLOCKED)) \
99 internal(position_string(position_arg), "rwmutex_lock_write: invalid mutex state %d", (m)->state);\
100 (m)->state = MUTEX_LOCKED_WRITE; \
103 #define do_rwmutex_unlock_write(m) \
105 if (unlikely((m)->state != MUTEX_LOCKED_WRITE)) \
106 internal(position_string(position_arg), "rwmutex_unlock_write: invalid mutex state %d", (m)->state);\
107 (m)->state = MUTEX_UNLOCKED; \
111 #define do_cond_init(c) \
113 mutex_init_position(&(c)->mutex pass_position); \
116 #define do_cond_done(c) \
118 mutex_done_position(&(c)->mutex pass_position); \
121 #define do_cond_lock(c) \
123 mutex_lock_position(&(c)->mutex pass_position); \
126 #define do_cond_unlock(c) \
128 mutex_unlock_position(&(c)->mutex pass_position); \
131 #define do_cond_unlock_signal(c) \
133 mutex_unlock_position(&(c)->mutex pass_position); \
136 #define do_cond_unlock_broadcast(c) \
138 mutex_unlock_position(&(c)->mutex pass_position); \
141 #define do_cond_wait(c) \
143 if (unlikely((c)->mutex.state != MUTEX_LOCKED)) \
144 internal(position_string(position_arg), "cond_wait: invalid mutex state %d", (c)->mutex.state);\
147 #define do_cond_wait_us(c, us) \
149 if (unlikely((c)->mutex.state != MUTEX_LOCKED)) \
150 internal(position_string(position_arg), "do_cond_wait_us: invalid mutex state %d", (c)->mutex.state);\
151 us = us + 1; /* avoid warning */ \
156 #include "th_com.inc"
158 void thread_init(void)
162 void thread_done(void)