All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai-core] [PATCH] Auto-allocation of minor values for pipe objects
@ 2005-11-15 16:38 Dmitry Adamushko
  2005-11-15 22:17 ` [Xenomai-core] " Philippe Gerum
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Adamushko @ 2005-11-15 16:38 UTC (permalink / raw)
  To: xenomai


[-- Attachment #1.1: Type: text/plain, Size: 435 bytes --]



Hello,

enclosed please find a patch that hopefully adds so desired functionality.
I have made various tests with it just now and it seems to work fine.

A size of the bitmap is dependent on XNPIPE_NDEVS parameter in the same
vein as xnpipe_states depends on it; so hopefully that is what you have
meant Philippe (?)

A few comment later since I have to go now.

---
Best regards,
Dmitry

(See attached file: pipe.auto-minor-2.patch)

[-- Attachment #1.2: Type: text/html, Size: 763 bytes --]

[-- Attachment #2: pipe.auto-minor-2.patch --]
[-- Type: application/octet-stream, Size: 3998 bytes --]

diff -ur xenomai/include/nucleus/pipe.h xenomai-pipe2/include/nucleus/pipe.h
--- xenomai/include/nucleus/pipe.h	2005-10-07 23:11:23.000000000 +0200
+++ xenomai-pipe2/include/nucleus/pipe.h	2005-11-15 10:19:19.000000000 +0100
@@ -30,6 +30,8 @@
 #define XNPIPE_NORMAL 0x0
 #define XNPIPE_URGENT 0x1
 
+#define XNPIPE_MINOR_AUTO	-1
+
 #ifdef __KERNEL__
 
 #include <nucleus/queue.h>
diff -ur xenomai/nucleus/pipe.c xenomai-pipe2/nucleus/pipe.c
--- xenomai/nucleus/pipe.c	2005-11-09 19:20:47.000000000 +0100
+++ xenomai-pipe2/nucleus/pipe.c	2005-11-15 18:44:51.000000000 +0100
@@ -43,6 +43,9 @@
 
 xnpipe_state_t xnpipe_states[XNPIPE_NDEVS];
 
+#define XNPIPE_BITMAP_SIZE	((XNPIPE_NDEVS + BITS_PER_LONG - 1) / BITS_PER_LONG)
+static unsigned long xnpipe_bitmap[XNPIPE_BITMAP_SIZE];
+
 xnqueue_t xnpipe_sleepq, xnpipe_asyncq;
 
 int xnpipe_wakeup_apc;
@@ -57,6 +60,37 @@
    #define class_destroy class_simple_destroy
 #endif
 
+/* Allocation of minor values */
+
+static inline int xnpipe_minor_alloc(int minor)
+{
+    spl_t s;
+
+    if ((minor < 0 && minor != XNPIPE_MINOR_AUTO) || minor >= XNPIPE_NDEVS)
+	return -ENODEV;
+
+    xnlock_get_irqsave(&nklock,s);
+
+    if (minor == XNPIPE_MINOR_AUTO)
+	minor = find_first_zero_bit(xnpipe_bitmap,XNPIPE_NDEVS);
+
+    if (minor == XNPIPE_NDEVS || testbits(xnpipe_bitmap[minor / BITS_PER_LONG], 1 << (minor % BITS_PER_LONG)))
+        minor = -EBUSY;
+    else    
+	__setbits(xnpipe_bitmap[minor / BITS_PER_LONG], 1 << (minor % BITS_PER_LONG));
+
+    xnlock_put_irqrestore(&nklock,s);
+    return minor;
+}
+
+static inline void xnpipe_minor_free(int minor)
+{
+    if (minor < 0 || minor >= XNPIPE_NDEVS)
+	return;
+
+    clrbits(xnpipe_bitmap[minor / BITS_PER_LONG], 1 << (minor % BITS_PER_LONG));
+}
+
 /* Get nklock locked before using this macro */
 
 #define xnpipe_read_wait(state,flags)						\
@@ -215,8 +249,9 @@
     int need_sched = 0;
     spl_t s;
 
-    if (minor < 0 || minor >= XNPIPE_NDEVS)
-	return -ENODEV;
+    minor = xnpipe_minor_alloc(minor);
+    if (minor < 0)
+	return minor;
 
     state = &xnpipe_states[minor];
 
@@ -224,12 +259,6 @@
        to prevent using a partially created object */
     xnlock_get_irqsave(&nklock,s);
 
-    if (testbits(state->status,XNPIPE_KERN_CONN))
-	{
-	xnlock_put_irqrestore(&nklock,s);
-	return -EBUSY;
-	}
-    
     setbits(state->status,XNPIPE_KERN_CONN);
     
     xnsynch_init(&state->synchbase,XNSYNCH_FIFO);
@@ -261,7 +290,7 @@
     if (need_sched)
         xnpipe_schedule_request();
 
-    return 0;
+    return minor;
 }
 
 int xnpipe_disconnect (int minor)
@@ -323,6 +352,8 @@
 	    }
 	}
 
+    xnpipe_minor_free(minor);
+
     xnlock_put_irqrestore(&nklock,s);
 
     if (need_sched)
diff -ur xenomai/skins/native/pipe.c xenomai-pipe2/skins/native/pipe.c
--- xenomai/skins/native/pipe.c	2005-11-14 17:11:22.000000000 +0100
+++ xenomai-pipe2/skins/native/pipe.c	2005-11-15 18:35:52.000000000 +0100
@@ -227,12 +227,11 @@
 		    const char *name,
 		    int minor)
 {
-    int err;
+    int err = 0;
 
     if (xnpod_asynch_p())
 	return -EPERM;
 
-    pipe->minor = minor;
     pipe->buffer = NULL;
     pipe->fillsz = 0;
     pipe->flushable = 0;
@@ -240,13 +239,16 @@
     pipe->magic = XENO_PIPE_MAGIC;
     xnobject_copy_name(pipe->name,name);
 
-    err = xnpipe_connect(minor,
+    minor = xnpipe_connect(minor,
 			 &__pipe_output_handler,
 			 NULL,
 			 &__pipe_alloc_handler,
 			 pipe);
-    if (err)
-	return err;
+
+    if (minor < 0)
+	return minor;
+
+    pipe->minor = minor;
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
     pipe->cpid = 0;
diff -ur xenomai/skins/native/pipe.h xenomai-pipe2/skins/native/pipe.h
--- xenomai/skins/native/pipe.h	2005-11-14 16:01:06.000000000 +0100
+++ xenomai-pipe2/skins/native/pipe.h	2005-11-15 11:34:00.000000000 +0100
@@ -29,6 +29,8 @@
 #define P_NORMAL  XNPIPE_NORMAL
 #define P_URGENT  XNPIPE_URGENT
 
+#define P_MINOR_AUTO	XNPIPE_MINOR_AUTO
+
 typedef struct rt_pipe_placeholder {
     rt_handle_t opaque;
 } RT_PIPE_PLACEHOLDER;

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-11-16 15:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-15 16:38 [Xenomai-core] [PATCH] Auto-allocation of minor values for pipe objects Dmitry Adamushko
2005-11-15 22:17 ` [Xenomai-core] " Philippe Gerum
2005-11-16 10:05   ` Dmitry Adamushko
2005-11-16 15:38     ` Philippe Gerum

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.