Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id ; Sun, 17 Dec 2000 13:55:27 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id ; Sun, 17 Dec 2000 13:55:17 -0500 Received: from hermes.mixx.net ([212.84.196.2]:25610 "HELO hermes.mixx.net") by vger.kernel.org with SMTP id ; Sun, 17 Dec 2000 13:55:06 -0500 From: Daniel Phillips To: linux-kernel@vger.kernel.org Subject: [RFC] Semaphores used for daemon wakeup Date: Sun, 17 Dec 2000 13:06:10 +0100 X-Mailer: KMail [version 1.0.28] Content-Type: Multipart/Mixed; boundary="Boundary-=_AfAdrrWsofsBcnuVqHFfbsAQxWpQ" MIME-Version: 1.0 Message-Id: <0012171922570J.00623@gimli> Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org --Boundary-=_AfAdrrWsofsBcnuVqHFfbsAQxWpQ Content-Type: text/plain Content-Transfer-Encoding: 8bit This patch illustrates an alternative approach to waking and waiting on daemons using semaphores instead of direct operations on wait queues. The idea of using semaphores to regulate the cycling of a daemon was suggested to me by Arjan Vos. The basic idea is simple: on each cycle a daemon down's a semaphore, and is reactivated when some other task up's the semaphore. Sometimes an activating task wants to wait until the daemon completes whatever it's supposed to do - flushing memory in this case. I generalized the above idea by adding another semaphore for wakers to sleep on, and a count variable to let the daemon know how many sleepers it needs to activate. This patch updates bdflush and wakeup_bdflush to use that mechanism. The implementation uses two semaphores and a counter: DECLARE_MUTEX_LOCKED(bdflush_request); DECLARE_MUTEX_LOCKED(bdflush_waiter); atomic_t bdflush_waiters /*= 0*/; A task wanting to activate bdflush does: up(&bdflush_request); A task wanting to activate bdflush and wait does: atomic_inc(&bdflush_waiters); up(&bdflush_request); down(&bdflush_waiter); When bdflush has finished its work it does: waiters = atomic_read(&bdflush_waiters); atomic_sub(waiters, &bdflush_waiters); while (waiters--) up(&bdflush_waiter); down(&bdflush_request); Since I wasn't sure whether the side effect in the existing code of setting the current task RUNNING was really wanted, I wrote this in explicitly in the places where the side effect was noted, with the obligatory comment. I've done some fairly heavy stress-testing and this new scheme (but not on non-x86 or SMP) and it does seem to work much the same as the existing one. I doubt that there is a measureable difference in execution overhead, nor is there a difference in correctness as far as I can see. But for me at least, it's considerably easier to verify that the semaphore approach is correct. OK, there it is. Is this better, worse, or lateral? -- Daniel --Boundary-=_AfAdrrWsofsBcnuVqHFfbsAQxWpQ Content-Type: text/x-c; name="semwake.patch.2.4.0-test10" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="semwake.patch.2.4.0-test10" LS0tIC4uLzIuNC4wLXRlc3QxMC5jbGVhbi9mcy9idWZmZXIuYwlUaHUgT2N0IDEyIDIzOjE5OjMy IDIwMDAKKysrIC4vZnMvYnVmZmVyLmMJTW9uIERlYyAxOCAwMzowMzowMSAyMDAwCkBAIC03MDgs NyArNzA4LDggQEAKIHN0YXRpYyB2b2lkIHJlZmlsbF9mcmVlbGlzdChpbnQgc2l6ZSkKIHsKIAlp ZiAoIWdyb3dfYnVmZmVycyhzaXplKSkgewotCQl3YWtldXBfYmRmbHVzaCgxKTsgIC8qIFNldHMg dGFzay0+c3RhdGUgdG8gVEFTS19SVU5OSU5HICovCisJCXdha2V1cF9iZGZsdXNoKDEpOworCQlf X3NldF9jdXJyZW50X3N0YXRlKFRBU0tfUlVOTklORyk7IC8qIG5lZWRlZD8/ICovCiAJCWN1cnJl bnQtPnBvbGljeSB8PSBTQ0hFRF9ZSUVMRDsKIAkJc2NoZWR1bGUoKTsKIAl9CkBAIC0yNDY5LDMz ICsyNDcwLDI4IEBACiAgKiByZXNwb25zZSB0byBkaXJ0eSBidWZmZXJzLiAgT25jZSB0aGlzIHBy b2Nlc3MgaXMgYWN0aXZhdGVkLCB3ZSB3cml0ZSBiYWNrCiAgKiBhIGxpbWl0ZWQgbnVtYmVyIG9m IGJ1ZmZlcnMgdG8gdGhlIGRpc2tzIGFuZCB0aGVuIGdvIGJhY2sgdG8gc2xlZXAgYWdhaW4uCiAg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nN0YXRlIHRv IFRBU0tfUlVOTklORyAqLworCQl3YWtldXBfYmRmbHVzaCgxKTsKKwkJX19zZXRfY3VycmVudF9z dGF0ZShUQVNLX1JVTk5JTkcpOyAvKiBuZWVkZWQ/PyAqLwogCQljdXJyZW50LT5wb2xpY3kgfD0g U0NIRURfWUlFTEQ7CiAJCXNjaGVkdWxlKCk7CiAJCWdvdG8gcmVwZWF0X3BhZ2U7Cg== --Boundary-=_AfAdrrWsofsBcnuVqHFfbsAQxWpQ-- - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org Please read the FAQ at http://www.tux.org/lkml/