mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] {double,triple}_down() in fs.h
@ 2001-11-13 22:32 Per Persson
  0 siblings, 0 replies; only message in thread
From: Per Persson @ 2001-11-13 22:32 UTC (permalink / raw)
  To: linux-kernel

This time I've read SubmittingPatches and tried to follow the rules,
especially the one saying that a patch shouldn't be sent as an attachment.

The code of triple_down() looked ugly and complicated, but I found that it
just sorted the semaphores and down()ed them. So I rewrote the code making
it simpler and more efficient.

The patch was made w.r.t 2.4.14.

Per Persson
per.persson@gnosjo.pp.se


--- include/linux/fs.h.orig	Mon Nov 12 01:14:10 2001
+++ include/linux/fs.h	Tue Nov 13 17:35:16 2001
@@ -1478,14 +1478,25 @@
 /*
  * Whee.. Deadlock country. Happily there are only two VFS
  * operations that does this..
+ *
+ * {double,triple}_down modified by Per Persson in September, 2001
  */
+
+#define exch(x,y)			\
+do {					\
+	typeof(x) __tmp__ = (x);	\
+	(x) = (y);			\
+	(y) = __tmp__;			\
+} while(0)
+
+#define sort(x,y)			\
+if((x) < (y))				\
+	exch((x), (y))
+
 static inline void double_down(struct semaphore *s1, struct semaphore *s2)
 {
-	if (s1 != s2) {
-		if ((unsigned long) s1 < (unsigned long) s2) {
-			struct semaphore *tmp = s2;
-			s2 = s1; s1 = tmp;
-		}
+	if (s1 != s2)
+		sort((ulong)s1, (ulong)s2);	// s1 > s2
 		down(s1);
 	}
 	down(s2);
@@ -1495,9 +1506,10 @@
  * Ewwwwwwww... _triple_ lock. We are guaranteed that the 3rd argument is
  * not equal to 1st and not equal to 2nd - the first case (target is parent
of
  * source) would be already caught, the second is plain impossible (target
is
- * its own parent and that case would be caught even earlier). Very messy.
- * I _think_ that it works, but no warranties - please, look it through.
- * Pox on bloody lusers who mandated overwriting rename() for
directories...
+ * its own parent and that case would be caught even earlier).
+ *
+ * Cleaner and more efficient code than the original.
+ *     /Per
  */

 static inline void triple_down(struct semaphore *s1,
@@ -1505,33 +1517,17 @@
 			       struct semaphore *s3)
 {
 	if (s1 != s2) {
-		if ((unsigned long) s1 < (unsigned long) s2) {
-			if ((unsigned long) s1 < (unsigned long) s3) {
-				struct semaphore *tmp = s3;
-				s3 = s1; s1 = tmp;
-			}
-			if ((unsigned long) s1 < (unsigned long) s2) {
-				struct semaphore *tmp = s2;
-				s2 = s1; s1 = tmp;
-			}
-		} else {
-			if ((unsigned long) s1 < (unsigned long) s3) {
-				struct semaphore *tmp = s3;
-				s3 = s1; s1 = tmp;
-			}
-			if ((unsigned long) s2 < (unsigned long) s3) {
-				struct semaphore *tmp = s3;
-				s3 = s2; s2 = tmp;
-			}
-		}
+		sort((ulong)s1, (ulong)s2);	// s1 > s2
+		sort((ulong)s1, (ulong)s3);	// s1 > s3
 		down(s1);
-	} else if ((unsigned long) s2 < (unsigned long) s3) {
-		struct semaphore *tmp = s3;
-		s3 = s2; s2 = tmp;
 	}
+	sort((ulong)s2, (ulong)s3);		// s2 > s3
 	down(s2);
 	down(s3);
 }
+
+#undef sort
+#undef exch

 static inline void double_up(struct semaphore *s1, struct semaphore *s2)
 {


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-11-13 22:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-13 22:32 [PATCH] {double,triple}_down() in fs.h Per Persson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®