VirtualBox

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

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

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

  • Property svn:eol-style set to native
File size: 5.3 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#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
33static char jobserver_semaphore_name[MAX_PATH + 1];
34static HANDLE jobserver_semaphore = NULL;
35
36unsigned int
37jobserver_setup (int slots)
38{
39 /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS objects
40 * and one of them is the job-server semaphore object. Limit the
41 * number of available job slots to (MAXIMUM_WAIT_OBJECTS - 1). */
42
43 if (slots >= MAXIMUM_WAIT_OBJECTS)
44 {
45 slots = MAXIMUM_WAIT_OBJECTS - 1;
46 DB (DB_JOBS, (_("Jobserver slots limited to %d\n"), slots));
47 }
48
49 sprintf (jobserver_semaphore_name, "gmake_semaphore_%d", _getpid ());
50
51 jobserver_semaphore = CreateSemaphore (
52 NULL, /* Use default security descriptor */
53 slots, /* Initial count */
54 slots, /* Maximum count */
55 jobserver_semaphore_name); /* Semaphore name */
56
57 if (jobserver_semaphore == NULL)
58 {
59 DWORD err = GetLastError ();
60 const char *estr = map_windows32_error_to_string (err);
61 ONS (fatal, NILF,
62 _("creating jobserver semaphore: (Error %ld: %s)"), err, estr);
63 }
64
65 return 1;
66}
67
68unsigned int
69jobserver_parse_auth (const char *auth)
70{
71 jobserver_semaphore = OpenSemaphore (
72 SEMAPHORE_ALL_ACCESS, /* Semaphore access setting */
73 FALSE, /* Child processes DON'T inherit */
74 auth); /* Semaphore name */
75
76 if (jobserver_semaphore == NULL)
77 {
78 DWORD err = GetLastError ();
79 const char *estr = map_windows32_error_to_string (err);
80 fatal (NILF, strlen (auth) + INTSTR_LENGTH + strlen (estr),
81 _("internal error: unable to open jobserver semaphore '%s': (Error %ld: %s)"),
82 auth, err, estr);
83 }
84 DB (DB_JOBS, (_("Jobserver client (semaphore %s)\n"), auth));
85
86 return 1;
87}
88
89char *
90jobserver_get_auth ()
91{
92 return xstrdup (jobserver_semaphore_name);
93}
94
95unsigned int
96jobserver_enabled ()
97{
98 return jobserver_semaphore != NULL;
99}
100
101/* Close jobserver semaphore */
102void
103jobserver_clear ()
104{
105 if (jobserver_semaphore != NULL)
106 {
107 CloseHandle (jobserver_semaphore);
108 jobserver_semaphore = NULL;
109 }
110}
111
112void
113jobserver_release (int is_fatal)
114{
115 if (! ReleaseSemaphore (
116 jobserver_semaphore, /* handle to semaphore */
117 1, /* increase count by one */
118 NULL)) /* not interested in previous count */
119 {
120 if (is_fatal)
121 {
122 DWORD err = GetLastError ();
123 const char *estr = map_windows32_error_to_string (err);
124 ONS (fatal, NILF,
125 _("release jobserver semaphore: (Error %ld: %s)"), err, estr);
126 }
127 perror_with_name ("release_jobserver_semaphore", "");
128 }
129}
130
131unsigned int
132jobserver_acquire_all ()
133{
134 unsigned int tokens = 0;
135 while (1)
136 {
137 DWORD dwEvent = WaitForSingleObject (
138 jobserver_semaphore, /* Handle to semaphore */
139 0); /* DON'T wait on semaphore */
140
141 if (dwEvent != WAIT_OBJECT_0)
142 return tokens;
143
144 ++tokens;
145 }
146}
147
148void
149jobserver_signal ()
150{
151}
152
153void jobserver_pre_child (int recursive)
154{
155}
156
157void jobserver_post_child (int recursive)
158{
159}
160
161void
162jobserver_pre_acquire ()
163{
164}
165
166/* Returns 1 if we got a token, or 0 if a child has completed.
167 The Windows implementation doesn't support load detection. */
168unsigned int
169jobserver_acquire (int timeout)
170{
171 HANDLE handles[MAXIMUM_WAIT_OBJECTS + 1]; /* bird: + 1 to prevent trashing the stack. */
172 DWORD dwHandleCount;
173 DWORD dwEvent;
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 = WaitForMultipleObjects (
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 if (dwEvent == WAIT_FAILED)
188 {
189 DWORD err = GetLastError ();
190 const char *estr = map_windows32_error_to_string (err);
191 ONS (fatal, NILF,
192 _("semaphore or child process wait: (Error %ld: %s)"),
193 err, estr);
194 }
195
196 /* WAIT_OBJECT_0 indicates that the semaphore was signalled. */
197 return dwEvent == WAIT_OBJECT_0;
198}
Note: See TracBrowser for help on using the repository browser.

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