[rtems commit] pc386/console/fb*.c: Use atomics to avoid dependency on pthreads

Joel Sherril joel at rtems.org
Thu Jun 11 15:11:20 UTC 2015


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

Author:    Joel Sherrill <joel.sherrill at oarcorp.com>
Date:      Tue Jun  9 13:48:22 2015 -0700

pc386/console/fb*.c: Use atomics to avoid dependency on pthreads

closes #2364.

---

 c/src/lib/libbsp/i386/pc386/console/fb_cirrus.c  | 29 ++++++++++-----------
 c/src/lib/libbsp/i386/pc386/console/fb_vesa_rm.c | 32 ++++++++++++------------
 c/src/lib/libbsp/i386/pc386/console/fb_vga.c     | 28 ++++++++++-----------
 3 files changed, 45 insertions(+), 44 deletions(-)

diff --git a/c/src/lib/libbsp/i386/pc386/console/fb_cirrus.c b/c/src/lib/libbsp/i386/pc386/console/fb_cirrus.c
index 1b311a1..15d36a3 100644
--- a/c/src/lib/libbsp/i386/pc386/console/fb_cirrus.c
+++ b/c/src/lib/libbsp/i386/pc386/console/fb_cirrus.c
@@ -27,9 +27,10 @@
 
 #include <rtems/fb.h>
 #include <rtems/framebuffer.h>
+#include <rtems/score/atomic.h>
 
-/* mutex to limit driver to protect against multiple opens */
-static pthread_mutex_t cirrus_mutex = PTHREAD_MUTEX_INITIALIZER;
+/* flag to limit driver to protect against multiple opens */
+static Atomic_Flag driver_mutex;
 
 /* screen information for the VGA driver
  * standard structures
@@ -332,6 +333,8 @@ frame_buffer_initialize(
           rtems_fatal_error_occurred( status );
       }
 
+      _Atomic_Flag_clear(&driver_mutex, ATOMIC_ORDER_RELEASE);
+
       return RTEMS_SUCCESSFUL;
   }
 }
@@ -591,8 +594,8 @@ frame_buffer_open(
   int r;
   uint32_t smem_start, regs_start;
 
-  if (pthread_mutex_trylock(&cirrus_mutex)!= 0){
-      printk( "FB_CIRRUS could not lock cirrus_mutex\n" );
+  if (_Atomic_Flag_test_and_set(&driver_mutex, ATOMIC_ORDER_ACQUIRE) != 0 ) {
+      printk( "FB_CIRRUS could not lock driver_mutex\n" );
 
       return RTEMS_UNSATISFIED;
   }
@@ -662,19 +665,17 @@ frame_buffer_close(
     void                      *arg
 )
 {
-  if (pthread_mutex_unlock(&cirrus_mutex) == 0){
-      /* restore previous state.  for VGA this means return to text mode.
-       * leave out if graphics hardware has been initialized in
-       * frame_buffer_initialize() */
+  _Atomic_Flag_clear(&driver_mutex, ATOMIC_ORDER_RELEASE);
 
-      /* VGA text mode */
-      fb_cirrus_write_gdc_reg(&cirrus_board_info, 0x06, 0x00);
+  /* restore previous state.  for VGA this means return to text mode.
+   * leave out if graphics hardware has been initialized in
+   * frame_buffer_initialize() */
 
-      printk( "FB_CIRRUS: close called.\n" );
-      return RTEMS_SUCCESSFUL;
-  }
+  /* VGA text mode */
+  fb_cirrus_write_gdc_reg(&cirrus_board_info, 0x06, 0x00);
 
-  return RTEMS_UNSATISFIED;
+  printk( "FB_CIRRUS: close called.\n" );
+  return RTEMS_SUCCESSFUL;
 }
 
 /*
diff --git a/c/src/lib/libbsp/i386/pc386/console/fb_vesa_rm.c b/c/src/lib/libbsp/i386/pc386/console/fb_vesa_rm.c
index 33dfc35..b758564 100644
--- a/c/src/lib/libbsp/i386/pc386/console/fb_vesa_rm.c
+++ b/c/src/lib/libbsp/i386/pc386/console/fb_vesa_rm.c
@@ -55,6 +55,8 @@
 #include <rtems/fb.h>
 #include <rtems/framebuffer.h>
 
+#include <rtems/score/atomic.h>
+
 #include <stdlib.h>
 
 #define FB_VESA_NAME    "FB_VESA_RM"
@@ -84,8 +86,8 @@ const char * const rtems_fb_default_mode;
  */
 void vesa_realmode_bootup_init(void);
 
-/* mutex for protection against multiple opens, when called frame_buffer_open */
-static pthread_mutex_t vesa_mutex = PTHREAD_MUTEX_INITIALIZER;
+/* flag to limit driver to protect against multiple opens */
+static Atomic_Flag driver_mutex;
 
 /* screen information for the VGA driver
  * standard structures - from RTEMS fb interface
@@ -840,9 +842,9 @@ frame_buffer_initialize(
 
     printk(FB_VESA_NAME " frame buffer -- driver initializing..\n" );
 
-/*
- * Register the device.
- */
+   /*
+    * Register the device.
+    */
     status = rtems_io_register_name(FRAMEBUFFER_DEVICE_0_NAME, major, 0);
     if (status != RTEMS_SUCCESSFUL)
     {
@@ -851,6 +853,8 @@ frame_buffer_initialize(
         rtems_fatal_error_occurred( status );
     }
 
+    _Atomic_Flag_clear(&driver_mutex, ATOMIC_ORDER_RELEASE);
+
     return RTEMS_SUCCESSFUL;
 }
 
@@ -866,7 +870,7 @@ frame_buffer_open(
 {
     printk( FB_VESA_NAME " open device\n" );
 
-    if (pthread_mutex_trylock(&vesa_mutex) != 0)
+    if (_Atomic_Flag_test_and_set(&driver_mutex, ATOMIC_ORDER_ACQUIRE) != 0 )
     {
         printk( FB_VESA_NAME " could not lock vesa_mutex\n" );
 
@@ -888,17 +892,13 @@ frame_buffer_close(
 )
 {
   printk( FB_VESA_NAME " close device\n" );
-  if (pthread_mutex_unlock(&vesa_mutex) == 0)
-  {
-      /* restore previous state.  for VGA this means return to text mode.
-       * leave out if graphics hardware has been initialized in
-       * frame_buffer_initialize() */
-
-      printk(FB_VESA_NAME ": close called.\n" );
-      return RTEMS_SUCCESSFUL;
-  }
+  _Atomic_Flag_clear(&driver_mutex, ATOMIC_ORDER_RELEASE);
+  /* restore previous state.  for VGA this means return to text mode.
+   * leave out if graphics hardware has been initialized in
+   * frame_buffer_initialize() */
 
-  return RTEMS_UNSATISFIED;
+  printk(FB_VESA_NAME ": close called.\n" );
+  return RTEMS_SUCCESSFUL;
 }
 
 /*
diff --git a/c/src/lib/libbsp/i386/pc386/console/fb_vga.c b/c/src/lib/libbsp/i386/pc386/console/fb_vga.c
index 444f44d..75ad56e 100644
--- a/c/src/lib/libbsp/i386/pc386/console/fb_vga.c
+++ b/c/src/lib/libbsp/i386/pc386/console/fb_vga.c
@@ -20,12 +20,14 @@
 #include <rtems/fb.h>
 #include <rtems/framebuffer.h>
 
+#include <rtems/score/atomic.h>
+
 /* these routines are defined in vgainit.c.*/
 extern void ega_hwinit( void );
 extern void ega_hwterm( void );
 
-/* mutex attribure */
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+/* flag to limit driver to protect against multiple opens */
+static Atomic_Flag driver_mutex;
 
 /* screen information for the VGA driver */
 static struct fb_var_screeninfo fb_var =
@@ -87,6 +89,8 @@ rtems_device_driver frame_buffer_initialize(
     rtems_fatal_error_occurred( status );
   }
 
+  _Atomic_Flag_clear(&driver_mutex, ATOMIC_ORDER_RELEASE);
+
   return RTEMS_SUCCESSFUL;
 }
 
@@ -99,7 +103,7 @@ rtems_device_driver frame_buffer_open(
   void                      *arg
 )
 {
-  if (pthread_mutex_trylock(&mutex)== 0){
+  if (_Atomic_Flag_test_and_set(&driver_mutex, ATOMIC_ORDER_ACQUIRE) != 0 ) {
       /* restore previous state.  for VGA this means return to text mode.
        * leave out if graphics hardware has been initialized in
        * frame_buffer_initialize()
@@ -121,17 +125,13 @@ rtems_device_driver frame_buffer_close(
   void                      *arg
 )
 {
-  if (pthread_mutex_unlock(&mutex) == 0){
-    /* restore previous state.  for VGA this means return to text mode.
-     * leave out if graphics hardware has been initialized in
-     * frame_buffer_initialize() */
-    ega_hwterm();
-    printk( "FBVGA close called.\n" );
-    return RTEMS_SUCCESSFUL;
-  }
-
-  return RTEMS_UNSATISFIED;
-}
+  _Atomic_Flag_clear(&driver_mutex, ATOMIC_ORDER_RELEASE);
+  /* restore previous state.  for VGA this means return to text mode.
+   * leave out if graphics hardware has been initialized in
+   * frame_buffer_initialize() */
+  ega_hwterm();
+  printk( "FBVGA close called.\n" );
+  return RTEMS_SUCCESSFUL;
 
 /*
  * fbvga device driver READ entry point.




More information about the vc mailing list