[PATCH 5/6] dev: Use C11 mutex for SPI framework

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Dec 14 13:39:41 UTC 2016


Update #2776.
---
 cpukit/dev/include/dev/spi/spi.h |  6 +++--
 cpukit/dev/spi/spi-bus.c         | 54 ++++++++++++++--------------------------
 testsuites/libtests/spi01/init.c |  2 --
 3 files changed, 22 insertions(+), 40 deletions(-)

diff --git a/cpukit/dev/include/dev/spi/spi.h b/cpukit/dev/include/dev/spi/spi.h
index 66bbac2..d5d1900 100644
--- a/cpukit/dev/include/dev/spi/spi.h
+++ b/cpukit/dev/include/dev/spi/spi.h
@@ -25,7 +25,9 @@
 
 #include <linux/spi/spidev.h>
 
-#include <rtems.h>
+#include <stdbool.h>
+#include <threads.h>
+
 #include <rtems/seterr.h>
 
 #ifdef __cplusplus
@@ -96,7 +98,7 @@ struct spi_bus {
   /**
    * @brief Mutex to protect the bus access.
    */
-  rtems_id mutex;
+  mtx_t mutex;
 
   /**
    * @brief Maximum Speed in Hz
diff --git a/cpukit/dev/spi/spi-bus.c b/cpukit/dev/spi/spi-bus.c
index dd84b02..7f022f8 100644
--- a/cpukit/dev/spi/spi-bus.c
+++ b/cpukit/dev/spi/spi-bus.c
@@ -33,20 +33,20 @@
 
 static void spi_bus_obtain(spi_bus *bus)
 {
-  rtems_status_code sc;
+  int error;
 
-  sc = rtems_semaphore_obtain(bus->mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
-  _Assert(sc == RTEMS_SUCCESSFUL);
-  (void) sc;
+  error = mtx_lock(&bus->mutex);
+  _Assert(error == thrd_success);
+  (void) error;
 }
 
 static void spi_bus_release(spi_bus *bus)
 {
-  rtems_status_code sc;
+  int error;
 
-  sc = rtems_semaphore_release(bus->mutex);
-  _Assert(sc == RTEMS_SUCCESSFUL);
-  (void) sc;
+  error = mtx_unlock(&bus->mutex);
+  _Assert(error == thrd_success);
+  (void) error;
 }
 
 static void spi_bus_set_defaults(spi_bus *bus, spi_ioc_transfer *msg)
@@ -298,39 +298,26 @@ static int spi_bus_setup_default(
   return -EIO;
 }
 
-static int spi_bus_do_init(
+static void spi_bus_do_init(
   spi_bus *bus,
   void (*destroy)(spi_bus *bus)
 )
 {
-  rtems_status_code sc;
-
-  sc = rtems_semaphore_create(
-    rtems_build_name('S', 'P', 'I', ' '),
-    1,
-    RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
-    0,
-    &bus->mutex
-  );
-  if (sc != RTEMS_SUCCESSFUL) {
-    (*destroy)(bus);
-    rtems_set_errno_and_return_minus_one(ENOMEM);
-  }
+  int error;
 
   bus->transfer = spi_bus_transfer_default;
   bus->setup = spi_bus_setup_default;
   bus->destroy = destroy;
   bus->bits_per_word = 8;
-  return 0;
+
+  error = mtx_init(&bus->mutex, mtx_recursive);
+  _Assert(error == thrd_success);
+  (void) error;
 }
 
 void spi_bus_destroy(spi_bus *bus)
 {
-  rtems_status_code sc;
-
-  sc = rtems_semaphore_delete(bus->mutex);
-  _Assert(sc == RTEMS_SUCCESSFUL);
-  (void) sc;
+  mtx_destroy(&bus->mutex);
 }
 
 void spi_bus_destroy_and_free(spi_bus *bus)
@@ -342,8 +329,8 @@ void spi_bus_destroy_and_free(spi_bus *bus)
 int spi_bus_init(spi_bus *bus)
 {
   memset(bus, 0, sizeof(*bus));
-
-  return spi_bus_do_init(bus, spi_bus_destroy);
+  spi_bus_do_init(bus, spi_bus_destroy);
+  return 0;
 }
 
 spi_bus *spi_bus_alloc_and_init(size_t size)
@@ -353,12 +340,7 @@ spi_bus *spi_bus_alloc_and_init(size_t size)
   if (size >= sizeof(*bus)) {
     bus = calloc(1, size);
     if (bus != NULL) {
-      int rv;
-
-      rv = spi_bus_do_init(bus, spi_bus_destroy_and_free);
-      if (rv != 0) {
-        return NULL;
-      }
+      spi_bus_do_init(bus, spi_bus_destroy_and_free);
     }
   }
 
diff --git a/testsuites/libtests/spi01/init.c b/testsuites/libtests/spi01/init.c
index 990d3ec..c08661b 100644
--- a/testsuites/libtests/spi01/init.c
+++ b/testsuites/libtests/spi01/init.c
@@ -277,8 +277,6 @@ static void Init(rtems_task_argument arg)
 
 #define CONFIGURE_MAXIMUM_TASKS 1
 
-#define CONFIGURE_MAXIMUM_SEMAPHORES 1
-
 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
 
 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
-- 
1.8.4.5




More information about the devel mailing list