LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: "Iñaky Pérez-González" <inaky.perez-gonzalez@intel.com>
To: linux-kernel@vger.kernel.org
Subject: RFC/patch: down_timeout_interruptible()
Date: Thu, 22 Feb 2007 17:54:30 -0800	[thread overview]
Message-ID: <200702221754.31165.inaky.perez-gonzalez@intel.com> (raw)


Hi All

I was in a situation where I could really simplify many things by
using down_interruptible() with a timeout. I went around looking for
how could one be implemented and ran away from the asm code in arch/.

At the end I realized I could make a simple one by setting up a timer
that would 'fake' a sigpending situation (setting the task's TIF_SIGPENDING
bit) so that the __down_interruptible_failed() path would quit when 
the timer went off.

I gave it a quick try (must admit, not too tested) and it seems that
the setting of TIF_SIGPENDING without really having a signal queued
is not having easily visible ugly consequences.

Does anybody have suggestions on how to alternatively implement this?

Thanks,

-- Inaky


 include/asm-i386/semaphore.h |    3 ++
 lib/semaphore-sleepers.c     |   51 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff -r 9ed3c4dc013f include/asm-i386/semaphore.h
--- a/include/asm-i386/semaphore.h	Wed Feb 21 13:02:17 2007 -0800
+++ b/include/asm-i386/semaphore.h	Thu Feb 22 17:09:13 2007 -0800
@@ -88,6 +88,9 @@ fastcall int  __down_failed_interruptibl
 fastcall int  __down_failed_interruptible(void  /* params in registers */);
 fastcall int  __down_failed_trylock(void  /* params in registers */);
 fastcall void __up_wakeup(void /* special register calling convention */);
+/* FIXME: need to add to other header files */
+extern void down_timeout_interruptible(struct semaphore *);
+
 
 /*
  * This is ugly, but we want the default case to fall through.
diff -r 9ed3c4dc013f lib/semaphore-sleepers.c
--- a/lib/semaphore-sleepers.c	Wed Feb 21 13:02:17 2007 -0800
+++ b/lib/semaphore-sleepers.c	Thu Feb 22 16:52:54 2007 -0800
@@ -174,3 +174,54 @@ fastcall int __down_trylock(struct semap
 	spin_unlock_irqrestore(&sem->wait.lock, flags);
 	return 1;
 }
+
+
+struct dit_data 
+{
+	struct task_struct *task;
+	int result;
+};
+
+/**
+ * dit_kick_process - set a fake 'signal pending' and kick
+ *
+ * Helper for down_interruptible_timeout(). Sets the signal pending
+ * flag on the task that is waiting for a semaphore and kicks it. That
+ * tells the down_interruptible() code to quit waiting.
+ */
+static 
+void dit_kick_process(unsigned long _data)
+{
+	struct dit_data *data = (void *) _data;
+	set_tsk_thread_flag(data->task, TIF_SIGPENDING);
+	data->result = -ETIMEDOUT;
+	kick_process(data->task);
+}
+
+
+/*
+ * Interruptible try to acquire a semaphore.  If we obtained
+ * it, return zero.  If we were interrupted, returns -EINTR. If we
+ * timedout, we return -ETIMEDOUT;
+ *
+ * Note the ugly hack, using a helper timer and function to wake up
+ * the waiter. We need to distinguish, if we get an error, if it was
+ * because we were woken up (-ETIMEDOUT) or for other reason (-EINTR),
+ * hence the last if block.
+ */
+int down_interruptible_timeout(struct semaphore *sem, unsigned long to) 
+{
+	int result;
+	struct dit_data data = {
+		.task = get_current(), .result = 0 
+	};
+	DEFINE_TIMER(dit_timer, dit_kick_process, to, (unsigned long) &data);
+	add_timer(&dit_timer);
+	result = down_interruptible(sem);
+	del_timer(&dit_timer);
+	if (result < 0 && data.result < 0)
+		result = data.result;
+	return result;
+}
+
+

             reply	other threads:[~2007-02-23  1:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-23  1:54 Iñaky Pérez-González [this message]
2007-02-25  6:45 ` Arjan van de Ven
2007-02-26  6:37   ` Perez-Gonzalez, Inaky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200702221754.31165.inaky.perez-gonzalez@intel.com \
    --to=inaky.perez-gonzalez@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: RFC/patch: down_timeout_interruptible()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).