[df49c60] | 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 | |
---|
[9c49db4] | 19 | #if HAVE_CONFIG_H |
---|
| 20 | #include "config.h" |
---|
| 21 | #endif |
---|
| 22 | |
---|
[df49c60] | 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> |
---|
[c4477581] | 32 | #include <rtems/error.h> /* rtems_panic */ |
---|
[df49c60] | 33 | #include <rtems/rtems/tasks.h> |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | * These typedefs should match with the ones defined in the file |
---|
| 37 | * gcc/gthr-rtems.h in the gcc distribution. |
---|
| 38 | */ |
---|
| 39 | typedef void *__gthread_key_t; |
---|
| 40 | typedef int __gthread_once_t; |
---|
| 41 | typedef void *__gthread_mutex_t; |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | /* uncomment this if you need to debug this interface */ |
---|
| 45 | |
---|
| 46 | /* |
---|
| 47 | #define DEBUG_GXX_WRAPPERS 1 |
---|
| 48 | */ |
---|
| 49 | |
---|
| 50 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 51 | /* local function to return the ID of the calling thread */ |
---|
| 52 | static rtems_id get_tid( void ) |
---|
| 53 | { |
---|
| 54 | rtems_id id = 0; |
---|
| 55 | rtems_task_ident( RTEMS_SELF, 0, &id ); |
---|
| 56 | return id; |
---|
| 57 | } |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | |
---|
[616b9f5] | 61 | int rtems_gxx_once(__gthread_once_t *once, void (*func) (void)) |
---|
[df49c60] | 62 | { |
---|
| 63 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 64 | printk( "gxx_wrappers: once=%x, func=%x\n", *once, func ); |
---|
| 65 | #endif |
---|
[49155d9] | 66 | if( *(volatile __gthread_once_t *)once == 0 ) |
---|
[df49c60] | 67 | { |
---|
[49155d9] | 68 | rtems_mode saveMode; |
---|
| 69 | rtems_task_mode(RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &saveMode); |
---|
| 70 | if( *(volatile __gthread_once_t *)once == 0 ) |
---|
| 71 | { |
---|
| 72 | *(volatile __gthread_once_t *)once = 1; |
---|
| 73 | (*func)(); |
---|
| 74 | } |
---|
| 75 | rtems_task_mode(saveMode, RTEMS_PREEMPT_MASK, &saveMode); |
---|
[df49c60] | 76 | } |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | int rtems_gxx_key_create (__gthread_key_t *key, void (*dtor) (void *)) |
---|
| 82 | { |
---|
| 83 | /* Ok, this can be a bit tricky. We are going to return a "key" as a |
---|
| 84 | * pointer to the buffer that will hold the value of the key itself. |
---|
| 85 | * We have to to this, becuase the others functions on this interface |
---|
| 86 | * deal with the value of the key, as used with the POSIX API. |
---|
| 87 | */ |
---|
| 88 | /* Do not pull your hair, trust me this works. :-) */ |
---|
| 89 | __gthread_key_t *new_key = ( __gthread_key_t * )malloc( sizeof( __gthread_key_t ) ); |
---|
| 90 | *key = ( __gthread_key_t )new_key; |
---|
| 91 | *new_key = NULL; |
---|
| 92 | |
---|
| 93 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 94 | printk( "gxx_wrappers: create key=%x, dtor=%x, new_key=%x\n", key, dtor, new_key ); |
---|
| 95 | #endif |
---|
| 96 | /* register with RTEMS the buffer that will hold the key values */ |
---|
| 97 | if( rtems_task_variable_add( RTEMS_SELF, (void **)new_key, NULL ) == RTEMS_SUCCESSFUL ) |
---|
| 98 | return 0; |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | int rtems_gxx_key_dtor (__gthread_key_t key, void *ptr) |
---|
| 103 | { |
---|
| 104 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 105 | printk( "gxx_wrappers: dtor key=%x, ptr=%x\n", key, ptr ); |
---|
| 106 | #endif |
---|
| 107 | *(void **)key = 0; |
---|
| 108 | return 0; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | int rtems_gxx_key_delete (__gthread_key_t key) |
---|
| 112 | { |
---|
| 113 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 114 | printk( "gxx_wrappers: delete key=%x\n", key ); |
---|
| 115 | #endif |
---|
| 116 | /* register with RTEMS the buffer that will hold the key values */ |
---|
| 117 | if( rtems_task_variable_delete( RTEMS_SELF, (void **)key ) == RTEMS_SUCCESSFUL ) |
---|
| 118 | { |
---|
| 119 | if( key ) free( (void *)key ); |
---|
| 120 | return 0; |
---|
| 121 | } |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | void *rtems_gxx_getspecific(__gthread_key_t key) |
---|
| 127 | { |
---|
| 128 | void *p= 0; |
---|
| 129 | |
---|
| 130 | /* register with RTEMS the buffer that will hold the key values */ |
---|
| 131 | if( rtems_task_variable_get( RTEMS_SELF, (void **)key, &p ) == RTEMS_SUCCESSFUL ) |
---|
| 132 | { |
---|
| 133 | /* We do not have to do this, but what the heck ! */ |
---|
| 134 | p= *( void **)key; |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | /* fisrt time, always set to zero, it is unknown the value that the others |
---|
| 139 | * threads are using at the moment of this call |
---|
| 140 | */ |
---|
| 141 | if( rtems_task_variable_add( RTEMS_SELF, (void **)key, NULL ) != RTEMS_SUCCESSFUL ) |
---|
| 142 | { |
---|
[6d135639] | 143 | rtems_panic ("rtems_gxx_getspecific"); |
---|
[df49c60] | 144 | } |
---|
| 145 | *( void ** )key = (void *)0; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 149 | printk( "gxx_wrappers: getspecific key=%x, ptr=%x, id=%x\n", key, p, get_tid() ); |
---|
| 150 | #endif |
---|
| 151 | return p; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | int rtems_gxx_setspecific(__gthread_key_t key, const void *ptr) |
---|
| 156 | { |
---|
| 157 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 158 | printk( "gxx_wrappers: setspecific key=%x, ptr=%x, id=%x\n", key, ptr, get_tid() ); |
---|
| 159 | #endif |
---|
| 160 | /* register with RTEMS the buffer that will hold the key values */ |
---|
| 161 | if( rtems_task_variable_add( RTEMS_SELF, (void **)key, NULL ) == RTEMS_SUCCESSFUL ) |
---|
| 162 | { |
---|
| 163 | /* now let's set the proper value */ |
---|
| 164 | *( void ** )key = (void *)ptr; |
---|
| 165 | return 0; |
---|
| 166 | } |
---|
| 167 | return -1; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | /* |
---|
| 172 | * MUTEX support |
---|
| 173 | */ |
---|
| 174 | void rtems_gxx_mutex_init (__gthread_mutex_t *mutex) |
---|
| 175 | { |
---|
| 176 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 177 | printk( "gxx_wrappers: mutex init =%X\n", *mutex ); |
---|
| 178 | #endif |
---|
| 179 | if( rtems_semaphore_create( rtems_build_name ('G', 'C', 'C', '2'), |
---|
| 180 | 1, |
---|
| 181 | RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE |
---|
| 182 | |RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL, |
---|
| 183 | 0, |
---|
| 184 | (rtems_id *)mutex ) != RTEMS_SUCCESSFUL ) |
---|
| 185 | { |
---|
[6d135639] | 186 | rtems_panic ("rtems_gxx_mutex_init"); |
---|
[df49c60] | 187 | } |
---|
| 188 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 189 | printk( "gxx_wrappers: mutex init complete =%X\n", *mutex ); |
---|
| 190 | #endif |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | int rtems_gxx_mutex_lock (__gthread_mutex_t *mutex) |
---|
| 194 | { |
---|
| 195 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 196 | printk( "gxx_wrappers: lock mutex=%X\n", *mutex ); |
---|
| 197 | #endif |
---|
| 198 | return ( rtems_semaphore_obtain( (rtems_id)*mutex, |
---|
| 199 | RTEMS_WAIT, RTEMS_NO_TIMEOUT ) == RTEMS_SUCCESSFUL) ? 0 : -1; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | int rtems_gxx_mutex_trylock (__gthread_mutex_t *mutex) |
---|
| 203 | { |
---|
| 204 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 205 | printk( "gxx_wrappers: trylock mutex=%X\n", *mutex ); |
---|
| 206 | #endif |
---|
| 207 | return (rtems_semaphore_obtain ((rtems_id)*mutex, |
---|
| 208 | RTEMS_NO_WAIT, 0) == RTEMS_SUCCESSFUL) ? 0 : -1; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | int rtems_gxx_mutex_unlock (__gthread_mutex_t *mutex) |
---|
| 212 | { |
---|
| 213 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 214 | printk( "gxx_wrappers: unlock mutex=%X\n", *mutex ); |
---|
| 215 | #endif |
---|
| 216 | return (rtems_semaphore_release( (rtems_id)*mutex ) == RTEMS_SUCCESSFUL) ? 0 :-1; |
---|
| 217 | } |
---|
| 218 | |
---|