[rtems commit] libio: Use FIFO for iop free list

Sebastian Huber sebh at rtems.org
Fri Sep 15 09:01:36 UTC 2017


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Thu Sep 14 15:21:14 2017 +0200

libio: Use FIFO for iop free list

Update #3136.

---

 cpukit/libcsupport/include/rtems/libio_.h  |  3 ++-
 cpukit/libcsupport/src/libio.c             | 23 ++++++++++++++--------
 cpukit/libcsupport/src/libio_init.c        | 10 +++++++---
 cpukit/libcsupport/src/resource_snapshot.c |  2 +-
 testsuites/fstests/fsclose01/init.c        | 31 ++++++++++++++++++++++++++----
 testsuites/libtests/termios01/init.c       | 15 +++++++++++----
 6 files changed, 63 insertions(+), 21 deletions(-)

diff --git a/cpukit/libcsupport/include/rtems/libio_.h b/cpukit/libcsupport/include/rtems/libio_.h
index dc1fe98..cf4ea78 100644
--- a/cpukit/libcsupport/include/rtems/libio_.h
+++ b/cpukit/libcsupport/include/rtems/libio_.h
@@ -66,7 +66,8 @@ extern rtems_id                          rtems_libio_semaphore;
 
 extern const uint32_t rtems_libio_number_iops;
 extern rtems_libio_t rtems_libio_iops[];
-extern rtems_libio_t *rtems_libio_iop_freelist;
+extern void *rtems_libio_iop_free_head;
+extern void **rtems_libio_iop_free_tail;
 
 extern const rtems_filesystem_file_handlers_r rtems_filesystem_null_handlers;
 
diff --git a/cpukit/libcsupport/src/libio.c b/cpukit/libcsupport/src/libio.c
index 26fa7b2..0abb082 100644
--- a/cpukit/libcsupport/src/libio.c
+++ b/cpukit/libcsupport/src/libio.c
@@ -108,14 +108,21 @@ int rtems_libio_to_fcntl_flags( unsigned int flags )
 
 rtems_libio_t *rtems_libio_allocate( void )
 {
-  rtems_libio_t *iop = NULL;
+  rtems_libio_t *iop;
 
   rtems_libio_lock();
 
-  if (rtems_libio_iop_freelist) {
-    iop = rtems_libio_iop_freelist;
-    rtems_libio_iop_freelist = iop->data1;
-    memset( iop, 0, sizeof(*iop) );
+  iop = rtems_libio_iop_free_head;
+
+  if ( iop != NULL ) {
+    void *next;
+
+    next = iop->data1;
+    rtems_libio_iop_free_head = next;
+
+    if ( next == NULL ) {
+      rtems_libio_iop_free_tail = &rtems_libio_iop_free_head;
+    }
   }
 
   rtems_libio_unlock();
@@ -131,9 +138,9 @@ void rtems_libio_free(
 
   rtems_libio_lock();
 
-    iop->flags = 0;
-    iop->data1 = rtems_libio_iop_freelist;
-    rtems_libio_iop_freelist = iop;
+  iop = memset( iop, 0, sizeof( *iop ) );
+  *rtems_libio_iop_free_tail = iop;
+  rtems_libio_iop_free_tail = &iop->data1;
 
   rtems_libio_unlock();
 }
diff --git a/cpukit/libcsupport/src/libio_init.c b/cpukit/libcsupport/src/libio_init.c
index 3fa9e09..9c24b14 100644
--- a/cpukit/libcsupport/src/libio_init.c
+++ b/cpukit/libcsupport/src/libio_init.c
@@ -38,8 +38,11 @@
  *  File descriptor Table Information
  */
 
-rtems_id           rtems_libio_semaphore;
-rtems_libio_t     *rtems_libio_iop_freelist;
+rtems_id rtems_libio_semaphore;
+
+void *rtems_libio_iop_free_head;
+
+void **rtems_libio_iop_free_tail = &rtems_libio_iop_free_head;
 
 static void rtems_libio_init( void )
 {
@@ -50,10 +53,11 @@ static void rtems_libio_init( void )
 
     if (rtems_libio_number_iops > 0)
     {
-        iop = rtems_libio_iop_freelist = &rtems_libio_iops[0];
+        iop = rtems_libio_iop_free_head = &rtems_libio_iops[0];
         for (i = 0 ; (i + 1) < rtems_libio_number_iops ; i++, iop++)
           iop->data1 = iop + 1;
         iop->data1 = NULL;
+        rtems_libio_iop_free_tail = &iop->data1;
     }
 
   /*
diff --git a/cpukit/libcsupport/src/resource_snapshot.c b/cpukit/libcsupport/src/resource_snapshot.c
index d0dda9f..9e026ff 100644
--- a/cpukit/libcsupport/src/resource_snapshot.c
+++ b/cpukit/libcsupport/src/resource_snapshot.c
@@ -87,7 +87,7 @@ static int open_files(void)
 
   rtems_libio_lock();
 
-  iop = rtems_libio_iop_freelist;
+  iop = rtems_libio_iop_free_head;
   while (iop != NULL) {
     ++free_count;
 
diff --git a/testsuites/fstests/fsclose01/init.c b/testsuites/fstests/fsclose01/init.c
index a9de652..77df082 100644
--- a/testsuites/fstests/fsclose01/init.c
+++ b/testsuites/fstests/fsclose01/init.c
@@ -405,6 +405,27 @@ static void worker_task(rtems_task_argument arg)
   }
 }
 
+static void test_fd_free_fifo(const char *path)
+{
+  int a;
+  int b;
+  int rv;
+
+  a = open(path, O_RDWR);
+  rtems_test_assert(a >= 0);
+
+  rv = close(a);
+  rtems_test_assert(rv == 0);
+
+  b = open(path, O_RDWR);
+  rtems_test_assert(b >= 0);
+
+  rv = close(b);
+  rtems_test_assert(rv == 0);
+
+  rtems_test_assert(a != b);
+}
+
 static void test(test_context *ctx)
 {
   const char *path = "generic";
@@ -420,6 +441,8 @@ static void test(test_context *ctx)
   );
   rtems_test_assert(rv == 0);
 
+  test_fd_free_fifo(path);
+
   sc = rtems_task_create(
     rtems_build_name('W', 'O', 'R', 'K'),
     1,
@@ -469,15 +492,15 @@ static void test(test_context *ctx)
   rv = unlink(path);
   rtems_test_assert(rv == 0);
 
-  rtems_test_assert(ctx->close_count == 15);
+  rtems_test_assert(ctx->close_count == 17);
   rtems_test_assert(ctx->fcntl_count == 1);
   rtems_test_assert(ctx->fdatasync_count == 1);
-  rtems_test_assert(ctx->fstat_count == 38);
+  rtems_test_assert(ctx->fstat_count == 42);
   rtems_test_assert(ctx->fsync_count == 1);
   rtems_test_assert(ctx->ftruncate_count == 1);
   rtems_test_assert(ctx->ioctl_count == 1);
   rtems_test_assert(ctx->lseek_count == 1);
-  rtems_test_assert(ctx->open_count == 15);
+  rtems_test_assert(ctx->open_count == 17);
   rtems_test_assert(ctx->read_count == 1);
   rtems_test_assert(ctx->readv_count == 1);
   rtems_test_assert(ctx->write_count == 1);
@@ -495,7 +518,7 @@ static void Init(rtems_task_argument arg)
 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
 #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
 
-#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
+#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5
 
 #define CONFIGURE_MAXIMUM_TASKS 2
 
diff --git a/testsuites/libtests/termios01/init.c b/testsuites/libtests/termios01/init.c
index 562b252..a892762 100644
--- a/testsuites/libtests/termios01/init.c
+++ b/testsuites/libtests/termios01/init.c
@@ -623,16 +623,23 @@ static rtems_status_code test_early_device_install(
   rtems_status_code sc;
   int fd;
   int rv;
+  int i;
 
   rtems_resource_snapshot_take( &snapshot );
 
   sc = rtems_termios_device_install( &dev[0], &handler, NULL, NULL );
   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
 
-  errno = 0;
-  fd = open( &dev[0], O_RDWR );
-  rtems_test_assert( fd == -1 );
-  rtems_test_assert( errno == ENXIO );
+  /*
+   * The loop ensures that file descriptor 0 is the first free file descriptor
+   * after this test case.
+   */
+  for (i = 0; i < 4; ++i) {
+    errno = 0;
+    fd = open( &dev[0], O_RDWR );
+    rtems_test_assert( fd == -1 );
+    rtems_test_assert( errno == ENXIO );
+  }
 
   rv = unlink( &dev[0] );
   rtems_test_assert( rv == 0 );



More information about the vc mailing list