From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/7105 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: semaphore redesign Date: Sat, 28 Feb 2015 10:42:48 -0500 Message-ID: <20150228154247.GQ23507@brightrain.aerifal.cx> References: <1409123141.4476.18.camel@eris.loria.fr> <20140827074310.GK12888@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="XIiC+We3v3zHqZ6Z" X-Trace: ger.gmane.org 1425138190 7402 80.91.229.3 (28 Feb 2015 15:43:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 28 Feb 2015 15:43:10 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-7118-gllmg-musl=m.gmane.org@lists.openwall.com Sat Feb 28 16:43:09 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1YRjXp-0001Ds-CI for gllmg-musl@m.gmane.org; Sat, 28 Feb 2015 16:43:09 +0100 Original-Received: (qmail 31866 invoked by uid 550); 28 Feb 2015 15:43:05 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 31790 invoked from network); 28 Feb 2015 15:43:00 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:7105 Archived-At: --XIiC+We3v3zHqZ6Z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Feb 28, 2015 at 02:21:22AM +0300, Alexander Monakov wrote: > Hello, > > As new cancellation has landed (except that __timedwait fails to propagate > ECANCELED in addition to ETIMEDOUT and EINTR), I'm happy to post semaphore > redesign. We discussed this implementation with Rich on IRC once, and > presently I'm not aware of any issues (finally!), but still testing and > performance comparison need to be done. Thanks! I'm attaching a performance testing program I used in the past. Its time measurement is not very precise, but it should show large-scale differences. It measures the number of messages that can be send back and forth between two threads in 2 seconds by cancelling the two threads after sleep(2). Some other things we should test for performance: - Do something like the attached but with multiple threads contending on semaphores rather than uncontended message passing. - Time/cycles per call for sem_post and sem_trywait and uncontended sem_wait -- this looks like it should be an obvious win though. - Perhaps something to hammer posts and trywait -- it's not clear that this would model any real-world behavior, but it could show something interesting anyway. I doubt we'll see any measurable differences in usage cases where the futex syscall is expected; it should dominate there. Rich --XIiC+We3v3zHqZ6Z Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="sem_bench.c" #include #include #include #include static sem_t sem[2]; static long count[2]; static void *func(void *arg) { long my_id = (long) arg; for (;;) { sem_wait(&sem[my_id]); count[my_id]++; sem_post(&sem[1-my_id]); } return NULL; } int main(void) { void *ret; pthread_t t1, t2; sem_init(&sem[0], 0, 1); sem_init(&sem[1], 0, 0); pthread_create(&t1, NULL, func, (void*) 0); pthread_create(&t2, NULL, func, (void*) 1); sleep(2); pthread_cancel(t1); pthread_cancel(t2); pthread_join(t1, &ret); pthread_join(t2, &ret); printf("count=%ld\n", count[0] + count[1]); return 0; } --XIiC+We3v3zHqZ6Z--