source: rtems/c/src/lib/libc/gxx_wrappers.c @ 98f3cfa

4.104.114.84.95
Last change on this file since 98f3cfa was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

  • configure.in: Add libc/config.h
  • libc/Makefile.am: Add INCLUDES += -I. to pickup config.h
  • libc/.cvsignore: Add config.h and stamp-h
  • libc/*.c: Add config.h support.
  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * RTEMS threads compatibily routines for libgcc2.
3 *
4 * by: Rosimildo da Silva ( rdasilva@connecttel.com
5 *
6 * Used ideas from:
7 *    W. Eric Norum
8 *    Canadian Light Source
9 *    University of Saskatchewan
10 *    Saskatoon, Saskatchewan, CANADA
11 *    eric@cls.usask.ca
12 *
13 * Eric sent some e-mail in the rtems-list as a start point for this
14 * module implementation.
15 *
16 *
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23/* We might not need, defined just in case */
24#define  __RTEMS_INSIDE__  1
25
26
27#include <stdlib.h>
28#include <stdio.h>
29
30#include <rtems.h>
31#include <rtems/system.h>
32#include <rtems/rtems/tasks.h>
33
34/*
35 * These typedefs should match with the ones defined in the file
36 * gcc/gthr-rtems.h in the gcc distribution.
37 */
38typedef void *__gthread_key_t;
39typedef int   __gthread_once_t;
40typedef void *__gthread_mutex_t;
41
42
43/* uncomment this if you need to debug this interface */
44
45/*
46#define DEBUG_GXX_WRAPPERS 1
47*/
48
49
50/* prototype for the terminate() */
51extern void __terminate( void );
52
53
54#ifdef DEBUG_GXX_WRAPPERS
55/* local function to return the ID of the calling thread */
56static rtems_id get_tid( void )
57{
58   rtems_id id = 0;
59   rtems_task_ident( RTEMS_SELF, 0, &id );
60   return id;
61}
62#endif
63
64
65int rtems_gxx_once(__gthread_once_t *once, void (*func) ())
66{
67#ifdef DEBUG_GXX_WRAPPERS
68   printk( "gxx_wrappers: once=%x, func=%x\n", *once, func );
69#endif
70   if( *once == 0 )
71   {
72      /*
73       * NOTE: could not use the call to disable "preemption", it causes
74       * one exception. Somebody might want to investiage it further
75       * sometime later.
76       */
77      _Thread_Disable_dispatch();
78      *once = 1;
79      (*func)();
80      _Thread_Enable_dispatch();
81   }
82   return 0;
83}
84
85
86int rtems_gxx_key_create (__gthread_key_t *key, void (*dtor) (void *))
87{
88  /* Ok, this can be a bit tricky. We are going to return a "key" as a
89   * pointer to the buffer that will hold the value of the key itself.
90   * We have to to this, becuase the others functions on this interface
91   * deal with the value of the key, as used with the POSIX API.
92   */
93   /* Do not pull your hair, trust me this works. :-) */
94  __gthread_key_t *new_key = ( __gthread_key_t * )malloc( sizeof( __gthread_key_t ) );
95  *key = ( __gthread_key_t )new_key;
96  *new_key = NULL;
97
98#ifdef DEBUG_GXX_WRAPPERS
99  printk( "gxx_wrappers: create key=%x, dtor=%x, new_key=%x\n", key, dtor, new_key );
100#endif
101  /* register with RTEMS the buffer that will hold the key values */
102  if( rtems_task_variable_add( RTEMS_SELF, (void **)new_key, NULL ) == RTEMS_SUCCESSFUL )
103       return 0;
104  return -1;
105}
106
107int rtems_gxx_key_dtor (__gthread_key_t key, void *ptr)
108{
109#ifdef DEBUG_GXX_WRAPPERS
110  printk( "gxx_wrappers: dtor key=%x, ptr=%x\n", key, ptr );
111#endif
112   *(void **)key = 0;
113   return 0;
114}
115
116int rtems_gxx_key_delete (__gthread_key_t key)
117{
118#ifdef DEBUG_GXX_WRAPPERS
119  printk( "gxx_wrappers: delete key=%x\n", key );
120#endif
121  /* register with RTEMS the buffer that will hold the key values */
122  if( rtems_task_variable_delete( RTEMS_SELF, (void **)key ) == RTEMS_SUCCESSFUL )
123  {
124     if( key ) free( (void *)key );
125     return 0;
126  }
127  return 0;
128}
129
130
131void *rtems_gxx_getspecific(__gthread_key_t key)
132{
133  void *p= 0;
134
135  /* register with RTEMS the buffer that will hold the key values */
136  if( rtems_task_variable_get( RTEMS_SELF, (void **)key, &p ) == RTEMS_SUCCESSFUL )
137  {
138    /* We do not have to do this, but what the heck ! */
139     p= *( void **)key;
140  }
141  else
142  {
143    /* fisrt time, always set to zero, it is unknown the value that the others
144     * threads are using at the moment of this call
145     */
146    if( rtems_task_variable_add( RTEMS_SELF, (void **)key, NULL ) != RTEMS_SUCCESSFUL )
147    {
148       __terminate ();
149    }
150    *( void ** )key = (void *)0;
151  }
152
153#ifdef DEBUG_GXX_WRAPPERS
154   printk( "gxx_wrappers: getspecific key=%x, ptr=%x, id=%x\n", key, p, get_tid() );
155#endif
156   return p;
157}
158
159
160int rtems_gxx_setspecific(__gthread_key_t key, const void *ptr)
161{
162#ifdef DEBUG_GXX_WRAPPERS
163  printk( "gxx_wrappers: setspecific key=%x, ptr=%x, id=%x\n", key, ptr, get_tid() );
164#endif
165  /* register with RTEMS the buffer that will hold the key values */
166  if( rtems_task_variable_add( RTEMS_SELF, (void **)key, NULL ) == RTEMS_SUCCESSFUL )
167  {
168    /* now let's set the proper value */
169    *( void ** )key = (void *)ptr;
170     return 0;
171  }
172  return -1;
173}
174
175
176/*
177 * MUTEX support
178 */
179void rtems_gxx_mutex_init (__gthread_mutex_t *mutex)
180{
181#ifdef DEBUG_GXX_WRAPPERS
182  printk( "gxx_wrappers: mutex init =%X\n", *mutex );
183#endif
184  if( rtems_semaphore_create( rtems_build_name ('G', 'C', 'C', '2'),
185                              1,
186                             RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE
187                             |RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL,
188                             0,
189                             (rtems_id *)mutex ) != RTEMS_SUCCESSFUL )
190  {
191      __terminate ();
192  }
193#ifdef DEBUG_GXX_WRAPPERS
194  printk( "gxx_wrappers: mutex init complete =%X\n", *mutex );
195#endif
196}
197
198int rtems_gxx_mutex_lock (__gthread_mutex_t *mutex)
199{
200#ifdef DEBUG_GXX_WRAPPERS
201  printk( "gxx_wrappers: lock mutex=%X\n", *mutex );
202#endif
203  return ( rtems_semaphore_obtain( (rtems_id)*mutex,
204            RTEMS_WAIT, RTEMS_NO_TIMEOUT ) ==  RTEMS_SUCCESSFUL) ? 0 : -1;
205}
206
207int rtems_gxx_mutex_trylock (__gthread_mutex_t *mutex)
208{
209#ifdef DEBUG_GXX_WRAPPERS
210  printk( "gxx_wrappers: trylock mutex=%X\n", *mutex );
211#endif
212  return (rtems_semaphore_obtain ((rtems_id)*mutex,
213               RTEMS_NO_WAIT, 0) == RTEMS_SUCCESSFUL) ? 0 : -1;
214}
215
216int rtems_gxx_mutex_unlock (__gthread_mutex_t *mutex)
217{
218#ifdef DEBUG_GXX_WRAPPERS
219   printk( "gxx_wrappers: unlock mutex=%X\n", *mutex );
220#endif
221  return (rtems_semaphore_release( (rtems_id)*mutex ) ==  RTEMS_SUCCESSFUL) ? 0 :-1;
222}
223
Note: See TracBrowser for help on using the repository browser.