1 /* Windows32-based operating system interface for GNU Make.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of GNU Make.
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
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.
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/>. */
25 #include "pathstuff.h"
31 /* This section provides OS-specific functions to support the jobserver. */
33 static char jobserver_semaphore_name[MAX_PATH + 1];
34 static HANDLE jobserver_semaphore = NULL;
37 jobserver_setup (int slots)
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). */
43 if (slots >= MAXIMUM_WAIT_OBJECTS)
45 slots = MAXIMUM_WAIT_OBJECTS - 1;
46 DB (DB_JOBS, (_("Jobserver slots limited to %d\n"), slots));
49 sprintf (jobserver_semaphore_name, "gmake_semaphore_%d", _getpid ());
51 jobserver_semaphore = CreateSemaphore (
52 NULL, /* Use default security descriptor */
53 slots, /* Initial count */
54 slots, /* Maximum count */
55 jobserver_semaphore_name); /* Semaphore name */
57 if (jobserver_semaphore == NULL)
59 DWORD err = GetLastError ();
60 const char *estr = map_windows32_error_to_string (err);
62 _("creating jobserver semaphore: (Error %ld: %s)"), err, estr);
69 jobserver_parse_auth (const char *auth)
71 jobserver_semaphore = OpenSemaphore (
72 SEMAPHORE_ALL_ACCESS, /* Semaphore access setting */
73 FALSE, /* Child processes DON'T inherit */
74 auth); /* Semaphore name */
76 if (jobserver_semaphore == NULL)
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)"),
84 DB (DB_JOBS, (_("Jobserver client (semaphore %s)\n"), auth));
92 return xstrdup (jobserver_semaphore_name);
98 return jobserver_semaphore != NULL;
101 /* Close jobserver semaphore */
105 if (jobserver_semaphore != NULL)
107 CloseHandle (jobserver_semaphore);
108 jobserver_semaphore = NULL;
113 jobserver_release (int is_fatal)
115 if (! ReleaseSemaphore (
116 jobserver_semaphore, /* handle to semaphore */
117 1, /* increase count by one */
118 NULL)) /* not interested in previous count */
122 DWORD err = GetLastError ();
123 const char *estr = map_windows32_error_to_string (err);
125 _("release jobserver semaphore: (Error %ld: %s)"), err, estr);
127 perror_with_name ("release_jobserver_semaphore", "");
132 jobserver_acquire_all ()
134 unsigned int tokens = 0;
137 DWORD dwEvent = WaitForSingleObject (
138 jobserver_semaphore, /* Handle to semaphore */
139 0); /* DON'T wait on semaphore */
141 if (dwEvent != WAIT_OBJECT_0)
153 void jobserver_pre_child (int recursive)
157 void jobserver_post_child (int recursive)
162 jobserver_pre_acquire ()
166 /* Returns 1 if we got a token, or 0 if a child has completed.
167 The Windows implementation doesn't support load detection. */
169 jobserver_acquire (int timeout)
171 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
175 /* Add jobserver semaphore to first slot. */
176 handles[0] = jobserver_semaphore;
178 /* Build array of handles to wait for. */
179 dwHandleCount = 1 + process_set_handles (&handles[1]);
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 */
187 if (dwEvent == WAIT_FAILED)
189 DWORD err = GetLastError ();
190 const char *estr = map_windows32_error_to_string (err);
192 _("semaphore or child process wait: (Error %ld: %s)"),
196 /* WAIT_OBJECT_0 indicates that the semaphore was signalled. */
197 return dwEvent == WAIT_OBJECT_0;