Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S261634AbUJaOpI (ORCPT ); Sun, 31 Oct 2004 09:45:08 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S261636AbUJaOpH (ORCPT ); Sun, 31 Oct 2004 09:45:07 -0500 Received: from baikonur.stro.at ([213.239.196.228]:13765 "EHLO baikonur.stro.at") by vger.kernel.org with ESMTP id S261634AbUJaOop (ORCPT ); Sun, 31 Oct 2004 09:44:45 -0500 Date: Sun, 31 Oct 2004 15:44:24 +0100 From: maximilian attems To: Jeff Garzik Cc: Margit Schubert-While , Nishanth Aravamudan , hvr@gnu.org, mcgrof@studorgs.rutgers.edu, kernel-janitors@lists.osdl.org, netdev@oss.sgi.com, Domen Puncer , linux-kernel@vger.kernel.org Subject: [patch 1/6] back port msleep(), msleep_interruptible() Message-ID: <20041031144424.GB28667@stro.at> Mail-Followup-To: Jeff Garzik , Margit Schubert-While , Nishanth Aravamudan , hvr@gnu.org, mcgrof@studorgs.rutgers.edu, kernel-janitors@lists.osdl.org, netdev@oss.sgi.com, Domen Puncer , linux-kernel@vger.kernel.org References: <20040923221303.GB13244@us.ibm.com> <20040923221303.GB13244@us.ibm.com> <5.1.0.14.2.20040924074745.00b1cd40@pop.t-online.de> <415CD9D9.2000607@pobox.com> <20041030222228.GB1456@stro.at> <41841886.2080609@pobox.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <41841886.2080609@pobox.com> User-Agent: Mutt/1.5.6+20040722i Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4780 Lines: 154 Backport suggested by prism54 folks. idea acked by Jeff. thanks for Domen Puncer at helping out. Belows patch adds msleep() and msleep_interruptible() as found in current 2.6 to 2.4. therefor adds the helper functions ssleep(), jiffies_to_msecs(), jiffies_to_usecs(), msecs_to_jiffies(). The namespace clashes for msleep() and msecs_to_jiffies() are cleanup by the next 5 patches. Signed-off-by: Maximilian Attems --- linux-2.4.28-rc1-max/include/linux/delay.h | 8 +++++ linux-2.4.28-rc1-max/include/linux/time.h | 41 +++++++++++++++++++++++++++++ linux-2.4.28-rc1-max/kernel/Makefile | 3 +- linux-2.4.28-rc1-max/kernel/timer.c | 33 +++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff -puN kernel/Makefile~add-msleep-2.4 kernel/Makefile --- linux-2.4.28-rc1/kernel/Makefile~add-msleep-2.4 2004-10-30 22:48:46.000000000 +0200 +++ linux-2.4.28-rc1-max/kernel/Makefile 2004-10-30 22:50:45.000000000 +0200 @@ -9,7 +9,8 @@ O_TARGET := kernel.o -export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o printk.o +export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o \ + printk.o timer.o obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \ module.o exit.o itimer.o info.o time.o softirq.o resource.o \ diff -puN kernel/timer.c~add-msleep-2.4 kernel/timer.c --- linux-2.4.28-rc1/kernel/timer.c~add-msleep-2.4 2004-10-30 22:48:46.000000000 +0200 +++ linux-2.4.28-rc1-max/kernel/timer.c 2004-10-30 22:50:09.000000000 +0200 @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -874,3 +875,35 @@ asmlinkage long sys_nanosleep(struct tim return 0; } +/** + * msleep - sleep safely even with waitqueue interruptions + * @msecs: Time in milliseconds to sleep for + */ +void msleep(unsigned int msecs) +{ + unsigned long timeout = msecs_to_jiffies(msecs) + 1; + + while (timeout) { + set_current_state(TASK_UNINTERRUPTIBLE); + timeout = schedule_timeout(timeout); + } +} + +EXPORT_SYMBOL(msleep); + +/** + * msleep_interruptible - sleep waiting for waitqueue interruptions + * @msecs: Time in milliseconds to sleep for + */ +unsigned long msleep_interruptible(unsigned int msecs) +{ + unsigned long timeout = msecs_to_jiffies(msecs) + 1; + + while (timeout && !signal_pending(current)) { + set_current_state(TASK_INTERRUPTIBLE); + timeout = schedule_timeout(timeout); + } + return jiffies_to_msecs(timeout); +} + +EXPORT_SYMBOL(msleep_interruptible); diff -puN include/linux/delay.h~add-msleep-2.4 include/linux/delay.h --- linux-2.4.28-rc1/include/linux/delay.h~add-msleep-2.4 2004-10-30 22:48:46.000000000 +0200 +++ linux-2.4.28-rc1-max/include/linux/delay.h 2004-10-30 22:48:46.000000000 +0200 @@ -34,4 +34,12 @@ extern unsigned long loops_per_jiffy; ({unsigned long msec=(n); while (msec--) udelay(1000);})) #endif +void msleep(unsigned int msecs); +unsigned long msleep_interruptible(unsigned int msecs); + +static inline void ssleep(unsigned int seconds) +{ + msleep(seconds * 1000); +} + #endif /* defined(_LINUX_DELAY_H) */ diff -puN include/linux/time.h~add-msleep-2.4 include/linux/time.h --- linux-2.4.28-rc1/include/linux/time.h~add-msleep-2.4 2004-10-30 22:48:46.000000000 +0200 +++ linux-2.4.28-rc1-max/include/linux/time.h 2004-10-30 22:57:44.000000000 +0200 @@ -126,4 +126,45 @@ struct itimerval { struct timeval it_value; /* current value */ }; +/* + * Convert jiffies to milliseconds and back. + * + * Avoid unnecessary multiplications/divisions in the + * two most common HZ cases: + */ +static inline unsigned int jiffies_to_msecs(const unsigned long j) +{ +#if HZ <= 1000 && !(1000 % HZ) + return (1000 / HZ) * j; +#elif HZ > 1000 && !(HZ % 1000) + return (j + (HZ / 1000) - 1)/(HZ / 1000); +#else + return (j * 1000) / HZ; +#endif +} + +static inline unsigned int jiffies_to_usecs(const unsigned long j) +{ +#if HZ <= 1000 && !(1000 % HZ) + return (1000000 / HZ) * j; +#elif HZ > 1000 && !(HZ % 1000) + return (j*1000 + (HZ - 1000))/(HZ / 1000); +#else + return (j * 1000000) / HZ; +#endif +} + +static inline unsigned long msecs_to_jiffies(const unsigned int m) +{ + if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) + return MAX_JIFFY_OFFSET; +#if HZ <= 1000 && !(1000 % HZ) + return (m + (1000 / HZ) - 1) / (1000 / HZ); +#elif HZ > 1000 && !(HZ % 1000) + return m * (HZ / 1000); +#else + return (m * HZ + 999) / 1000; +#endif +} + #endif _ - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/