From 5c0c0cf2a6a9e3fdbcd1ada3f79399c453b1fbd1 Mon Sep 17 00:00:00 2001 From: Christian Mauderer Date: Thu, 27 Mar 2014 14:23:21 +0100 Subject: privateenv: Use POSIX keys instead of task variables. --- cpukit/libcsupport/include/rtems/libio_.h | 5 +++++ cpukit/libcsupport/src/__usrenv.c | 2 +- cpukit/libcsupport/src/libio_init.c | 12 ++++++++++ cpukit/libcsupport/src/privateenv.c | 37 ++++++++++++++++--------------- 4 files changed, 37 insertions(+), 19 deletions(-) (limited to 'cpukit/libcsupport') diff --git a/cpukit/libcsupport/include/rtems/libio_.h b/cpukit/libcsupport/include/rtems/libio_.h index 9960288c20..d7f9034e3d 100644 --- a/cpukit/libcsupport/include/rtems/libio_.h +++ b/cpukit/libcsupport/include/rtems/libio_.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -238,6 +239,10 @@ void rtems_filesystem_location_free( rtems_filesystem_location_info_t *loc ); */ #include +void rtems_libio_free_user_env( void *env ); + +extern pthread_key_t rtems_current_user_env_key; + static inline void rtems_libio_lock( void ) { rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT ); diff --git a/cpukit/libcsupport/src/__usrenv.c b/cpukit/libcsupport/src/__usrenv.c index 3ce6a2dfea..71efda9d7f 100644 --- a/cpukit/libcsupport/src/__usrenv.c +++ b/cpukit/libcsupport/src/__usrenv.c @@ -257,4 +257,4 @@ rtems_user_env_t rtems_global_user_env = { .umask = S_IWGRP | S_IWOTH }; -rtems_user_env_t *rtems_current_user_env = &rtems_global_user_env; +pthread_key_t rtems_current_user_env_key; diff --git a/cpukit/libcsupport/src/libio_init.c b/cpukit/libcsupport/src/libio_init.c index 4d705fb8ee..e64ddd6e3a 100644 --- a/cpukit/libcsupport/src/libio_init.c +++ b/cpukit/libcsupport/src/libio_init.c @@ -46,6 +46,7 @@ void rtems_libio_init( void ) rtems_status_code rc; uint32_t i; rtems_libio_t *iop; + int eno; if (rtems_libio_number_iops > 0) { @@ -60,6 +61,17 @@ void rtems_libio_init( void ) iop->data1 = NULL; } + /* + * Create the posix key for user environment. + */ + eno = pthread_key_create( + &rtems_current_user_env_key, + rtems_libio_free_user_env + ); + if (eno != 0) { + rtems_fatal_error_occurred( RTEMS_UNSATISFIED ); + } + /* * Create the binary semaphore used to provide mutual exclusion * on the IOP Table. diff --git a/cpukit/libcsupport/src/privateenv.c b/cpukit/libcsupport/src/privateenv.c index bee94c117f..60207b0815 100644 --- a/cpukit/libcsupport/src/privateenv.c +++ b/cpukit/libcsupport/src/privateenv.c @@ -29,7 +29,16 @@ * Instantiate a private user environment for the calling thread. */ -static void free_user_env(void *arg) +rtems_user_env_t * rtems_current_user_env_get(void) +{ + void *ptr = pthread_getspecific(rtems_current_user_env_key); + if (ptr == NULL) { + ptr = &rtems_global_user_env; + } + return (rtems_user_env_t *) ptr; +} + +void rtems_libio_free_user_env(void *arg) { rtems_user_env_t *env = arg; bool uses_global_env = env == &rtems_global_user_env; @@ -44,7 +53,7 @@ static void free_user_env(void *arg) static void free_user_env_protected(rtems_user_env_t *env) { _Thread_Disable_dispatch(); - free_user_env(env); + rtems_libio_free_user_env(env); _Thread_Enable_dispatch(); } @@ -68,14 +77,13 @@ rtems_status_code rtems_libio_set_private_env(void) !rtems_filesystem_global_location_is_null(new_env->root_directory) && !rtems_filesystem_global_location_is_null(new_env->current_directory) ) { - sc = rtems_task_variable_add( - RTEMS_SELF, - (void **) &rtems_current_user_env, - free_user_env + int eno = pthread_setspecific( + rtems_current_user_env_key, + new_env ); - if (sc == RTEMS_SUCCESSFUL) { + + if (eno == 0) { free_user_env_protected(old_env); - rtems_current_user_env = new_env; } else { sc = RTEMS_TOO_MANY; } @@ -84,7 +92,7 @@ rtems_status_code rtems_libio_set_private_env(void) } if (sc != RTEMS_SUCCESSFUL) { - free_user_env(new_env); + rtems_libio_free_user_env(new_env); } } else { sc = RTEMS_NO_MEMORY; @@ -101,14 +109,7 @@ void rtems_libio_use_global_env(void) bool uses_private_env = env != &rtems_global_user_env; if (uses_private_env) { - sc = rtems_task_variable_delete( - RTEMS_SELF, - (void **) &rtems_current_user_env - ); - if (sc != RTEMS_SUCCESSFUL) { - rtems_fatal_error_occurred(0xdeadbeef); - } - - rtems_current_user_env = &rtems_global_user_env; + free_user_env_protected(env); + pthread_setspecific(rtems_current_user_env_key, NULL); } } -- cgit v1.2.3