codegen: a small improvement in do_bswap and do_brev
[ajla.git] / th_none.c
blob64908fb5aabb2a8a89f9d93d834befc75a4d680c
1 /*
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
9 * version.
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/>.
19 #include "ajla.h"
21 #include "obj_reg.h"
23 #include "thread.h"
25 #ifdef THREAD_NONE
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) \
33 do { \
34 (m)->state = MUTEX_UNLOCKED; \
35 } while (0)
37 #define do_mutex_done(m) \
38 do { \
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; \
42 } while (0)
44 #define do_mutex_lock(m) \
45 do { \
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; \
49 } while (0)
51 #define do_mutex_trylock(m) \
52 do { \
53 if (likely((m)->state == MUTEX_UNLOCKED)) { \
54 (m)->state = MUTEX_LOCKED; \
55 return true; \
56 } \
57 if (unlikely((m)->state != MUTEX_LOCKED)) \
58 internal(position_string(position_arg), "mutex_trylock: invalid mutex state %d", (m)->state);\
59 return false; \
60 } while (0)
62 #define do_mutex_unlock(m) \
63 do { \
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; \
67 } while (0)
70 #define do_rwmutex_init(m) \
71 do { \
72 (m)->state = MUTEX_UNLOCKED; \
73 } while (0)
75 #define do_rwmutex_done(m) \
76 do { \
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; \
80 } while (0)
82 #define do_rwmutex_lock_read(m) \
83 do { \
84 if (unlikely((m)->state < MUTEX_UNLOCKED)) \
85 internal(position_string(position_arg), "rwmutex_lock_read: invalid mutex state %d", (m)->state);\
86 (m)->state++; \
87 } while (0)
89 #define do_rwmutex_unlock_read(m) \
90 do { \
91 if (unlikely((m)->state < MUTEX_LOCKED)) \
92 internal(position_string(position_arg), "rwmutex_unlock_read: invalid mutex state %d", (m)->state);\
93 (m)->state--; \
94 } while (0)
96 #define do_rwmutex_lock_write(m) \
97 do { \
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; \
101 } while (0)
103 #define do_rwmutex_unlock_write(m) \
104 do { \
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; \
108 } while (0)
111 #define do_cond_init(c) \
112 do { \
113 mutex_init_position(&(c)->mutex pass_position); \
114 } while (0)
116 #define do_cond_done(c) \
117 do { \
118 mutex_done_position(&(c)->mutex pass_position); \
119 } while (0)
121 #define do_cond_lock(c) \
122 do { \
123 mutex_lock_position(&(c)->mutex pass_position); \
124 } while (0)
126 #define do_cond_unlock(c) \
127 do { \
128 mutex_unlock_position(&(c)->mutex pass_position); \
129 } while (0)
131 #define do_cond_unlock_signal(c) \
132 do { \
133 mutex_unlock_position(&(c)->mutex pass_position); \
134 } while (0)
136 #define do_cond_unlock_broadcast(c) \
137 do { \
138 mutex_unlock_position(&(c)->mutex pass_position); \
139 } while (0)
141 #define do_cond_wait(c) \
142 do { \
143 if (unlikely((c)->mutex.state != MUTEX_LOCKED)) \
144 internal(position_string(position_arg), "cond_wait: invalid mutex state %d", (c)->mutex.state);\
145 } while (0)
147 #define do_cond_wait_us(c, us) \
148 do { \
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 */ \
152 return false; \
153 } while (0)
156 #include "th_com.inc"
158 void thread_init(void)
162 void thread_done(void)
166 #endif