| 1 |
/* Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Zack Weinberg <[email protected]> |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
/* Threads compatibility routines for libgcc2 for VxWorks. |
| 26 |
|
| 27 |
This file implements the init-once service exposed by gthr-vxworks.h. */ |
| 28 |
|
| 29 |
#include "tconfig.h" |
| 30 |
#include "tsystem.h" |
| 31 |
#include "gthr.h" |
| 32 |
|
| 33 |
#if defined(__GTHREADS) |
| 34 |
|
| 35 |
#include <vxWorks.h> |
| 36 |
#include <taskLib.h> |
| 37 |
|
| 38 |
#ifndef __RTP__ |
| 39 |
# include <vxLib.h> |
| 40 |
# include <taskHookLib.h> |
| 41 |
#else /* __RTP__ */ |
| 42 |
# include <errno.h> |
| 43 |
#endif /* __RTP__ */ |
| 44 |
|
| 45 |
/* ----------------------------- Init-once ------------------------------- */ |
| 46 |
|
| 47 |
static void |
| 48 |
__release (__gthread_once_t ** __guard) |
| 49 |
{ |
| 50 |
(*__guard)->busy = 0; |
| 51 |
} |
| 52 |
|
| 53 |
int |
| 54 |
__gthread_once (__gthread_once_t * __guard, void (*__func) (void)) |
| 55 |
{ |
| 56 |
if (__guard->done) |
| 57 |
return 0; |
| 58 |
|
| 59 |
/* Busy-wait until we have exclusive access to the state. Check if |
| 60 |
another thread managed to perform the init call in the interim. */ |
| 61 |
|
| 62 |
while (!__TAS(&__guard->busy)) |
| 63 |
{ |
| 64 |
if (__guard->done) |
| 65 |
return 0; |
| 66 |
taskDelay (1); |
| 67 |
} |
| 68 |
|
| 69 |
if (!__guard->done) |
| 70 |
{ |
| 71 |
#ifndef __USING_SJLJ_EXCEPTIONS__ |
| 72 |
/* Setup a cleanup to release the guard when __func() throws an |
| 73 |
exception. We cannot use this with SJLJ exceptions as |
| 74 |
Unwind_Register calls __gthread_once, leading to an infinite |
| 75 |
recursion. */ |
| 76 |
__attribute__ ((cleanup (__release))) |
| 77 |
__gthread_once_t *__temp = __guard; |
| 78 |
#endif |
| 79 |
|
| 80 |
__func (); |
| 81 |
__guard->done = 1; |
| 82 |
} |
| 83 |
|
| 84 |
__release(&__guard); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
#endif /* __GTHREADS */ |