[PATCH 02/12] libblock: Use self-contained mutex for disk lock

Sebastian Huber sebastian.huber at embedded-brains.de
Thu Dec 21 14:09:50 UTC 2017


Update #2843.
---
 cpukit/libblock/src/diskdevs.c | 101 ++++++++++++-----------------------------
 cpukit/sapi/include/confdefs.h |   9 +---
 2 files changed, 32 insertions(+), 78 deletions(-)

diff --git a/cpukit/libblock/src/diskdevs.c b/cpukit/libblock/src/diskdevs.c
index cb1f15707f..836cc4973e 100644
--- a/cpukit/libblock/src/diskdevs.c
+++ b/cpukit/libblock/src/diskdevs.c
@@ -10,7 +10,7 @@
  * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
  * Author: Victor V. Vengerov <vvv at oktet.ru>
  *
- * Copyright (c) 2009-2012 embedded brains GmbH.
+ * Copyright (c) 2009, 2017 embedded brains GmbH.
  */
 
 #if HAVE_CONFIG_H
@@ -26,6 +26,7 @@
 #include <rtems/diskdevs.h>
 #include <rtems/blkdev.h>
 #include <rtems/bdbuf.h>
+#include <rtems/thread.h>
 
 #define DISKTAB_INITIAL_SIZE 8
 
@@ -42,7 +43,7 @@ static rtems_disk_device_table *disktab;
 static rtems_device_major_number disktab_size;
 
 /* Mutual exclusion semaphore for disk devices table */
-static rtems_id diskdevs_mutex;
+static rtems_mutex diskdevs_mutex = RTEMS_MUTEX_INITIALIZER("diskdevs");
 
 /* diskdevs data structures protection flag.
  * Normally, only table lookup operations performed. It is quite fast, so
@@ -59,33 +60,20 @@ static volatile bool diskdevs_protected;
 
 RTEMS_INTERRUPT_LOCK_DEFINE(static, diskdevs_lock, "diskdevs")
 
-static rtems_status_code
+static rtems_status_code disk_delete_locked(dev_t dev);
+
+static void
 disk_lock(void)
 {
-  rtems_status_code sc = RTEMS_SUCCESSFUL;
-
-  sc = rtems_semaphore_obtain(diskdevs_mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
-  if (sc == RTEMS_SUCCESSFUL) {
-    diskdevs_protected = true;
-
-    return RTEMS_SUCCESSFUL;
-  } else {
-    return RTEMS_NOT_CONFIGURED;
-  }
+  rtems_mutex_lock(&diskdevs_mutex);
+  diskdevs_protected = true;
 }
 
 static void
 disk_unlock(void)
 {
-  rtems_status_code sc = RTEMS_SUCCESSFUL;
-
   diskdevs_protected = false;
-
-  sc = rtems_semaphore_release(diskdevs_mutex);
-  if (sc != RTEMS_SUCCESSFUL) {
-    /* FIXME: Error number */
-    rtems_fatal_error_occurred(0xdeadbeef);
-  }
+  rtems_mutex_unlock(&diskdevs_mutex);
 }
 
 static rtems_disk_device *
@@ -247,10 +235,7 @@ rtems_status_code rtems_disk_create_phys(
     return RTEMS_INVALID_ADDRESS;
   }
 
-  sc = disk_lock();
-  if (sc != RTEMS_SUCCESSFUL) {
-    return sc;
-  }
+  disk_lock();
 
   sc = create_disk(dev, name, &dd, &alloc_name);
   if (sc != RTEMS_SUCCESSFUL) {
@@ -272,7 +257,7 @@ rtems_status_code rtems_disk_create_phys(
 
   if (sc != RTEMS_SUCCESSFUL) {
     dd->ioctl = null_handler;
-    rtems_disk_delete(dev);
+    disk_delete_locked(dev);
     disk_unlock();
 
     return sc;
@@ -302,10 +287,7 @@ rtems_status_code rtems_disk_create_log(
   rtems_disk_device *dd = NULL;
   char *alloc_name = NULL;
 
-  sc = disk_lock();
-  if (sc != RTEMS_SUCCESSFUL) {
-    return sc;
-  }
+  disk_lock();
 
   phys_dd = get_disk_entry(phys, true);
   if (phys_dd == NULL) {
@@ -335,7 +317,7 @@ rtems_status_code rtems_disk_create_log(
 
   if (sc != RTEMS_SUCCESSFUL) {
     dd->ioctl = null_handler;
-    rtems_disk_delete(dev);
+    disk_delete_locked(dev);
     disk_unlock();
 
     return sc;
@@ -404,36 +386,37 @@ rtems_disk_cleanup(rtems_disk_device *disk_to_remove)
   }
 }
 
-rtems_status_code
-rtems_disk_delete(dev_t dev)
+static rtems_status_code
+disk_delete_locked(dev_t dev)
 {
-  rtems_status_code sc = RTEMS_SUCCESSFUL;
   rtems_disk_device *dd = NULL;
 
-  sc = disk_lock();
-  if (sc != RTEMS_SUCCESSFUL) {
-    return sc;
-  }
-
   dd = get_disk_entry(dev, true);
   if (dd == NULL) {
-    disk_unlock();
-
     return RTEMS_INVALID_ID;
   }
 
   dd->deleted = true;
   rtems_disk_cleanup(dd);
 
+  return RTEMS_SUCCESSFUL;
+}
+
+rtems_status_code
+rtems_disk_delete(dev_t dev)
+{
+  rtems_status_code sc;
+
+  disk_lock();
+  sc = disk_delete_locked(dev);
   disk_unlock();
 
-  return RTEMS_SUCCESSFUL;
+  return sc;
 }
 
 rtems_disk_device *
 rtems_disk_obtain(dev_t dev)
 {
-  rtems_status_code sc = RTEMS_SUCCESSFUL;
   rtems_disk_device *dd = NULL;
   rtems_interrupt_lock_context lock_context;
 
@@ -445,11 +428,9 @@ rtems_disk_obtain(dev_t dev)
   } else {
     rtems_interrupt_lock_release(&diskdevs_lock, &lock_context);
 
-    sc = disk_lock();
-    if (sc == RTEMS_SUCCESSFUL) {
-      dd = get_disk_entry(dev, false);
-      disk_unlock();
-    }
+    disk_lock();
+    dd = get_disk_entry(dev, false);
+    disk_unlock();
   }
 
   return dd;
@@ -478,7 +459,6 @@ rtems_disk_release(rtems_disk_device *dd)
 rtems_disk_device *
 rtems_disk_next(dev_t dev)
 {
-  rtems_status_code sc = RTEMS_SUCCESSFUL;
   rtems_disk_device_table *dtab = NULL;
   rtems_device_major_number major = 0;
   rtems_device_minor_number minor = 0;
@@ -499,10 +479,7 @@ rtems_disk_next(dev_t dev)
     }
   }
 
-  sc = disk_lock();
-  if (sc != RTEMS_SUCCESSFUL) {
-    return NULL;
-  }
+  disk_lock();
 
   if (major >= disktab_size) {
     disk_unlock();
@@ -547,23 +524,8 @@ rtems_disk_io_initialize(void)
     return RTEMS_NO_MEMORY;
   }
 
-  diskdevs_protected = false;
-  sc = rtems_semaphore_create(
-    rtems_build_name('D', 'D', 'E', 'V'),
-    1,
-    RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY,
-    0,
-    &diskdevs_mutex
-  );
-  if (sc != RTEMS_SUCCESSFUL) {
-    free(disktab);
-
-    return RTEMS_NO_MEMORY;
-  }
-
   sc = rtems_bdbuf_init();
   if (sc != RTEMS_SUCCESSFUL) {
-    rtems_semaphore_delete(diskdevs_mutex);
     free(disktab);
 
     return RTEMS_UNSATISFIED;
@@ -594,9 +556,6 @@ rtems_disk_io_done(void)
   }
   free(disktab);
 
-  rtems_semaphore_delete(diskdevs_mutex);
-
-  diskdevs_mutex = RTEMS_ID_NONE;
   disktab = NULL;
   disktab_size = 0;
 
diff --git a/cpukit/sapi/include/confdefs.h b/cpukit/sapi/include/confdefs.h
index 1c993dd099..06e9f74732 100755
--- a/cpukit/sapi/include/confdefs.h
+++ b/cpukit/sapi/include/confdefs.h
@@ -1832,22 +1832,17 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
     0 : CONFIGURE_BDBUF_TASK_STACK_SIZE - CONFIGURE_MINIMUM_TASK_STACK_SIZE))
 
   #ifdef RTEMS_BDBUF_USE_PTHREAD
-    /*
-     * Semaphores:
-     *   o disk lock
-     */
-    #define _CONFIGURE_LIBBLOCK_SEMAPHORES 1
+    #define _CONFIGURE_LIBBLOCK_SEMAPHORES 0
   #else
     /*
      * Semaphores:
-     *   o disk lock
      *   o bdbuf lock
      *   o bdbuf sync lock
      *   o bdbuf access condition
      *   o bdbuf transfer condition
      *   o bdbuf buffer condition
      */
-    #define _CONFIGURE_LIBBLOCK_SEMAPHORES 6
+    #define _CONFIGURE_LIBBLOCK_SEMAPHORES 5
   #endif
 
   #if defined(CONFIGURE_HAS_OWN_BDBUF_TABLE) || \
-- 
2.12.3




More information about the devel mailing list