[rtems commit] NFS: Use self-contained recursive mutex

Sebastian Huber sebh at rtems.org
Fri Feb 2 14:23:27 UTC 2018


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Dec 15 06:20:09 2017 +0100

NFS: Use self-contained recursive mutex

Update #2843.

---

 cpukit/include/rtems/confdefs.h        | 29 ++---------
 cpukit/libfs/src/nfsclient/src/nfs.c   | 90 +++++++++-------------------------
 cpukit/libfs/src/nfsclient/src/rpcio.c | 76 +++++++---------------------
 3 files changed, 45 insertions(+), 150 deletions(-)

diff --git a/cpukit/include/rtems/confdefs.h b/cpukit/include/rtems/confdefs.h
index 0d00726..75ea142 100755
--- a/cpukit/include/rtems/confdefs.h
+++ b/cpukit/include/rtems/confdefs.h
@@ -390,14 +390,8 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
 #if !defined(CONFIGURE_FILESYSTEM_ENTRY_NFS) && \
     defined(CONFIGURE_FILESYSTEM_NFS)
   #include <librtemsNfs.h>
-  #if !defined(CONFIGURE_MAXIMUM_NFS_MOUNTS)
-    #define CONFIGURE_MAXIMUM_NFS_MOUNTS 1
-  #endif
   #define CONFIGURE_FILESYSTEM_ENTRY_NFS \
     { RTEMS_FILESYSTEM_TYPE_NFS, rtems_nfs_initialize }
-  #define _CONFIGURE_SEMAPHORES_FOR_NFS ((CONFIGURE_MAXIMUM_NFS_MOUNTS * 2) + 1)
-#else
-  #define _CONFIGURE_SEMAPHORES_FOR_NFS 0
 #endif
 
 /**
@@ -430,12 +424,6 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
     { RTEMS_FILESYSTEM_TYPE_JFFS2, rtems_jffs2_initialize }
 #endif
 
-/**
- * This computes the number of semaphores required for the various
- * file systems including the FIFO plugin to the IMFS.
- */
-#define _CONFIGURE_SEMAPHORES_FOR_FILE_SYSTEMS _CONFIGURE_SEMAPHORES_FOR_NFS
-
 #ifdef CONFIGURE_INIT
 
   /**
@@ -2023,15 +2011,6 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
   #endif
 
   /*
-   * This macro is calculated to specify the number of Classic API
-   * semaphores required by the application and configured RTEMS
-   * capabilities.
-   */
-  #define _CONFIGURE_SEMAPHORES \
-    (CONFIGURE_MAXIMUM_SEMAPHORES + \
-      _CONFIGURE_SEMAPHORES_FOR_FILE_SYSTEMS)
-
-  /*
    * This macro is calculated to specify the memory required for
    * Classic API Semaphores using MRSP. This is only available in
    * SMP configurations.
@@ -2054,7 +2033,7 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
    * If there are no user or support semaphores defined, then we can assume
    * that no memory need be allocated at all for semaphores.
    */
-  #if _CONFIGURE_SEMAPHORES == 0
+  #if CONFIGURE_MAXIMUM_SEMAPHORES == 0
     #define _CONFIGURE_MEMORY_FOR_SEMAPHORES(_semaphores) 0
   #else
     #define _CONFIGURE_MEMORY_FOR_SEMAPHORES(_semaphores) \
@@ -2740,7 +2719,7 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
 #define _CONFIGURE_MEMORY_FOR_CLASSIC \
    (_CONFIGURE_MEMORY_FOR_TIMERS(CONFIGURE_MAXIMUM_TIMERS + \
     _CONFIGURE_TIMER_FOR_SHARED_MEMORY_DRIVER ) + \
-   _CONFIGURE_MEMORY_FOR_SEMAPHORES(_CONFIGURE_SEMAPHORES) + \
+   _CONFIGURE_MEMORY_FOR_SEMAPHORES(CONFIGURE_MAXIMUM_SEMAPHORES) + \
    _CONFIGURE_MEMORY_FOR_MESSAGE_QUEUES(CONFIGURE_MAXIMUM_MESSAGE_QUEUES) + \
    _CONFIGURE_MEMORY_FOR_PARTITIONS(CONFIGURE_MAXIMUM_PARTITIONS) + \
    _CONFIGURE_MEMORY_FOR_REGIONS( CONFIGURE_MAXIMUM_REGIONS ) + \
@@ -3029,7 +3008,7 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
   rtems_api_configuration_table Configuration_RTEMS_API = {
     _CONFIGURE_TASKS,
     CONFIGURE_MAXIMUM_TIMERS + _CONFIGURE_TIMER_FOR_SHARED_MEMORY_DRIVER,
-    _CONFIGURE_SEMAPHORES,
+    CONFIGURE_MAXIMUM_SEMAPHORES,
     CONFIGURE_MAXIMUM_MESSAGE_QUEUES,
     CONFIGURE_MAXIMUM_PARTITIONS,
     CONFIGURE_MAXIMUM_REGIONS,
@@ -3292,7 +3271,7 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
     /* Classic API Pieces */
     _CONFIGURE_MEMORY_FOR_TASKS(CONFIGURE_MAXIMUM_TASKS, 0),
     _CONFIGURE_MEMORY_FOR_TIMERS(CONFIGURE_MAXIMUM_TIMERS),
-    _CONFIGURE_MEMORY_FOR_SEMAPHORES(_CONFIGURE_SEMAPHORES),
+    _CONFIGURE_MEMORY_FOR_SEMAPHORES(CONFIGURE_MAXIMUM_SEMAPHORES),
     _CONFIGURE_MEMORY_FOR_MESSAGE_QUEUES(CONFIGURE_MAXIMUM_MESSAGE_QUEUES),
     _CONFIGURE_MEMORY_FOR_PARTITIONS(CONFIGURE_MAXIMUM_PARTITIONS),
     _CONFIGURE_MEMORY_FOR_REGIONS( CONFIGURE_MAXIMUM_REGIONS ),
diff --git a/cpukit/libfs/src/nfsclient/src/nfs.c b/cpukit/libfs/src/nfsclient/src/nfs.c
index ca42264..4eb4249 100644
--- a/cpukit/libfs/src/nfsclient/src/nfs.c
+++ b/cpukit/libfs/src/nfsclient/src/nfs.c
@@ -67,6 +67,7 @@
 #include <rtems/libio.h>
 #include <rtems/libio_.h>
 #include <rtems/seterr.h>
+#include <rtems/thread.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -197,19 +198,9 @@ static struct timeval _nfscalltimeout = { 10, 0 };	/* {secs, us } */
 #define STATIC static
 #endif
 
-#define MUTEX_ATTRIBUTES    (RTEMS_LOCAL           |   \
-                            RTEMS_PRIORITY         |   \
-                            RTEMS_INHERIT_PRIORITY |   \
-                            RTEMS_BINARY_SEMAPHORE)
+#define LOCK(s)		rtems_recursive_mutex_lock(&(s))
 
-#define LOCK(s)		do {                               \
-						rtems_semaphore_obtain((s),    \
-									RTEMS_WAIT,        \
-									RTEMS_NO_TIMEOUT); \
-					} while (0)
-
-#define UNLOCK(s)	do { rtems_semaphore_release((s)); \
-					} while (0)
+#define UNLOCK(s)	rtems_recursive_mutex_unlock(&(s))
 
 RTEMS_INTERRUPT_LOCK_DEFINE(static, nfs_global_lock, "NFS")
 
@@ -1036,6 +1027,9 @@ rtems_status_code status;
 	if (0==bigPoolDepth)
 		bigPoolDepth   = 10;
 
+	rtems_recursive_mutex_init(&nfsGlob.llock, "NFSl");
+	rtems_recursive_mutex_init(&nfsGlob.lock, "NFSm");
+
 	/* it's crucial to zero out the 'next' pointer
 	 * because it terminates the xdr_entry recursion
 	 *
@@ -1067,26 +1061,6 @@ rtems_status_code status;
 		goto cleanup;
 	}
 
-	status = rtems_semaphore_create(
-		rtems_build_name('N','F','S','l'),
-		1,
-		MUTEX_ATTRIBUTES,
-		0,
-		&nfsGlob.llock);
-	if (status != RTEMS_SUCCESSFUL) {
-		goto cleanup;
-	}
-
-	status = rtems_semaphore_create(
-		rtems_build_name('N','F','S','m'),
-		1,
-		MUTEX_ATTRIBUTES,
-		0,
-		&nfsGlob.lock);
-	if (status != RTEMS_SUCCESSFUL) {
-		goto cleanup;
-	}
-
 	if (sizeof(ino_t) < sizeof(u_int)) {
 		fprintf(stderr,
 			"WARNING: Using 'short st_ino' hits performance and may fail to access/find correct files\n");
@@ -1112,23 +1086,16 @@ nfsCleanup(void)
 {
 int			refuse;
 
-	if (nfsGlob.llock != 0) {
-		LOCK(nfsGlob.llock);
-		if ( (refuse = nfsGlob.num_mounted_fs) ) {
-			fprintf(stderr,"Refuse to unload NFS; %i filesystems still mounted.\n",
-							refuse);
-			nfsMountsShow(stderr);
-			/* yes, printing is slow - but since you try to unload the driver,
-			 * you assume nobody is using NFS, so what if they have to wait?
-			 */
-			UNLOCK(nfsGlob.llock);
-			return -1;
-		}
-	}
-
-	if (nfsGlob.lock != 0) {
-		rtems_semaphore_delete(nfsGlob.lock);
-		nfsGlob.lock = 0;
+	LOCK(nfsGlob.llock);
+	if ( (refuse = nfsGlob.num_mounted_fs) ) {
+		fprintf(stderr,"Refuse to unload NFS; %i filesystems still mounted.\n",
+						refuse);
+		nfsMountsShow(stderr);
+		/* yes, printing is slow - but since you try to unload the driver,
+		 * you assume nobody is using NFS, so what if they have to wait?
+		 */
+		UNLOCK(nfsGlob.llock);
+		return -1;
 	}
 
 	if (nfsGlob.smallPool != NULL) {
@@ -1146,10 +1113,10 @@ int			refuse;
 		nfsGlob.nfs_major = 0xffffffff;
 	}
 
-	if (nfsGlob.llock != 0) {
-		rtems_semaphore_delete(nfsGlob.llock);
-		nfsGlob.llock = 0;
-	}
+	UNLOCK(nfsGlob.llock);
+
+	rtems_recursive_mutex_destroy(&nfsGlob.lock);
+	rtems_recursive_mutex_destroy(&nfsGlob.llock);
 
 	return 0;
 }
@@ -3138,7 +3105,7 @@ rtems_filesystem_location_info_t	old;
 		/* must restore the cwd because 'freenode' will be called on it */
 		rtems_filesystem_current->location = old;
 	}
-	rtems_semaphore_release(rpa->sync);
+	rtems_binary_semaphore_post(&rpa->sync);
 	rtems_task_delete(RTEMS_SELF);
 }
 
@@ -3165,15 +3132,7 @@ rtems_status_code	status;
 	arg.len  = len;
 	arg.sync = 0;
 
-	status = rtems_semaphore_create(
-					rtems_build_name('r','e','s','s'),
-					0,
-					RTEMS_SIMPLE_BINARY_SEMAPHORE,
-					0,
-					&arg.sync);
-
-	if (RTEMS_SUCCESSFUL != status)
-		goto cleanup;
+	rtems_binary_semaphore_init(&arg.sync, "NFSress");
 
 	rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &pri);
 
@@ -3197,13 +3156,12 @@ rtems_status_code	status;
 
 
 	/* synchronize with the helper task */
-	rtems_semaphore_obtain(arg.sync, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
+	rtems_binary_semaphore_wait(&arg.sync);
 
 	status = arg.status;
 
 cleanup:
-	if (arg.sync)
-		rtems_semaphore_delete(arg.sync);
+	rtems_binary_semaphore_destroy(&arg.sync);
 
 	return status;
 }
diff --git a/cpukit/libfs/src/nfsclient/src/rpcio.c b/cpukit/libfs/src/nfsclient/src/rpcio.c
index 56e3341..f10c0bd 100644
--- a/cpukit/libfs/src/nfsclient/src/rpcio.c
+++ b/cpukit/libfs/src/nfsclient/src/rpcio.c
@@ -72,6 +72,7 @@
 #include <rtems.h>
 #include <rtems/error.h>
 #include <rtems/rtems_bsdnet.h>
+#include <rtems/thread.h>
 #include <stdlib.h>
 #include <time.h>
 #include <rpc/rpc.h>
@@ -81,6 +82,7 @@
 #include <assert.h>
 #include <stdio.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <string.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -199,50 +201,13 @@ static struct timeval _rpc_default_timeout = { 10 /* secs */, 0 /* usecs */ };
 #define XACT_HASH_MSK	((XACT_HASHS)-1)	/* mask to extract the hash index from a RPC-XID */
 
 
-#define MU_LOCK(mutex)		do { 							\
-							assert(							\
-								RTEMS_SUCCESSFUL ==			\
-								rtems_semaphore_obtain(		\
-										(mutex),			\
-										RTEMS_WAIT,			\
-										RTEMS_NO_TIMEOUT	\
-										) );				\
-							} while(0)
-
-#define MU_UNLOCK(mutex)	do {							\
-							assert(							\
-								RTEMS_SUCCESSFUL ==			\
-								rtems_semaphore_release(	\
-										(mutex)				\
-										) );				\
-							} while(0)
-
-#define MU_CREAT(pmutex)	do {							\
-							assert(							\
-								RTEMS_SUCCESSFUL ==			\
-								rtems_semaphore_create(		\
-										rtems_build_name(	\
-											'R','P','C','l'	\
-											),				\
-										1,					\
-										MUTEX_ATTRIBUTES,	\
-										0,					\
-										(pmutex)) );		\
-							} while (0)
-
-
-#define MU_DESTROY(mutex)	do {							\
-							assert(							\
-								RTEMS_SUCCESSFUL ==			\
-								rtems_semaphore_delete(		\
-										mutex				\
-										) );				\
-							} while (0)
-
-#define MUTEX_ATTRIBUTES	(RTEMS_LOCAL           | 		\
-			   				RTEMS_PRIORITY         | 		\
-			   				RTEMS_INHERIT_PRIORITY | 		\
-						   	RTEMS_BINARY_SEMAPHORE)
+#define MU_LOCK(mutex)		rtems_recursive_mutex_lock(&(mutex))
+
+#define MU_UNLOCK(mutex)	rtems_recursive_mutex_unlock(&(mutex))
+
+#define MU_CREAT(pmutex)	rtems_recursive_mutex_init((pmutex), "RPCl")
+
+#define MU_DESTROY(mutex)	rtems_recursive_mutex_destroy(&(mutex))
 
 #define FIRST_ATTEMPT		0x88888888 /* some time that is never reached */
 
@@ -269,7 +234,7 @@ typedef struct RpcUdpServerRec_ {
 		struct sockaddr     sa;
 		}					addr;
 		AUTH				*auth;
-		rtems_id			authlock;		/* must MUTEX the auth object - it's not clear
+		rtems_recursive_mutex			authlock;		/* must MUTEX the auth object - it's not clear
 											 *  what is better:
 											 *   1 having one (MUTEXed) auth per server
 											 *	   who is shared among all transactions
@@ -397,10 +362,10 @@ static rtems_id			msgQ    = 0;		/* message queue where the daemon picks up
 											 * requests
 											 */
 #ifndef NDEBUG
-static rtems_id			llock	= 0;		/* MUTEX protecting the server list */
-static rtems_id			hlock	= 0;		/* MUTEX protecting the hash table and the list of servers */
+static rtems_recursive_mutex	llock;		/* MUTEX protecting the server list */
+static rtems_recursive_mutex	hlock;		/* MUTEX protecting the hash table and the list of servers */
 #endif
-static rtems_id			fini	= 0;		/* a synchronization semaphore we use during
+static rtems_binary_semaphore	fini	= RTEMS_BINARY_SEMAPHORE_INITIALIZER("RPCf");	/* a synchronization semaphore we use during
 											 * module cleanup / driver unloading
 											 */
 static rtems_interval	ticksPerSec;		/* cached system clock rate (WHO IS ASSUMED NOT
@@ -1021,20 +986,13 @@ struct sockwakeup	wkup;
 int
 rpcUdpCleanup(void)
 {
-	rtems_semaphore_create(
-			rtems_build_name('R','P','C','f'),
-			0,
-			RTEMS_DEFAULT_ATTRIBUTES,
-			0,
-			&fini);
 	rtems_event_send(rpciod, RPCIOD_KILL_EVENT);
 	/* synchronize with daemon */
-	rtems_semaphore_obtain(fini, RTEMS_WAIT, 5*ticksPerSec);
+	rtems_binary_semaphore_wait_timed_ticks(&fini, 5*ticksPerSec);
 	/* if the message queue is still there, something went wrong */
 	if (!msgQ) {
 		rtems_task_delete(rpciod);
 	}
-	rtems_semaphore_delete(fini);
 	return (msgQ !=0);
 }
 
@@ -1228,7 +1186,7 @@ rtems_status_code	status;
 			if (i>=0) {
 				fprintf(stderr,"RPCIO There are still transactions circulating; I refuse to go away\n");
 				fprintf(stderr,"(1st in slot %i)\n",i);
-				rtems_semaphore_release(fini);
+				rtems_binary_semaphore_post(&fini);
 			} else {
 				break;
 			}
@@ -1349,7 +1307,7 @@ rtems_status_code	status;
 					fprintf(stderr,"RPCIO XACT timed out; waking up requestor\n");
 #endif
 					if ( rtems_event_send(xact->requestor, RTEMS_RPC_EVENT) ) {
-						rtems_panic("RPCIO PANIC: requestor id was 0x%08x",
+						rtems_panic("RPCIO PANIC: requestor id was 0x%08" PRIx32,
 									xact->requestor);
 					}
 
@@ -1527,7 +1485,7 @@ rtems_status_code	status;
 
 	fprintf(stderr,"RPC daemon exited...\n");
 
-	rtems_semaphore_release(fini);
+	rtems_binary_semaphore_post(&fini);
 	rtems_task_suspend(RTEMS_SELF);
 }
 



More information about the vc mailing list