[PATCH rtems 1/3] libi2c: Use rtems thread api

Vijay Kumar Banerjee vijay at rtems.org
Thu Jul 20 01:10:01 UTC 2023


Replace custom mutex handling with rtems thread api
---
 cpukit/include/rtems/libi2c.h |   2 +
 cpukit/libi2c/libi2c.c        | 114 +++++++++++-----------------------
 2 files changed, 37 insertions(+), 79 deletions(-)

diff --git a/cpukit/include/rtems/libi2c.h b/cpukit/include/rtems/libi2c.h
index 7b86ab5f99..b8a4a5e5d9 100644
--- a/cpukit/include/rtems/libi2c.h
+++ b/cpukit/include/rtems/libi2c.h
@@ -119,6 +119,8 @@ rtems_i2c_ioctl (
 
 extern const rtems_driver_address_table rtems_libi2c_io_ops;
 
+extern bool rtems_libi2c_is_initialized;
+
 /* Unfortunately, if you want to add this driver to
  * a RTEMS configuration table then you need all the
  * members explicitly :-( (Device_driver_table should
diff --git a/cpukit/libi2c/libi2c.c b/cpukit/libi2c/libi2c.c
index 027c4859c1..53f17d878e 100644
--- a/cpukit/libi2c/libi2c.c
+++ b/cpukit/libi2c/libi2c.c
@@ -64,6 +64,7 @@
 #include <rtems/bspIo.h>
 #include <rtems/libio.h>
 #include <rtems/score/sysstate.h>
+#include <rtems/thread.h>
 
 #include <rtems/libi2c.h>
 
@@ -103,35 +104,22 @@
 
 rtems_device_major_number rtems_libi2c_major;
 
-static bool is_initialized = false;
+bool rtems_libi2c_is_initialized = false;
 
 static struct i2cbus
 {
   rtems_libi2c_bus_t *bush;
-  volatile rtems_id mutex;      /* lock this across start -> stop */
+  rtems_mutex mutex;      /* lock this across start -> stop */
   volatile bool started;
   char *name;
-} busses[MAX_NO_BUSSES] = { { NULL, RTEMS_ID_NONE, false, NULL } };
+} busses[MAX_NO_BUSSES] = { { NULL, {}, false, NULL } };
 
 static struct
 {
   rtems_libi2c_drv_t *drv;
 } drvs[MAX_NO_DRIVERS] = { { NULL } };
 
-static rtems_id libmutex = RTEMS_ID_NONE;
-
-#define LOCK(m)		assert(!rtems_semaphore_obtain((m), RTEMS_WAIT, RTEMS_NO_TIMEOUT))
-#define UNLOCK(m)	rtems_semaphore_release((m))
-
-#define LIBLOCK()	LOCK(libmutex)
-#define LIBUNLOCK()	UNLOCK(libmutex)
-
-#define MUTEX_ATTS 	\
-	(  RTEMS_PRIORITY			\
-	 | RTEMS_BINARY_SEMAPHORE	\
-	 |RTEMS_INHERIT_PRIORITY	\
-	 |RTEMS_NO_PRIORITY_CEILING	\
-	 |RTEMS_LOCAL )
+static rtems_mutex libmutex;
 
 /* During early stages of life, stdio is not available */
 
@@ -148,21 +136,6 @@ va_list ap;
 	va_end(ap);
 }
 
-static rtems_status_code
-mutexCreate (rtems_name nm, rtems_id *pm)
-{
-  rtems_status_code sc;
-
-  if (RTEMS_SUCCESSFUL !=
-      (sc = rtems_semaphore_create (nm, 1, MUTEX_ATTS, 0, pm))) {
-	if ( _System_state_Is_up( _System_state_Get() ) )
-    	rtems_error (sc, DRVNM "Unable to create mutex\n");
-	else
-		printk (DRVNM "Unable to create mutex (status code %i)\n", sc);
-  }
-  return sc;
-}
-
 /* Lock a bus avoiding to have a mutex, which is mostly
  * unused, hanging around all the time. We just create
  * and delete it on the fly...
@@ -174,37 +147,27 @@ static void
 lock_bus (int busno)
 {
   struct i2cbus *bus = &busses[busno];
+  char name[5];
 
-  if (bus->mutex == RTEMS_ID_NONE) {
-    /*
-     * Nobody is holding the bus mutex - it's not there.  Create it on the fly.
-     */
-    LIBLOCK ();
-    if (bus->mutex == RTEMS_ID_NONE) {
-      rtems_id m = RTEMS_ID_NONE;
-      rtems_status_code sc = mutexCreate (
-        rtems_build_name ('i', '2', 'c', '0' + busno),
-        &m
-      );
-      if (sc != RTEMS_SUCCESSFUL) {
-        LIBUNLOCK ();
-        rtems_panic (DRVNM "Unable to create bus lock");
-        return;
-      }
-      bus->mutex = m;
-    }
-    LIBUNLOCK ();
-  }
+  rtems_mutex_lock(&libmutex);
+
+  snprintf(name, 5, "I2C%d", busno);
+
+  rtems_mutex_init(&bus->mutex, name);
+
+  rtems_mutex_unlock(&libmutex);
 
   /* Now lock this bus */
-  LOCK (bus->mutex);
+
+  rtems_mutex_lock(&bus->mutex);
 }
 
 static void
 unlock_bus (int busno)
 {
   struct i2cbus *bus = &busses[busno];
-  UNLOCK (bus->mutex);
+
+  rtems_mutex_unlock(&bus->mutex);
 }
 
 /* Note that 'arg' is always passed in as NULL */
@@ -212,21 +175,16 @@ rtems_status_code
 rtems_i2c_init (rtems_device_major_number major, rtems_device_minor_number minor,
           void *arg)
 {
-  rtems_status_code rval;
   /* No busses or drivers can be registered at this point;
    * avoid the macro aborting with an error
   DECL_CHECKED_DRV (drv, busno, minor)
    */
 
-  rval = mutexCreate (rtems_build_name ('l', 'I', '2', 'C'), &libmutex);
+  rtems_mutex_init(&libmutex, "LIBI2C");
+  rtems_libi2c_is_initialized     = true;
+  rtems_libi2c_major = major;
+  return 0;
 
-  if ( RTEMS_SUCCESSFUL == rval ) {
-  	is_initialized     = true;
-	rtems_libi2c_major = major;
-  } else {
-  	libmutex = RTEMS_ID_NONE;
-  }
-  return rval;
 }
 
 rtems_status_code
@@ -350,7 +308,7 @@ rtems_libi2c_initialize (void)
 {
   rtems_status_code sc;
 
-  if (is_initialized) {
+  if (rtems_libi2c_is_initialized) {
     /*
      * already called before? then skip this step
      */
@@ -361,18 +319,15 @@ rtems_libi2c_initialize (void)
    * the return code of the 'init' operation, so we cannot
    * rely on return code since it may seem OK even if the driver 'init;
    * op failed.
-   * Let 'init' handle 'is_initialized'...
+   * Let 'init' handle 'rtems_libi2c_is_initialized'...
    */
   sc = rtems_io_register_driver (0, &rtems_libi2c_io_ops, &rtems_libi2c_major);
   if (RTEMS_SUCCESSFUL != sc) {
     safe_printf(
              DRVNM "Claiming driver slot failed (rtems status code %i)\n",
              sc);
-    if (libmutex != RTEMS_ID_NONE) {
-      rtems_semaphore_delete (libmutex);
-    }
-    libmutex = RTEMS_ID_NONE;
-    is_initialized = false;
+    rtems_mutex_destroy(&libmutex);
+    rtems_libi2c_is_initialized = false;
     return -1;
   }
 
@@ -423,8 +378,7 @@ rtems_libi2c_register_bus (const char *name, rtems_libi2c_bus_t * bus)
   }
   /* should be a directory since name terminates in '/' */
 
-
-  if (libmutex == RTEMS_ID_NONE) {
+  if (!rtems_libi2c_is_initialized){
     safe_printf ( DRVNM "Library not initialized\n");
     free( nmcpy );
     return -RTEMS_NOT_DEFINED;
@@ -436,12 +390,12 @@ rtems_libi2c_register_bus (const char *name, rtems_libi2c_bus_t * bus)
     return -RTEMS_NOT_CONFIGURED;
   }
 
-  LIBLOCK ();
+  rtems_mutex_lock(&libmutex);
+
   for (i = 0; i < MAX_NO_BUSSES; i++) {
     if (!busses[i].bush) {
       /* found a free slot */
       busses[i].bush = bus;
-      busses[i].mutex = RTEMS_ID_NONE;
       busses[i].started = false;
 
       if (!name)
@@ -458,7 +412,7 @@ rtems_libi2c_register_bus (const char *name, rtems_libi2c_bus_t * bus)
       break;
     }
   }
-  LIBUNLOCK ();
+  rtems_mutex_unlock(&libmutex);
 
   if (i >= MAX_NO_BUSSES) {
     i = -RTEMS_TOO_MANY;
@@ -704,7 +658,7 @@ rtems_libi2c_register_drv (const char *name, rtems_libi2c_drv_t * drvtbl,
   rtems_status_code err;
   rtems_device_minor_number minor;
 
-  if (libmutex == RTEMS_ID_NONE) {
+  if (!rtems_libi2c_is_initialized){
     safe_printf ( DRVNM "Library not initialized\n");
     return -RTEMS_NOT_DEFINED;
   }
@@ -725,7 +679,7 @@ rtems_libi2c_register_drv (const char *name, rtems_libi2c_drv_t * drvtbl,
   }
 
   /* allocate slot */
-  LIBLOCK ();
+  rtems_mutex_lock(&libmutex);
   for (i = 0; i < MAX_NO_DRIVERS; i++) {
     /* driver # 0 is special, it is the built-in raw driver */
     if (!drvs[i].drv) {
@@ -768,10 +722,12 @@ rtems_libi2c_register_drv (const char *name, rtems_libi2c_drv_t * drvtbl,
       else
         err = RTEMS_SUCCESSFUL;
 
-      LIBUNLOCK ();
+      rtems_mutex_unlock(&libmutex);
       return err ? -err : minor;
     }
+    rtems_mutex_unlock(&libmutex);
   }
-  LIBUNLOCK ();
+  rtems_mutex_unlock(&libmutex);
+
   return -RTEMS_TOO_MANY;
 }
-- 
2.34.3



More information about the devel mailing list