1/* Copyright (C) 2012-2015 Free Software Foundation, Inc.
2   Contributed by Richard Henderson <rth@redhat.com>.
3
4   This file is part of the GNU Atomic Library (libatomic).
5
6   Libatomic is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14   more details.
15
16   Under Section 7 of GPL version 3, you are granted additional
17   permissions described in the GCC Runtime Library Exception, version
18   3.1, as published by the Free Software Foundation.
19
20   You should have received a copy of the GNU General Public License and
21   a copy of the GCC Runtime Library Exception along with this program;
22   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23   <http://www.gnu.org/licenses/>.  */
24
25#include "libatomic_i.h"
26
27
28/* If we support the builtin, just use it.  */
29#if !DONE && defined(atomic_compare_exchange_n)
30bool
31SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval,
32			      int smodel, int fmodel UNUSED)
33{
34  if (maybe_specialcase_relaxed(smodel))
35    return atomic_compare_exchange_n (mptr, eptr, newval, false,
36				      __ATOMIC_RELAXED, __ATOMIC_RELAXED);
37  else if (maybe_specialcase_acqrel(smodel))
38    return atomic_compare_exchange_n (mptr, eptr, newval, false,
39				      __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
40  else
41    return atomic_compare_exchange_n (mptr, eptr, newval, false,
42				      __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
43}
44
45#define DONE 1
46#endif /* HAVE_ATOMIC_CAS */
47
48
49/* If this type is not larger than word-sized, fall back to a word-sized
50   compare-and-swap loop, possibly assisted by the OS.  */
51#if !DONE && N <= WORDSIZE && defined(atomic_compare_exchange_w)
52bool
53SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval,
54			      int smodel, int fmodel)
55{
56  UWORD mask, shift, weval, woldval, wnewval, t, *wptr;
57
58  pre_barrier (smodel);
59
60  if (N < WORDSIZE)
61    {
62      wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE);
63      shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK);
64      mask = SIZE(MASK) << shift;
65    }
66  else
67    {
68      wptr = (UWORD *)mptr;
69      shift = 0;
70      mask = -1;
71    }
72
73  weval = (UWORD)*eptr << shift;
74  wnewval = (UWORD)newval << shift;
75  woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
76  do
77    {
78      if ((woldval & mask) != weval)
79	goto failure;
80      t = (woldval & ~mask) | wnewval;
81    }
82  while (!atomic_compare_exchange_w (wptr, &woldval, t, true,
83				     __ATOMIC_RELAXED, __ATOMIC_RELAXED));
84  post_barrier (smodel);
85  return true;
86
87 failure:
88  *eptr = woldval >> shift;
89  post_barrier (fmodel);
90  return false;
91}
92
93#define DONE 1
94#endif /* HAVE_ATOMIC_CAS && N <= WORDSIZE */
95
96
97/* Otherwise, fall back to some sort of protection mechanism.  */
98#if !DONE
99bool
100SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval,
101			      int smodel, int fmodel UNUSED)
102{
103  UTYPE oldval;
104  UWORD magic;
105  bool ret;
106
107  pre_seq_barrier (smodel);
108  magic = protect_start (mptr);
109
110  oldval = *mptr;
111  ret = (oldval == *eptr);
112  if (ret)
113    *mptr = newval;
114  else
115    *eptr = oldval;
116
117  protect_end (mptr, magic);
118  post_seq_barrier (smodel);
119
120  return ret;
121}
122#endif
123
124EXPORT_ALIAS (SIZE(compare_exchange));
125