From 87b7117ffdbed0afdafc929f1f6d864d6132e96a Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 3 Jan 2018 16:07:38 +0100 Subject: libdl: Use self-contained mutex for RAP Update #2843. --- cpukit/libdl/rap.c | 92 ++++++++++-------------------------------------------- 1 file changed, 16 insertions(+), 76 deletions(-) diff --git a/cpukit/libdl/rap.c b/cpukit/libdl/rap.c index 87b3bc36e5..c512571ac8 100644 --- a/cpukit/libdl/rap.c +++ b/cpukit/libdl/rap.c @@ -19,6 +19,7 @@ #include "config.h" #endif +#include #include #include #include @@ -38,7 +39,8 @@ */ typedef struct rtems_rap_data_s { - rtems_id lock; /**< The RAP lock id */ + pthread_once_t once; + rtems_mutex lock; /**< The RAP lock id */ rtems_chain_control apps; /**< List if loaded application. */ int last_errno; /**< Last error number. */ char last_error[64]; /**< Last error string. */ @@ -55,13 +57,6 @@ typedef struct rtems_rap_app_s void* handle; /**< The dlopen handle. */ } rtems_rap_app_t; -/** - * Semaphore configuration to create a mutex. - */ -#define RTEMS_MUTEX_ATTRIBS \ - (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE | \ - RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL) - /** * RTL entry. */ @@ -74,7 +69,7 @@ typedef struct rtems_rap_app_s /** * Static RAP data is returned to the user when the loader is locked. */ -static rtems_rap_data_t rap_; +static rtems_rap_data_t rap_ = { .once = PTHREAD_ONCE_INIT }; /** * Verbose level for the RAP loader. @@ -89,90 +84,35 @@ typedef int (*rtems_rap_entry_t)(int argc, const char* argv[]); /** * Forward decl. */ -static bool rtems_rap_unlock (void); +static void rtems_rap_unlock (void); -static bool +static void rtems_rap_data_init (void) { /* - * Lock the RAP. We only create a lock if a call is made. First we test if a - * lock is present. If one is present we lock it. If not the libio lock is - * locked and we then test the lock again. If not present we create the lock - * then release libio lock. + * Create the RAP lock. */ - if (!rap_.lock) - { - rtems_libio_lock (); - - if (!rap_.lock) - { - rtems_status_code sc; - rtems_id lock; - - /* - * Create the RAP lock. - */ - sc = rtems_semaphore_create (rtems_build_name ('R', 'A', 'P', '_'), - 1, RTEMS_MUTEX_ATTRIBS, - RTEMS_NO_PRIORITY, &lock); - if (sc != RTEMS_SUCCESSFUL) - return false; - - sc = rtems_semaphore_obtain (lock, RTEMS_WAIT, RTEMS_NO_TIMEOUT); - if (sc != RTEMS_SUCCESSFUL) - { - rtems_semaphore_delete (lock); - return false; - } - - rap_.lock = lock; - - /* - * Initialise the objects list and create any required services. - */ - rtems_chain_initialize_empty (&rap_.apps); - } - - rtems_libio_unlock (); + rtems_mutex_init (&rap_.lock, "RAP"); - rtems_rap_unlock (); - } - return true; + /* + * Initialise the objects list and create any required services. + */ + rtems_chain_initialize_empty (&rap_.apps); } static rtems_rap_data_t* rtems_rap_lock (void) { - rtems_status_code sc; - - if (!rtems_rap_data_init ()) - return NULL; - - sc = rtems_semaphore_obtain (rap_.lock, - RTEMS_WAIT, RTEMS_NO_TIMEOUT); - if (sc != RTEMS_SUCCESSFUL) - { - errno = EINVAL; - return NULL; - } + pthread_once (&rap_.once, rtems_rap_data_init); + rtems_mutex_lock (&rap_.lock); return &rap_; } -static bool +static void rtems_rap_unlock (void) { - /* - * Not sure any error should be returned or an assert. - */ - rtems_status_code sc; - sc = rtems_semaphore_release (rap_.lock); - if ((sc != RTEMS_SUCCESSFUL) && (errno == 0)) - { - errno = EINVAL; - return false; - } - return true; + rtems_mutex_unlock (&rap_.lock); } static rtems_rap_app_t* -- cgit v1.2.3