From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Per Persson" Subject: triple_down et al. Date: Fri, 24 Jan 2003 21:40:42 +0100 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <002e01c2c3e8$e32a89c0$0e02a8c0@solveig> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Cc: Return-path: To: , List-Id: linux-fsdevel.vger.kernel.org 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);