annotate libatomic/config/mingw/lock.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Copyright (C) 2014-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
2 Contributed by Kai Tietz <ktietz@redhat.com>.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of the GNU Atomic Library (libatomic).
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 Libatomic is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
7 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
kono
parents:
diff changeset
9 (at your option) any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kono
parents:
diff changeset
14 more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #define UWORD __shadow_UWORD
kono
parents:
diff changeset
26 #include <windows.h>
kono
parents:
diff changeset
27 #undef UWORD
kono
parents:
diff changeset
28 #include "libatomic_i.h"
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 /* The target page size. Must be no larger than the runtime page size,
kono
parents:
diff changeset
31 lest locking fail with virtual address aliasing (i.e. a page mmaped
kono
parents:
diff changeset
32 at two locations). */
kono
parents:
diff changeset
33 #ifndef PAGE_SIZE
kono
parents:
diff changeset
34 #define PAGE_SIZE 4096
kono
parents:
diff changeset
35 #endif
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 /* The target cacheline size. This is an optimization; the padding that
kono
parents:
diff changeset
38 should be applied to the locks to keep them from interfering. */
kono
parents:
diff changeset
39 #ifndef CACHLINE_SIZE
kono
parents:
diff changeset
40 #define CACHLINE_SIZE 64
kono
parents:
diff changeset
41 #endif
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* The granularity at which locks are applied. Almost certainly the
kono
parents:
diff changeset
44 cachline size is the right thing to use here. */
kono
parents:
diff changeset
45 #ifndef WATCH_SIZE
kono
parents:
diff changeset
46 #define WATCH_SIZE CACHLINE_SIZE
kono
parents:
diff changeset
47 #endif
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 struct lock
kono
parents:
diff changeset
50 {
kono
parents:
diff changeset
51 HANDLE mutex;
kono
parents:
diff changeset
52 char pad[sizeof (HANDLE) < CACHLINE_SIZE
kono
parents:
diff changeset
53 ? CACHLINE_SIZE - sizeof (HANDLE)
kono
parents:
diff changeset
54 : 0];
kono
parents:
diff changeset
55 };
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 #define NLOCKS (PAGE_SIZE / WATCH_SIZE)
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 static struct lock locks[NLOCKS] = {
kono
parents:
diff changeset
60 [0 ... NLOCKS-1].mutex = NULL
kono
parents:
diff changeset
61 };
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 static inline uintptr_t
kono
parents:
diff changeset
64 addr_hash (void *ptr)
kono
parents:
diff changeset
65 {
kono
parents:
diff changeset
66 return ((uintptr_t)ptr / WATCH_SIZE) % NLOCKS;
kono
parents:
diff changeset
67 }
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 void
kono
parents:
diff changeset
70 libat_lock_1 (void *ptr)
kono
parents:
diff changeset
71 {
kono
parents:
diff changeset
72 if (!locks[addr_hash (ptr)].mutex)
kono
parents:
diff changeset
73 locks[addr_hash (ptr)].mutex = CreateMutex (NULL, FALSE, NULL);
kono
parents:
diff changeset
74 WaitForSingleObject (locks[addr_hash (ptr)].mutex, INFINITE);
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 void
kono
parents:
diff changeset
78 libat_unlock_1 (void *ptr)
kono
parents:
diff changeset
79 {
kono
parents:
diff changeset
80 if (locks[addr_hash (ptr)].mutex)
kono
parents:
diff changeset
81 ReleaseMutex (locks[addr_hash (ptr)].mutex);
kono
parents:
diff changeset
82 }
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 void
kono
parents:
diff changeset
85 libat_lock_n (void *ptr, size_t n)
kono
parents:
diff changeset
86 {
kono
parents:
diff changeset
87 uintptr_t h = addr_hash (ptr);
kono
parents:
diff changeset
88 size_t i = 0;
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 /* Don't lock more than all the locks we have. */
kono
parents:
diff changeset
91 if (n > PAGE_SIZE)
kono
parents:
diff changeset
92 n = PAGE_SIZE;
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 do
kono
parents:
diff changeset
95 {
kono
parents:
diff changeset
96 if (!locks[h].mutex)
kono
parents:
diff changeset
97 locks[h].mutex = CreateMutex (NULL, FALSE, NULL);
kono
parents:
diff changeset
98 WaitForSingleObject (locks[h].mutex, INFINITE);
kono
parents:
diff changeset
99 if (++h == NLOCKS)
kono
parents:
diff changeset
100 h = 0;
kono
parents:
diff changeset
101 i += WATCH_SIZE;
kono
parents:
diff changeset
102 }
kono
parents:
diff changeset
103 while (i < n);
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 void
kono
parents:
diff changeset
107 libat_unlock_n (void *ptr, size_t n)
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 uintptr_t h = addr_hash (ptr);
kono
parents:
diff changeset
110 size_t i = 0;
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 if (n > PAGE_SIZE)
kono
parents:
diff changeset
113 n = PAGE_SIZE;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 do
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 if (locks[h].mutex)
kono
parents:
diff changeset
118 ReleaseMutex (locks[h].mutex);
kono
parents:
diff changeset
119 if (++h == NLOCKS)
kono
parents:
diff changeset
120 h = 0;
kono
parents:
diff changeset
121 i += WATCH_SIZE;
kono
parents:
diff changeset
122 }
kono
parents:
diff changeset
123 while (i < n);
kono
parents:
diff changeset
124 }