glfree.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2012-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Atomic Library (libatomic).
  4. Libatomic is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libatomic_i.h"
  20. #define EXACT(N) \
  21. do { \
  22. if (!C2(HAVE_INT,N)) break; \
  23. if ((uintptr_t)ptr & (N - 1)) break; \
  24. if (__atomic_always_lock_free(N, 0)) return true; \
  25. if (C2(MAYBE_HAVE_ATOMIC_CAS_,N)) return true; \
  26. } while (0)
  27. #define LARGER(N) \
  28. do { \
  29. uintptr_t r = (uintptr_t)ptr & (N - 1); \
  30. if (!C2(HAVE_INT,N)) break; \
  31. if (!C2(HAVE_ATOMIC_LDST_,N)) break; \
  32. if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break; \
  33. if (r + n <= N) return true; \
  34. } while (0)
  35. bool
  36. libat_is_lock_free (size_t n, void *ptr)
  37. {
  38. switch (n)
  39. {
  40. case 0: return true;
  41. case 1: EXACT(1); goto L4;
  42. case 2: EXACT(2); goto L4;
  43. case 4: EXACT(4); goto L8;
  44. case 8: EXACT(8); goto L16;
  45. case 16: EXACT(16); break;
  46. case 3: L4: LARGER(4); /* FALLTHRU */
  47. case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
  48. case 9 ... 15: L16: LARGER(16); break;
  49. }
  50. return false;
  51. }
  52. EXPORT_ALIAS (is_lock_free);