lock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <pthread.h>
  21. /* The target page size. Must be no larger than the runtime page size,
  22. lest locking fail with virtual address aliasing (i.e. a page mmaped
  23. at two locations). */
  24. #ifndef PAGE_SIZE
  25. #define PAGE_SIZE 4096
  26. #endif
  27. /* The target cacheline size. This is an optimization; the padding that
  28. should be applied to the locks to keep them from interfering. */
  29. #ifndef CACHLINE_SIZE
  30. #define CACHLINE_SIZE 64
  31. #endif
  32. /* The granularity at which locks are applied. Almost certainly the
  33. cachline size is the right thing to use here. */
  34. #ifndef WATCH_SIZE
  35. #define WATCH_SIZE CACHLINE_SIZE
  36. #endif
  37. struct lock
  38. {
  39. pthread_mutex_t mutex;
  40. char pad[sizeof(pthread_mutex_t) < CACHLINE_SIZE
  41. ? CACHLINE_SIZE - sizeof(pthread_mutex_t)
  42. : 0];
  43. };
  44. #define NLOCKS (PAGE_SIZE / WATCH_SIZE)
  45. static struct lock locks[NLOCKS] = {
  46. [0 ... NLOCKS-1].mutex = PTHREAD_MUTEX_INITIALIZER
  47. };
  48. static inline uintptr_t
  49. addr_hash (void *ptr)
  50. {
  51. return ((uintptr_t)ptr / WATCH_SIZE) % NLOCKS;
  52. }
  53. void
  54. libat_lock_1 (void *ptr)
  55. {
  56. pthread_mutex_lock (&locks[addr_hash (ptr)].mutex);
  57. }
  58. void
  59. libat_unlock_1 (void *ptr)
  60. {
  61. pthread_mutex_unlock (&locks[addr_hash (ptr)].mutex);
  62. }
  63. void
  64. libat_lock_n (void *ptr, size_t n)
  65. {
  66. uintptr_t h = addr_hash (ptr);
  67. size_t i = 0;
  68. /* Don't lock more than all the locks we have. */
  69. if (n > PAGE_SIZE)
  70. n = PAGE_SIZE;
  71. do
  72. {
  73. pthread_mutex_lock (&locks[h].mutex);
  74. if (++h == NLOCKS)
  75. h = 0;
  76. i += WATCH_SIZE;
  77. }
  78. while (i < n);
  79. }
  80. void
  81. libat_unlock_n (void *ptr, size_t n)
  82. {
  83. uintptr_t h = addr_hash (ptr);
  84. size_t i = 0;
  85. if (n > PAGE_SIZE)
  86. n = PAGE_SIZE;
  87. do
  88. {
  89. pthread_mutex_unlock (&locks[h].mutex);
  90. if (++h == NLOCKS)
  91. h = 0;
  92. i += WATCH_SIZE;
  93. }
  94. while (i < n);
  95. }