lock.c 3.1 KB

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