|
| 1 | +// Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "tokenpool.h" |
| 16 | + |
| 17 | +#include <errno.h> |
| 18 | +#include <fcntl.h> |
| 19 | +#include <poll.h> |
| 20 | +#include <unistd.h> |
| 21 | +#include <signal.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <string.h> |
| 24 | +#include <stdlib.h> |
| 25 | + |
| 26 | +// TokenPool implementation for GNU make jobserver |
| 27 | +// (http://make.mad-scientist.net/papers/jobserver-implementation/) |
| 28 | +struct GNUmakeTokenPool : public TokenPool { |
| 29 | + GNUmakeTokenPool(); |
| 30 | + virtual ~GNUmakeTokenPool(); |
| 31 | + |
| 32 | + virtual bool Acquire(); |
| 33 | + virtual void Reserve(); |
| 34 | + virtual void Release(); |
| 35 | + virtual void Clear(); |
| 36 | + |
| 37 | + bool Setup(); |
| 38 | + |
| 39 | + private: |
| 40 | + int available_; |
| 41 | + int used_; |
| 42 | + |
| 43 | +#ifdef _WIN32 |
| 44 | + // @TODO |
| 45 | +#else |
| 46 | + int rfd_; |
| 47 | + int wfd_; |
| 48 | + |
| 49 | + struct sigaction old_act_; |
| 50 | + bool restore_; |
| 51 | + |
| 52 | + static int dup_rfd_; |
| 53 | + static void CloseDupRfd(int signum); |
| 54 | + |
| 55 | + bool CheckFd(int fd); |
| 56 | + bool SetAlarmHandler(); |
| 57 | +#endif |
| 58 | + |
| 59 | + void Return(); |
| 60 | +}; |
| 61 | + |
| 62 | +// every instance owns an implicit token -> available_ == 1 |
| 63 | +GNUmakeTokenPool::GNUmakeTokenPool() : available_(1), used_(0), |
| 64 | + rfd_(-1), wfd_(-1), restore_(false) { |
| 65 | +} |
| 66 | + |
| 67 | +GNUmakeTokenPool::~GNUmakeTokenPool() { |
| 68 | + Clear(); |
| 69 | + if (restore_) |
| 70 | + sigaction(SIGALRM, &old_act_, NULL); |
| 71 | +} |
| 72 | + |
| 73 | +bool GNUmakeTokenPool::CheckFd(int fd) { |
| 74 | + if (fd < 0) |
| 75 | + return false; |
| 76 | + int ret = fcntl(fd, F_GETFD); |
| 77 | + if (ret < 0) |
| 78 | + return false; |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +int GNUmakeTokenPool::dup_rfd_ = -1; |
| 83 | + |
| 84 | +void GNUmakeTokenPool::CloseDupRfd(int signum) { |
| 85 | + close(dup_rfd_); |
| 86 | + dup_rfd_ = -1; |
| 87 | +} |
| 88 | + |
| 89 | +bool GNUmakeTokenPool::SetAlarmHandler() { |
| 90 | + struct sigaction act; |
| 91 | + memset(&act, 0, sizeof(act)); |
| 92 | + act.sa_handler = CloseDupRfd; |
| 93 | + if (sigaction(SIGALRM, &act, &old_act_) < 0) { |
| 94 | + perror("sigaction:"); |
| 95 | + return(false); |
| 96 | + } else { |
| 97 | + restore_ = true; |
| 98 | + return(true); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +bool GNUmakeTokenPool::Setup() { |
| 103 | + const char *value = getenv("MAKEFLAGS"); |
| 104 | + if (value) { |
| 105 | + // GNU make <= 4.1 |
| 106 | + const char *jobserver = strstr(value, "--jobserver-fds="); |
| 107 | + // GNU make => 4.2 |
| 108 | + if (!jobserver) |
| 109 | + jobserver = strstr(value, "--jobserver-auth="); |
| 110 | + if (jobserver) { |
| 111 | + int rfd = -1; |
| 112 | + int wfd = -1; |
| 113 | + if ((sscanf(jobserver, "%*[^=]=%d,%d", &rfd, &wfd) == 2) && |
| 114 | + CheckFd(rfd) && |
| 115 | + CheckFd(wfd) && |
| 116 | + SetAlarmHandler()) { |
| 117 | + printf("ninja: using GNU make jobserver.\n"); |
| 118 | + rfd_ = rfd; |
| 119 | + wfd_ = wfd; |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return false; |
| 126 | +} |
| 127 | + |
| 128 | +bool GNUmakeTokenPool::Acquire() { |
| 129 | + if (available_ > 0) |
| 130 | + return true; |
| 131 | + |
| 132 | +#ifdef USE_PPOLL |
| 133 | + pollfd pollfds[] = {{rfd_, POLLIN, 0}}; |
| 134 | + int ret = poll(pollfds, 1, 0); |
| 135 | +#else |
| 136 | + fd_set set; |
| 137 | + struct timeval timeout = { 0, 0 }; |
| 138 | + FD_ZERO(&set); |
| 139 | + FD_SET(rfd_, &set); |
| 140 | + int ret = select(rfd_ + 1, &set, NULL, NULL, &timeout); |
| 141 | +#endif |
| 142 | + if (ret > 0) { |
| 143 | + dup_rfd_ = dup(rfd_); |
| 144 | + |
| 145 | + if (dup_rfd_ != -1) { |
| 146 | + struct sigaction act, old_act; |
| 147 | + int ret = 0; |
| 148 | + |
| 149 | + memset(&act, 0, sizeof(act)); |
| 150 | + act.sa_handler = CloseDupRfd; |
| 151 | + if (sigaction(SIGCHLD, &act, &old_act) == 0) { |
| 152 | + char buf; |
| 153 | + |
| 154 | + // block until token read, child exits or timeout |
| 155 | + alarm(1); |
| 156 | + ret = read(dup_rfd_, &buf, 1); |
| 157 | + alarm(0); |
| 158 | + |
| 159 | + sigaction(SIGCHLD, &old_act, NULL); |
| 160 | + } |
| 161 | + |
| 162 | + CloseDupRfd(0); |
| 163 | + |
| 164 | + if (ret > 0) { |
| 165 | + available_++; |
| 166 | + return true; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + return false; |
| 171 | +} |
| 172 | + |
| 173 | +void GNUmakeTokenPool::Reserve() { |
| 174 | + available_--; |
| 175 | + used_++; |
| 176 | +} |
| 177 | + |
| 178 | +void GNUmakeTokenPool::Return() { |
| 179 | + const char buf = '+'; |
| 180 | + while (1) { |
| 181 | + int ret = write(wfd_, &buf, 1); |
| 182 | + if (ret > 0) |
| 183 | + available_--; |
| 184 | + if ((ret != -1) || (errno != EINTR)) |
| 185 | + return; |
| 186 | + // write got interrupted - retry |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +void GNUmakeTokenPool::Release() { |
| 191 | + available_++; |
| 192 | + used_--; |
| 193 | + if (available_ > 1) |
| 194 | + Return(); |
| 195 | +} |
| 196 | + |
| 197 | +void GNUmakeTokenPool::Clear() { |
| 198 | + while (used_ > 0) |
| 199 | + Release(); |
| 200 | + while (available_ > 1) |
| 201 | + Return(); |
| 202 | +} |
| 203 | + |
| 204 | +struct TokenPool *TokenPool::Get(void) { |
| 205 | + GNUmakeTokenPool *tokenpool = new GNUmakeTokenPool; |
| 206 | + if (tokenpool->Setup()) |
| 207 | + return tokenpool; |
| 208 | + else |
| 209 | + delete tokenpool; |
| 210 | + return NULL; |
| 211 | +} |
0 commit comments