Skip to contentSkip to conversation

Linux Kernel: down_timeout

RFC Rupert Eibauer 2 participants 2 messages Aug 15, 2002 – Aug 19, 2002

lore
Share & tools
lore
#0 lore raw

Please reply to me directly, I am not
subscribed to the lkml.

---------- Forwarded Message ----------

Subject: Re: Fwd: Linux Kernel: down_timeout
Date: Thu, 15 Aug 2002 10:44:55 +0100 (BST)
From: Matthew Kirkwood <[email protected]>
To: Rupert Eibauer <[email protected]>

Quoted 4 lines · Rupert Eibauer

I found your thread
[PATCH][RFC] Lightweight user-level semaphores
(jan 2002)
later, so please read on..

I'm no kernel guru, but this looks really useful. You should probably send it to the linux-kernel list and to Rusty Russel -- it would be a particularly useful extra feature for his futexes.

Cheers,
Matthew.

Quoted 16 lines

---------- Forwarded Message ----------

Subject: Linux Kernel: down_timeout
Date: Thu, 15 Aug 2002 11:14:24 +0200
From: Rupert Eibauer <[email protected]>
To: [email protected], [email protected], [email protected]

Hi,

I found your thread (Apr 2001) about down_timeout on the lkml. down_timeout is needed more often then you might think.

Does anybody of you know why it hasnt been implemented in the kernel until now?

Quoted 8 lines

My version of down_timeout is attached below.

waiting for your answer,

Rupert

---------- End of Forwarded Message ----------

Here is the header snippet:
-8<-------------------------------------------------------------------------- extern int __down_timeout(struct semaphore * sem, int timeout);

extern inline int down_timeout(struct semaphore * sem, int timeout)

{
        int ret = 0;
        if (atomic_dec_if_positive(&sem->count) < 0)
                ret = __down_timeout(sem, timeout);
        smp_wmb();
        return ret;
}

-8<--------------------------------------------------------------------------

This is for the .c file:
-8<-------------------------------------------------------------------------- static void process_sema_timeout(unsigned long data) {
struct task_struct *p = (struct task_struct *)data;

wake_up_process(p);
}

extern int __down_timeout(struct semaphore * sem, int timeout)

{
        int ret = 0;
        unsigned long expire;
        struct timer_list timer;

        if (timeout > 0) {

                expire = jiffies + timeout;
                init_timer(&timer);
                timer.expires = expire;
                timer.data    = (unsigned long) current;
                timer.function = process_sema_timeout;
                add_timer(&timer);

                if (ret = __down_interruptible(sema)) {
                        /* check whether the timeout woke us up */
                        if (jiffies > expire)
                                ret = -ETIME;
                        else
                                del_timer_sync(&timer);
                } else
                        del_timer_sync(&timer);

        } else if (timeout == 0) { // timeout=0 means now
                /* our caller (inline int down_timeout) already tried to
                get the semaphore _now_ and it failed ! */
                ret = !0;
        } else { // timeout<0 means wait forever
                ret = __down_interruptible(sema)
        }
        return ret;
}

-8<--------------------------------------------------------------------------

Signature · 7 lines
+-----------------------------------------------------------------------------+
| CCCC EEEE SSSS  Creative Electronic Systems | Rupert Eibauer                |
| C    E    S     38 av. Eugene Lance         | Linux Kernel Hacker           |
| C    EEE  SSSS  PO Box 584                  |                               |
| C    E       S  CH-1212 Grand-Lancy 1       |                               |
| CCCC EEEE SSSS  Switzerland                 | email: [email protected]          |
+-----------------------------------------------------------------------------+
#1 lore raw

In message <200208151157327.SM00152@there> you write:

Quoted 4 lines

I'm no kernel guru, but this looks really useful. You should probably send it to the linux-kernel list and to Rusty Russel -- it would be a particularly useful extra feature for his futexes.

Hi Rupert,
That's Russell (two l's) 8)

Quoted 2 lines
Quoted 2 lines

Does anybody of you know why it hasnt been implemented in the kernel until now?

I think because it can be faked up with SIGALRM. And if that's too slow, you can use futexes which have this anyway.

Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.