]> git.ipfire.org Git - thirdparty/gcc.git/blame - libatomic/config/mingw/lock.c
Update copyright years.
[thirdparty/gcc.git] / libatomic / config / mingw / lock.c
CommitLineData
a945c346 1/* Copyright (C) 2014-2024 Free Software Foundation, Inc.
dc3368d0
KT
2 Contributed by Kai Tietz <ktietz@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#define UWORD __shadow_UWORD
902c7559 26#define WIN32_LEAN_AND_MEAN
dc3368d0
KT
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
50struct 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
60static struct lock locks[NLOCKS] = {
61 [0 ... NLOCKS-1].mutex = NULL
62};
63
64static inline uintptr_t
65addr_hash (void *ptr)
66{
67 return ((uintptr_t)ptr / WATCH_SIZE) % NLOCKS;
68}
69
70void
71libat_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
78void
79libat_unlock_1 (void *ptr)
80{
81 if (locks[addr_hash (ptr)].mutex)
82 ReleaseMutex (locks[addr_hash (ptr)].mutex);
83}
84
85void
86libat_lock_n (void *ptr, size_t n)
87{
88 uintptr_t h = addr_hash (ptr);
89 size_t i = 0;
90
91 /* Don't lock more than all the locks we have. */
92 if (n > PAGE_SIZE)
93 n = PAGE_SIZE;
94
95 do
96 {
97 if (!locks[h].mutex)
98 locks[h].mutex = CreateMutex (NULL, FALSE, NULL);
99 WaitForSingleObject (locks[h].mutex, INFINITE);
100 if (++h == NLOCKS)
101 h = 0;
102 i += WATCH_SIZE;
103 }
104 while (i < n);
105}
106
107void
108libat_unlock_n (void *ptr, size_t n)
109{
110 uintptr_t h = addr_hash (ptr);
111 size_t i = 0;
112
113 if (n > PAGE_SIZE)
114 n = PAGE_SIZE;
115
116 do
117 {
118 if (locks[h].mutex)
119 ReleaseMutex (locks[h].mutex);
120 if (++h == NLOCKS)
121 h = 0;
122 i += WATCH_SIZE;
123 }
124 while (i < n);
125}