Skip to contentSkip to conversation

whatever happened to down_timeout()?

DISCUSSION Chris Friesen 1 participant 2 messages Feb 13, 2007

lore
Share & tools
lore
#0 lore raw

There has been some discussion on lkml about a function that would either down a semaphore or else abort if it couldn't get the semaphore in a certain amount of time. Something along the lines of:

down_timeout(struct semaphore *sem, long timeout);

Does something like this exist? Does anyone have a working implementation? Does anyone see anything obviously wrong with the following version (that I loosely based on one by Rupert Eibauer)?

semaphore.h:

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

/* "timeout" is the number of jiffies to wait.
* Returns -ETIME if timeout period expires.
*/
static inline int down_timeout(struct semaphore * sem, unsigned int timeout)

{
	int ret = down_trylock(sem);
	if (!ret)
		ret = __down_timeout(sem, timeout);
	return ret;
}

kernel/timer.c

int __down_timeout(struct semaphore * sem, int timeout)

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

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

	ret = down_interruptible(sema);
	if (ret && (jiffies > expire))
		ret = -ETIME;
	 else
		del_timer_sync(&timer);

        return ret;
}

Thanks,
Chris

#1 lore raw
Quoted 7 lines · Chris Friesen

static inline int down_timeout(struct semaphore * sem, unsigned int timeout)

{
    int ret = down_trylock(sem);
    if (!ret)
        ret = __down_timeout(sem, timeout);
    return ret;
}

Sorry, I think that should be:

static inline int down_timeout(struct semaphore * sem, unsigned int timeout)

{
	int ret = down_trylock(sem);
	if (ret)
		ret = __down_timeout(sem, timeout);
	return ret;
}

Chris