VirtualBox

source: kBuild/trunk/src/kmk/w32/w32os.c@ 3186

Last change on this file since 3186 was 3173, checked in by bird, 7 years ago

kmkbultin: environment fixes and stats.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette