2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / config / vxlib.c
blob2c6a5c086728900d68b04838d95e935ca69b18ef
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Zack Weinberg <zack@codesourcery.com>
4 This file is part of GCC.
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 2, or (at your option) any later
9 version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
28 /* Threads compatibility routines for libgcc2 for VxWorks.
29 These are out-of-line routines called from gthr-vxworks.h. */
31 #include "tconfig.h"
32 #include "tsystem.h"
33 #include "gthr.h"
35 #if defined(__GTHREADS)
36 #include <vxWorks.h>
37 #ifndef __RTP__
38 #include <vxLib.h>
39 #endif
40 #include <taskLib.h>
41 #ifndef __RTP__
42 #include <taskHookLib.h>
43 #else
44 # include <errno.h>
45 #endif
47 /* Init-once operation.
49 This would be a clone of the implementation from gthr-solaris.h,
50 except that we have a bootstrap problem - the whole point of this
51 exercise is to prevent double initialization, but if two threads
52 are racing with each other, once->mutex is liable to be initialized
53 by both. Then each thread will lock its own mutex, and proceed to
54 call the initialization routine.
56 So instead we use a bare atomic primitive (vxTas()) to handle
57 mutual exclusion. Threads losing the race then busy-wait, calling
58 taskDelay() to yield the processor, until the initialization is
59 completed. Inefficient, but reliable. */
61 int
62 __gthread_once (__gthread_once_t *guard, void (*func)(void))
64 if (guard->done)
65 return 0;
67 #ifdef __RTP__
68 __gthread_lock_library ();
69 #else
70 while (!vxTas ((void *)&guard->busy))
71 taskDelay (1);
72 #endif
74 /* Only one thread at a time gets here. Check ->done again, then
75 go ahead and call func() if no one has done it yet. */
76 if (!guard->done)
78 func ();
79 guard->done = 1;
82 #ifdef __RTP__
83 __gthread_unlock_library ();
84 #else
85 guard->busy = 0;
86 #endif
87 return 0;
90 #endif /* __GTHREADS */