1
1
#pragma once
2
- #ifdef ARDUINO_ARCH_AVR
2
+ // 此文件为架构特定,由埃博拉酱手动实现
3
3
/* Threads compatibility routines for libgcc2 and libobjc. */
4
4
/* Compile this one with gcc. */
5
5
/* Copyright (C) 1997-2024 Free Software Foundation, Inc.
@@ -28,6 +28,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
28
28
#ifndef GCC_GTHR_SINGLE_H
29
29
#define GCC_GTHR_SINGLE_H
30
30
31
+ #include < unordered_set>
32
+ #include < cerrno>
33
+ #include < Arduino.h>
34
+
31
35
/* Just provide compatibility for mutex handling. */
32
36
33
37
typedef int __gthread_key_t ;
@@ -37,7 +41,10 @@ typedef int __gthread_recursive_mutex_t;
37
41
38
42
#define __GTHREAD_ONCE_INIT 0
39
43
#define __GTHREAD_MUTEX_INIT 0
40
- #define __GTHREAD_MUTEX_INIT_FUNCTION (mx ) do {} while (0)
44
+ #define __GTHREAD_MUTEX_INIT_FUNCTION (mx ) \
45
+ do \
46
+ { \
47
+ } while (0 )
41
48
#define __GTHREAD_RECURSIVE_MUTEX_INIT 0
42
49
43
50
#define UNUSED __attribute__ ((__unused__))
@@ -51,15 +58,15 @@ static void *thread_local_storage = NULL;
51
58
52
59
/* Initialize the threads subsystem. */
53
60
static inline int
54
- __gthread_objc_init_thread_system (void )
61
+ __gthread_objc_init_thread_system (void )
55
62
{
56
63
/* No thread support available */
57
64
return -1 ;
58
65
}
59
66
60
67
/* Close the threads subsystem. */
61
68
static inline int
62
- __gthread_objc_close_thread_system (void )
69
+ __gthread_objc_close_thread_system (void )
63
70
{
64
71
/* No thread support available */
65
72
return -1 ;
@@ -69,37 +76,37 @@ __gthread_objc_close_thread_system (void)
69
76
70
77
/* Create a new thread of execution. */
71
78
static inline objc_thread_t
72
- __gthread_objc_thread_detach (void (* func )(void * ), void * arg UNUSED )
79
+ __gthread_objc_thread_detach (void (*func)(void *), void *arg UNUSED)
73
80
{
74
81
/* No thread support available */
75
82
return NULL ;
76
83
}
77
84
78
85
/* Set the current thread's priority. */
79
86
static inline int
80
- __gthread_objc_thread_set_priority (int priority UNUSED )
87
+ __gthread_objc_thread_set_priority (int priority UNUSED)
81
88
{
82
89
/* No thread support available */
83
90
return -1 ;
84
91
}
85
92
86
93
/* Return the current thread's priority. */
87
94
static inline int
88
- __gthread_objc_thread_get_priority (void )
95
+ __gthread_objc_thread_get_priority (void )
89
96
{
90
97
return OBJC_THREAD_INTERACTIVE_PRIORITY;
91
98
}
92
99
93
100
/* Yield our process time to another thread. */
94
101
static inline void
95
- __gthread_objc_thread_yield (void )
102
+ __gthread_objc_thread_yield (void )
96
103
{
97
104
return ;
98
105
}
99
106
100
107
/* Terminate the current thread. */
101
108
static inline int
102
- __gthread_objc_thread_exit (void )
109
+ __gthread_objc_thread_exit (void )
103
110
{
104
111
/* No thread support available */
105
112
/* Should we really exit the program */
@@ -109,23 +116,23 @@ __gthread_objc_thread_exit (void)
109
116
110
117
/* Returns an integer value which uniquely describes a thread. */
111
118
static inline objc_thread_t
112
- __gthread_objc_thread_id (void )
119
+ __gthread_objc_thread_id (void )
113
120
{
114
121
/* No thread support, use 1. */
115
- return (objc_thread_t ) 1 ;
122
+ return (objc_thread_t )1 ;
116
123
}
117
124
118
125
/* Sets the thread's local storage pointer. */
119
126
static inline int
120
- __gthread_objc_thread_set_data (void * value )
127
+ __gthread_objc_thread_set_data (void *value)
121
128
{
122
129
thread_local_storage = value;
123
130
return 0 ;
124
131
}
125
132
126
133
/* Returns the thread's local storage pointer. */
127
134
static inline void *
128
- __gthread_objc_thread_get_data (void )
135
+ __gthread_objc_thread_get_data (void )
129
136
{
130
137
return thread_local_storage;
131
138
}
@@ -134,37 +141,37 @@ __gthread_objc_thread_get_data (void)
134
141
135
142
/* Allocate a mutex. */
136
143
static inline int
137
- __gthread_objc_mutex_allocate (objc_mutex_t mutex UNUSED )
144
+ __gthread_objc_mutex_allocate (objc_mutex_t mutex UNUSED)
138
145
{
139
146
return 0 ;
140
147
}
141
148
142
149
/* Deallocate a mutex. */
143
150
static inline int
144
- __gthread_objc_mutex_deallocate (objc_mutex_t mutex UNUSED )
151
+ __gthread_objc_mutex_deallocate (objc_mutex_t mutex UNUSED)
145
152
{
146
153
return 0 ;
147
154
}
148
155
149
156
/* Grab a lock on a mutex. */
150
157
static inline int
151
- __gthread_objc_mutex_lock (objc_mutex_t mutex UNUSED )
158
+ __gthread_objc_mutex_lock (objc_mutex_t mutex UNUSED)
152
159
{
153
160
/* There can only be one thread, so we always get the lock */
154
161
return 0 ;
155
162
}
156
163
157
164
/* Try to grab a lock on a mutex. */
158
165
static inline int
159
- __gthread_objc_mutex_trylock (objc_mutex_t mutex UNUSED )
166
+ __gthread_objc_mutex_trylock (objc_mutex_t mutex UNUSED)
160
167
{
161
168
/* There can only be one thread, so we always get the lock */
162
169
return 0 ;
163
170
}
164
171
165
172
/* Unlock the mutex */
166
173
static inline int
167
- __gthread_objc_mutex_unlock (objc_mutex_t mutex UNUSED )
174
+ __gthread_objc_mutex_unlock (objc_mutex_t mutex UNUSED)
168
175
{
169
176
return 0 ;
170
177
}
@@ -173,131 +180,136 @@ __gthread_objc_mutex_unlock (objc_mutex_t mutex UNUSED)
173
180
174
181
/* Allocate a condition. */
175
182
static inline int
176
- __gthread_objc_condition_allocate (objc_condition_t condition UNUSED )
183
+ __gthread_objc_condition_allocate (objc_condition_t condition UNUSED)
177
184
{
178
185
return 0 ;
179
186
}
180
187
181
188
/* Deallocate a condition. */
182
189
static inline int
183
- __gthread_objc_condition_deallocate (objc_condition_t condition UNUSED )
190
+ __gthread_objc_condition_deallocate (objc_condition_t condition UNUSED)
184
191
{
185
192
return 0 ;
186
193
}
187
194
188
195
/* Wait on the condition */
189
196
static inline int
190
- __gthread_objc_condition_wait (objc_condition_t condition UNUSED ,
191
- objc_mutex_t mutex UNUSED )
197
+ __gthread_objc_condition_wait (objc_condition_t condition UNUSED,
198
+ objc_mutex_t mutex UNUSED)
192
199
{
193
200
return 0 ;
194
201
}
195
202
196
203
/* Wake up all threads waiting on this condition. */
197
204
static inline int
198
- __gthread_objc_condition_broadcast (objc_condition_t condition UNUSED )
205
+ __gthread_objc_condition_broadcast (objc_condition_t condition UNUSED)
199
206
{
200
207
return 0 ;
201
208
}
202
209
203
210
/* Wake up one thread waiting on this condition. */
204
211
static inline int
205
- __gthread_objc_condition_signal (objc_condition_t condition UNUSED )
212
+ __gthread_objc_condition_signal (objc_condition_t condition UNUSED)
206
213
{
207
214
return 0 ;
208
215
}
209
216
210
217
#else /* _LIBOBJC */
211
218
212
219
static inline int
213
- __gthread_active_p (void )
220
+ __gthread_active_p (void )
214
221
{
215
222
return 0 ;
216
223
}
217
224
218
225
static inline int
219
- __gthread_once (__gthread_once_t * __once UNUSED , void (* __func ) (void ) UNUSED )
226
+ __gthread_once (__gthread_once_t *__once UNUSED, void (*__func)(void ) UNUSED)
220
227
{
221
228
return 0 ;
222
229
}
223
230
224
231
static inline int UNUSED
225
- __gthread_key_create (__gthread_key_t * __key UNUSED , void (* __func ) (void * ) UNUSED )
232
+ __gthread_key_create (__gthread_key_t *__key UNUSED, void (*__func)(void *) UNUSED)
226
233
{
227
234
return 0 ;
228
235
}
229
236
230
237
static int UNUSED
231
- __gthread_key_delete (__gthread_key_t __key UNUSED )
238
+ __gthread_key_delete (__gthread_key_t __key UNUSED)
232
239
{
233
240
return 0 ;
234
241
}
235
242
236
243
static inline void *
237
- __gthread_getspecific (__gthread_key_t __key UNUSED )
244
+ __gthread_getspecific (__gthread_key_t __key UNUSED)
238
245
{
239
246
return 0 ;
240
247
}
241
248
242
249
static inline int
243
- __gthread_setspecific (__gthread_key_t __key UNUSED , const void * __v UNUSED )
250
+ __gthread_setspecific (__gthread_key_t __key UNUSED, const void *__v UNUSED)
244
251
{
245
252
return 0 ;
246
253
}
247
254
248
255
static inline int
249
- __gthread_mutex_destroy (__gthread_mutex_t * __mutex UNUSED )
256
+ __gthread_mutex_destroy (__gthread_mutex_t *__mutex UNUSED)
250
257
{
251
258
return 0 ;
252
259
}
253
-
260
+ extern std::unordered_set< __gthread_mutex_t *> _CSL_MutexSet;
254
261
static inline int
255
- __gthread_mutex_lock (__gthread_mutex_t * __mutex UNUSED )
262
+ __gthread_mutex_lock (__gthread_mutex_t *__mutex)
256
263
{
257
- return 0 ;
264
+ if (_CSL_MutexSet.contains (__mutex))
265
+ return EBUSY;
266
+ else
267
+ {
268
+ if (_CSL_MutexSet.empty ())
269
+ noInterrupts ();
270
+ _CSL_MutexSet.insert (__mutex);
271
+ return 0 ;
272
+ }
258
273
}
259
274
260
275
static inline int
261
- __gthread_mutex_trylock (__gthread_mutex_t * __mutex UNUSED )
276
+ __gthread_mutex_trylock (__gthread_mutex_t *__mutex UNUSED)
262
277
{
263
278
return 0 ;
264
279
}
265
280
266
281
static inline int
267
- __gthread_mutex_unlock (__gthread_mutex_t * __mutex UNUSED )
282
+ __gthread_mutex_unlock (__gthread_mutex_t *__mutex UNUSED)
268
283
{
269
284
return 0 ;
270
285
}
271
286
272
287
static inline int
273
- __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t * __mutex )
288
+ __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
274
289
{
275
- return __gthread_mutex_lock (__mutex );
290
+ return __gthread_mutex_lock (__mutex);
276
291
}
277
292
278
293
static inline int
279
- __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t * __mutex )
294
+ __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
280
295
{
281
- return __gthread_mutex_trylock (__mutex );
296
+ return __gthread_mutex_trylock (__mutex);
282
297
}
283
298
284
299
static inline int
285
- __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t * __mutex )
300
+ __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
286
301
{
287
- return __gthread_mutex_unlock (__mutex );
302
+ return __gthread_mutex_unlock (__mutex);
288
303
}
289
304
290
305
static inline int
291
- __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t * __mutex )
306
+ __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
292
307
{
293
- return __gthread_mutex_destroy (__mutex );
308
+ return __gthread_mutex_destroy (__mutex);
294
309
}
295
310
296
311
#endif /* _LIBOBJC */
297
312
298
313
#undef UNUSED
299
314
300
- #endif /* ! GCC_GTHR_SINGLE_H */
301
- #else
302
- #include_next <bits/gthr.h>
303
- #endif
315
+ #endif /* ! GCC_GTHR_SINGLE_H */
0 commit comments