From patchwork Mon Jul 15 02:49:15 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 258932 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 34BC62C017B for ; Mon, 15 Jul 2013 12:51:01 +1000 (EST) Received: from localhost ([::1]:49474 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UyYsM-0007dT-Bj for incoming@patchwork.ozlabs.org; Sun, 14 Jul 2013 22:50:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41542) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UyYs8-0007dL-IJ for qemu-devel@nongnu.org; Sun, 14 Jul 2013 22:50:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UyYs7-0004XN-Ka for qemu-devel@nongnu.org; Sun, 14 Jul 2013 22:50:44 -0400 Received: from mail-pd0-x233.google.com ([2607:f8b0:400e:c02::233]:41551) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UyYs7-0004XB-DK for qemu-devel@nongnu.org; Sun, 14 Jul 2013 22:50:43 -0400 Received: by mail-pd0-f179.google.com with SMTP id q10so10224580pdj.38 for ; Sun, 14 Jul 2013 19:50:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=hmwXX034nVDlJJLDSxyK2R8fiftg56MZGdb1hqB6fP0=; b=JlLhikb0rLUijW/5QCnsmOoVwe8tFDd9cMM5T15SgtEEwy888DRbOTHvgyPvyDYOOa bIGsEljLC6FwPiqp/W6HffIpyoF/mrVv/PDnqcKZhcwXH5JNl+oYGgQ596F36k7orLLJ 2Pq+petXAsMR77Cbnr5mwzKkk20lOqNEKaHMRN7xXrv+K/ECAINsRkjJMJww8Ep49/FO qhnodrw4EzHM1mZy10CxX3OqHcdzBGZ2yb2M0rKpcjZ8JR4eataXaJ2NgG0dX03q5gpn 82wKVr/94WRtIwhNWhxWtC8RRAGFzl57BkTNuO5DK/bzHokpJfNQVOv+2OQDSZrrg+eQ 70eQ== X-Received: by 10.66.219.1 with SMTP id pk1mr18997859pac.29.1373856641528; Sun, 14 Jul 2013 19:50:41 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id iq6sm58094742pbc.1.2013.07.14.19.50.38 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sun, 14 Jul 2013 19:50:40 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Mon, 15 Jul 2013 10:49:15 +0800 Message-Id: <1373856556-22272-1-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c02::233 Cc: Paolo Bonzini , Richard Henderson Subject: [Qemu-devel] [PATCH 1/2] object: introduce Qref to abstract the refcnt interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Qref is similar to kref. It hides the refcnt detail and provides a common interface. And this patch is based on the idiom of refcnt, and adopts some optimization about memory model, which finally falls back on gcc implementation. Signed-off-by: Liu Ping Fan --- include/qom/object.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index 23fc048..2e165fb 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -18,6 +18,7 @@ #include #include #include "qemu/queue.h" +#include "qemu/osdep.h" struct Visitor; struct Error; @@ -363,6 +364,10 @@ struct ObjectClass ObjectUnparent *unparent; }; +typedef struct Qref { + int32_t count; +} Qref; + /** * Object: * @@ -1133,5 +1138,34 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), */ Object *container_get(Object *root, const char *path); +static inline void qref_get(Qref *qref) +{ +#ifdef __ATOMIC_RELAXED + __atomic_fetch_add(&qref->count, 1, __ATOMIC_RELAXED); +#else + __sync_fetch_and_add(&qref->count, 1); +#endif +} + +typedef void (*ReleaseFn)(Qref *); +static inline void qref_put(Qref *qref, ReleaseFn release) +{ + int32_t i; + +#ifdef __ATOMIC_RELAXED + i = __atomic_fetch_add(&qref->count, -1, __ATOMIC_RELAXED); + g_assert(i > 0); + if (unlikely(i == 1)) { + __atomic_thread_fence(__ATOMIC_SEQ_CST); + release(qref); + } +#else + i = __sync_fetch_and_add(&qref->count, -1); + g_assert(i > 0); + if (unlikely(i == 1)) { + release(qref); + } +#endif +} #endif