| 1 |
/* Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Kai Tietz <[email protected]>. |
| 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 |
#define UWORD __shadow_UWORD |
| 26 |
#define WIN32_LEAN_AND_MEAN |
| 27 |
#include <windows.h> |
| 28 |
#undef UWORD |
| 29 |
#include "libatomic_i.h" |
| 30 |
|
| 31 |
/* The target page size. Must be no larger than the runtime page size, |
| 32 |
lest locking fail with virtual address aliasing (i.e. a page mmaped |
| 33 |
at two locations). */ |
| 34 |
#ifndef PAGE_SIZE |
| 35 |
#define PAGE_SIZE 4096 |
| 36 |
#endif |
| 37 |
|
| 38 |
/* The target cacheline size. This is an optimization; the padding that |
| 39 |
should be applied to the locks to keep them from interfering. */ |
| 40 |
#ifndef CACHLINE_SIZE |
| 41 |
#define CACHLINE_SIZE 64 |
| 42 |
#endif |
| 43 |
|
| 44 |
/* The granularity at which locks are applied. Almost certainly the |
| 45 |
cachline size is the right thing to use here. */ |
| 46 |
#ifndef WATCH_SIZE |
| 47 |
#define WATCH_SIZE CACHLINE_SIZE |
| 48 |
#endif |
| 49 |
|
| 50 |
struct lock |
| 51 |
{ |
| 52 |
HANDLE mutex; |
| 53 |
char pad[sizeof (HANDLE) < CACHLINE_SIZE |
| 54 |
? CACHLINE_SIZE - sizeof (HANDLE) |
| 55 |
: 0]; |
| 56 |
}; |
| 57 |
|
| 58 |
#define NLOCKS (PAGE_SIZE / WATCH_SIZE) |
| 59 |
|
| 60 |
static struct lock locks[NLOCKS] = { |
| 61 |
[0 ... NLOCKS-1].mutex = NULL |
| 62 |
}; |
| 63 |
|
| 64 |
static inline uintptr_t |
| 65 |
addr_hash (void *ptr) |
| 66 |
{ |
| 67 |
return ((uintptr_t)ptr / WATCH_SIZE) % NLOCKS; |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
libat_lock_1 (void *ptr) |
| 72 |
{ |
| 73 |
if (!locks[addr_hash (ptr)].mutex) |
| 74 |
locks[addr_hash (ptr)].mutex = CreateMutex (NULL, FALSE, NULL); |
| 75 |
WaitForSingleObject (locks[addr_hash (ptr)].mutex, INFINITE); |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
libat_unlock_1 (void *ptr) |
| 80 |
{ |
| 81 |
if (locks[addr_hash (ptr)].mutex) |
| 82 |
ReleaseMutex (locks[addr_hash (ptr)].mutex); |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
libat_lock_n (void *ptr, size_t n) |
| 87 |
{ |
| 88 |
uintptr_t h = addr_hash (ptr); |
| 89 |
size_t i = 0; |
| 90 |
size_t nlocks |
| 91 |
= (n + ((uintptr_t)ptr % WATCH_SIZE) + WATCH_SIZE - 1) / WATCH_SIZE; |
| 92 |
|
| 93 |
/* Don't lock more than all the locks we have. */ |
| 94 |
if (nlocks > NLOCKS) |
| 95 |
nlocks = NLOCKS; |
| 96 |
|
| 97 |
if (__builtin_expect (h + nlocks > NLOCKS, 0)) |
| 98 |
{ |
| 99 |
size_t j = h + nlocks - NLOCKS; |
| 100 |
for (; i < j; ++i) |
| 101 |
{ |
| 102 |
if (!locks[i].mutex) |
| 103 |
locks[i].mutex = CreateMutex (NULL, FALSE, NULL); |
| 104 |
WaitForSingleObject (locks[i].mutex, INFINITE); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
for (; i < nlocks; ++i) |
| 109 |
{ |
| 110 |
if (!locks[h].mutex) |
| 111 |
locks[h].mutex = CreateMutex (NULL, FALSE, NULL); |
| 112 |
WaitForSingleObject (locks[h++].mutex, INFINITE); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
libat_unlock_n (void *ptr, size_t n) |
| 118 |
{ |
| 119 |
uintptr_t h = addr_hash (ptr); |
| 120 |
size_t i = 0; |
| 121 |
size_t nlocks |
| 122 |
= (n + ((uintptr_t)ptr % WATCH_SIZE) + WATCH_SIZE - 1) / WATCH_SIZE; |
| 123 |
|
| 124 |
/* Don't lock more than all the locks we have. */ |
| 125 |
if (nlocks > NLOCKS) |
| 126 |
nlocks = NLOCKS; |
| 127 |
|
| 128 |
if (__builtin_expect (h + nlocks > NLOCKS, 0)) |
| 129 |
{ |
| 130 |
size_t j = h + nlocks - NLOCKS; |
| 131 |
for (; i < j; ++i) |
| 132 |
if (locks[i].mutex) |
| 133 |
ReleaseMutex (locks[i].mutex); |
| 134 |
} |
| 135 |
|
| 136 |
for (; i < nlocks; ++i, ++h) |
| 137 |
if (locks[h].mutex) |
| 138 |
ReleaseMutex (locks[h].mutex); |
| 139 |
} |