linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* triple_down et al.
@ 2003-01-24 20:40 Per Persson
  2003-01-27 14:49 ` Alan Cox
  0 siblings, 1 reply; 3+ messages in thread
From: Per Persson @ 2003-01-24 20:40 UTC (permalink / raw)
  To: marcelo, alan; +Cc: linux-fsdevel

Here's a patch I tried to get into the 2.4 series in autumn 2001. I was
however told that it was too close to the start of the 2.5 series.

The patch cleans up the "messy" triple_down code in fs.h.

  /Per


--- include/linux/fs.h.orig 2003-01-11 00:34:56.000000000 +0100
+++ include/linux/fs.h 2003-01-24 10:12:46.000000000 +0100
@@ -1564,14 +1564,25 @@
 /*
  * Whee.. Deadlock country. Happily there are only two VFS
  * operations that does this..
+ *
+ * {double,triple}_down modified by Per Persson
  */
+
+#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);
@@ -1581,9 +1592,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,
@@ -1591,34 +1603,18 @@
           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)
 {
  up(s1);



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

end of thread, other threads:[~2003-02-02  0:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-24 20:40 triple_down et al Per Persson
2003-01-27 14:49 ` Alan Cox
2003-02-02  0:03   ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).