Changeset 87b7117f in rtems


Ignore:
Timestamp:
Jan 3, 2018, 3:07:38 PM (4 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
5, master
Children:
71a8446
Parents:
d9800ac
git-author:
Sebastian Huber <sebastian.huber@…> (01/03/18 15:07:38)
git-committer:
Sebastian Huber <sebastian.huber@…> (02/07/18 07:58:30)
Message:

libdl: Use self-contained mutex for RAP

Update #2843.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libdl/rap.c

    rd9800ac r87b7117f  
    2020#endif
    2121
     22#include <pthread.h>
    2223#include <stdarg.h>
    2324#include <stdlib.h>
     
    3940typedef struct rtems_rap_data_s
    4041{
    41   rtems_id            lock;           /**< The RAP lock id */
     42  pthread_once_t      once;
     43  rtems_mutex         lock;           /**< The RAP lock id */
    4244  rtems_chain_control apps;           /**< List if loaded application. */
    4345  int                 last_errno;     /**< Last error number. */
     
    5557  void*            handle;       /**< The dlopen handle. */
    5658} rtems_rap_app_t;
    57 
    58 /**
    59  * Semaphore configuration to create a mutex.
    60  */
    61 #define RTEMS_MUTEX_ATTRIBS \
    62   (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE | \
    63    RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL)
    6459
    6560/**
     
    7570 * Static RAP data is returned to the user when the loader is locked.
    7671 */
    77 static rtems_rap_data_t rap_;
     72static rtems_rap_data_t rap_ = { .once = PTHREAD_ONCE_INIT };
    7873
    7974/**
     
    9085 * Forward decl.
    9186 */
    92 static bool rtems_rap_unlock (void);
    93 
    94 static bool
     87static void rtems_rap_unlock (void);
     88
     89static void
    9590rtems_rap_data_init (void)
    9691{
    9792  /*
    98    * Lock the RAP. We only create a lock if a call is made. First we test if a
    99    * lock is present. If one is present we lock it. If not the libio lock is
    100    * locked and we then test the lock again. If not present we create the lock
    101    * then release libio lock.
     93   * Create the RAP lock.
    10294   */
    103   if (!rap_.lock)
    104   {
    105     rtems_libio_lock ();
    106 
    107     if (!rap_.lock)
    108     {
    109       rtems_status_code sc;
    110       rtems_id          lock;
    111 
    112       /*
    113        * Create the RAP lock.
    114        */
    115       sc = rtems_semaphore_create (rtems_build_name ('R', 'A', 'P', '_'),
    116                                    1, RTEMS_MUTEX_ATTRIBS,
    117                                    RTEMS_NO_PRIORITY, &lock);
    118       if (sc != RTEMS_SUCCESSFUL)
    119         return false;
    120 
    121       sc = rtems_semaphore_obtain (lock, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    122       if (sc != RTEMS_SUCCESSFUL)
    123       {
    124         rtems_semaphore_delete (lock);
    125         return false;
    126       }
    127 
    128       rap_.lock = lock;
    129 
    130       /*
    131        * Initialise the objects list and create any required services.
    132        */
    133       rtems_chain_initialize_empty (&rap_.apps);
    134     }
    135 
    136     rtems_libio_unlock ();
    137 
    138     rtems_rap_unlock ();
    139   }
    140   return true;
     95  rtems_mutex_init (&rap_.lock, "RAP");
     96
     97  /*
     98   * Initialise the objects list and create any required services.
     99   */
     100  rtems_chain_initialize_empty (&rap_.apps);
    141101}
    142102
     
    144104rtems_rap_lock (void)
    145105{
    146   rtems_status_code sc;
    147 
    148   if (!rtems_rap_data_init ())
    149     return NULL;
    150 
    151   sc = rtems_semaphore_obtain (rap_.lock,
    152                                RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    153   if (sc != RTEMS_SUCCESSFUL)
    154   {
    155     errno = EINVAL;
    156     return NULL;
    157   }
     106  pthread_once (&rap_.once, rtems_rap_data_init);
     107  rtems_mutex_lock (&rap_.lock);
    158108
    159109  return &rap_;
    160110}
    161111
    162 static bool
     112static void
    163113rtems_rap_unlock (void)
    164114{
    165   /*
    166    * Not sure any error should be returned or an assert.
    167    */
    168   rtems_status_code sc;
    169   sc = rtems_semaphore_release (rap_.lock);
    170   if ((sc != RTEMS_SUCCESSFUL) && (errno == 0))
    171   {
    172     errno = EINVAL;
    173     return false;
    174   }
    175   return true;
     115  rtems_mutex_unlock (&rap_.lock);
    176116}
    177117
Note: See TracChangeset for help on using the changeset viewer.