From owner-freebsd-arch@FreeBSD.ORG Mon Oct 27 19:27:27 2014 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 212D9123; Mon, 27 Oct 2014 19:27:27 +0000 (UTC) Received: from mail-wi0-x22d.google.com (mail-wi0-x22d.google.com [IPv6:2a00:1450:400c:c05::22d]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 868497DC; Mon, 27 Oct 2014 19:27:26 +0000 (UTC) Received: by mail-wi0-f173.google.com with SMTP id ex7so7372473wid.0 for ; Mon, 27 Oct 2014 12:27:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=yXICYh3kL1CHBrhvQ6Z/yrJkE3HEaaFW7CBNvky6pM0=; b=bi237DWtzoSXkJLCwC0IiVgtjP8lYWewR+jpO9MVDPuTh1f22ssKmuDbDQ9LGEws6R BATpyGhS3K9Z5cmJhuFuSWKsF4rzdjhQoaxKGzigA22BUZZ+zC5EPC4/zXCCGoogqUZu NFiAPoYkQOedDDrrRjrz7DVsFKAYPrkI53lyWUUudTT1RFKm0Ral+Y3TwpK7vHv+Mg1P OA6Tkiw8C/dmoXLrYlV87utdSPezViE0YxjORsTQvm1D3C8pfMd7dx7US6oCCAikjh4X wine9HmHFccCL7ifh1v8MEHcAxHK/XTjtKhn73Ir/2ZKkupGiihP4MrDpdMKtzSLiyKG AEfQ== X-Received: by 10.180.20.162 with SMTP id o2mr12790602wie.57.1414438044546; Mon, 27 Oct 2014 12:27:24 -0700 (PDT) Received: from dft-labs.eu (n1x0n-1-pt.tunnel.tserv5.lon1.ipv6.he.net. [2001:470:1f08:1f7::2]) by mx.google.com with ESMTPSA id c5sm16603504wje.30.2014.10.27.12.27.23 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 27 Oct 2014 12:27:23 -0700 (PDT) Date: Mon, 27 Oct 2014 20:27:21 +0100 From: Mateusz Guzik To: John Baldwin Subject: Re: refcount_release_take_##lock Message-ID: <20141027192721.GA28049@dft-labs.eu> References: <20141025184448.GA19066@dft-labs.eu> <20141025190407.GU82214@funkthat.com> <2629048.tOq3sNXcCP@ralph.baldwin.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <2629048.tOq3sNXcCP@ralph.baldwin.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: John-Mark Gurney , freebsd-arch@freebsd.org X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Oct 2014 19:27:27 -0000 On Mon, Oct 27, 2014 at 11:27:45AM -0400, John Baldwin wrote: > Please keep the refcount_*() prefix so it matches the rest of the API. I > would just declare the functions directly in refcount.h rather than requiring > a macro to be invoked in each C file. We can also just implement the needed > lock types for now instead of all of them. > > You could maybe replace 'take' with 'lock', but either name is fine. > We need sx and rwlocks (and temporarily mutexes, but that is going away in few days). I ran into the following issue: opensolaris code has its own rwlock.h, and their refcount.h eventually includes ours refcount.h (and it has to since e.g. our file.h requires it). I don't know any good solution. We could add locking funcs to a separate header (refcount_lock.h?) or use the following hack: diff --git a/sys/sys/refcount.h b/sys/sys/refcount.h index 4611664..ce35131 100644 --- a/sys/sys/refcount.h +++ b/sys/sys/refcount.h @@ -29,15 +29,19 @@ #ifndef __SYS_REFCOUNT_H__ #define __SYS_REFCOUNT_H__ -#include -#include - #ifdef _KERNEL +#include #include +#include +#include +#include #else #define KASSERT(exp, msg) /* */ #endif +#include +#include + static __inline void refcount_init(volatile u_int *count, u_int value) { @@ -64,4 +68,36 @@ refcount_release(volatile u_int *count) return (old == 1); } +#ifdef _KERNEL + +#define REFCOUNT_RELEASE_LOCK_DEFINE(NAME, TYPE, LOCK, UNLOCK) \ +static __inline int \ +refcount_release_lock_##NAME(volatile u_int *count, TYPE *v) \ +{ \ + u_int old; \ + \ + old = *count; \ + if (old > 1 && atomic_cmpset_int(count, old, old - 1)) \ + return (0); \ + LOCK(v); \ + if (refcount_release(count)) \ + return (1); \ + UNLOCK(v); \ + return (0); \ +} + +REFCOUNT_RELEASE_LOCK_DEFINE(sx, struct sx, sx_xlock, sx_xunlock); + +#ifdef _SYS_RWLOCK_H_ +REFCOUNT_RELEASE_LOCK_DEFINE(rwlock, struct rwlock, rw_wlock, rw_wunlock); +#else +/* + * A hack to resolve header conflict with opensolaris which provides its own + * rwlock.h + */ +#define refcount_release_lock_rwlock CTASSERT(0, "not implemented") +#endif /* ! _SYS_RWLOCK_H_ */ + +#endif /* ! _KERNEL */ + #endif /* ! __SYS_REFCOUNT_H__ */ -- Mateusz Guzik