Bläddra i källkod

提出resource id功能

shaojinchun 4 år sedan
förälder
incheckning
94847aa980

+ 4 - 0
components/drivers/Kconfig

@@ -4,6 +4,10 @@ config RT_USING_DEVICE_IPC
     bool "Using device drivers IPC"
     default y
 
+config RT_UNAMED_PIPE_NUMBER
+    int "The number of unamed pipe"
+    default 64
+
 if RT_USING_DEVICE_IPC
     config RT_PIPE_BUFSZ
         int "Set pipe buffer size"

+ 20 - 41
components/drivers/src/pipe.c

@@ -16,49 +16,28 @@
 #include <dfs_file.h>
 #include <dfs_posix.h>
 #include <dfs_poll.h>
+#include <resource_id.h>
 
-#define PIPE_ARY_SIZE 1000
-static void *pipe_res[PIPE_ARY_SIZE];
-static int pipe_noused = 0;
-static void **pipe_free = RT_NULL;
-static int pipeno_get(void)
-{
-    rt_base_t level;
+/* check RT_UNAMED_PIPE_NUMBER */
 
-    void **cur;
+#ifndef RT_UNAMED_PIPE_NUMBER
+#define RT_UNAMED_PIPE_NUMBER 64
+#endif
 
-    level = rt_hw_interrupt_disable();
-    if (pipe_free)
-    {
-        cur = pipe_free;
-        pipe_free = (void **)*pipe_free;
-        rt_hw_interrupt_enable(level);
-        return cur - pipe_res;
-    }
-    else if (pipe_noused < PIPE_ARY_SIZE)
-    {
-        cur = &pipe_res[pipe_noused++];
-        rt_hw_interrupt_enable(level);
-        return cur - pipe_res;
-    }
-    rt_hw_interrupt_enable(level);
-    return -1;
-}
+#define BITS(x) _BITS(x)
+#define _BITS(x) (sizeof(#x) - 1)
 
-static void pipeno_put(int no)
+struct check_rt_unamed_pipe_number
 {
-    rt_base_t level;
-    void **cur;
+    /* -4 for "pipe" prefix */
+    /* -1 for '\0' postfix */
+    char _check[RT_NAME_MAX - 4 - 1 - BITS(RT_UNAMED_PIPE_NUMBER)];
+};
 
-    if (no >= 0 && no < PIPE_ARY_SIZE)
-    {
-        level = rt_hw_interrupt_disable();
-        cur = &pipe_res[no];
-        *cur = (void *)pipe_free;
-        pipe_free = cur;
-        rt_hw_interrupt_enable(level);
-    }
-}
+/* check end */
+
+static void *resoure_id[RT_UNAMED_PIPE_NUMBER];
+static resource_id_t id_mgr = RESOURCE_ID_INIT(RT_UNAMED_PIPE_NUMBER, resoure_id);
 
 static int pipe_fops_open(struct dfs_fd *fd)
 {
@@ -472,7 +451,7 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
     {
         rt_mutex_detach(&pipe->lock);
 #ifdef RT_USING_POSIX
-        pipeno_put(pipe->pipeno);
+        resource_id_put(&id_mgr, pipe->pipeno);
 #endif
         rt_free(pipe);
         return RT_NULL;
@@ -500,7 +479,7 @@ int rt_pipe_delete(const char *name)
 
             rt_mutex_detach(&pipe->lock);
 #ifdef RT_USING_POSIX
-            pipeno_put(pipe->pipeno);
+            resource_id_put(&id_mgr, pipe->pipeno);
 #endif
             rt_device_unregister(device);
 
@@ -533,7 +512,7 @@ int pipe(int fildes[2])
     char dev_name[32];
     int pipeno = 0;
 
-    pipeno = pipeno_get();
+    pipeno = resource_id_get(&id_mgr);
     if (pipeno == -1)
     {
         return -1;
@@ -543,7 +522,7 @@ int pipe(int fildes[2])
     pipe = rt_pipe_create(dname, PIPE_BUFSZ);
     if (pipe == RT_NULL)
     {
-        pipeno_put(pipeno);
+        resource_id_put(&id_mgr, pipeno);
         return -1;
     }
 

+ 8 - 0
components/utilities/resource/SConscript

@@ -0,0 +1,8 @@
+from building import *
+
+cwd     = GetCurrentDir()
+src     = Glob('*.c')
+CPPPATH = [cwd]
+group   = DefineGroup('resource', src, depend = [], CPPPATH = CPPPATH)
+
+Return('group')

+ 62 - 0
components/utilities/resource/resource_id.c

@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-08-25     RT-Thread    First version
+ */
+#include <rthw.h>
+#include <rtthread.h>
+#include <resource_id.h>
+
+void resource_id_init(resource_id_t *mgr, int size, void **res)
+{
+    if (mgr)
+    {
+        mgr->size = size;
+        mgr->_res = res;
+        mgr->noused = 0;
+        mgr->_free = RT_NULL;
+    }
+}
+
+int resource_id_get(resource_id_t *mgr)
+{
+    rt_base_t level;
+    void **cur;
+
+    level = rt_hw_interrupt_disable();
+    if (mgr->_free)
+    {
+        cur = mgr->_free;
+        mgr->_free = (void **)*mgr->_free;
+        rt_hw_interrupt_enable(level);
+        return cur - mgr->_res;
+    }
+    else if (mgr->noused < mgr->size)
+    {
+        cur = &mgr->_res[mgr->noused++];
+        rt_hw_interrupt_enable(level);
+        return cur - mgr->_res;
+    }
+    rt_hw_interrupt_enable(level);
+    return -1;
+}
+
+void resource_id_put(resource_id_t *mgr, int no)
+{
+    rt_base_t level;
+    void **cur;
+
+    if (no >= 0 && no < mgr->size)
+    {
+        level = rt_hw_interrupt_disable();
+        cur = &mgr->_res[no];
+        *cur = (void *)mgr->_free;
+        mgr->_free = cur;
+        rt_hw_interrupt_enable(level);
+    }
+}
+

+ 31 - 0
components/utilities/resource/resource_id.h

@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-08-25     RT-Thread    First version
+ */
+
+#ifndef  RESOURCE_ID_H__
+#define  RESOURCE_ID_H__
+
+#include <rthw.h>
+#include <rtthread.h>
+
+#define RESOURCE_ID_INIT(size, pool)  {size, pool, 0, RT_NULL}
+
+typedef struct
+{
+    int size;
+    void **_res;
+    int noused;
+    void **_free;
+} resource_id_t;
+
+void resource_id_init(resource_id_t *mgr, int size, void **res);
+int resource_id_get(resource_id_t *mgr);
+void resource_id_put(resource_id_t *mgr, int no);
+
+#endif  /*RESOURCE_ID_H__*/