[rtems commit] libblock: Use self-contained mutex for flashdisk

Sebastian Huber sebh at rtems.org
Thu Feb 8 08:16:42 UTC 2018


Module:    rtems
Branch:    master
Commit:    f9027ccfd35245017ef93fa4f7f0db59e83a0eaf
Changeset: http://git.rtems.org/rtems/commit/?id=f9027ccfd35245017ef93fa4f7f0db59e83a0eaf

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Jan  2 16:34:54 2018 +0100

libblock: Use self-contained mutex for flashdisk

Update #2843.

---

 cpukit/libblock/src/flashdisk.c | 131 +++++++++++++++++-----------------------
 1 file changed, 57 insertions(+), 74 deletions(-)

diff --git a/cpukit/libblock/src/flashdisk.c b/cpukit/libblock/src/flashdisk.c
index 1761178..0543f30 100644
--- a/cpukit/libblock/src/flashdisk.c
+++ b/cpukit/libblock/src/flashdisk.c
@@ -43,9 +43,10 @@
 #include <string.h>
 #include <inttypes.h>
 
-#include "rtems/blkdev.h"
-#include "rtems/diskdevs.h"
-#include "rtems/flashdisk.h"
+#include <rtems/blkdev.h>
+#include <rtems/diskdevs.h>
+#include <rtems/flashdisk.h>
+#include <rtems/thread.h>
 
 /**
  * Control tracing. It can be compiled out of the code for small
@@ -195,7 +196,7 @@ typedef struct rtems_flashdisk
                                                 erased. */
   rtems_fdisk_segment_ctl_queue failed;    /**< The list of segments that failed
                                                 when being erased. */
-  rtems_id lock;                           /**< Mutex for threading protection.*/
+  rtems_mutex lock;                        /**< Mutex for threading protection.*/
 
   uint8_t* copy_buffer;                    /**< Copy buf used during compacting */
 
@@ -2376,78 +2377,70 @@ rtems_fdisk_ioctl (rtems_disk_device *dd, uint32_t req, void* argp)
   dev_t                     dev = rtems_disk_get_device_identifier (dd);
   rtems_device_minor_number minor = rtems_filesystem_dev_minor_t (dev);
   rtems_blkdev_request*     r = argp;
-  rtems_status_code         sc;
 
   errno = 0;
 
-  sc = rtems_semaphore_obtain (rtems_flashdisks[minor].lock, RTEMS_WAIT, 0);
-  if (sc != RTEMS_SUCCESSFUL)
-    errno = EIO;
-  else
+  rtems_mutex_lock (&rtems_flashdisks[minor].lock);
+
+  switch (req)
   {
-    errno = 0;
-    switch (req)
-    {
-      case RTEMS_BLKIO_REQUEST:
-        if ((minor >= rtems_flashdisk_count) ||
-            (rtems_flashdisks[minor].device_count == 0))
-        {
-          errno = ENODEV;
-        }
-        else
+    case RTEMS_BLKIO_REQUEST:
+      if ((minor >= rtems_flashdisk_count) ||
+          (rtems_flashdisks[minor].device_count == 0))
+      {
+        errno = ENODEV;
+      }
+      else
+      {
+        switch (r->req)
         {
-          switch (r->req)
-          {
-            case RTEMS_BLKDEV_REQ_READ:
-              errno = rtems_fdisk_read (&rtems_flashdisks[minor], r);
-              break;
+          case RTEMS_BLKDEV_REQ_READ:
+            errno = rtems_fdisk_read (&rtems_flashdisks[minor], r);
+            break;
 
-            case RTEMS_BLKDEV_REQ_WRITE:
-              errno = rtems_fdisk_write (&rtems_flashdisks[minor], r);
-              break;
+          case RTEMS_BLKDEV_REQ_WRITE:
+            errno = rtems_fdisk_write (&rtems_flashdisks[minor], r);
+            break;
 
-            default:
-              errno = EINVAL;
-              break;
-          }
+          default:
+            errno = EINVAL;
+            break;
         }
-        break;
-
-      case RTEMS_FDISK_IOCTL_ERASE_DISK:
-        errno = rtems_fdisk_erase_disk (&rtems_flashdisks[minor]);
-        break;
+      }
+      break;
 
-      case RTEMS_FDISK_IOCTL_COMPACT:
-        errno = rtems_fdisk_compact (&rtems_flashdisks[minor]);
-        break;
+    case RTEMS_FDISK_IOCTL_ERASE_DISK:
+      errno = rtems_fdisk_erase_disk (&rtems_flashdisks[minor]);
+      break;
 
-      case RTEMS_FDISK_IOCTL_ERASE_USED:
-        errno = rtems_fdisk_erase_used (&rtems_flashdisks[minor]);
-        break;
+    case RTEMS_FDISK_IOCTL_COMPACT:
+      errno = rtems_fdisk_compact (&rtems_flashdisks[minor]);
+      break;
 
-      case RTEMS_FDISK_IOCTL_MONITORING:
-        errno = rtems_fdisk_monitoring_data (&rtems_flashdisks[minor],
-                                             (rtems_fdisk_monitor_data*) argp);
-        break;
+    case RTEMS_FDISK_IOCTL_ERASE_USED:
+      errno = rtems_fdisk_erase_used (&rtems_flashdisks[minor]);
+      break;
 
-      case RTEMS_FDISK_IOCTL_INFO_LEVEL:
-        rtems_flashdisks[minor].info_level = (uintptr_t) argp;
-        break;
+    case RTEMS_FDISK_IOCTL_MONITORING:
+      errno = rtems_fdisk_monitoring_data (&rtems_flashdisks[minor],
+                                           (rtems_fdisk_monitor_data*) argp);
+      break;
 
-      case RTEMS_FDISK_IOCTL_PRINT_STATUS:
-        errno = rtems_fdisk_print_status (&rtems_flashdisks[minor]);
-        break;
+    case RTEMS_FDISK_IOCTL_INFO_LEVEL:
+      rtems_flashdisks[minor].info_level = (uintptr_t) argp;
+      break;
 
-      default:
-        rtems_blkdev_ioctl (dd, req, argp);
-        break;
-    }
+    case RTEMS_FDISK_IOCTL_PRINT_STATUS:
+      errno = rtems_fdisk_print_status (&rtems_flashdisks[minor]);
+      break;
 
-    sc = rtems_semaphore_release (rtems_flashdisks[minor].lock);
-    if (sc != RTEMS_SUCCESSFUL)
-      errno = EIO;
+    default:
+      rtems_blkdev_ioctl (dd, req, argp);
+      break;
   }
 
+  rtems_mutex_unlock (&rtems_flashdisks[minor].lock);
+
   return errno == 0 ? 0 : -1;
 }
 
@@ -2525,24 +2518,14 @@ rtems_fdisk_initialize (rtems_device_major_number major,
     if (!fd->devices)
       return RTEMS_NO_MEMORY;
 
-    sc = rtems_semaphore_create (rtems_build_name ('F', 'D', 'S', 'K'), 1,
-                                 RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE |
-                                 RTEMS_INHERIT_PRIORITY, 0, &fd->lock);
-    if (sc != RTEMS_SUCCESSFUL)
-    {
-      rtems_fdisk_error ("disk lock create failed");
-      free (fd->copy_buffer);
-      free (fd->blocks);
-      free (fd->devices);
-      return sc;
-    }
+    rtems_mutex_init (&fd->lock, "Flash Disk");
 
     sc = rtems_disk_create_phys(dev, c->block_size,
                                 blocks - fd->unavail_blocks,
                                 rtems_fdisk_ioctl, NULL, name);
     if (sc != RTEMS_SUCCESSFUL)
     {
-      rtems_semaphore_delete (fd->lock);
+      rtems_mutex_destroy (&fd->lock);
       rtems_disk_delete (dev);
       free (fd->copy_buffer);
       free (fd->blocks);
@@ -2564,7 +2547,7 @@ rtems_fdisk_initialize (rtems_device_major_number major,
       if (!fd->devices[device].segments)
       {
         rtems_disk_delete (dev);
-        rtems_semaphore_delete (fd->lock);
+        rtems_mutex_destroy (&fd->lock);
         free (fd->copy_buffer);
         free (fd->blocks);
         free (fd->devices);
@@ -2599,7 +2582,7 @@ rtems_fdisk_initialize (rtems_device_major_number major,
     if (ret)
     {
       rtems_disk_delete (dev);
-      rtems_semaphore_delete (fd->lock);
+      rtems_mutex_destroy (&fd->lock);
       free (fd->copy_buffer);
       free (fd->blocks);
       free (fd->devices);
@@ -2612,7 +2595,7 @@ rtems_fdisk_initialize (rtems_device_major_number major,
     if (ret)
     {
       rtems_disk_delete (dev);
-      rtems_semaphore_delete (fd->lock);
+      rtems_mutex_destroy (&fd->lock);
       free (fd->copy_buffer);
       free (fd->blocks);
       free (fd->devices);




More information about the vc mailing list