1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
24 /* Threads compatibility routines for libgcc2 for VxWorks.
26 This file implements the GTHREAD_CXX0X part of the interface
27 exposed by gthr-vxworks.h, using APIs exposed by regular (!AE/653)
33 #define __TIMESPEC_TO_NSEC(timespec) \
34 ((long long)timespec.tv_sec * 1000000000 + (long long)timespec.tv_nsec)
36 #define __TIMESPEC_TO_TICKS(timespec) \
37 ((long long)(sysClkRateGet() * __TIMESPEC_TO_NSEC(timespec) + 999999999) \
41 void tls_delete_hook ();
42 #define __CALL_DELETE_HOOK(tcb) tls_delete_hook()
44 /* In kernel mode, we need to pass the TCB to task_delete_hook. The TCB is
45 the pointer to the WIND_TCB structure and is the ID of the task. */
46 void tls_delete_hook (void *TCB
);
47 #define __CALL_DELETE_HOOK(tcb) tls_delete_hook((WIND_TCB *) ((tcb)->task_id))
50 /* -------------------- Timed Condition Variables --------------------- */
53 __gthread_cond_signal (__gthread_cond_t
*cond
)
58 return __CHECK_RESULT (semGive (*cond
));
62 __gthread_cond_timedwait (__gthread_cond_t
*cond
,
63 __gthread_mutex_t
*mutex
,
64 const __gthread_time_t
*abs_timeout
)
75 struct timespec current
;
76 if (clock_gettime (CLOCK_REALTIME
, ¤t
) == ERROR
)
77 /* CLOCK_REALTIME is not supported. */
80 const long long abs_timeout_ticks
= __TIMESPEC_TO_TICKS ((*abs_timeout
));
81 const long long current_ticks
= __TIMESPEC_TO_TICKS (current
);
83 long long waiting_ticks
;
85 if (current_ticks
< abs_timeout_ticks
)
86 waiting_ticks
= abs_timeout_ticks
- current_ticks
;
88 /* The point until we would need to wait is in the past,
89 no need to wait at all. */
92 /* We check that waiting_ticks can be safely casted as an int. */
93 if (waiting_ticks
> INT_MAX
)
94 waiting_ticks
= INT_MAX
;
96 __RETURN_ERRNO_IF_NOT_OK (semGive (*mutex
));
98 __RETURN_ERRNO_IF_NOT_OK (semTake (*cond
, waiting_ticks
));
100 __RETURN_ERRNO_IF_NOT_OK (semTake (*mutex
, WAIT_FOREVER
));
105 /* --------------------------- Timed Mutexes ------------------------------ */
108 __gthread_mutex_timedlock (__gthread_mutex_t
*m
,
109 const __gthread_time_t
*abs_time
)
117 struct timespec current
;
118 if (clock_gettime (CLOCK_REALTIME
, ¤t
) == ERROR
)
119 /* CLOCK_REALTIME is not supported. */
122 const long long abs_timeout_ticks
= __TIMESPEC_TO_TICKS ((*abs_time
));
123 const long long current_ticks
= __TIMESPEC_TO_TICKS (current
);
124 long long waiting_ticks
;
126 if (current_ticks
< abs_timeout_ticks
)
127 waiting_ticks
= abs_timeout_ticks
- current_ticks
;
129 /* The point until we would need to wait is in the past,
130 no need to wait at all. */
133 /* Make sure that waiting_ticks can be safely casted as an int. */
134 if (waiting_ticks
> INT_MAX
)
135 waiting_ticks
= INT_MAX
;
137 return __CHECK_RESULT (semTake (*m
, waiting_ticks
));
141 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t
*mutex
,
142 const __gthread_time_t
*abs_timeout
)
144 return __gthread_mutex_timedlock ((__gthread_mutex_t
*)mutex
, abs_timeout
);
147 /* ------------------------------ Threads --------------------------------- */
149 /* Task control block initialization and destruction functions. */
152 __init_gthread_tcb (__gthread_t __tcb
)
157 __gthread_mutex_init (&(__tcb
->return_value_available
));
158 if (__tcb
->return_value_available
== SEM_ID_NULL
)
161 __gthread_mutex_init (&(__tcb
->delete_ok
));
162 if (__tcb
->delete_ok
== SEM_ID_NULL
)
163 goto return_sem_delete
;
165 /* We lock the two mutexes used for signaling. */
166 if (__gthread_mutex_lock (&(__tcb
->delete_ok
)) != OK
)
167 goto delete_sem_delete
;
169 if (__gthread_mutex_lock (&(__tcb
->return_value_available
)) != OK
)
170 goto delete_sem_delete
;
172 __tcb
->task_id
= TASK_ID_NULL
;
176 semDelete (__tcb
->delete_ok
);
178 semDelete (__tcb
->return_value_available
);
182 /* Here, we pass a pointer to a tcb to allow calls from
183 cleanup attributes. */
185 __delete_gthread_tcb (__gthread_t
* __tcb
)
187 semDelete ((*__tcb
)->return_value_available
);
188 semDelete ((*__tcb
)->delete_ok
);
192 /* This __gthread_t stores the address of the TCB malloc'ed in
193 __gthread_create. It is then accessible via __gthread_self(). */
194 __thread __gthread_t __local_tcb
= NULL
;
197 __gthread_self (void)
201 /* We are in the initial thread, we need to initialize the TCB. */
202 __local_tcb
= malloc (sizeof (*__local_tcb
));
206 if (__init_gthread_tcb (__local_tcb
) != OK
)
208 __delete_gthread_tcb (&__local_tcb
);
211 /* We do not set the mutexes in the structure as a thread is not supposed
212 to join or detach himself. */
213 __local_tcb
->task_id
= taskIdSelf ();
219 __task_wrapper (__gthread_t tcb
, FUNCPTR __func
, _Vx_usr_arg_t __args
)
226 /* We use this variable to avoid memory leaks in the case where
227 the underlying function throws an exception. */
228 __attribute__ ((cleanup (__delete_gthread_tcb
))) __gthread_t __tmp
= tcb
;
230 void *return_value
= (void *) __func (__args
);
231 tcb
->return_value
= return_value
;
233 /* Call the destructors. */
234 __CALL_DELETE_HOOK (tcb
);
236 /* Future calls of join() will be able to retrieve the return value. */
237 __gthread_mutex_unlock (&tcb
->return_value_available
);
239 /* We wait for the thread to be joined or detached. */
240 __gthread_mutex_lock (&(tcb
->delete_ok
));
241 __gthread_mutex_unlock (&(tcb
->delete_ok
));
243 /* Memory deallocation is done by the cleanup attribute of the tmp variable. */
248 /* Proper gthreads API. */
251 __gthread_create (__gthread_t
* __threadid
, void *(*__func
) (void *),
258 __RETURN_ERRNO_IF_NOT_OK (taskPriorityGet (taskIdSelf (), &priority
));
261 __RETURN_ERRNO_IF_NOT_OK (taskOptionsGet (taskIdSelf (), &options
));
263 #if defined (__SPE__)
264 options
|= VX_SPE_TASK
;
266 options
|= VX_FP_TASK
;
268 options
&= VX_USR_TASK_OPTIONS
;
270 int stacksize
= 20 * 1024;
272 __gthread_t tcb
= malloc (sizeof (*tcb
));
276 if (__init_gthread_tcb (tcb
) != OK
)
282 TASK_ID task_id
= taskCreate (NULL
,
283 priority
, options
, stacksize
,
284 (FUNCPTR
) & __task_wrapper
,
286 (_Vx_usr_arg_t
) __func
,
287 (_Vx_usr_arg_t
) __args
,
288 0, 0, 0, 0, 0, 0, 0);
290 /* If taskCreate succeeds, task_id will be a valid TASK_ID and not zero. */
291 __RETURN_ERRNO_IF_NOT_OK (!task_id
);
293 tcb
->task_id
= task_id
;
296 return __CHECK_RESULT (taskActivate (task_id
));
300 __gthread_equal (__gthread_t __t1
, __gthread_t __t2
)
302 return (__t1
== __t2
) ? OK
: ERROR
;
306 __gthread_yield (void)
308 return taskDelay (0);
312 __gthread_join (__gthread_t __threadid
, void **__value_ptr
)
317 /* A thread cannot join itself. */
318 if (__threadid
->task_id
== taskIdSelf ())
321 /* Waiting for the task to set the return value. */
322 __gthread_mutex_lock (&__threadid
->return_value_available
);
323 __gthread_mutex_unlock (&__threadid
->return_value_available
);
326 *__value_ptr
= __threadid
->return_value
;
328 /* The task will be safely be deleted. */
329 __gthread_mutex_unlock (&(__threadid
->delete_ok
));
331 __RETURN_ERRNO_IF_NOT_OK (taskWait (__threadid
->task_id
, WAIT_FOREVER
));
337 __gthread_detach (__gthread_t __threadid
)
342 if (taskIdVerify (__threadid
->task_id
) != OK
)
345 /* The task will be safely be deleted. */
346 __gthread_mutex_unlock (&(__threadid
->delete_ok
));