[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 | * |
---|
[e8ece0b] | 16 | * $Id$ |
---|
[df49c60] | 17 | */ |
---|
| 18 | |
---|
[e8ece0b] | 19 | /* |
---|
| 20 | * This file is only used if using gcc |
---|
| 21 | */ |
---|
| 22 | #if defined(__GNUC__) |
---|
| 23 | |
---|
[9c49db4] | 24 | #if HAVE_CONFIG_H |
---|
| 25 | #include "config.h" |
---|
| 26 | #endif |
---|
| 27 | |
---|
[df49c60] | 28 | /* We might not need, defined just in case */ |
---|
| 29 | #define __RTEMS_INSIDE__ 1 |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <stdio.h> |
---|
| 34 | |
---|
| 35 | #include <rtems.h> |
---|
| 36 | #include <rtems/system.h> |
---|
[c4477581] | 37 | #include <rtems/error.h> /* rtems_panic */ |
---|
[df49c60] | 38 | #include <rtems/rtems/tasks.h> |
---|
| 39 | |
---|
[e8ece0b] | 40 | /* |
---|
[df49c60] | 41 | * These typedefs should match with the ones defined in the file |
---|
| 42 | * gcc/gthr-rtems.h in the gcc distribution. |
---|
| 43 | */ |
---|
| 44 | typedef void *__gthread_key_t; |
---|
| 45 | typedef int __gthread_once_t; |
---|
| 46 | typedef void *__gthread_mutex_t; |
---|
[e8ece0b] | 47 | typedef void *__gthread_recursive_mutex_t; |
---|
[df49c60] | 48 | |
---|
| 49 | /* uncomment this if you need to debug this interface */ |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | #define DEBUG_GXX_WRAPPERS 1 |
---|
| 53 | */ |
---|
| 54 | |
---|
| 55 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 56 | /* local function to return the ID of the calling thread */ |
---|
| 57 | static rtems_id get_tid( void ) |
---|
| 58 | { |
---|
[e8ece0b] | 59 | rtems_id id = 0; |
---|
[df49c60] | 60 | rtems_task_ident( RTEMS_SELF, 0, &id ); |
---|
| 61 | return id; |
---|
| 62 | } |
---|
| 63 | #endif |
---|
| 64 | |
---|
| 65 | |
---|
[616b9f5] | 66 | int rtems_gxx_once(__gthread_once_t *once, void (*func) (void)) |
---|
[df49c60] | 67 | { |
---|
| 68 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 69 | printk( "gxx_wrappers: once=%x, func=%x\n", *once, func ); |
---|
| 70 | #endif |
---|
[49155d9] | 71 | if( *(volatile __gthread_once_t *)once == 0 ) |
---|
[df49c60] | 72 | { |
---|
[49155d9] | 73 | rtems_mode saveMode; |
---|
| 74 | rtems_task_mode(RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &saveMode); |
---|
| 75 | if( *(volatile __gthread_once_t *)once == 0 ) |
---|
| 76 | { |
---|
| 77 | *(volatile __gthread_once_t *)once = 1; |
---|
| 78 | (*func)(); |
---|
| 79 | } |
---|
| 80 | rtems_task_mode(saveMode, RTEMS_PREEMPT_MASK, &saveMode); |
---|
[df49c60] | 81 | } |
---|
| 82 | return 0; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | int 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. |
---|
[e8ece0b] | 90 | * We have to to this, because the others functions on this interface |
---|
[df49c60] | 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 | |
---|
| 107 | int 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 | |
---|
| 116 | int 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 | |
---|
| 131 | void *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 | { |
---|
[6d135639] | 148 | rtems_panic ("rtems_gxx_getspecific"); |
---|
[df49c60] | 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 | |
---|
| 160 | int 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 | */ |
---|
| 179 | void 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 | { |
---|
[6d135639] | 191 | rtems_panic ("rtems_gxx_mutex_init"); |
---|
[df49c60] | 192 | } |
---|
| 193 | #ifdef DEBUG_GXX_WRAPPERS |
---|
| 194 | printk( "gxx_wrappers: mutex init complete =%X\n", *mutex ); |
---|
| 195 | #endif |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | int 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 |
---|
[e8ece0b] | 203 | return ( rtems_semaphore_obtain( (rtems_id)*mutex, |
---|
[df49c60] | 204 | RTEMS_WAIT, RTEMS_NO_TIMEOUT ) == RTEMS_SUCCESSFUL) ? 0 : -1; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | int 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 |
---|
[e8ece0b] | 212 | return (rtems_semaphore_obtain ((rtems_id)*mutex, |
---|
[df49c60] | 213 | RTEMS_NO_WAIT, 0) == RTEMS_SUCCESSFUL) ? 0 : -1; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | int 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 | |
---|
[e8ece0b] | 224 | void rtems_gxx_recursive_mutex_init_function(__gthread_recursive_mutex_t *mutex) |
---|
| 225 | { |
---|
| 226 | rtems_gxx_mutex_init(mutex); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | int rtems_gxx_recursive_mutex_lock(__gthread_recursive_mutex_t *mutex) |
---|
| 230 | { |
---|
| 231 | return rtems_gxx_mutex_lock(mutex); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | int rtems_gxx_recursive_mutex_trylock(__gthread_recursive_mutex_t *mutex) |
---|
| 235 | { |
---|
| 236 | return rtems_gxx_mutex_trylock(mutex); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | int rtems_gxx_recursive_mutex_unlock(__gthread_recursive_mutex_t *mutex) |
---|
| 240 | { |
---|
| 241 | return rtems_gxx_mutex_unlock(mutex); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | #endif /* __GNUC__ */ |
---|