From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Linas Vepstas Newsgroups: gmane.lisp.guile.devel Subject: Re: Locks and threads Date: Wed, 11 Mar 2009 20:29:12 -0500 Message-ID: <3ae3aa420903111829u2970b35w63b567dd2fc95752@mail.gmail.com> References: <87mycsd2rj.fsf@arudy.ossau.uklinux.net> <87vdqovofz.fsf@arudy.ossau.uklinux.net> <87vdqhc4oi.fsf@arudy.ossau.uklinux.net> <87mybrwqmj.fsf@arudy.ossau.uklinux.net> <87iqmfwohh.fsf@arudy.ossau.uklinux.net> Reply-To: linasvepstas@gmail.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1236821374 10051 80.91.229.12 (12 Mar 2009 01:29:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 12 Mar 2009 01:29:34 +0000 (UTC) Cc: Guile Development To: Neil Jerram Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Thu Mar 12 02:30:50 2009 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LhZl0-0008CJ-Pk for guile-devel@m.gmane.org; Thu, 12 Mar 2009 02:30:47 +0100 Original-Received: from localhost ([127.0.0.1]:38678 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LhZje-0001c0-MW for guile-devel@m.gmane.org; Wed, 11 Mar 2009 21:29:22 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LhZjY-0001bl-8f for guile-devel@gnu.org; Wed, 11 Mar 2009 21:29:16 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LhZjW-0001bY-RL for guile-devel@gnu.org; Wed, 11 Mar 2009 21:29:14 -0400 Original-Received: from [199.232.76.173] (port=59625 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LhZjW-0001bV-M3 for guile-devel@gnu.org; Wed, 11 Mar 2009 21:29:14 -0400 Original-Received: from ag-out-0708.google.com ([72.14.246.249]:46410) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LhZjW-000530-CI for guile-devel@gnu.org; Wed, 11 Mar 2009 21:29:14 -0400 Original-Received: by ag-out-0708.google.com with SMTP id 26so362714agb.10 for ; Wed, 11 Mar 2009 18:29:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:reply-to:in-reply-to :references:date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=ptUqd/aIRux5ccIr6TShPq32vnyNSSu1n+jZvh4xH6Q=; b=HxilzrsKtGo/Hk/WVUhLeVXLm5ozGfnZ+mEeTWrRwIQy76fMj3epY8LqpBZNO7Rm0A cRTJHsJTFkgc9gL1X4vggt0CBMygbnnypH5XlqnyuQEMbmaNrydBM517ectSJzL9IvJC cCDU2SCjvfV/ZyiS6Rofda9yCwEeK3/8Ic95o= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:content-transfer-encoding; b=ibh290fD/ItEVKUKAv1fKnLb0o68Gl0SThwPXy+cfYl+hK5ut8+PtdHa5vFN53KfWa EHFljYlmmOPzw8OJ6KGV3wI5wT6mzPS22HxzTe+HoJjNN3VOvF8YPRngJBneXLVfLs+0 GVQlh9caYlhKwklGqMzuXqmLKryu+N092AxY4= Original-Received: by 10.100.138.10 with SMTP id l10mr3087352and.158.1236821352300; Wed, 11 Mar 2009 18:29:12 -0700 (PDT) In-Reply-To: <87iqmfwohh.fsf@arudy.ossau.uklinux.net> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:8263 Archived-At: 2009/3/11 Neil Jerram : > Neil Jerram writes: > >> # >> # >> ERROR: ERROR: Unbound variable: x1-100499 >> Unbound variable: define >> ERROR: Unbound variable: x4-100596 >> ERROR: Unbound variable: define >> ERROR: Unbound variable: define >> ERROR: Unbound variable: define >> guile-define test case: good-bye! >> test-define-race: 2 error(s) >> FAIL: test-define-race >> >> I'm off to sleep now, so I thought I'd post what I've done in case >> others have thoughts on it or can see something wrong. > > Thanks to a hint from helgrind, I think the problem might be that the > symbols obarray is not thread-safe. =C2=A0But surely using a mutex for > every symbol that Guile reads would be terrible for performance... so > I hope there is an alternative. Well, once you identify the section that needs locking, you'll want to use an rwlock instead of a mutex. The rwlock (pthread_rwlock_rdlock) allows multiple simultaneous readers. The writers, however, get exclusive access. (pthread_rwlock_wrlock) I have no clue as to what the overhead is. I know that the powerpc architecture implements locking primitives that are extremely fast (a few cycles); I assume Intel does as well. These are used liberally within the kernel to implement all sorts of fast atomic ops. Every now and then, some newcomer reinvents a locking system using these primitives, "for performance reasons", and posts it to some mailing list. The standard, canonical response is "you're an idiot", followed by pointers to 5 different bugs in the poster's code, and an explanation that the pthreads implementation is faster than his, and that its also debugged. (The response is less blunt when the poster is a customer of yours, and the email was delivered via some bushy-tailed executive). An alternative idea is to try to apply some principles from functional programming, e.g. "copy on write" (COW): when the obarray needs to be updated, make a copy of it. Any readers in other threads continue to safely use the original version. When the new obarray is fully constructed, new readers will use it. The old obarray is garbage-collected in due time. I dunno if this could be applied for this case; at least we have garbage collection, which removes one major stumbling block for using COW. --linas