1 /* Threads compatibility routines for libgcc2 and libobjc for VxWorks.  */
2 /* Compile this one with gcc.  */
3 /* Copyright (C) 1997-2019 Free Software Foundation, Inc.
4    Contributed by Mike Stump <mrs@wrs.com>.
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12 
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21 
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 <http://www.gnu.org/licenses/>.  */
26 
27 #ifndef GCC_GTHR_VXWORKS_H
28 #define GCC_GTHR_VXWORKS_H
29 
30 #ifdef _LIBOBJC
31 
32 /* libobjc requires the optional pthreads component.  */
33 #include "gthr-posix.h"
34 
35 #else
36 #ifdef __cplusplus
37 #define UNUSED(x)
38 #else
39 #define UNUSED(x) x __attribute__((__unused__))
40 #endif
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #define __GTHREADS 1
47 #define __gthread_active_p() 1
48 
49 /* Mutexes are easy, except that they need to be initialized at runtime.  */
50 
51 #include <semLib.h>
52 
53 typedef SEM_ID __gthread_mutex_t;
54 /* All VxWorks mutexes are recursive.  */
55 typedef SEM_ID __gthread_recursive_mutex_t;
56 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
58 
59 static inline void
__gthread_mutex_init_function(__gthread_mutex_t * mutex)60 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
61 {
62   *mutex = semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
63 }
64 
65 static inline int
__gthread_mutex_destroy(__gthread_mutex_t * mutex)66 __gthread_mutex_destroy (__gthread_mutex_t *mutex)
67 {
68   semDelete(*mutex);
69   return 0;
70 }
71 
72 static inline int
__gthread_mutex_lock(__gthread_mutex_t * mutex)73 __gthread_mutex_lock (__gthread_mutex_t *mutex)
74 {
75   return semTake (*mutex, WAIT_FOREVER);
76 }
77 
78 static inline int
__gthread_mutex_trylock(__gthread_mutex_t * mutex)79 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
80 {
81   return semTake (*mutex, NO_WAIT);
82 }
83 
84 static inline int
__gthread_mutex_unlock(__gthread_mutex_t * mutex)85 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
86 {
87   return semGive (*mutex);
88 }
89 
90 static inline void
__gthread_recursive_mutex_init_function(__gthread_recursive_mutex_t * mutex)91 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
92 {
93   __gthread_mutex_init_function (mutex);
94 }
95 
96 static inline int
__gthread_recursive_mutex_lock(__gthread_recursive_mutex_t * mutex)97 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
98 {
99   return __gthread_mutex_lock (mutex);
100 }
101 
102 static inline int
__gthread_recursive_mutex_trylock(__gthread_recursive_mutex_t * mutex)103 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
104 {
105   return __gthread_mutex_trylock (mutex);
106 }
107 
108 static inline int
__gthread_recursive_mutex_unlock(__gthread_recursive_mutex_t * mutex)109 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
110 {
111   return __gthread_mutex_unlock (mutex);
112 }
113 
114 static inline int
__gthread_recursive_mutex_destroy(__gthread_recursive_mutex_t * __mutex)115 __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
116 {
117   return __gthread_mutex_destroy (__mutex);
118 }
119 
120 /* pthread_once is complicated enough that it's implemented
121    out-of-line.  See config/vxlib.c.  */
122 
123 typedef struct
124 {
125 #if !defined(__RTP__)
126 #if defined(__PPC__)
127   __attribute ((aligned (__alignof (unsigned))))
128 #endif
129   volatile unsigned char busy;
130 #endif
131   volatile unsigned char done;
132 #if !defined(__RTP__) && defined(__PPC__)
133   /* PPC's test-and-set implementation requires a 4 byte aligned
134      object, of which it only sets the first byte.  We use padding
135      here, in order to maintain some amount of backwards
136      compatibility.  Without this padding, gthread_once objects worked
137      by accident because they happen to be static objects and the ppc
138      port automatically increased their alignment to 4 bytes.  */
139   unsigned char pad1;
140   unsigned char pad2;
141 #endif
142 }
143 __gthread_once_t;
144 
145 #if defined (__RTP__)
146 # define __GTHREAD_ONCE_INIT { 0 }
147 #elif defined (__PPC__)
148 # define __GTHREAD_ONCE_INIT { 0, 0, 0, 0 }
149 #else
150 # define __GTHREAD_ONCE_INIT { 0, 0 }
151 #endif
152 
153 extern int __gthread_once (__gthread_once_t *__once, void (*__func)(void));
154 
155 /* Thread-specific data requires a great deal of effort, since VxWorks
156    is not really set up for it.  See config/vxlib.c for the gory
157    details.  All the TSD routines are sufficiently complex that they
158    need to be implemented out of line.  */
159 
160 typedef unsigned int __gthread_key_t;
161 
162 extern int __gthread_key_create (__gthread_key_t *__keyp, void (*__dtor)(void *));
163 extern int __gthread_key_delete (__gthread_key_t __key);
164 
165 extern void *__gthread_getspecific (__gthread_key_t __key);
166 extern int __gthread_setspecific (__gthread_key_t __key, void *__ptr);
167 
168 #undef UNUSED
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif /* not _LIBOBJC */
175 
176 #endif /* gthr-vxworks.h */
177