OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [tools/] [riscv-gnu-toolchain-master/] [newlib/] [libgloss/] [riscv/] [machine/] [bthread.h] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
#ifndef GCC_GTHR_BTHREAD_H
2
#define GCC_GTHR_BTHREAD_H
3
 
4
#define __GTHREADS 1
5
 
6
#include <errno.h>
7
 
8
#define __BTHREAD_MUTEX_INIT { 0 }
9
#define __BTHREAD_ONCE_INIT  { __BTHREAD_MUTEX_INIT }
10
#define __BTHREAD_KEYS_MAX 128
11
#define __BTHREAD_THREADS_MAX 128
12
 
13
#ifdef __cplusplus
14
extern "C" {
15
#endif
16
 
17
typedef struct
18
{
19
  unsigned int lock;
20
} __bthread_mutex_t;
21
 
22
typedef struct
23
{
24
  void (*dtor)(void*);
25
  __bthread_mutex_t busy;
26
} __bthread_key_data_t;
27
 
28
extern __bthread_key_data_t __bthread_keys[__BTHREAD_KEYS_MAX];
29
// should use TLS for this!
30
extern void* __bthread_key_data[__BTHREAD_THREADS_MAX][__BTHREAD_KEYS_MAX];
31
 
32
typedef unsigned int __bthread_t;
33
 
34
typedef struct
35
{
36
  unsigned int key;
37
} __bthread_key_t;
38
 
39
typedef struct {
40
  __bthread_mutex_t once;
41
} __bthread_once_t;
42
 
43
 
44
static inline __bthread_t __bthread_self(void)
45
{
46
  // in the future, we should use TLS for this.
47
  register __bthread_t __id asm("tp");
48
  return __id;
49
}
50
 
51
// returns true if there is more than 1 core in the system
52
static inline int __bthread_threading(void)
53
{
54
  return 1;
55
}
56
 
57
static inline int __bthread_mutex_init(__bthread_mutex_t* lock)
58
{
59
  lock->lock = 0;
60
  return 0;
61
}
62
 
63
static inline int __bthread_mutex_trylock(__bthread_mutex_t* lock)
64
{
65
  return __sync_lock_test_and_set(&lock->lock, 1);
66
}
67
 
68
static inline int __bthread_mutex_locked(__bthread_mutex_t* lock)
69
{
70
  return lock->lock;
71
}
72
 
73
static inline int __bthread_mutex_lock(__bthread_mutex_t* lock)
74
{
75
  do
76
  {
77
    while(__bthread_mutex_locked(lock));
78
  }
79
  while(__bthread_mutex_trylock(lock));
80
 
81
  return 0;
82
}
83
 
84
static inline int __bthread_mutex_unlock(__bthread_mutex_t* lock)
85
{
86
  lock->lock = 0;
87
  return 0;
88
}
89
 
90
static inline int
91
__bthread_once (__bthread_once_t *__once, void (*__func) (void))
92
{
93
  if(!__once || !__func)
94
    return EINVAL;
95
 
96
  if(__bthread_mutex_locked(&__once->once))
97
    return 0;
98
 
99
  if(__bthread_mutex_trylock(&__once->once))
100
    return 0;
101
 
102
  (*__func)();
103
 
104
  return 0;
105
}
106
 
107
static inline int
108
__bthread_key_create( __bthread_key_t* __key, void (*__dtor) (void*) )
109
{
110
  int i;
111
  for(i = 0; i < __BTHREAD_KEYS_MAX; i++)
112
  {
113
    if(!__bthread_mutex_locked(&__bthread_keys[i].busy))
114
      if(!__bthread_mutex_trylock(&__bthread_keys[i].busy))
115
        break;
116
  }
117
 
118
  if(i == __BTHREAD_KEYS_MAX)
119
    return ENOMEM;
120
 
121
  __bthread_keys[i].dtor = __dtor;
122
  __key->key = i;
123
 
124
  return 0;
125
}
126
 
127
static inline int
128
__bthread_key_valid( __bthread_key_t __key )
129
{
130
  if(__bthread_self() >= __BTHREAD_THREADS_MAX)
131
    return 0;
132
 
133
  if(__key.key >= __BTHREAD_KEYS_MAX)
134
    return 0;
135
 
136
  if(!__bthread_mutex_locked(&__bthread_keys[__key.key].busy))
137
    return 0;
138
 
139
  return 1;
140
}
141
 
142
static inline int
143
__bthread_key_delete( __bthread_key_t __key )
144
{
145
  if(!__bthread_key_valid(__key))
146
    return EINVAL;
147
 
148
  __bthread_mutex_unlock(&__bthread_keys[__key.key].busy);
149
 
150
  return 0;
151
}
152
 
153
static inline int
154
__bthread_setspecific( __bthread_key_t __key, void* __ptr )
155
{
156
  if(!__bthread_key_valid(__key))
157
    return EINVAL;
158
 
159
  __bthread_key_data[__bthread_self()][__key.key] = __ptr;
160
 
161
  return 0;
162
}
163
 
164
static inline void*
165
__bthread_getspecific( __bthread_key_t __key )
166
{
167
  if(!__bthread_key_valid(__key))
168
    return 0;
169
 
170
  return (void*)__bthread_key_data[__bthread_self()][__key.key];
171
}
172
 
173
#ifdef __cplusplus
174
}
175
#endif
176
 
177
// bthread.h
178
 
179
#include <unistd.h>
180
 
181
#ifdef __cplusplus
182
extern "C" {
183
#endif
184
 
185
typedef __bthread_key_t __gthread_key_t;
186
typedef __bthread_once_t __gthread_once_t;
187
typedef __bthread_mutex_t __gthread_mutex_t;
188
 
189
typedef struct {
190
  long depth;
191
  __bthread_t owner;
192
  __bthread_mutex_t actual;
193
} __gthread_recursive_mutex_t;
194
 
195
#define __GTHREAD_MUTEX_INIT __BTHREAD_MUTEX_INIT
196
#define __GTHREAD_ONCE_INIT __BTHREAD_ONCE_INIT
197
 
198
static inline int
199
__gthread_recursive_mutex_init_function(__gthread_recursive_mutex_t *__mutex);
200
#define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
201
 
202
#define __gthrw(name)
203
#define __gthrw_(name) name
204
 
205
__gthrw(__bthread_once)
206
__gthrw(__bthread_key_create)
207
__gthrw(__bthread_key_delete)
208
__gthrw(__bthread_getspecific)
209
__gthrw(__bthread_setspecific)
210
 
211
__gthrw(__bthread_self)
212
 
213
__gthrw(__bthread_mutex_lock)
214
__gthrw(__bthread_mutex_trylock)
215
__gthrw(__bthread_mutex_unlock)
216
 
217
__gthrw(__bthread_threading)
218
 
219
static inline int
220
__gthread_active_p (void)
221
{
222
  return (__bthread_threading)();
223
}
224
 
225
static inline int
226
__gthread_once (__gthread_once_t *__once, void (*__func) (void))
227
{
228
  if (__gthread_active_p ())
229
    return __gthrw_(__bthread_once) (__once, __func);
230
  else
231
    return -1;
232
}
233
 
234
static inline int
235
__gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
236
{
237
  return __gthrw_(__bthread_key_create) (__key, __dtor);
238
}
239
 
240
static inline int
241
__gthread_key_delete (__gthread_key_t __key)
242
{
243
  return __gthrw_(__bthread_key_delete) (__key);
244
}
245
 
246
static inline void *
247
__gthread_getspecific (__gthread_key_t __key)
248
{
249
  return __gthrw_(__bthread_getspecific) (__key);
250
}
251
 
252
static inline int
253
__gthread_setspecific (__gthread_key_t __key, void *__ptr)
254
{
255
  return __gthrw_(__bthread_setspecific) (__key, __ptr);
256
}
257
 
258
static inline int
259
__gthread_mutex_destroy (__gthread_mutex_t *__mutex)
260
{
261
  return __mutex == 0 ? EINVAL : 0;
262
}
263
 
264
static inline int
265
__gthread_mutex_lock (__gthread_mutex_t *__mutex)
266
{
267
  if (__gthread_active_p ())
268
    return __gthrw_(__bthread_mutex_lock) (__mutex);
269
  else
270
    return 0;
271
}
272
 
273
static inline int
274
__gthread_mutex_trylock (__gthread_mutex_t *__mutex)
275
{
276
  if (__gthread_active_p ())
277
    return __gthrw_(__bthread_mutex_trylock) (__mutex);
278
  else
279
    return 0;
280
}
281
 
282
static inline int
283
__gthread_mutex_unlock (__gthread_mutex_t *__mutex)
284
{
285
  if (__gthread_active_p ())
286
    return __gthrw_(__bthread_mutex_unlock) (__mutex);
287
  else
288
    return 0;
289
}
290
 
291
static inline int
292
__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
293
{
294
  __mutex->depth = 0;
295
  __mutex->owner = __gthrw_(__bthread_self) ();
296
  __bthread_mutex_init(&__mutex->actual);
297
  return 0;
298
}
299
 
300
static inline int
301
__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
302
{
303
  if (__gthread_active_p ())
304
    {
305
      __bthread_t __me = __gthrw_(__bthread_self) ();
306
 
307
      if (__mutex->owner != __me)
308
        {
309
          __gthrw_(__bthread_mutex_lock) (&__mutex->actual);
310
          __mutex->owner = __me;
311
        }
312
 
313
      __mutex->depth++;
314
    }
315
  return 0;
316
}
317
 
318
static inline int
319
__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
320
{
321
  if (__gthread_active_p ())
322
    {
323
      __bthread_t __me = __gthrw_(__bthread_self) ();
324
 
325
      if (__mutex->owner != __me)
326
        {
327
          if (__gthrw_(__bthread_mutex_trylock) (&__mutex->actual))
328
            return 1;
329
          __mutex->owner = __me;
330
        }
331
 
332
      __mutex->depth++;
333
    }
334
  return 0;
335
}
336
 
337
// todo: add error checking - what to do if this is called when
338
// we aren't the owner of the lock?
339
static inline int
340
__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
341
{
342
  if (__gthread_active_p ())
343
    {
344
      if (--__mutex->depth == 0)
345
          {
346
           __mutex->owner = (__bthread_t) 0;
347
           __gthrw_(__bthread_mutex_unlock) (&__mutex->actual);
348
          }
349
    }
350
  return 0;
351
}
352
 
353
#ifdef __cplusplus
354
}
355
#endif
356
 
357
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.