Selaa lähdekoodia

merge some semaphores to one mutex in DFS

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@135 bbd45198-f89e-11dd-88c7-29a3b14d5316
bernard.xiong 15 vuotta sitten
vanhempi
commit
428b10bb91

+ 3 - 4
filesystem/dfs/include/dfs_fs.h

@@ -78,10 +78,9 @@ int dfs_unmount(const char *specialfile);
 extern struct dfs_filesystem_operation* filesystem_operation_table[];
 extern struct dfs_filesystem filesystem_table[];
 
-extern rt_sem_t filesystem_table_lock;
-extern rt_sem_t filesystem_operation_table_lock;
-
 extern char working_directory[];
-extern rt_sem_t working_directory_lock;
+
+void dfs_lock(void);
+void dfs_unlock(void);
 
 #endif

+ 42 - 35
filesystem/dfs/src/dfs_fs.c

@@ -32,16 +32,22 @@
 */
 int dfs_register(struct dfs_filesystem_operation* ops)
 {
-    int index;
+    int index, result;
+
+	result = 0;
 
-    rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
+	/* lock filesystem */
+	dfs_lock();
 
     /* check if this filesystem was already registered */
     for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
     {
         if (filesystem_operation_table[index] != RT_NULL &&
                 strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
+        {
+        	result = -1;
             goto err;
+        }
     }
 
     /* find out an empty filesystem type entry */
@@ -49,17 +55,18 @@ int dfs_register(struct dfs_filesystem_operation* ops)
             index++) ;
 
     /* filesystem type table full */
-    if (index == DFS_FILESYSTEM_TYPES_MAX) goto err;
+    if (index == DFS_FILESYSTEM_TYPES_MAX)
+	{
+		result = -1;
+		goto err;
+    }
 
     /* save the filesystem's operations */
     filesystem_operation_table[index] = ops;
 
-    rt_sem_release(filesystem_operation_table_lock);
-    return 0;
-
 err:
-    rt_sem_release(filesystem_operation_table_lock);
-    return -1;
+	dfs_unlock();
+    return result;
 }
 
 /*
@@ -80,8 +87,8 @@ struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
     fs = RT_NULL;
     prefixlen = 0;
 
-    /* lock filesystem table */
-    rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
+    /* lock filesystem */
+	dfs_lock();
 
     /* lookup it in the filesystem table */
     for (index = 0; index < DFS_FILESYSTEMS_MAX + 1; index++)
@@ -97,7 +104,8 @@ struct dfs_filesystem* dfs_filesystem_lookup(const char *path)
         }
     }
 
-    rt_sem_release(filesystem_table_lock);
+	dfs_unlock();
+
     return fs;
 }
 
@@ -191,7 +199,7 @@ int dfs_mount(const char* device_name, const char* path,
     struct dfs_filesystem_operation* ops;
     struct dfs_filesystem* fs;
     char *fullpath=RT_NULL;
-#ifdef RT_USING_WORKDIR
+#ifdef DFS_USING_WORKDIR
     char full_path[DFS_PATH_MAX + 1];
 #endif
     rt_device_t dev_id;
@@ -207,7 +215,7 @@ int dfs_mount(const char* device_name, const char* path,
     }
 
     /* find out specific filesystem */
-    rt_sem_take(filesystem_operation_table_lock, RT_WAITING_FOREVER);
+	dfs_lock();
     for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ )
     {
         if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)break;
@@ -217,22 +225,22 @@ int dfs_mount(const char* device_name, const char* path,
     if ( index == DFS_FILESYSTEM_TYPES_MAX )
     {
         rt_set_errno(-DFS_STATUS_ENODEV);
-        rt_sem_release(filesystem_operation_table_lock);
+        dfs_unlock();
         return -1;
     }
     ops = filesystem_operation_table[index];
-    rt_sem_release(filesystem_operation_table_lock);
+    dfs_unlock();
 
     /* make full path for special file */
     fullpath = (char*)path;
     if ( fullpath[0] != '/') /* not an abstract path */
     {
-#ifdef RT_USING_WORKDIR
+#ifdef DFS_USING_WORKDIR
         /* build full path */
         fullpath = full_path;
-        rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+        dfs_lock();
         build_fullpath(working_directory, path, fullpath);
-        rt_sem_release(working_directory_lock);
+        dfs_unlock();
 #else
         rt_set_errno(-DFS_STATUS_ENOTDIR);
         return -1;
@@ -253,7 +261,7 @@ int dfs_mount(const char* device_name, const char* path,
     }
 
     /* check whether the file system mounted or not */
-    rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
+	dfs_lock();
     for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
     {
         if ( filesystem_table[index].ops != RT_NULL &&
@@ -279,19 +287,17 @@ int dfs_mount(const char* device_name, const char* path,
     fs->ops = ops;
     fs->dev_id = dev_id;
     /* release filesystem_table lock */
-    rt_sem_release(filesystem_table_lock);
+	dfs_unlock();
 
     /* open device, but do not check the status of device */
     rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
 
     if ( ops->mount == RT_NULL ) /* there is no mount implementation */
     {
-        rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
-
+    	dfs_lock();
         /* clear filesystem table entry */
         rt_memset(fs, 0, sizeof(struct dfs_filesystem));
-
-        rt_sem_release(filesystem_table_lock);
+		dfs_unlock();
 
         rt_set_errno(-DFS_STATUS_ENOSYS);
         return -1;
@@ -300,7 +306,7 @@ int dfs_mount(const char* device_name, const char* path,
     else if ( ops->mount(fs) < 0 )
     {
         /* mount failed */
-        rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
+		dfs_lock();
 
         /* close device */
         rt_device_close(fs->dev_id);
@@ -308,14 +314,14 @@ int dfs_mount(const char* device_name, const char* path,
         /* clear filesystem table entry */
         rt_memset(fs, 0, sizeof(struct dfs_filesystem));
 
-        rt_sem_release(filesystem_table_lock);
+		dfs_unlock();
         return -1;
     }
 
     return 0;
 
 err1:
-    rt_sem_release(filesystem_table_lock);
+    dfs_unlock();
     return -1;
 }
 
@@ -334,7 +340,7 @@ err1:
 int dfs_unmount(const char *specialfile)
 {
     char *fullpath;
-#ifdef RT_USING_WORKDIR
+#ifdef DFS_USING_WORKDIR
     char full_path[DFS_PATH_MAX + 1];
 #endif
     struct dfs_filesystem* fs = RT_NULL;
@@ -342,20 +348,20 @@ int dfs_unmount(const char *specialfile)
     fullpath = (char*)specialfile;
     if ( fullpath[0] != '/')
     {
-#ifdef RT_USING_WORKDIR
+#ifdef DFS_USING_WORKDIR
         /* build full path */
         fullpath = full_path;
-        rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+		dfs_lock();
         build_fullpath(working_directory, specialfile, fullpath);
-        rt_sem_release(working_directory_lock);
+        dfs_unlock();
 #else
         rt_set_errno(-DFS_STATUS_ENOTDIR);
         return -1;
 #endif
     }
 
-    /* lock filesystem table */
-    rt_sem_take(filesystem_table_lock, RT_WAITING_FOREVER);
+    /* lock filesystem */
+    dfs_lock();
 
     fs = dfs_filesystem_lookup(fullpath);
     if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
@@ -369,11 +375,12 @@ int dfs_unmount(const char *specialfile)
     /* clear this filesystem table entry */
     rt_memset(fs, 0, sizeof(struct dfs_filesystem));
 
-    rt_sem_release(filesystem_table_lock);
+	dfs_unlock();
 
     return 0;
 
 err1:
-    rt_sem_release(filesystem_table_lock);
+	dfs_unlock();
+
     return -1;
 }

+ 25 - 29
filesystem/dfs/src/dfs_init.c

@@ -25,16 +25,18 @@
 struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX + 1];
 struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX + 1];
 
-rt_sem_t filesystem_table_lock;
-rt_sem_t filesystem_operation_table_lock;
+/* device filesystem lock */
+struct rt_mutex dlock;
 
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 char working_directory[DFS_PATH_MAX + 1];
-rt_sem_t working_directory_lock;
 #endif
 
-struct dfs_fd fd_table[DFS_FD_MAX + 1];
-rt_sem_t  fd_table_lock;
+#ifdef DFS_USING_STDIO
+struct dfs_fd fd_table[3 + DFS_FD_MAX];
+#else
+struct dfs_fd fd_table[DFS_FD_MAX];
+#endif
 
 /*
 +------------------------------------------------------------------------------
@@ -53,40 +55,34 @@ void dfs_init()
 	for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX + 1; index++) 
 		filesystem_operation_table[index] = RT_NULL;
 
-	/* create filesystem operations table lock */
-	filesystem_operation_table_lock = rt_sem_create("sem_fot", 1, RT_IPC_FLAG_FIFO); 
+	/* create device filesystem lock */
+	rt_mutex_init(&dlock, "dlock", RT_IPC_FLAG_FIFO);
 
 	/* clear filesystem table */
-	for (index = 0; index < DFS_FILESYSTEMS_MAX + 1; index++)
-	{
-		rt_memset(filesystem_table[index].path, 0, DFS_PATH_MAX + 1);
-
-		filesystem_table[index].ops = RT_NULL;
-		filesystem_table[index].dev_id = 0;
-		filesystem_table[index].block_id = 0;
-		filesystem_table[index].data = RT_NULL;
-	}
-
-	/* create filesystem table lock */
-	filesystem_table_lock = rt_sem_create("sem_fst", 1, RT_IPC_FLAG_FIFO); 
+	rt_memset(filesystem_table, 0, sizeof(filesystem_table));
 
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR	
 	/* set current working directory */
 	strcpy(working_directory, "/");
-	working_directory_lock = rt_sem_create("sem_wd", 1, RT_IPC_FLAG_FIFO); 
 #endif
 
 	/* clean fd table */
-	for ( index = 0; index < DFS_FD_MAX; index ++)
-	{
-		rt_memset(&fd_table[index], 0, sizeof(struct dfs_fd));
-	}
-
-	/* create fd table lock */
-	fd_table_lock = rt_sem_create("sem_fdt", 1, RT_IPC_FLAG_FIFO); 
+	rt_memset(fd_table, 0, sizeof(fd_table));
 
 #if defined(RT_USING_FINSH) && !defined(FINSH_USING_SYMTAB)
 	dfs_export_finsh();
 #endif
 }
 
+void dfs_lock()
+{
+	rt_err_t result;
+
+	result = rt_mutex_take(&dlock, RT_WAITING_FOREVER);
+	RT_ASSERT(result == RT_EOK);
+}
+
+void dfs_unlock()
+{
+	rt_mutex_release(&dlock);
+}

+ 13 - 9
filesystem/dfs/src/dfs_posix.c

@@ -12,6 +12,8 @@
 | 2009-05-27     Yi.qiu       The first version.                     
 +------------------------------------------------------------------------------
 */
+#include <string.h>
+#include <dfs_util.h>
 #include <dfs_posix.h>
 
 /*
@@ -521,20 +523,20 @@ int chdir(const char *path)
 	{
 		/* build full path */
 		fullpath = full_path;
-#ifdef RT_USING_WORKDIR	
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+#ifdef DFS_USING_WORKDIR
+		dfs_lock();
 		build_fullpath(working_directory, path, fullpath);
 		strcpy(working_directory, fullpath);
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #endif		
 	}
 	else
 	{
-#ifdef RT_USING_WORKDIR	
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+#ifdef DFS_USING_WORKDIR
+		dfs_lock();
 		rt_strncpy(working_directory, path, strlen(path) + 1);
 		working_directory[strlen(path)] = '\0';
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #endif		
 	}
 	
@@ -554,10 +556,12 @@ int chdir(const char *path)
 */
 char* getcwd(char *buf, rt_size_t size)
 {
-#ifdef RT_USING_WORKDIR	
-	rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+#ifdef DFS_USING_WORKDIR
+	dfs_lock();
 	rt_strncpy(buf, working_directory, size);
-	rt_sem_release(working_directory_lock);
+	dfs_unlock();
+#else
+	rt_kprintf("WARNING: not support working directory\n");
 #endif
 	return buf;
 }

+ 44 - 43
filesystem/dfs/src/dfs_raw.c

@@ -16,8 +16,7 @@
 #include <dfs_util.h>
 #include <string.h>
 
-extern struct dfs_fd fd_table[DFS_FD_MAX + 1];
-extern rt_sem_t  fd_table_lock;
+extern struct dfs_fd fd_table[];
 
 /*
 +------------------------------------------------------------------------------
@@ -35,26 +34,32 @@ int fd_new(void)
 	struct dfs_fd* d;
 	int idx;
 
-	rt_sem_take(fd_table_lock, RT_WAITING_FOREVER);
+	/* lock filesystem */
+	dfs_lock();
 
 	/* find an empty fd entry */
-#ifdef RT_USING_STDIO
-	for (idx = 3; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
+#ifdef DFS_USING_STDIO
+	for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++);
 #else
 	for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
 #endif
 
 	/* can't find an empty fd entry */
+#ifdef DFS_USING_STDIO
+	if (idx == DFS_FD_MAX + 3)
+#else
 	if (idx == DFS_FD_MAX)
+#endif
 	{
-		rt_sem_release(fd_table_lock);
-		return -1;
+		idx = -1;
+		goto __result;
 	}
 
 	d = &(fd_table[idx]);
 	d->ref_count = 1;
-	rt_sem_release(fd_table_lock);
 
+__result:
+	dfs_unlock();
 	return idx;
 }
 
@@ -73,17 +78,17 @@ struct dfs_fd* fd_get(int fd)
 {
 	struct dfs_fd* d;
 
-#ifdef RT_USING_STDIO
-	if ( fd < 3 || fd > DFS_FD_MAX ) return RT_NULL;
+#ifdef DFS_USING_STDIO
+	if ( fd < 3 || fd > DFS_FD_MAX + 3) return RT_NULL;
 #else
 	if ( fd < 0 || fd > DFS_FD_MAX ) return RT_NULL;
 #endif
 
 	d = &fd_table[fd];
 
-	rt_sem_take(fd_table_lock, RT_WAITING_FOREVER);
+	dfs_lock();
 	d->ref_count ++;
-	rt_sem_release(fd_table_lock);
+	dfs_unlock();
 
 	return d;
 }
@@ -101,14 +106,14 @@ struct dfs_fd* fd_get(int fd)
 */
 void fd_put(struct dfs_fd* fd)
 {
-	rt_sem_take(fd_table_lock, RT_WAITING_FOREVER);
+	dfs_lock();
 	fd->ref_count --;
 	/* clear this fd entry */
 	if ( fd->ref_count == 0 )
 	{
 		rt_memset(fd, 0, sizeof(struct dfs_fd));
 	}
-	rt_sem_release(fd_table_lock);
+	dfs_unlock();
 };
 
 /*
@@ -126,7 +131,7 @@ int dfile_raw_open(struct dfs_fd* fd, const char *path, int flags)
 {
 	struct dfs_filesystem* fs;
 	char *fullpath;
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 	char full_path[DFS_PATH_MAX + 1];
 #endif
 	int fspathlen, result;
@@ -138,12 +143,12 @@ int dfile_raw_open(struct dfs_fd* fd, const char *path, int flags)
 	fullpath = (char*)path;
 	if ( fullpath[0] != '/')
 	{
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 		/* build full path */
 		fullpath = &full_path[0];
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+		dfs_lock();
 		build_fullpath(working_directory, path, fullpath);
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #else
 #ifdef RT_USING_FINSH
 		rt_kprintf("bad filename");
@@ -341,7 +346,7 @@ int dfile_raw_unlink(const char *path)
 {
 	struct dfs_filesystem* fs;
 	char *fullpath, *real_path, search_path[DFS_PATH_MAX + 1];
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 	char full_path[DFS_PATH_MAX+1];
 #endif
 	struct dfs_fd* fd;
@@ -351,12 +356,12 @@ int dfile_raw_unlink(const char *path)
 	fullpath = (char*)path;
 	if ( fullpath[0] != '/')
 	{
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 		/* build full path */
 		fullpath = full_path;
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+		dfs_lock();
 		build_fullpath(working_directory, path, fullpath);
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #else
 #ifdef RT_USING_FINSH
 		rt_kprintf("bad filename");
@@ -368,7 +373,7 @@ int dfile_raw_unlink(const char *path)
 	if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) return -DFS_STATUS_ENOENT;
 
 	/* Check whether file is already open */
-	rt_sem_take(fd_table_lock, RT_WAITING_FOREVER);
+	dfs_lock();
 	for (index = 0; index < DFS_FD_MAX; index++)
 	{
 		fd = &(fd_table[index]);
@@ -377,11 +382,11 @@ int dfile_raw_unlink(const char *path)
 		build_fullpath(fd->fs->path, fd->path, search_path);
 		if (strcmp(fullpath, search_path) == 0)
 		{
-			rt_sem_release(fd_table_lock);
+			dfs_unlock();
 			return -DFS_STATUS_EEXIST;
 		}
 	}
-	rt_sem_release(fd_table_lock);
+	dfs_unlock();
 
 	fspathlen = strlen(fs->path);
 	real_path = search_path;
@@ -452,7 +457,7 @@ int dfile_raw_stat(const char *path, struct dfs_stat *buf)
 {
 	struct dfs_filesystem* fs;
 	char* fullpath, real_path[DFS_PATH_MAX + 1];
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 	char full_path[DFS_PATH_MAX + 1];
 #endif
 	int fspathlen;
@@ -460,15 +465,15 @@ int dfile_raw_stat(const char *path, struct dfs_stat *buf)
 	fullpath = (char*)path;
 	if ( fullpath[0] != '/' )
 	{
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 		/* build full path */
 		fullpath = full_path;
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+		dfs_lock();
 		build_fullpath(working_directory, path, fullpath);
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #else
 #ifdef RT_USING_FINSH
-		rt_kprintf("bad filename");
+		rt_kprintf("not support working directory, bad filename\n");
 #endif
 		return -1;
 #endif
@@ -509,42 +514,38 @@ int dfile_raw_rename(const char* oldpath, const char* newpath)
 {
 	struct dfs_filesystem *oldfs, *newfs;
 	char *oldfullpath, *newfullpath;
-	#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 	/* Change DFS_PATH_MAX to DFS_PATH_MAX + 1, yi.qiu@2008.09.23*/
 	char old_realpath[DFS_PATH_MAX + 1], new_realpath[DFS_PATH_MAX + 1];
-	#endif
+#endif
 
 	oldfullpath = (char*)oldpath;
 	newfullpath = (char*)newpath;
 
 	if ( oldfullpath[0] != '/' )
 	{
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 		/* build full path */
 		oldfullpath = old_realpath;
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+		dfs_lock();
 		build_fullpath(working_directory, oldpath, oldfullpath);
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #else
-#ifdef RT_USING_FINSH
-		rt_kprintf("bad filename");
-#endif
+		rt_kprintf("bad filename\n");
 		return -1;
 #endif
 	}
 
 	if ( newfullpath[0] != '/' )
 	{
-#ifdef RT_USING_WORKDIR	
+#ifdef DFS_USING_WORKDIR
 		/* build full path */
 		newfullpath = new_realpath;
-		rt_sem_take(working_directory_lock, RT_WAITING_FOREVER);
+		dfs_lock();
 		build_fullpath(working_directory, newpath, newfullpath);
-		rt_sem_release(working_directory_lock);
+		dfs_unlock();
 #else
-#ifdef RT_USING_FINSH
 		rt_kprintf("bad filename");
-#endif
 		return -1;
 #endif
 	}