Debian Codegraph @

src/w32/w32os.c

raw symbols SCIP
1 /* Windows32-based operating system interface for GNU Make.
2 Copyright (C) 2016-2020 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "makeint.h"
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <windows.h>
23 #include <process.h>
24 #include <io.h>
25 #include "pathstuff.h"
26 #include "sub_proc.h"
27 #include "w32err.h"
28 #include "os.h"
29 #include "debug.h"
30
31 /* This section provides OS-specific functions to support the jobserver. */
32
33 static char jobserver_semaphore_name[MAX_PATH + 1];
34 static HANDLE jobserver_semaphore = NULL;
35
36 unsigned int
37 jobserver_setup (int slots)
38 {
39 /* sub_proc.c is limited in the number of objects it can wait for. */
40
41 if (slots > process_table_usable_size())
42 {
43 slots = process_table_usable_size();
44 DB (DB_JOBS, (_("Jobserver slots limited to %d\n"), slots));
45 }
46
47 sprintf (jobserver_semaphore_name, "gmake_semaphore_%d", _getpid ());
48
49 jobserver_semaphore = CreateSemaphore (
50 NULL, /* Use default security descriptor */
51 slots, /* Initial count */
52 slots, /* Maximum count */
53 jobserver_semaphore_name); /* Semaphore name */
54
55 if (jobserver_semaphore == NULL)
56 {
57 DWORD err = GetLastError ();
58 const char *estr = map_windows32_error_to_string (err);
59 ONS (fatal, NILF,
60 _("creating jobserver semaphore: (Error %ld: %s)"), err, estr);
61 }
62
63 return 1;
64 }
65
66 unsigned int
67 jobserver_parse_auth (const char *auth)
68 {
69 jobserver_semaphore = OpenSemaphore (
70 SEMAPHORE_ALL_ACCESS, /* Semaphore access setting */
71 FALSE, /* Child processes DON'T inherit */
72 auth); /* Semaphore name */
73
74 if (jobserver_semaphore == NULL)
75 {
76 DWORD err = GetLastError ();
77 const char *estr = map_windows32_error_to_string (err);
78 fatal (NILF, strlen (auth) + INTSTR_LENGTH + strlen (estr),
79 _("internal error: unable to open jobserver semaphore '%s': (Error %ld: %s)"),
80 auth, err, estr);
81 }
82 DB (DB_JOBS, (_("Jobserver client (semaphore %s)\n"), auth));
83
84 return 1;
85 }
86
87 char *
88 jobserver_get_auth ()
89 {
90 return xstrdup (jobserver_semaphore_name);
91 }
92
93 unsigned int
94 jobserver_enabled ()
95 {
96 return jobserver_semaphore != NULL;
97 }
98
99 /* Close jobserver semaphore */
100 void
101 jobserver_clear ()
102 {
103 if (jobserver_semaphore != NULL)
104 {
105 CloseHandle (jobserver_semaphore);
106 jobserver_semaphore = NULL;
107 }
108 }
109
110 void
111 jobserver_release (int is_fatal)
112 {
113 if (! ReleaseSemaphore (
114 jobserver_semaphore, /* handle to semaphore */
115 1, /* increase count by one */
116 NULL)) /* not interested in previous count */
117 {
118 if (is_fatal)
119 {
120 DWORD err = GetLastError ();
121 const char *estr = map_windows32_error_to_string (err);
122 ONS (fatal, NILF,
123 _("release jobserver semaphore: (Error %ld: %s)"), err, estr);
124 }
125 perror_with_name ("release_jobserver_semaphore", "");
126 }
127 }
128
129 unsigned int
130 jobserver_acquire_all ()
131 {
132 unsigned int tokens = 0;
133 while (1)
134 {
135 DWORD dwEvent = WaitForSingleObject (
136 jobserver_semaphore, /* Handle to semaphore */
137 0); /* DON'T wait on semaphore */
138
139 if (dwEvent != WAIT_OBJECT_0)
140 return tokens;
141
142 ++tokens;
143 }
144 }
145
146 void
147 jobserver_signal ()
148 {
149 }
150
151 void jobserver_pre_child (int recursive)
152 {
153 }
154
155 void jobserver_post_child (int recursive)
156 {
157 }
158
159 void
160 jobserver_pre_acquire ()
161 {
162 }
163
164 /* Returns 1 if we got a token, or 0 if a child has completed.
165 The Windows implementation doesn't support load detection. */
166 unsigned int
167 jobserver_acquire (int timeout)
168 {
169 HANDLE *handles;
170 DWORD dwHandleCount;
171 DWORD dwEvent;
172
173 handles = xmalloc(process_table_actual_size() * sizeof(HANDLE));
174
175 /* Add jobserver semaphore to first slot. */
176 handles[0] = jobserver_semaphore;
177
178 /* Build array of handles to wait for. */
179 dwHandleCount = 1 + process_set_handles (&handles[1]);
180
181 dwEvent = process_wait_for_multiple_objects (
182 dwHandleCount, /* number of objects in array */
183 handles, /* array of objects */
184 FALSE, /* wait for any object */
185 INFINITE); /* wait until object is signalled */
186
187 free(handles);
188
189 if (dwEvent == WAIT_FAILED)
190 {
191 DWORD err = GetLastError ();
192 const char *estr = map_windows32_error_to_string (err);
193 ONS (fatal, NILF,
194 _("semaphore or child process wait: (Error %ld: %s)"),
195 err, estr);
196 }
197
198 /* WAIT_OBJECT_0 indicates that the semaphore was signalled. */
199 return dwEvent == WAIT_OBJECT_0;
200 }
201
202 void
203 fd_inherit(int fd)
204 {
205 HANDLE fh = (HANDLE)_get_osfhandle(fd);
206
207 if (fh && fh != INVALID_HANDLE_VALUE)
208 SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 1);
209 }
210
211 void
212 fd_noinherit(int fd)
213 {
214 HANDLE fh = (HANDLE)_get_osfhandle(fd);
215
216 if (fh && fh != INVALID_HANDLE_VALUE)
217 SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 0);
218 }