diff options
author | Richard Henderson <rth@redhat.com> | 2012-05-01 08:48:28 -0700 |
---|---|---|
committer | Richard Henderson <rth@gcc.gnu.org> | 2012-05-01 08:48:28 -0700 |
commit | 483104922ad93037381da8f37a8b2bb471799650 (patch) | |
tree | 729dc91fa97f38042aa7777ca6267ebed2edb3e8 /libatomic/config/posix | |
parent | e8053cf5b5b8d982efd4d77d9922ac63fcf478d2 (diff) | |
download | gcc-483104922ad93037381da8f37a8b2bb471799650.zip gcc-483104922ad93037381da8f37a8b2bb471799650.tar.gz gcc-483104922ad93037381da8f37a8b2bb471799650.tar.bz2 |
Add libatomic as a target library.
From-SVN: r187018
Diffstat (limited to 'libatomic/config/posix')
-rw-r--r-- | libatomic/config/posix/host-config.h | 55 | ||||
-rw-r--r-- | libatomic/config/posix/lock.c | 116 |
2 files changed, 171 insertions, 0 deletions
diff --git a/libatomic/config/posix/host-config.h b/libatomic/config/posix/host-config.h new file mode 100644 index 0000000..a296fff --- /dev/null +++ b/libatomic/config/posix/host-config.h @@ -0,0 +1,55 @@ +/* Copyright (C) 2012 Free Software Foundation, Inc. + Contributed by Richard Henderson <rth@redhat.com>. + + This file is part of the GNU Atomic Library (libatomic). + + Libatomic 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 of the License, or + (at your option) any later version. + + Libatomic 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 + <http://www.gnu.org/licenses/>. */ + +/* Included after all more target-specific host-config.h. */ + + +#ifndef protect_start_end +# ifdef HAVE_ATTRIBUTE_VISIBILITY +# pragma GCC visibility push(hidden) +# endif + +void libat_lock_1 (void *ptr); +void libat_unlock_1 (void *ptr); + +static inline UWORD +protect_start (void *ptr) +{ + libat_lock_1 (ptr); + return 0; +} + +static inline void +protect_end (void *ptr, UWORD dummy UNUSED) +{ + libat_unlock_1 (ptr); +} + +# define protect_start_end 1 +# ifdef HAVE_ATTRIBUTE_VISIBILITY +# pragma GCC visibility pop +# endif +#endif /* protect_start_end */ + +#include_next <host-config.h> diff --git a/libatomic/config/posix/lock.c b/libatomic/config/posix/lock.c new file mode 100644 index 0000000..e57d333 --- /dev/null +++ b/libatomic/config/posix/lock.c @@ -0,0 +1,116 @@ +/* Copyright (C) 2012 Free Software Foundation, Inc. + Contributed by Richard Henderson <rth@redhat.com>. + + This file is part of the GNU Atomic Library (libatomic). + + Libatomic 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 of the License, or + (at your option) any later version. + + Libatomic 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 + <http://www.gnu.org/licenses/>. */ + +#include "libatomic_i.h" +#include <pthread.h> + + +/* The target page size. Must be no larger than the runtime page size, + lest locking fail with virtual address aliasing (i.e. a page mmaped + at two locations). */ +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif + +/* The target cacheline size. This is an optimization; the padding that + should be applied to the locks to keep them from interfering. */ +#ifndef CACHLINE_SIZE +#define CACHLINE_SIZE 64 +#endif + +/* The granularity at which locks are applied. Almost certainly the + cachline size is the right thing to use here. */ +#ifndef WATCH_SIZE +#define WATCH_SIZE CACHLINE_SIZE +#endif + +struct lock +{ + pthread_mutex_t mutex; + char pad[sizeof(pthread_mutex_t) < CACHLINE_SIZE + ? CACHLINE_SIZE - sizeof(pthread_mutex_t) + : 0]; +}; + +#define NLOCKS (PAGE_SIZE / WATCH_SIZE) +static struct lock locks[NLOCKS] = { + [0 ... NLOCKS-1].mutex = PTHREAD_MUTEX_INITIALIZER +}; + +static inline uintptr_t +addr_hash (void *ptr) +{ + return ((uintptr_t)ptr / WATCH_SIZE) % NLOCKS; +} + +void +libat_lock_1 (void *ptr) +{ + pthread_mutex_lock (&locks[addr_hash (ptr)].mutex); +} + +void +libat_unlock_1 (void *ptr) +{ + pthread_mutex_unlock (&locks[addr_hash (ptr)].mutex); +} + +void +libat_lock_n (void *ptr, size_t n) +{ + uintptr_t h = addr_hash (ptr); + size_t i = 0; + + /* Don't lock more than all the locks we have. */ + if (n > PAGE_SIZE) + n = PAGE_SIZE; + + do + { + pthread_mutex_lock (&locks[h].mutex); + if (++h == NLOCKS) + h = 0; + i += WATCH_SIZE; + } + while (i < n); +} + +void +libat_unlock_n (void *ptr, size_t n) +{ + uintptr_t h = addr_hash (ptr); + size_t i = 0; + + if (n > PAGE_SIZE) + n = PAGE_SIZE; + + do + { + pthread_mutex_unlock (&locks[h].mutex); + if (++h == NLOCKS) + h = 0; + i += WATCH_SIZE; + } + while (i < n); +} |