0
|
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
|
|
2 Contributed by Zack Weinberg <zack@codesourcery.com>
|
|
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 These are out-of-line routines called from gthr-vxworks.h. */
|
|
27
|
|
28 #include "tconfig.h"
|
|
29 #include "tsystem.h"
|
|
30 #include "gthr.h"
|
|
31
|
|
32 #if defined(__GTHREADS)
|
|
33 #include <vxWorks.h>
|
|
34 #ifndef __RTP__
|
|
35 #include <vxLib.h>
|
|
36 #endif
|
|
37 #include <taskLib.h>
|
|
38 #ifndef __RTP__
|
|
39 #include <taskHookLib.h>
|
|
40 #else
|
|
41 # include <errno.h>
|
|
42 #endif
|
|
43
|
|
44 /* Init-once operation.
|
|
45
|
|
46 This would be a clone of the implementation from gthr-solaris.h,
|
|
47 except that we have a bootstrap problem - the whole point of this
|
|
48 exercise is to prevent double initialization, but if two threads
|
|
49 are racing with each other, once->mutex is liable to be initialized
|
|
50 by both. Then each thread will lock its own mutex, and proceed to
|
|
51 call the initialization routine.
|
|
52
|
|
53 So instead we use a bare atomic primitive (vxTas()) to handle
|
|
54 mutual exclusion. Threads losing the race then busy-wait, calling
|
|
55 taskDelay() to yield the processor, until the initialization is
|
|
56 completed. Inefficient, but reliable. */
|
|
57
|
|
58 int
|
|
59 __gthread_once (__gthread_once_t *guard, void (*func)(void))
|
|
60 {
|
|
61 if (guard->done)
|
|
62 return 0;
|
|
63
|
|
64 #ifdef __RTP__
|
|
65 __gthread_lock_library ();
|
|
66 #else
|
|
67 while (!vxTas ((void *)&guard->busy))
|
|
68 taskDelay (1);
|
|
69 #endif
|
|
70
|
|
71 /* Only one thread at a time gets here. Check ->done again, then
|
|
72 go ahead and call func() if no one has done it yet. */
|
|
73 if (!guard->done)
|
|
74 {
|
|
75 func ();
|
|
76 guard->done = 1;
|
|
77 }
|
|
78
|
|
79 #ifdef __RTP__
|
|
80 __gthread_unlock_library ();
|
|
81 #else
|
|
82 guard->busy = 0;
|
|
83 #endif
|
|
84 return 0;
|
|
85 }
|
|
86
|
|
87 #endif /* __GTHREADS */
|