| 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.
|
|---|
| 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 | #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 |
|
|---|
| 37 | static char jobserver_semaphore_name[MAX_PATH + 1];
|
|---|
| 38 | static HANDLE jobserver_semaphore = NULL;
|
|---|
| 39 |
|
|---|
| 40 | unsigned int
|
|---|
| 41 | jobserver_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 |
|
|---|
| 72 | unsigned int
|
|---|
| 73 | jobserver_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 |
|
|---|
| 93 | char *
|
|---|
| 94 | jobserver_get_auth ()
|
|---|
| 95 | {
|
|---|
| 96 | return xstrdup (jobserver_semaphore_name);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | unsigned int
|
|---|
| 100 | jobserver_enabled ()
|
|---|
| 101 | {
|
|---|
| 102 | return jobserver_semaphore != NULL;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /* Close jobserver semaphore */
|
|---|
| 106 | void
|
|---|
| 107 | jobserver_clear ()
|
|---|
| 108 | {
|
|---|
| 109 | if (jobserver_semaphore != NULL)
|
|---|
| 110 | {
|
|---|
| 111 | CloseHandle (jobserver_semaphore);
|
|---|
| 112 | jobserver_semaphore = NULL;
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void
|
|---|
| 117 | jobserver_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 |
|
|---|
| 135 | unsigned int
|
|---|
| 136 | jobserver_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 |
|
|---|
| 152 | void
|
|---|
| 153 | jobserver_signal ()
|
|---|
| 154 | {
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void jobserver_pre_child (int recursive)
|
|---|
| 158 | {
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void jobserver_post_child (int recursive)
|
|---|
| 162 | {
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | void
|
|---|
| 166 | jobserver_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. */
|
|---|
| 172 | unsigned int
|
|---|
| 173 | jobserver_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 | #else
|
|---|
| 190 | /* Add the completed children event as the 2nd one. */
|
|---|
| 191 | handles[1] = (HANDLE)MkWinChildGetCompleteEventHandle();
|
|---|
| 192 | dwHandleCount = 2;
|
|---|
| 193 | #endif
|
|---|
| 194 |
|
|---|
| 195 | dwEvent = WaitForMultipleObjects (
|
|---|
| 196 | dwHandleCount, /* number of objects in array */
|
|---|
| 197 | handles, /* array of objects */
|
|---|
| 198 | FALSE, /* wait for any object */
|
|---|
| 199 | INFINITE); /* wait until object is signalled */
|
|---|
| 200 |
|
|---|
| 201 | if (dwEvent == WAIT_FAILED)
|
|---|
| 202 | {
|
|---|
| 203 | DWORD err = GetLastError ();
|
|---|
| 204 | const char *estr = map_windows32_error_to_string (err);
|
|---|
| 205 | ONS (fatal, NILF,
|
|---|
| 206 | _("semaphore or child process wait: (Error %ld: %s)"),
|
|---|
| 207 | err, estr);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /* WAIT_OBJECT_0 indicates that the semaphore was signalled. */
|
|---|
| 211 | return dwEvent == WAIT_OBJECT_0;
|
|---|
| 212 | }
|
|---|