From patchwork Thu Nov 17 11:56:47 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 126196 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 35D71B7224 for ; Thu, 17 Nov 2011 22:57:13 +1100 (EST) Received: (qmail 9087 invoked by alias); 17 Nov 2011 11:57:10 -0000 Received: (qmail 9077 invoked by uid 22791); 17 Nov 2011 11:57:09 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-gx0-f175.google.com (HELO mail-gx0-f175.google.com) (209.85.161.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 17 Nov 2011 11:56:54 +0000 Received: by ggnh4 with SMTP id h4so1021375ggn.20 for ; Thu, 17 Nov 2011 03:56:54 -0800 (PST) Received: by 10.236.173.233 with SMTP id v69mr8457702yhl.46.1321531013805; Thu, 17 Nov 2011 03:56:53 -0800 (PST) Received: from bubble.grove.modra.org ([115.187.252.19]) by mx.google.com with ESMTPS id y71sm5623781yhh.6.2011.11.17.03.56.51 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 17 Nov 2011 03:56:53 -0800 (PST) Received: by bubble.grove.modra.org (Postfix, from userid 1000) id BCD2A170C2BF; Thu, 17 Nov 2011 22:26:47 +1030 (CST) Date: Thu, 17 Nov 2011 22:26:47 +1030 From: Alan Modra To: gcc-patches@gcc.gnu.org Subject: RFC: powerpc libgomp locking Message-ID: <20111117115647.GB14325@bubble.grove.modra.org> Mail-Followup-To: gcc-patches@gcc.gnu.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This is part of work in progress getting locking back into shape on powerpc. (If we ever were in shape, which is doubtful.) Using the ia64 version means we have a needless sync at the start of gomp_mutex_lock (courtesy of __sync_val_compare_and_swap) and a needless isync at the end of gomp_mutex_unlock (slightly odd use of __sync_lock_test_and_set to release a lock). It would be nice to get rid of the __sync_val_compare_and_swap in config/linux/mutex.c too. * config/linux/powerpc/mutex.h: Rewrite. Index: libgomp/config/linux/powerpc/mutex.h =================================================================== --- libgomp/config/linux/powerpc/mutex.h (revision 181425) +++ libgomp/config/linux/powerpc/mutex.h (working copy) @@ -1,2 +1,75 @@ -/* On PowerPC __sync_lock_test_and_set isn't a full barrier. */ -#include "config/linux/ia64/mutex.h" +/* Copyright (C) 2011 Free Software Foundation, Inc. + Contributed by Alan Modra + + This file is part of the GNU OpenMP Library (libgomp). + + Libgomp is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + . */ + +/* This is a Linux specific implementation of a mutex synchronization + mechanism for libgomp. This type is private to the library. This + implementation uses atomic instructions and the futex syscall. */ + +#ifndef GOMP_MUTEX_H +#define GOMP_MUTEX_H 1 + +typedef int gomp_mutex_t; +extern void gomp_mutex_lock_slow (gomp_mutex_t *mutex, int); +extern void gomp_mutex_unlock_slow (gomp_mutex_t *mutex); + +#define GOMP_MUTEX_INIT_0 1 + +enum memmodel +{ + MEMMODEL_RELAXED = 0, + MEMMODEL_CONSUME = 1, + MEMMODEL_ACQUIRE = 2, + MEMMODEL_RELEASE = 3, + MEMMODEL_ACQ_REL = 4, + MEMMODEL_SEQ_CST = 5, + MEMMODEL_LAST = 6 +}; + +static inline void gomp_mutex_init (gomp_mutex_t *mutex) +{ + *mutex = 0; +} + +static inline void gomp_mutex_destroy (gomp_mutex_t *mutex) +{ +} + +static inline void gomp_mutex_lock (gomp_mutex_t *mutex) +{ + int oldval = 0; + + __atomic_compare_exchange_4 (mutex, &oldval, 1, false, + MEMMODEL_ACQUIRE, MEMMODEL_RELAXED); + if (__builtin_expect (oldval != 0, 0)) + gomp_mutex_lock_slow (mutex, oldval); +} + +static inline void gomp_mutex_unlock (gomp_mutex_t *mutex) +{ + int oldval = __atomic_exchange_4 (mutex, 0, MEMMODEL_RELEASE); + + if (__builtin_expect (oldval > 1, 0)) + gomp_mutex_unlock_slow (mutex); +} +#endif /* GOMP_MUTEX_H */