├── .github ├── actions │ └── compile-make │ │ └── action.yml ├── dependabot.yml └── workflows │ ├── main.yml │ └── publish.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── src ├── error.rs ├── lib.rs ├── unix.rs ├── wasm.rs └── windows.rs └── tests ├── client-of-myself.rs ├── client.rs ├── helper.rs ├── make-as-a-client.rs └── server.rs /.github/actions/compile-make/action.yml: -------------------------------------------------------------------------------- 1 | name: Compile make 2 | description: compile-make 3 | inputs: 4 | version: 5 | description: make version 6 | required: true 7 | workaround: 8 | description: enable workaround for _alloc bug 9 | required: false 10 | default: "false" 11 | 12 | runs: 13 | using: composite 14 | steps: 15 | - name: Cache make compiled 16 | if: ${{ !startsWith(runner.os, 'windows') }} 17 | id: cache-maka 18 | uses: actions/cache@v4 19 | with: 20 | path: /usr/local/bin/make-${{ inputs.version }} 21 | key: v1-${{ runner.os }}-make-${{ inputs.version }} 22 | 23 | - name: Make GNU Make from source 24 | if: ${{ !startsWith(runner.os, 'windows') && steps.cache-make.outputs.cache-hit != 'true' }} 25 | env: 26 | VERSION: ${{ inputs.version }} 27 | WORKAROUND: ${{ inputs.workaround }} 28 | shell: bash 29 | run: | 30 | curl "https://ftp.gnu.org/gnu/make/make-${VERSION}.tar.gz" | tar xz 31 | pushd "make-${VERSION}" 32 | ./configure 33 | [[ "$WORKAROUND" = "true" ]] && sed -i 's/#if !defined __alloca \&\& !defined __GNU_LIBRARY__/#if !defined __alloca \&\& defined __GNU_LIBRARY__/g; s/#ifndef __GNU_LIBRARY__/#ifdef __GNU_LIBRARY__/g' "./glob/glob.c" 34 | make -j 4 35 | popd 36 | cp -p "make-${VERSION}/make" "/usr/local/bin/make-${VERSION}" 37 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: cargo 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "08:00" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | 4 | env: 5 | CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse 6 | 7 | jobs: 8 | test: 9 | name: Test 10 | runs-on: ${{ matrix.os }} 11 | strategy: 12 | matrix: 13 | rust: [stable, beta, nightly] 14 | os: [ubuntu-latest, macos-14, windows-latest] 15 | steps: 16 | - uses: actions/checkout@master 17 | - name: Install Rust (rustup) 18 | run: | 19 | rustup toolchain install ${{ matrix.rust }} --no-self-update --profile minimal 20 | rustup default ${{ matrix.rust }} 21 | shell: bash 22 | 23 | - uses: Swatinem/rust-cache@v2 24 | 25 | - run: cargo test --locked 26 | 27 | - name: Compile make 4.4.1 28 | uses: ./.github/actions/compile-make 29 | with: 30 | version: 4.4.1 31 | 32 | - name: Test against GNU Make 4.4.1 33 | if: ${{ !startsWith(matrix.os, 'windows') }} 34 | shell: bash 35 | run: cargo test --locked 36 | env: 37 | MAKE: /usr/local/bin/make-4.4.1 38 | 39 | - name: Ensure wasm32-unknown-unknown be buildable 40 | if: matrix.os == 'ubuntu-latest' 41 | shell: bash 42 | run: | 43 | rustup target add wasm32-unknown-unknown 44 | cargo build --locked --target wasm32-unknown-unknown 45 | 46 | test_musl: 47 | name: Test (stable, alpine-latest) 48 | container: rust:alpine 49 | runs-on: ubuntu-latest 50 | steps: 51 | - uses: actions/checkout@master 52 | 53 | - name: Install make dependencies 54 | run: apk add musl-dev bash curl make tar 55 | 56 | - uses: Swatinem/rust-cache@v2 57 | 58 | - name: Compile make 4.4.1 59 | uses: ./.github/actions/compile-make 60 | with: 61 | version: 4.4.1 62 | 63 | - name: Test against GNU Make 4.4.1 64 | shell: bash 65 | run: cargo test --locked 66 | env: 67 | MAKE: /usr/local/bin/make-4.4.1 68 | 69 | rustfmt: 70 | name: Rustfmt 71 | runs-on: ubuntu-latest 72 | steps: 73 | - uses: actions/checkout@master 74 | - name: Install Rust 75 | run: rustup update stable && rustup default stable && rustup component add rustfmt 76 | - run: cargo fmt -- --check 77 | 78 | publish_docs: 79 | name: Publish Documentation 80 | runs-on: ubuntu-latest 81 | steps: 82 | - uses: actions/checkout@master 83 | - name: Install Rust 84 | run: rustup update stable && rustup default stable 85 | - name: Build documentation 86 | run: cargo doc --no-deps --all-features 87 | - name: Publish documentation 88 | run: | 89 | cd target/doc 90 | git init 91 | git add . 92 | git -c user.name='ci' -c user.email='ci' commit -m init 93 | git push -f -q https://git:${{ secrets.github_token }}@github.com/${{ github.repository }} HEAD:gh-pages 94 | if: github.event_name == 'push' && github.event.ref == 'refs/heads/master' 95 | 96 | msrv: 97 | runs-on: ${{ matrix.os }} 98 | strategy: 99 | matrix: 100 | os: [ubuntu-latest, macos-14, windows-latest] 101 | steps: 102 | - uses: actions/checkout@master 103 | - name: Install Rust (rustup) 104 | run: rustup toolchain install nightly --no-self-update --profile minimal 105 | shell: bash 106 | 107 | - uses: taiki-e/install-action@cargo-hack 108 | - uses: Swatinem/rust-cache@v2 109 | 110 | - run: cargo hack check --lib --rust-version --ignore-private --locked 111 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | tags: 4 | - "*" 5 | 6 | jobs: 7 | publish: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v4 11 | - name: Publish to crates.io 12 | run: | 13 | cargo publish 14 | env: 15 | CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "bitflags" 7 | version = "2.4.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" 10 | 11 | [[package]] 12 | name = "cfg-if" 13 | version = "1.0.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 16 | 17 | [[package]] 18 | name = "cfg_aliases" 19 | version = "0.1.1" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" 22 | 23 | [[package]] 24 | name = "errno" 25 | version = "0.3.8" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" 28 | dependencies = [ 29 | "libc", 30 | "windows-sys", 31 | ] 32 | 33 | [[package]] 34 | name = "fastrand" 35 | version = "2.0.1" 36 | source = "registry+https://github.com/rust-lang/crates.io-index" 37 | checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" 38 | 39 | [[package]] 40 | name = "getrandom" 41 | version = "0.3.2" 42 | source = "registry+https://github.com/rust-lang/crates.io-index" 43 | checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" 44 | dependencies = [ 45 | "cfg-if", 46 | "libc", 47 | "r-efi", 48 | "wasi", 49 | ] 50 | 51 | [[package]] 52 | name = "jobserver" 53 | version = "0.1.33" 54 | dependencies = [ 55 | "getrandom", 56 | "libc", 57 | "nix", 58 | "tempfile", 59 | ] 60 | 61 | [[package]] 62 | name = "libc" 63 | version = "0.2.171" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" 66 | 67 | [[package]] 68 | name = "linux-raw-sys" 69 | version = "0.4.12" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" 72 | 73 | [[package]] 74 | name = "nix" 75 | version = "0.28.0" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" 78 | dependencies = [ 79 | "bitflags", 80 | "cfg-if", 81 | "cfg_aliases", 82 | "libc", 83 | ] 84 | 85 | [[package]] 86 | name = "r-efi" 87 | version = "5.2.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" 90 | 91 | [[package]] 92 | name = "rustix" 93 | version = "0.38.31" 94 | source = "registry+https://github.com/rust-lang/crates.io-index" 95 | checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" 96 | dependencies = [ 97 | "bitflags", 98 | "errno", 99 | "libc", 100 | "linux-raw-sys", 101 | "windows-sys", 102 | ] 103 | 104 | [[package]] 105 | name = "tempfile" 106 | version = "3.10.1" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" 109 | dependencies = [ 110 | "cfg-if", 111 | "fastrand", 112 | "rustix", 113 | "windows-sys", 114 | ] 115 | 116 | [[package]] 117 | name = "wasi" 118 | version = "0.14.2+wasi-0.2.4" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" 121 | dependencies = [ 122 | "wit-bindgen-rt", 123 | ] 124 | 125 | [[package]] 126 | name = "windows-sys" 127 | version = "0.52.0" 128 | source = "registry+https://github.com/rust-lang/crates.io-index" 129 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 130 | dependencies = [ 131 | "windows-targets", 132 | ] 133 | 134 | [[package]] 135 | name = "windows-targets" 136 | version = "0.52.0" 137 | source = "registry+https://github.com/rust-lang/crates.io-index" 138 | checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" 139 | dependencies = [ 140 | "windows_aarch64_gnullvm", 141 | "windows_aarch64_msvc", 142 | "windows_i686_gnu", 143 | "windows_i686_msvc", 144 | "windows_x86_64_gnu", 145 | "windows_x86_64_gnullvm", 146 | "windows_x86_64_msvc", 147 | ] 148 | 149 | [[package]] 150 | name = "windows_aarch64_gnullvm" 151 | version = "0.52.0" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" 154 | 155 | [[package]] 156 | name = "windows_aarch64_msvc" 157 | version = "0.52.0" 158 | source = "registry+https://github.com/rust-lang/crates.io-index" 159 | checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" 160 | 161 | [[package]] 162 | name = "windows_i686_gnu" 163 | version = "0.52.0" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" 166 | 167 | [[package]] 168 | name = "windows_i686_msvc" 169 | version = "0.52.0" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" 172 | 173 | [[package]] 174 | name = "windows_x86_64_gnu" 175 | version = "0.52.0" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" 178 | 179 | [[package]] 180 | name = "windows_x86_64_gnullvm" 181 | version = "0.52.0" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" 184 | 185 | [[package]] 186 | name = "windows_x86_64_msvc" 187 | version = "0.52.0" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" 190 | 191 | [[package]] 192 | name = "wit-bindgen-rt" 193 | version = "0.39.0" 194 | source = "registry+https://github.com/rust-lang/crates.io-index" 195 | checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" 196 | dependencies = [ 197 | "bitflags", 198 | ] 199 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "jobserver" 3 | version = "0.1.33" 4 | authors = ["Alex Crichton "] 5 | license = "MIT OR Apache-2.0" 6 | repository = "https://github.com/rust-lang/jobserver-rs" 7 | homepage = "https://github.com/rust-lang/jobserver-rs" 8 | documentation = "https://docs.rs/jobserver" 9 | description = """ 10 | An implementation of the GNU Make jobserver for Rust. 11 | """ 12 | edition = "2021" 13 | rust-version = "1.63" 14 | 15 | [target.'cfg(unix)'.dependencies] 16 | libc = "0.2.171" 17 | 18 | [target.'cfg(unix)'.dev-dependencies] 19 | nix = { version = "0.28.0", features = ["fs"] } 20 | 21 | [target.'cfg(windows)'.dependencies] 22 | getrandom = { version = "0.3.2", features = ["std"] } 23 | 24 | [dev-dependencies] 25 | tempfile = "3.10.1" 26 | 27 | [[test]] 28 | name = "client" 29 | harness = false 30 | path = "tests/client.rs" 31 | 32 | [[test]] 33 | name = "server" 34 | path = "tests/server.rs" 35 | 36 | [[test]] 37 | name = "client-of-myself" 38 | path = "tests/client-of-myself.rs" 39 | harness = false 40 | 41 | [[test]] 42 | name = "make-as-a-client" 43 | path = "tests/make-as-a-client.rs" 44 | harness = false 45 | 46 | [[test]] 47 | name = "helper" 48 | path = "tests/helper.rs" 49 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Alex Crichton 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jobserver-rs 2 | 3 | An implementation of the GNU Make jobserver for Rust. 4 | 5 | [![crates.io](https://img.shields.io/crates/v/jobserver.svg?maxAge=2592000)](https://crates.io/crates/jobserver) 6 | 7 | [Documentation](https://docs.rs/jobserver) 8 | 9 | ## Usage 10 | 11 | Add this to your `Cargo.toml`: 12 | 13 | ```toml 14 | [dependencies] 15 | jobserver = "0.1" 16 | ``` 17 | 18 | # License 19 | 20 | This project is licensed under either of 21 | 22 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or 23 | https://www.apache.org/licenses/LICENSE-2.0) 24 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or 25 | https://opensource.org/license/mit) 26 | 27 | at your option. 28 | 29 | ### Contribution 30 | 31 | Unless you explicitly state otherwise, any contribution intentionally submitted 32 | for inclusion in jobserver-rs by you, as defined in the Apache-2.0 license, 33 | shall be dual licensed as above, without any additional terms or conditions. 34 | -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | #[cfg(unix)] 2 | type RawFd = std::os::unix::io::RawFd; 3 | #[cfg(not(unix))] 4 | type RawFd = std::convert::Infallible; 5 | 6 | /// Error type for [`Client::from_env_ext`] function. 7 | /// 8 | /// [`Client::from_env_ext`]: crate::Client::from_env_ext 9 | #[derive(Debug)] 10 | pub struct FromEnvError { 11 | pub(crate) inner: FromEnvErrorInner, 12 | } 13 | 14 | /// Kind of an error returned from [`Client::from_env_ext`] function. 15 | /// 16 | /// [`Client::from_env_ext`]: crate::Client::from_env_ext 17 | #[derive(Debug)] 18 | #[non_exhaustive] 19 | pub enum FromEnvErrorKind { 20 | /// There is no environment variable that describes jobserver to inherit. 21 | NoEnvVar, 22 | /// There is no jobserver in the environment variable. 23 | /// Variables associated with Make can be used for passing data other than jobserver info. 24 | NoJobserver, 25 | /// Cannot parse jobserver environment variable value, incorrect format. 26 | CannotParse, 27 | /// Cannot open path or name from the jobserver environment variable value. 28 | CannotOpenPath, 29 | /// Cannot open file descriptor from the jobserver environment variable value. 30 | CannotOpenFd, 31 | /// The jobserver style is a simple pipe, but at least one of the file descriptors 32 | /// is negative, which means it is disabled for this process 33 | /// ([GNU `make` manual: POSIX Jobserver Interaction](https://www.gnu.org/software/make/manual/make.html#POSIX-Jobserver)). 34 | NegativeFd, 35 | /// File descriptor from the jobserver environment variable value is not a pipe. 36 | NotAPipe, 37 | /// Jobserver inheritance is not supported on this platform. 38 | Unsupported, 39 | } 40 | 41 | impl FromEnvError { 42 | /// Get the error kind. 43 | pub fn kind(&self) -> FromEnvErrorKind { 44 | match self.inner { 45 | FromEnvErrorInner::NoEnvVar => FromEnvErrorKind::NoEnvVar, 46 | FromEnvErrorInner::NoJobserver => FromEnvErrorKind::NoJobserver, 47 | FromEnvErrorInner::CannotParse(_) => FromEnvErrorKind::CannotParse, 48 | FromEnvErrorInner::CannotOpenPath(..) => FromEnvErrorKind::CannotOpenPath, 49 | FromEnvErrorInner::CannotOpenFd(..) => FromEnvErrorKind::CannotOpenFd, 50 | FromEnvErrorInner::NegativeFd(..) => FromEnvErrorKind::NegativeFd, 51 | FromEnvErrorInner::NotAPipe(..) => FromEnvErrorKind::NotAPipe, 52 | FromEnvErrorInner::Unsupported => FromEnvErrorKind::Unsupported, 53 | } 54 | } 55 | } 56 | 57 | impl std::fmt::Display for FromEnvError { 58 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 59 | match &self.inner { 60 | FromEnvErrorInner::NoEnvVar => write!(f, "there is no environment variable that describes jobserver to inherit"), 61 | FromEnvErrorInner::NoJobserver => write!(f, "there is no `--jobserver-fds=` or `--jobserver-auth=` in the environment variable"), 62 | FromEnvErrorInner::CannotParse(s) => write!(f, "cannot parse jobserver environment variable value: {s}"), 63 | FromEnvErrorInner::CannotOpenPath(s, err) => write!(f, "cannot open path or name {s} from the jobserver environment variable value: {err}"), 64 | FromEnvErrorInner::CannotOpenFd(fd, err) => write!(f, "cannot open file descriptor {fd} from the jobserver environment variable value: {err}"), 65 | FromEnvErrorInner::NegativeFd(fd) => write!(f, "file descriptor {fd} from the jobserver environment variable value is negative"), 66 | FromEnvErrorInner::NotAPipe(fd, None) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe"), 67 | FromEnvErrorInner::NotAPipe(fd, Some(err)) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe: {err}"), 68 | FromEnvErrorInner::Unsupported => write!(f, "jobserver inheritance is not supported on this platform"), 69 | } 70 | } 71 | } 72 | impl std::error::Error for FromEnvError { 73 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 74 | match &self.inner { 75 | FromEnvErrorInner::CannotOpenPath(_, err) => Some(err), 76 | FromEnvErrorInner::NotAPipe(_, Some(err)) | FromEnvErrorInner::CannotOpenFd(_, err) => { 77 | Some(err) 78 | } 79 | _ => None, 80 | } 81 | } 82 | } 83 | 84 | #[allow(dead_code)] 85 | #[derive(Debug)] 86 | pub(crate) enum FromEnvErrorInner { 87 | NoEnvVar, 88 | NoJobserver, 89 | CannotParse(String), 90 | CannotOpenPath(String, std::io::Error), 91 | CannotOpenFd(RawFd, std::io::Error), 92 | NegativeFd(RawFd), 93 | NotAPipe(RawFd, Option), 94 | Unsupported, 95 | } 96 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! An implementation of the GNU make jobserver. 2 | //! 3 | //! This crate is an implementation, in Rust, of the GNU `make` jobserver for 4 | //! CLI tools that are interoperating with make or otherwise require some form 5 | //! of parallelism limiting across process boundaries. This was originally 6 | //! written for usage in Cargo to both (a) work when `cargo` is invoked from 7 | //! `make` (using `make`'s jobserver) and (b) work when `cargo` invokes build 8 | //! scripts, exporting a jobserver implementation for `make` processes to 9 | //! transitively use. 10 | //! 11 | //! The jobserver implementation can be found in [detail online][docs] but 12 | //! basically boils down to a cross-process semaphore. On Unix this is 13 | //! implemented with the `pipe` syscall and read/write ends of a pipe and on 14 | //! Windows this is implemented literally with IPC semaphores. Starting from 15 | //! GNU `make` version 4.4, named pipe becomes the default way in communication 16 | //! on Unix. This crate also supports that feature in the sense of inheriting 17 | //! and forwarding the correct environment. 18 | //! 19 | //! The jobserver protocol in `make` also dictates when tokens are acquired to 20 | //! run child work, and clients using this crate should take care to implement 21 | //! such details to ensure correct interoperation with `make` itself. 22 | //! 23 | //! ## Examples 24 | //! 25 | //! Connect to a jobserver that was set up by `make` or a different process: 26 | //! 27 | //! ```no_run 28 | //! use jobserver::Client; 29 | //! 30 | //! // See API documentation for why this is `unsafe` 31 | //! let client = match unsafe { Client::from_env() } { 32 | //! Some(client) => client, 33 | //! None => panic!("client not configured"), 34 | //! }; 35 | //! ``` 36 | //! 37 | //! Acquire and release token from a jobserver: 38 | //! 39 | //! ```no_run 40 | //! use jobserver::Client; 41 | //! 42 | //! let client = unsafe { Client::from_env().unwrap() }; 43 | //! let token = client.acquire().unwrap(); // blocks until it is available 44 | //! drop(token); // releases the token when the work is done 45 | //! ``` 46 | //! 47 | //! Create a new jobserver and configure a child process to have access: 48 | //! 49 | //! ``` 50 | //! use std::process::Command; 51 | //! use jobserver::Client; 52 | //! 53 | //! let client = Client::new(4).expect("failed to create jobserver"); 54 | //! let mut cmd = Command::new("make"); 55 | //! client.configure(&mut cmd); 56 | //! ``` 57 | //! 58 | //! ## Caveats 59 | //! 60 | //! This crate makes no attempt to release tokens back to a jobserver on 61 | //! abnormal exit of a process. If a process which acquires a token is killed 62 | //! with ctrl-c or some similar signal then tokens will not be released and the 63 | //! jobserver may be in a corrupt state. 64 | //! 65 | //! Note that this is typically ok as ctrl-c means that an entire build process 66 | //! is being torn down, but it's worth being aware of at least! 67 | //! 68 | //! ## Windows caveats 69 | //! 70 | //! There appear to be two implementations of `make` on Windows. On MSYS2 one 71 | //! typically comes as `mingw32-make` and the other as `make` itself. I'm not 72 | //! personally too familiar with what's going on here, but for jobserver-related 73 | //! information the `mingw32-make` implementation uses Windows semaphores 74 | //! whereas the `make` program does not. The `make` program appears to use file 75 | //! descriptors and I'm not really sure how it works, so this crate is not 76 | //! compatible with `make` on Windows. It is, however, compatible with 77 | //! `mingw32-make`. 78 | //! 79 | //! [docs]: https://make.mad-scientist.net/papers/jobserver-implementation/ 80 | 81 | #![deny(missing_docs, missing_debug_implementations)] 82 | #![doc(html_root_url = "https://docs.rs/jobserver/0.1")] 83 | 84 | use std::env; 85 | use std::ffi::OsString; 86 | use std::io; 87 | use std::process::Command; 88 | use std::sync::{Arc, Condvar, Mutex, MutexGuard}; 89 | 90 | mod error; 91 | #[cfg(unix)] 92 | #[path = "unix.rs"] 93 | mod imp; 94 | #[cfg(windows)] 95 | #[path = "windows.rs"] 96 | mod imp; 97 | #[cfg(not(any(unix, windows)))] 98 | #[path = "wasm.rs"] 99 | mod imp; 100 | 101 | /// A client of a jobserver 102 | /// 103 | /// This structure is the main type exposed by this library, and is where 104 | /// interaction to a jobserver is configured through. Clients are either created 105 | /// from scratch in which case the internal semphore is initialied on the spot, 106 | /// or a client is created from the environment to connect to a jobserver 107 | /// already created. 108 | /// 109 | /// Some usage examples can be found in the crate documentation for using a 110 | /// client. 111 | /// 112 | /// Note that a [`Client`] implements the [`Clone`] trait, and all instances of 113 | /// a [`Client`] refer to the same jobserver instance. 114 | #[derive(Clone, Debug)] 115 | pub struct Client { 116 | inner: Arc, 117 | } 118 | 119 | /// An acquired token from a jobserver. 120 | /// 121 | /// This token will be released back to the jobserver when it is dropped and 122 | /// otherwise represents the ability to spawn off another thread of work. 123 | #[derive(Debug)] 124 | pub struct Acquired { 125 | client: Arc, 126 | data: imp::Acquired, 127 | disabled: bool, 128 | } 129 | 130 | impl Acquired { 131 | /// This drops the [`Acquired`] token without releasing the associated token. 132 | /// 133 | /// This is not generally useful, but can be helpful if you do not have the 134 | /// ability to store an Acquired token but need to not yet release it. 135 | /// 136 | /// You'll typically want to follow this up with a call to 137 | /// [`Client::release_raw`] or similar to actually release the token later on. 138 | pub fn drop_without_releasing(mut self) { 139 | self.disabled = true; 140 | } 141 | } 142 | 143 | #[derive(Default, Debug)] 144 | struct HelperState { 145 | lock: Mutex, 146 | cvar: Condvar, 147 | } 148 | 149 | #[derive(Default, Debug)] 150 | struct HelperInner { 151 | requests: usize, 152 | producer_done: bool, 153 | consumer_done: bool, 154 | } 155 | 156 | use error::FromEnvErrorInner; 157 | pub use error::{FromEnvError, FromEnvErrorKind}; 158 | 159 | /// Return type for [`Client::from_env_ext`] function. 160 | #[derive(Debug)] 161 | pub struct FromEnv { 162 | /// Result of trying to get jobserver client from env. 163 | pub client: Result, 164 | /// Name and value of the environment variable. 165 | /// `None` if no relevant environment variable is found. 166 | pub var: Option<(&'static str, OsString)>, 167 | } 168 | 169 | impl FromEnv { 170 | fn new_ok(client: Client, var_name: &'static str, var_value: OsString) -> FromEnv { 171 | FromEnv { 172 | client: Ok(client), 173 | var: Some((var_name, var_value)), 174 | } 175 | } 176 | fn new_err(kind: FromEnvErrorInner, var_name: &'static str, var_value: OsString) -> FromEnv { 177 | FromEnv { 178 | client: Err(FromEnvError { inner: kind }), 179 | var: Some((var_name, var_value)), 180 | } 181 | } 182 | } 183 | 184 | impl Client { 185 | /// Creates a new jobserver initialized with the given parallelism limit. 186 | /// 187 | /// A client to the jobserver created will be returned. This client will 188 | /// allow at most `limit` tokens to be acquired from it in parallel. More 189 | /// calls to [`Client::acquire`] will cause the calling thread to block. 190 | /// 191 | /// Note that the created [`Client`] is not automatically inherited into 192 | /// spawned child processes from this program. Manual usage of the 193 | /// [`Client::configure`] function is required for a child process to have 194 | /// access to a job server. 195 | /// 196 | /// # Examples 197 | /// 198 | /// ``` 199 | /// use jobserver::Client; 200 | /// 201 | /// let client = Client::new(4).expect("failed to create jobserver"); 202 | /// ``` 203 | /// 204 | /// # Errors 205 | /// 206 | /// Returns an error if any I/O error happens when attempting to create the 207 | /// jobserver client. 208 | pub fn new(limit: usize) -> io::Result { 209 | Ok(Client { 210 | inner: Arc::new(imp::Client::new(limit)?), 211 | }) 212 | } 213 | 214 | /// Attempts to connect to the jobserver specified in this process's 215 | /// environment. 216 | /// 217 | /// When the a `make` executable calls a child process it will configure the 218 | /// environment of the child to ensure that it has handles to the jobserver 219 | /// it's passing down. This function will attempt to look for these details 220 | /// and connect to the jobserver. 221 | /// 222 | /// Note that the created [`Client`] is not automatically inherited into 223 | /// spawned child processes from this program. Manual usage of the 224 | /// [`Client::configure`] function is required for a child process to have 225 | /// access to a job server. 226 | /// 227 | /// # Return value 228 | /// 229 | /// [`FromEnv`] contains result and relevant environment variable. 230 | /// If a jobserver was found in the environment and it looks correct then 231 | /// result with the connected client will be returned. In other cases 232 | /// result will contain `Err(FromEnvErr)`. 233 | /// 234 | /// Additionally on Unix this function will configure the file descriptors 235 | /// with `CLOEXEC` so they're not automatically inherited by spawned 236 | /// children. 237 | /// 238 | /// On unix if `check_pipe` enabled this function will check if provided 239 | /// files are actually pipes. 240 | /// 241 | /// # Safety 242 | /// 243 | /// This function is `unsafe` to call on Unix specifically as it 244 | /// transitively requires usage of the `from_raw_fd` function, which is 245 | /// itself unsafe in some circumstances. 246 | /// 247 | /// It's recommended to call this function very early in the lifetime of a 248 | /// program before any other file descriptors are opened. That way you can 249 | /// make sure to take ownership properly of the file descriptors passed 250 | /// down, if any. 251 | /// 252 | /// It is ok to call this function any number of times. 253 | pub unsafe fn from_env_ext(check_pipe: bool) -> FromEnv { 254 | let (env, var_os) = match ["CARGO_MAKEFLAGS", "MAKEFLAGS", "MFLAGS"] 255 | .iter() 256 | .map(|&env| env::var_os(env).map(|var| (env, var))) 257 | .find_map(|p| p) 258 | { 259 | Some((env, var_os)) => (env, var_os), 260 | None => return FromEnv::new_err(FromEnvErrorInner::NoEnvVar, "", Default::default()), 261 | }; 262 | 263 | let var = match var_os.to_str() { 264 | Some(var) => var, 265 | None => { 266 | let err = FromEnvErrorInner::CannotParse("not valid UTF-8".to_string()); 267 | return FromEnv::new_err(err, env, var_os); 268 | } 269 | }; 270 | 271 | let s = match find_jobserver_auth(var) { 272 | Some(s) => s, 273 | None => return FromEnv::new_err(FromEnvErrorInner::NoJobserver, env, var_os), 274 | }; 275 | match imp::Client::open(s, check_pipe) { 276 | Ok(c) => FromEnv::new_ok(Client { inner: Arc::new(c) }, env, var_os), 277 | Err(err) => FromEnv::new_err(err, env, var_os), 278 | } 279 | } 280 | 281 | /// Attempts to connect to the jobserver specified in this process's 282 | /// environment. 283 | /// 284 | /// Wraps [`Client::from_env_ext`] and discards error details. 285 | /// 286 | /// # Safety 287 | /// 288 | /// This function is `unsafe` to call on Unix specifically as it 289 | /// transitively requires usage of the `from_raw_fd` function, which is 290 | /// itself unsafe in some circumstances. 291 | /// 292 | /// It's recommended to call this function very early in the lifetime of a 293 | /// program before any other file descriptors are opened. That way you can 294 | /// make sure to take ownership properly of the file descriptors passed 295 | /// down, if any. 296 | /// 297 | /// It is ok to call this function any number of times. 298 | pub unsafe fn from_env() -> Option { 299 | Self::from_env_ext(false).client.ok() 300 | } 301 | 302 | /// Acquires a token from this jobserver client. 303 | /// 304 | /// This function will block the calling thread until a new token can be 305 | /// acquired from the jobserver. 306 | /// 307 | /// # Return value 308 | /// 309 | /// On successful acquisition of a token an instance of [`Acquired`] is 310 | /// returned. This structure, when dropped, will release the token back to 311 | /// the jobserver. It's recommended to avoid leaking this value. 312 | /// 313 | /// # Errors 314 | /// 315 | /// If an I/O error happens while acquiring a token then this function will 316 | /// return immediately with the error. If an error is returned then a token 317 | /// was not acquired. 318 | pub fn acquire(&self) -> io::Result { 319 | let data = self.inner.acquire()?; 320 | Ok(Acquired { 321 | client: self.inner.clone(), 322 | data, 323 | disabled: false, 324 | }) 325 | } 326 | 327 | /// Acquires a token from this jobserver client in a non-blocking way. 328 | /// 329 | /// # Return value 330 | /// 331 | /// On successful acquisition of a token an instance of [`Acquired`] is 332 | /// returned. This structure, when dropped, will release the token back to 333 | /// the jobserver. It's recommended to avoid leaking this value. 334 | /// 335 | /// # Errors 336 | /// 337 | /// If an I/O error happens while acquiring a token then this function will 338 | /// return immediately with the error. If an error is returned then a token 339 | /// was not acquired. 340 | /// 341 | /// If non-blocking acquire is not supported, the return error will have its `kind()` 342 | /// set to [`io::ErrorKind::Unsupported`]. 343 | pub fn try_acquire(&self) -> io::Result> { 344 | let ret = self.inner.try_acquire()?; 345 | 346 | Ok(ret.map(|data| Acquired { 347 | client: self.inner.clone(), 348 | data, 349 | disabled: false, 350 | })) 351 | } 352 | 353 | /// Returns amount of tokens in the read-side pipe. 354 | /// 355 | /// # Return value 356 | /// 357 | /// Number of bytes available to be read from the jobserver pipe 358 | /// 359 | /// # Errors 360 | /// 361 | /// Underlying errors from the ioctl will be passed up. 362 | pub fn available(&self) -> io::Result { 363 | self.inner.available() 364 | } 365 | 366 | /// Configures a child process to have access to this client's jobserver as 367 | /// well. 368 | /// 369 | /// This function is required to be called to ensure that a jobserver is 370 | /// properly inherited to a child process. If this function is *not* called 371 | /// then this [`Client`] will not be accessible in the child process. In 372 | /// other words, if not called, then [`Client::from_env`] will return `None` 373 | /// in the child process (or the equivalent of [`Client::from_env`] that 374 | /// `make` uses). 375 | /// 376 | /// ## Platform-specific behavior 377 | /// 378 | /// On Unix and Windows this will clobber the `CARGO_MAKEFLAGS` environment 379 | /// variables for the child process, and on Unix this will also allow the 380 | /// two file descriptors for this client to be inherited to the child. 381 | /// 382 | /// On platforms other than Unix and Windows this panics. 383 | pub fn configure(&self, cmd: &mut Command) { 384 | cmd.env("CARGO_MAKEFLAGS", &self.mflags_env()); 385 | self.inner.configure(cmd); 386 | } 387 | 388 | /// Configures a child process to have access to this client's jobserver as 389 | /// well. 390 | /// 391 | /// This function is required to be called to ensure that a jobserver is 392 | /// properly inherited to a child process. If this function is *not* called 393 | /// then this [`Client`] will not be accessible in the child process. In 394 | /// other words, if not called, then [`Client::from_env`] will return `None` 395 | /// in the child process (or the equivalent of [`Client::from_env`] that 396 | /// `make` uses). 397 | /// 398 | /// ## Platform-specific behavior 399 | /// 400 | /// On Unix and Windows this will clobber the `CARGO_MAKEFLAGS`, 401 | /// `MAKEFLAGS` and `MFLAGS` environment variables for the child process, 402 | /// and on Unix this will also allow the two file descriptors for 403 | /// this client to be inherited to the child. 404 | /// 405 | /// On platforms other than Unix and Windows this panics. 406 | pub fn configure_make(&self, cmd: &mut Command) { 407 | let value = self.mflags_env(); 408 | cmd.env("CARGO_MAKEFLAGS", &value); 409 | cmd.env("MAKEFLAGS", &value); 410 | cmd.env("MFLAGS", &value); 411 | self.inner.configure(cmd); 412 | } 413 | 414 | fn mflags_env(&self) -> String { 415 | let arg = self.inner.string_arg(); 416 | // Older implementations of make use `--jobserver-fds` and newer 417 | // implementations use `--jobserver-auth`, pass both to try to catch 418 | // both implementations. 419 | format!("-j --jobserver-fds={0} --jobserver-auth={0}", arg) 420 | } 421 | 422 | /// Converts this [`Client`] into a helper thread to deal with a blocking 423 | /// [`Client::acquire`] function a little more easily. 424 | /// 425 | /// The fact that the [`Client::acquire`] isn't always the easiest to work 426 | /// with. Typically you're using a jobserver to manage running other events 427 | /// in parallel! This means that you need to either (a) wait for an existing 428 | /// job to finish or (b) wait for a new token to become available. 429 | /// 430 | /// Unfortunately the blocking in [`Client::acquire`] happens at the 431 | /// implementation layer of jobservers. On Unix this requires a blocking 432 | /// call to `read` and on Windows this requires one of the `WaitFor*` 433 | /// functions. Both of these situations aren't the easiest to deal with: 434 | /// 435 | /// * On Unix there's basically only one way to wake up a `read` early, and 436 | /// that's through a signal. This is what the `make` implementation 437 | /// itself uses, relying on `SIGCHLD` to wake up a blocking acquisition 438 | /// of a new job token. Unfortunately nonblocking I/O is not an option 439 | /// here, so it means that "waiting for one of two events" means that 440 | /// the latter event must generate a signal! This is not always the case 441 | /// on unix for all jobservers. 442 | /// 443 | /// * On Windows you'd have to basically use the `WaitForMultipleObjects` 444 | /// which means that you've got to canonicalize all your event sources 445 | /// into a `HANDLE` which also isn't the easiest thing to do 446 | /// unfortunately. 447 | /// 448 | /// This function essentially attempts to ease these limitations by 449 | /// converting this [`Client`] into a helper thread spawned into this 450 | /// process. The application can then request that the helper thread 451 | /// acquires tokens and the provided closure will be invoked for each token 452 | /// acquired. 453 | /// 454 | /// The intention is that this function can be used to translate the event 455 | /// of a token acquisition into an arbitrary user-defined event. 456 | /// 457 | /// # Arguments 458 | /// 459 | /// This function will consume the [`Client`] provided to be transferred to 460 | /// the helper thread that is spawned. Additionally a closure `f` is 461 | /// provided to be invoked whenever a token is acquired. 462 | /// 463 | /// This closure is only invoked after calls to 464 | /// [`HelperThread::request_token`] have been made and a token itself has 465 | /// been acquired. If an error happens while acquiring the token then 466 | /// an error will be yielded to the closure as well. 467 | /// 468 | /// # Return Value 469 | /// 470 | /// This function will return an instance of the [`HelperThread`] structure 471 | /// which is used to manage the helper thread associated with this client. 472 | /// Through the [`HelperThread`] you'll request that tokens are acquired. 473 | /// When acquired, the closure provided here is invoked. 474 | /// 475 | /// When the [`HelperThread`] structure is returned it will be gracefully 476 | /// torn down, and the calling thread will be blocked until the thread is 477 | /// torn down (which should be prompt). 478 | /// 479 | /// # Errors 480 | /// 481 | /// This function may fail due to creation of the helper thread or 482 | /// auxiliary I/O objects to manage the helper thread. In any of these 483 | /// situations the error is propagated upwards. 484 | /// 485 | /// # Platform-specific behavior 486 | /// 487 | /// On Windows this function behaves pretty normally as expected, but on 488 | /// Unix the implementation is... a little heinous. As mentioned above 489 | /// we're forced into blocking I/O for token acquisition, namely a blocking 490 | /// call to `read`. We must be able to unblock this, however, to tear down 491 | /// the helper thread gracefully! 492 | /// 493 | /// Essentially what happens is that we'll send a signal to the helper 494 | /// thread spawned and rely on `EINTR` being returned to wake up the helper 495 | /// thread. This involves installing a global `SIGUSR1` handler that does 496 | /// nothing along with sending signals to that thread. This may cause 497 | /// odd behavior in some applications, so it's recommended to review and 498 | /// test thoroughly before using this. 499 | pub fn into_helper_thread(self, f: F) -> io::Result 500 | where 501 | F: FnMut(io::Result) + Send + 'static, 502 | { 503 | let state = Arc::new(HelperState::default()); 504 | Ok(HelperThread { 505 | inner: Some(imp::spawn_helper(self, state.clone(), Box::new(f))?), 506 | state, 507 | }) 508 | } 509 | 510 | /// Blocks the current thread until a token is acquired. 511 | /// 512 | /// This is the same as [`Client::acquire`], except that it doesn't return 513 | /// an RAII helper. If successful the process will need to guarantee that 514 | /// [`Client::release_raw`] is called in the future. 515 | pub fn acquire_raw(&self) -> io::Result<()> { 516 | self.inner.acquire()?; 517 | Ok(()) 518 | } 519 | 520 | /// Releases a jobserver token back to the original jobserver. 521 | /// 522 | /// This is intended to be paired with [`Client::acquire_raw`] if it was 523 | /// called, but in some situations it could also be called to relinquish a 524 | /// process's implicit token temporarily which is then re-acquired later. 525 | pub fn release_raw(&self) -> io::Result<()> { 526 | self.inner.release(None)?; 527 | Ok(()) 528 | } 529 | } 530 | 531 | impl Drop for Acquired { 532 | fn drop(&mut self) { 533 | if !self.disabled { 534 | drop(self.client.release(Some(&self.data))); 535 | } 536 | } 537 | } 538 | 539 | /// Structure returned from [`Client::into_helper_thread`] to manage the lifetime 540 | /// of the helper thread returned, see those associated docs for more info. 541 | #[derive(Debug)] 542 | pub struct HelperThread { 543 | inner: Option, 544 | state: Arc, 545 | } 546 | 547 | impl HelperThread { 548 | /// Request that the helper thread acquires a token, eventually calling the 549 | /// original closure with a token when it's available. 550 | /// 551 | /// For more information, see the docs on [`Client::into_helper_thread`]. 552 | pub fn request_token(&self) { 553 | // Indicate that there's one more request for a token and then wake up 554 | // the helper thread if it's sleeping. 555 | self.state.lock().requests += 1; 556 | self.state.cvar.notify_one(); 557 | } 558 | } 559 | 560 | impl Drop for HelperThread { 561 | fn drop(&mut self) { 562 | // Flag that the producer half is done so the helper thread should exit 563 | // quickly if it's waiting. Wake it up if it's actually waiting 564 | self.state.lock().producer_done = true; 565 | self.state.cvar.notify_one(); 566 | 567 | // ... and afterwards perform any thread cleanup logic 568 | self.inner.take().unwrap().join(); 569 | } 570 | } 571 | 572 | impl HelperState { 573 | fn lock(&self) -> MutexGuard<'_, HelperInner> { 574 | self.lock.lock().unwrap_or_else(|e| e.into_inner()) 575 | } 576 | 577 | /// Executes `f` for each request for a token, where `f` is expected to 578 | /// block and then provide the original closure with a token once it's 579 | /// acquired. 580 | /// 581 | /// This is an infinite loop until the helper thread is dropped, at which 582 | /// point everything should get interrupted. 583 | fn for_each_request(&self, mut f: impl FnMut(&HelperState)) { 584 | let mut lock = self.lock(); 585 | 586 | // We only execute while we could receive requests, but as soon as 587 | // that's `false` we're out of here. 588 | while !lock.producer_done { 589 | // If no one's requested a token then we wait for someone to 590 | // request a token. 591 | if lock.requests == 0 { 592 | lock = self.cvar.wait(lock).unwrap_or_else(|e| e.into_inner()); 593 | continue; 594 | } 595 | 596 | // Consume the request for a token, and then actually acquire a 597 | // token after unlocking our lock (not that acquisition happens in 598 | // `f`). This ensures that we don't actually hold the lock if we 599 | // wait for a long time for a token. 600 | lock.requests -= 1; 601 | drop(lock); 602 | f(self); 603 | lock = self.lock(); 604 | } 605 | lock.consumer_done = true; 606 | self.cvar.notify_one(); 607 | } 608 | } 609 | 610 | /// Finds and returns the value of `--jobserver-auth=` in the given 611 | /// environment variable. 612 | /// 613 | /// Precedence rules: 614 | /// 615 | /// * The last instance wins [^1]. 616 | /// * `--jobserver-fds=` as a fallback when no `--jobserver-auth=` is present [^2]. 617 | /// 618 | /// [^1]: See ["GNU `make` manual: Sharing Job Slots with GNU `make`"](https://www.gnu.org/software/make/manual/make.html#Job-Slots) 619 | /// _"Be aware that the `MAKEFLAGS` variable may contain multiple instances of 620 | /// the `--jobserver-auth=` option. Only the last instance is relevant."_ 621 | /// 622 | /// [^2]: Refer to [the release announcement](https://git.savannah.gnu.org/cgit/make.git/tree/NEWS?h=4.2#n31) 623 | /// of GNU Make 4.2, which states that `--jobserver-fds` was initially an 624 | /// internal-only flag and was later renamed to `--jobserver-auth`. 625 | fn find_jobserver_auth(var: &str) -> Option<&str> { 626 | ["--jobserver-auth=", "--jobserver-fds="] 627 | .iter() 628 | .find_map(|&arg| var.rsplit_once(arg).map(|(_, s)| s)) 629 | .and_then(|s| s.split(' ').next()) 630 | } 631 | 632 | #[cfg(test)] 633 | mod test { 634 | use super::*; 635 | 636 | pub(super) fn run_named_fifo_try_acquire_tests(client: &Client) { 637 | assert!(client.try_acquire().unwrap().is_none()); 638 | client.release_raw().unwrap(); 639 | 640 | let acquired = client.try_acquire().unwrap().unwrap(); 641 | assert!(client.try_acquire().unwrap().is_none()); 642 | 643 | drop(acquired); 644 | client.try_acquire().unwrap().unwrap(); 645 | } 646 | 647 | #[cfg(windows)] 648 | #[test] 649 | fn test_try_acquire() { 650 | let client = Client::new(0).unwrap(); 651 | 652 | run_named_fifo_try_acquire_tests(&client); 653 | } 654 | 655 | #[test] 656 | fn no_helper_deadlock() { 657 | let x = crate::Client::new(32).unwrap(); 658 | let _y = x.clone(); 659 | std::mem::drop(x.into_helper_thread(|_| {}).unwrap()); 660 | } 661 | 662 | #[test] 663 | fn test_find_jobserver_auth() { 664 | let cases = [ 665 | ("", None), 666 | ("-j2", None), 667 | ("-j2 --jobserver-auth=3,4", Some("3,4")), 668 | ("--jobserver-auth=3,4 -j2", Some("3,4")), 669 | ("--jobserver-auth=3,4", Some("3,4")), 670 | ("--jobserver-auth=fifo:/myfifo", Some("fifo:/myfifo")), 671 | ("--jobserver-auth=", Some("")), 672 | ("--jobserver-auth", None), 673 | ("--jobserver-fds=3,4", Some("3,4")), 674 | ("--jobserver-fds=fifo:/myfifo", Some("fifo:/myfifo")), 675 | ("--jobserver-fds=", Some("")), 676 | ("--jobserver-fds", None), 677 | ( 678 | "--jobserver-auth=auth-a --jobserver-auth=auth-b", 679 | Some("auth-b"), 680 | ), 681 | ( 682 | "--jobserver-auth=auth-b --jobserver-auth=auth-a", 683 | Some("auth-a"), 684 | ), 685 | ("--jobserver-fds=fds-a --jobserver-fds=fds-b", Some("fds-b")), 686 | ("--jobserver-fds=fds-b --jobserver-fds=fds-a", Some("fds-a")), 687 | ( 688 | "--jobserver-auth=auth-a --jobserver-fds=fds-a --jobserver-auth=auth-b", 689 | Some("auth-b"), 690 | ), 691 | ( 692 | "--jobserver-fds=fds-a --jobserver-auth=auth-a --jobserver-fds=fds-b", 693 | Some("auth-a"), 694 | ), 695 | ]; 696 | for (var, expected) in cases { 697 | let actual = find_jobserver_auth(var); 698 | assert_eq!( 699 | actual, expected, 700 | "expect {expected:?}, got {actual:?}, input `{var:?}`" 701 | ); 702 | } 703 | } 704 | } 705 | -------------------------------------------------------------------------------- /src/unix.rs: -------------------------------------------------------------------------------- 1 | use libc::c_int; 2 | 3 | use crate::FromEnvErrorInner; 4 | use std::fs::{File, OpenOptions}; 5 | use std::io::{self, Read, Write}; 6 | use std::mem; 7 | use std::mem::MaybeUninit; 8 | use std::os::unix::prelude::*; 9 | use std::path::Path; 10 | use std::process::Command; 11 | use std::ptr; 12 | use std::sync::{ 13 | atomic::{AtomicBool, Ordering}, 14 | Arc, Once, 15 | }; 16 | use std::thread::{self, Builder, JoinHandle}; 17 | use std::time::Duration; 18 | 19 | #[derive(Debug)] 20 | /// This preserves the `--jobserver-auth` type at creation time, 21 | /// so auth type will be passed down to and inherit from sub-Make processes correctly. 22 | /// 23 | /// See for details. 24 | enum ClientCreationArg { 25 | Fds { read: c_int, write: c_int }, 26 | Fifo(Box), 27 | } 28 | 29 | #[derive(Debug)] 30 | pub struct Client { 31 | read: File, 32 | write: File, 33 | creation_arg: ClientCreationArg, 34 | /// It is set to `None` if the pipe is shared with other processes, so it 35 | /// cannot support non-blocking mode. 36 | /// 37 | /// If it is set to `Some`, then it can only go from 38 | /// `Some(false)` -> `Some(true)` but not the other way around, 39 | /// since that could cause a race condition. 40 | is_non_blocking: Option, 41 | } 42 | 43 | #[derive(Debug)] 44 | pub struct Acquired { 45 | byte: u8, 46 | } 47 | 48 | impl Client { 49 | pub fn new(mut limit: usize) -> io::Result { 50 | let client = unsafe { Client::mk()? }; 51 | 52 | // I don't think the character written here matters, but I could be 53 | // wrong! 54 | const BUFFER: [u8; 128] = [b'|'; 128]; 55 | 56 | let mut write = &client.write; 57 | 58 | set_nonblocking(write.as_raw_fd(), true)?; 59 | 60 | while limit > 0 { 61 | let n = limit.min(BUFFER.len()); 62 | 63 | write.write_all(&BUFFER[..n])?; 64 | limit -= n; 65 | } 66 | 67 | set_nonblocking(write.as_raw_fd(), false)?; 68 | 69 | Ok(client) 70 | } 71 | 72 | unsafe fn mk() -> io::Result { 73 | let mut pipes = [0; 2]; 74 | 75 | // Attempt atomically-create-with-cloexec if we can on Linux, 76 | // detected by using the `syscall` function in `libc` to try to work 77 | // with as many kernels/glibc implementations as possible. 78 | #[cfg(target_os = "linux")] 79 | { 80 | static PIPE2_AVAILABLE: AtomicBool = AtomicBool::new(true); 81 | if PIPE2_AVAILABLE.load(Ordering::SeqCst) { 82 | match libc::syscall(libc::SYS_pipe2, pipes.as_mut_ptr(), libc::O_CLOEXEC) { 83 | -1 => { 84 | let err = io::Error::last_os_error(); 85 | if err.raw_os_error() == Some(libc::ENOSYS) { 86 | PIPE2_AVAILABLE.store(false, Ordering::SeqCst); 87 | } else { 88 | return Err(err); 89 | } 90 | } 91 | _ => return Ok(Client::from_fds(pipes[0], pipes[1])), 92 | } 93 | } 94 | } 95 | 96 | cvt(libc::pipe(pipes.as_mut_ptr()))?; 97 | drop(set_cloexec(pipes[0], true)); 98 | drop(set_cloexec(pipes[1], true)); 99 | Ok(Client::from_fds(pipes[0], pipes[1])) 100 | } 101 | 102 | pub(crate) unsafe fn open(s: &str, check_pipe: bool) -> Result { 103 | if let Some(client) = Self::from_fifo(s)? { 104 | return Ok(client); 105 | } 106 | if let Some(client) = Self::from_pipe(s, check_pipe)? { 107 | return Ok(client); 108 | } 109 | Err(FromEnvErrorInner::CannotParse(format!( 110 | "expected `fifo:PATH` or `R,W`, found `{s}`" 111 | ))) 112 | } 113 | 114 | /// `--jobserver-auth=fifo:PATH` 115 | fn from_fifo(s: &str) -> Result, FromEnvErrorInner> { 116 | let mut parts = s.splitn(2, ':'); 117 | if parts.next().unwrap() != "fifo" { 118 | return Ok(None); 119 | } 120 | let path_str = parts.next().ok_or_else(|| { 121 | FromEnvErrorInner::CannotParse("expected a path after `fifo:`".to_string()) 122 | })?; 123 | let path = Path::new(path_str); 124 | 125 | let open_file = || { 126 | // Opening with read write is necessary, since opening with 127 | // read-only or write-only could block the thread until another 128 | // thread opens it with write-only or read-only (or RDWR) 129 | // correspondingly. 130 | OpenOptions::new() 131 | .read(true) 132 | .write(true) 133 | .open(path) 134 | .map_err(|err| FromEnvErrorInner::CannotOpenPath(path_str.to_string(), err)) 135 | }; 136 | 137 | Ok(Some(Client { 138 | read: open_file()?, 139 | write: open_file()?, 140 | creation_arg: ClientCreationArg::Fifo(path.into()), 141 | is_non_blocking: Some(AtomicBool::new(false)), 142 | })) 143 | } 144 | 145 | /// `--jobserver-auth=R,W` 146 | unsafe fn from_pipe(s: &str, check_pipe: bool) -> Result, FromEnvErrorInner> { 147 | let mut parts = s.splitn(2, ','); 148 | let read = parts.next().unwrap(); 149 | let write = match parts.next() { 150 | Some(w) => w, 151 | None => return Ok(None), 152 | }; 153 | let read = read 154 | .parse() 155 | .map_err(|e| FromEnvErrorInner::CannotParse(format!("cannot parse `read` fd: {e}")))?; 156 | let write = write 157 | .parse() 158 | .map_err(|e| FromEnvErrorInner::CannotParse(format!("cannot parse `write` fd: {e}")))?; 159 | 160 | // If either or both of these file descriptors are negative, 161 | // it means the jobserver is disabled for this process. 162 | if read < 0 { 163 | return Err(FromEnvErrorInner::NegativeFd(read)); 164 | } 165 | if write < 0 { 166 | return Err(FromEnvErrorInner::NegativeFd(write)); 167 | } 168 | 169 | let creation_arg = ClientCreationArg::Fds { read, write }; 170 | 171 | // Ok so we've got two integers that look like file descriptors, but 172 | // for extra sanity checking let's see if they actually look like 173 | // valid files and instances of a pipe if feature enabled before we 174 | // return the client. 175 | // 176 | // If we're called from `make` *without* the leading + on our rule 177 | // then we'll have `MAKEFLAGS` env vars but won't actually have 178 | // access to the file descriptors. 179 | // 180 | // `NotAPipe` is a worse error, return it if it's reported for any of the two fds. 181 | match (fd_check(read, check_pipe), fd_check(write, check_pipe)) { 182 | (read_err @ Err(FromEnvErrorInner::NotAPipe(..)), _) => read_err?, 183 | (_, write_err @ Err(FromEnvErrorInner::NotAPipe(..))) => write_err?, 184 | (read_err, write_err) => { 185 | read_err?; 186 | write_err?; 187 | 188 | // Optimization: Try converting it to a fifo by using /dev/fd 189 | // 190 | // On linux, opening `/dev/fd/$fd` returns a fd with a new file description, 191 | // so we can set `O_NONBLOCK` on it without affecting other processes. 192 | // 193 | // On macOS, opening `/dev/fd/$fd` seems to be the same as `File::try_clone`. 194 | // 195 | // I tested this on macOS 14 and Linux 6.5.13 196 | #[cfg(target_os = "linux")] 197 | if let (Ok(read), Ok(write)) = ( 198 | File::open(format!("/dev/fd/{}", read)), 199 | OpenOptions::new() 200 | .write(true) 201 | .open(format!("/dev/fd/{}", write)), 202 | ) { 203 | return Ok(Some(Client { 204 | read, 205 | write, 206 | creation_arg, 207 | is_non_blocking: Some(AtomicBool::new(false)), 208 | })); 209 | } 210 | } 211 | } 212 | 213 | Ok(Some(Client { 214 | read: clone_fd_and_set_cloexec(read)?, 215 | write: clone_fd_and_set_cloexec(write)?, 216 | creation_arg, 217 | is_non_blocking: None, 218 | })) 219 | } 220 | 221 | unsafe fn from_fds(read: c_int, write: c_int) -> Client { 222 | Client { 223 | read: File::from_raw_fd(read), 224 | write: File::from_raw_fd(write), 225 | creation_arg: ClientCreationArg::Fds { read, write }, 226 | is_non_blocking: None, 227 | } 228 | } 229 | 230 | pub fn acquire(&self) -> io::Result { 231 | // Ignore interrupts and keep trying if that happens 232 | loop { 233 | if let Some(token) = self.acquire_allow_interrupts()? { 234 | return Ok(token); 235 | } 236 | } 237 | } 238 | 239 | /// Block waiting for a token, returning `None` if we're interrupted with 240 | /// EINTR. 241 | fn acquire_allow_interrupts(&self) -> io::Result> { 242 | // We don't actually know if the file descriptor here is set in 243 | // blocking or nonblocking mode. AFAIK all released versions of 244 | // `make` use blocking fds for the jobserver, but the unreleased 245 | // version of `make` doesn't. In the unreleased version jobserver 246 | // fds are set to nonblocking and combined with `pselect` 247 | // internally. 248 | // 249 | // Here we try to be compatible with both strategies. We optimistically 250 | // try to read from the file descriptor which then may block, return 251 | // a token or indicate that polling is needed. 252 | // Blocking reads (if possible) allows the kernel to be more selective 253 | // about which readers to wake up when a token is written to the pipe. 254 | // 255 | // We use `poll` here to block this thread waiting for read 256 | // readiness, and then afterwards we perform the `read` itself. If 257 | // the `read` returns that it would block then we start over and try 258 | // again. 259 | // 260 | // Also note that we explicitly don't handle EINTR here. That's used 261 | // to shut us down, so we otherwise punt all errors upwards. 262 | unsafe { 263 | let mut fd: libc::pollfd = mem::zeroed(); 264 | let mut read = &self.read; 265 | fd.fd = read.as_raw_fd(); 266 | fd.events = libc::POLLIN; 267 | loop { 268 | let mut buf = [0]; 269 | match read.read(&mut buf) { 270 | Ok(1) => return Ok(Some(Acquired { byte: buf[0] })), 271 | Ok(_) => { 272 | return Err(io::Error::new( 273 | io::ErrorKind::UnexpectedEof, 274 | "early EOF on jobserver pipe", 275 | )); 276 | } 277 | Err(e) => match e.kind() { 278 | io::ErrorKind::WouldBlock => { /* fall through to polling */ } 279 | io::ErrorKind::Interrupted => return Ok(None), 280 | _ => return Err(e), 281 | }, 282 | } 283 | 284 | loop { 285 | fd.revents = 0; 286 | if libc::poll(&mut fd, 1, -1) == -1 { 287 | let e = io::Error::last_os_error(); 288 | return match e.kind() { 289 | io::ErrorKind::Interrupted => Ok(None), 290 | _ => Err(e), 291 | }; 292 | } 293 | if fd.revents != 0 { 294 | break; 295 | } 296 | } 297 | } 298 | } 299 | } 300 | 301 | pub fn try_acquire(&self) -> io::Result> { 302 | let mut buf = [0]; 303 | let mut fifo = &self.read; 304 | 305 | if let Some(is_non_blocking) = self.is_non_blocking.as_ref() { 306 | if !is_non_blocking.load(Ordering::Relaxed) { 307 | set_nonblocking(fifo.as_raw_fd(), true)?; 308 | is_non_blocking.store(true, Ordering::Relaxed); 309 | } 310 | } else { 311 | return Err(io::ErrorKind::Unsupported.into()); 312 | } 313 | 314 | loop { 315 | match fifo.read(&mut buf) { 316 | Ok(1) => break Ok(Some(Acquired { byte: buf[0] })), 317 | Ok(_) => { 318 | break Err(io::Error::new( 319 | io::ErrorKind::UnexpectedEof, 320 | "early EOF on jobserver pipe", 321 | )) 322 | } 323 | 324 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => break Ok(None), 325 | Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, 326 | 327 | Err(err) => break Err(err), 328 | } 329 | } 330 | } 331 | 332 | pub fn release(&self, data: Option<&Acquired>) -> io::Result<()> { 333 | // Note that the fd may be nonblocking but we're going to go ahead 334 | // and assume that the writes here are always nonblocking (we can 335 | // always quickly release a token). If that turns out to not be the 336 | // case we'll get an error anyway! 337 | let byte = data.map(|d| d.byte).unwrap_or(b'+'); 338 | match (&self.write).write(&[byte])? { 339 | 1 => Ok(()), 340 | _ => Err(io::Error::new( 341 | io::ErrorKind::Other, 342 | "failed to write token back to jobserver", 343 | )), 344 | } 345 | } 346 | 347 | pub fn string_arg(&self) -> String { 348 | match &self.creation_arg { 349 | ClientCreationArg::Fifo(path) => format!("fifo:{}", path.display()), 350 | ClientCreationArg::Fds { read, write } => format!("{},{}", read, write), 351 | } 352 | } 353 | 354 | pub fn available(&self) -> io::Result { 355 | let mut len = MaybeUninit::::uninit(); 356 | cvt(unsafe { libc::ioctl(self.read.as_raw_fd(), libc::FIONREAD, len.as_mut_ptr()) })?; 357 | Ok(unsafe { len.assume_init() } as usize) 358 | } 359 | 360 | pub fn configure(&self, cmd: &mut Command) { 361 | if matches!(self.creation_arg, ClientCreationArg::Fifo { .. }) { 362 | // We `File::open`ed it when inheriting from environment, 363 | // so no need to set cloexec for fifo. 364 | return; 365 | } 366 | // Here we basically just want to say that in the child process 367 | // we'll configure the read/write file descriptors to *not* be 368 | // cloexec, so they're inherited across the exec and specified as 369 | // integers through `string_arg` above. 370 | let read = self.read.as_raw_fd(); 371 | let write = self.write.as_raw_fd(); 372 | unsafe { 373 | cmd.pre_exec(move || { 374 | set_cloexec(read, false)?; 375 | set_cloexec(write, false)?; 376 | Ok(()) 377 | }); 378 | } 379 | } 380 | } 381 | 382 | #[derive(Debug)] 383 | pub struct Helper { 384 | thread: JoinHandle<()>, 385 | state: Arc, 386 | } 387 | 388 | pub(crate) fn spawn_helper( 389 | client: crate::Client, 390 | state: Arc, 391 | mut f: Box) + Send>, 392 | ) -> io::Result { 393 | static USR1_INIT: Once = Once::new(); 394 | let mut err = None; 395 | USR1_INIT.call_once(|| unsafe { 396 | let mut new: libc::sigaction = mem::zeroed(); 397 | new.sa_sigaction = sigusr1_handler as usize; 398 | new.sa_flags = libc::SA_SIGINFO as _; 399 | if libc::sigaction(libc::SIGUSR1, &new, ptr::null_mut()) != 0 { 400 | err = Some(io::Error::last_os_error()); 401 | } 402 | }); 403 | 404 | if let Some(e) = err.take() { 405 | return Err(e); 406 | } 407 | 408 | let state2 = state.clone(); 409 | let thread = Builder::new().spawn(move || { 410 | state2.for_each_request(|helper| loop { 411 | match client.inner.acquire_allow_interrupts() { 412 | Ok(Some(data)) => { 413 | break f(Ok(crate::Acquired { 414 | client: client.inner.clone(), 415 | data, 416 | disabled: false, 417 | })); 418 | } 419 | Err(e) => break f(Err(e)), 420 | Ok(None) if helper.lock().producer_done => break, 421 | Ok(None) => {} 422 | } 423 | }); 424 | })?; 425 | 426 | Ok(Helper { thread, state }) 427 | } 428 | 429 | impl Helper { 430 | pub fn join(self) { 431 | let dur = Duration::from_millis(10); 432 | let mut state = self.state.lock(); 433 | debug_assert!(state.producer_done); 434 | 435 | // We need to join our helper thread, and it could be blocked in one 436 | // of two locations. First is the wait for a request, but the 437 | // initial drop of `HelperState` will take care of that. Otherwise 438 | // it may be blocked in `client.acquire()`. We actually have no way 439 | // of interrupting that, so resort to `pthread_kill` as a fallback. 440 | // This signal should interrupt any blocking `read` call with 441 | // `io::ErrorKind::Interrupt` and cause the thread to cleanly exit. 442 | // 443 | // Note that we don't do this forever though since there's a chance 444 | // of bugs, so only do this opportunistically to make a best effort 445 | // at clearing ourselves up. 446 | for _ in 0..100 { 447 | if state.consumer_done { 448 | break; 449 | } 450 | unsafe { 451 | // Ignore the return value here of `pthread_kill`, 452 | // apparently on OSX if you kill a dead thread it will 453 | // return an error, but on other platforms it may not. In 454 | // that sense we don't actually know if this will succeed or 455 | // not! 456 | libc::pthread_kill(self.thread.as_pthread_t() as _, libc::SIGUSR1); 457 | } 458 | state = self 459 | .state 460 | .cvar 461 | .wait_timeout(state, dur) 462 | .unwrap_or_else(|e| e.into_inner()) 463 | .0; 464 | thread::yield_now(); // we really want the other thread to run 465 | } 466 | 467 | // If we managed to actually see the consumer get done, then we can 468 | // definitely wait for the thread. Otherwise it's... off in the ether 469 | // I guess? 470 | if state.consumer_done { 471 | drop(self.thread.join()); 472 | } 473 | } 474 | } 475 | 476 | unsafe fn fcntl_check(fd: c_int) -> Result<(), FromEnvErrorInner> { 477 | match libc::fcntl(fd, libc::F_GETFD) { 478 | -1 => Err(FromEnvErrorInner::CannotOpenFd( 479 | fd, 480 | io::Error::last_os_error(), 481 | )), 482 | _ => Ok(()), 483 | } 484 | } 485 | 486 | unsafe fn fd_check(fd: c_int, check_pipe: bool) -> Result<(), FromEnvErrorInner> { 487 | if check_pipe { 488 | let mut stat = mem::zeroed(); 489 | if libc::fstat(fd, &mut stat) == -1 { 490 | let last_os_error = io::Error::last_os_error(); 491 | fcntl_check(fd)?; 492 | Err(FromEnvErrorInner::NotAPipe(fd, Some(last_os_error))) 493 | } else { 494 | // On android arm and i686 mode_t is u16 and st_mode is u32, 495 | // this generates a type mismatch when S_IFIFO (declared as mode_t) 496 | // is used in operations with st_mode, so we use this workaround 497 | // to get the value of S_IFIFO with the same type of st_mode. 498 | #[allow(unused_assignments)] 499 | let mut s_ififo = stat.st_mode; 500 | s_ififo = libc::S_IFIFO as _; 501 | if stat.st_mode & s_ififo == s_ififo { 502 | return Ok(()); 503 | } 504 | Err(FromEnvErrorInner::NotAPipe(fd, None)) 505 | } 506 | } else { 507 | fcntl_check(fd) 508 | } 509 | } 510 | 511 | fn clone_fd_and_set_cloexec(fd: c_int) -> Result { 512 | // Safety: fd is a valid fd and it remains open until returns 513 | unsafe { BorrowedFd::borrow_raw(fd) } 514 | .try_clone_to_owned() 515 | .map(File::from) 516 | .map_err(|err| FromEnvErrorInner::CannotOpenFd(fd, err)) 517 | } 518 | 519 | fn set_cloexec(fd: c_int, set: bool) -> io::Result<()> { 520 | unsafe { 521 | let previous = cvt(libc::fcntl(fd, libc::F_GETFD))?; 522 | let new = if set { 523 | previous | libc::FD_CLOEXEC 524 | } else { 525 | previous & !libc::FD_CLOEXEC 526 | }; 527 | if new != previous { 528 | cvt(libc::fcntl(fd, libc::F_SETFD, new))?; 529 | } 530 | Ok(()) 531 | } 532 | } 533 | 534 | fn set_nonblocking(fd: c_int, set: bool) -> io::Result<()> { 535 | let status_flag = if set { libc::O_NONBLOCK } else { 0 }; 536 | 537 | unsafe { 538 | cvt(libc::fcntl(fd, libc::F_SETFL, status_flag))?; 539 | } 540 | 541 | Ok(()) 542 | } 543 | 544 | fn cvt(t: c_int) -> io::Result { 545 | if t == -1 { 546 | Err(io::Error::last_os_error()) 547 | } else { 548 | Ok(t) 549 | } 550 | } 551 | 552 | extern "C" fn sigusr1_handler( 553 | _signum: c_int, 554 | _info: *mut libc::siginfo_t, 555 | _ptr: *mut libc::c_void, 556 | ) { 557 | // nothing to do 558 | } 559 | 560 | #[cfg(test)] 561 | mod test { 562 | use super::Client as ClientImp; 563 | 564 | use crate::{test::run_named_fifo_try_acquire_tests, Client}; 565 | 566 | use std::{ 567 | fs::File, 568 | io::{self, Write}, 569 | os::unix::io::AsRawFd, 570 | sync::Arc, 571 | }; 572 | 573 | fn from_imp_client(imp: ClientImp) -> Client { 574 | Client { 575 | inner: Arc::new(imp), 576 | } 577 | } 578 | 579 | fn new_client_from_fifo() -> (Client, String) { 580 | let file = tempfile::NamedTempFile::new().unwrap(); 581 | let fifo_path = file.path().to_owned(); 582 | file.close().unwrap(); // Remove the NamedTempFile to create fifo 583 | 584 | nix::unistd::mkfifo(&fifo_path, nix::sys::stat::Mode::S_IRWXU).unwrap(); 585 | 586 | let arg = format!("fifo:{}", fifo_path.to_str().unwrap()); 587 | 588 | ( 589 | ClientImp::from_fifo(&arg) 590 | .unwrap() 591 | .map(from_imp_client) 592 | .unwrap(), 593 | arg, 594 | ) 595 | } 596 | 597 | fn new_client_from_pipe() -> (Client, String) { 598 | let (read, write) = nix::unistd::pipe().unwrap(); 599 | let read = File::from(read); 600 | let mut write = File::from(write); 601 | 602 | write.write_all(b"1").unwrap(); 603 | 604 | let arg = format!("{},{}", read.as_raw_fd(), write.as_raw_fd()); 605 | 606 | ( 607 | unsafe { ClientImp::from_pipe(&arg, true) } 608 | .unwrap() 609 | .map(from_imp_client) 610 | .unwrap(), 611 | arg, 612 | ) 613 | } 614 | 615 | #[test] 616 | fn test_try_acquire_named_fifo() { 617 | run_named_fifo_try_acquire_tests(&new_client_from_fifo().0); 618 | } 619 | 620 | #[test] 621 | fn test_try_acquire_annoymous_pipe_linux_specific_optimization() { 622 | #[cfg(not(target_os = "linux"))] 623 | assert_eq!( 624 | new_client_from_pipe().0.try_acquire().unwrap_err().kind(), 625 | io::ErrorKind::Unsupported 626 | ); 627 | 628 | #[cfg(target_os = "linux")] 629 | { 630 | let client = new_client_from_pipe().0; 631 | client.acquire().unwrap().drop_without_releasing(); 632 | run_named_fifo_try_acquire_tests(&client); 633 | } 634 | } 635 | 636 | #[test] 637 | fn test_string_arg() { 638 | let (client, arg) = new_client_from_fifo(); 639 | assert_eq!(client.inner.string_arg(), arg); 640 | 641 | let (client, arg) = new_client_from_pipe(); 642 | assert_eq!(client.inner.string_arg(), arg); 643 | } 644 | } 645 | -------------------------------------------------------------------------------- /src/wasm.rs: -------------------------------------------------------------------------------- 1 | use crate::FromEnvErrorInner; 2 | use std::io; 3 | use std::process::Command; 4 | use std::sync::{Arc, Condvar, Mutex}; 5 | use std::thread::{Builder, JoinHandle}; 6 | 7 | #[derive(Debug)] 8 | pub struct Client { 9 | inner: Arc, 10 | } 11 | 12 | #[derive(Debug)] 13 | struct Inner { 14 | count: Mutex, 15 | cvar: Condvar, 16 | } 17 | 18 | #[derive(Debug)] 19 | pub struct Acquired(()); 20 | 21 | impl Client { 22 | pub fn new(limit: usize) -> io::Result { 23 | Ok(Client { 24 | inner: Arc::new(Inner { 25 | count: Mutex::new(limit), 26 | cvar: Condvar::new(), 27 | }), 28 | }) 29 | } 30 | 31 | pub(crate) unsafe fn open(_s: &str, _check_pipe: bool) -> Result { 32 | Err(FromEnvErrorInner::Unsupported) 33 | } 34 | 35 | pub fn acquire(&self) -> io::Result { 36 | let mut lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner()); 37 | while *lock == 0 { 38 | lock = self 39 | .inner 40 | .cvar 41 | .wait(lock) 42 | .unwrap_or_else(|e| e.into_inner()); 43 | } 44 | *lock -= 1; 45 | Ok(Acquired(())) 46 | } 47 | 48 | pub fn try_acquire(&self) -> io::Result> { 49 | let mut lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner()); 50 | if *lock == 0 { 51 | Ok(None) 52 | } else { 53 | *lock -= 1; 54 | Ok(Some(Acquired(()))) 55 | } 56 | } 57 | 58 | pub fn release(&self, _data: Option<&Acquired>) -> io::Result<()> { 59 | let mut lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner()); 60 | *lock += 1; 61 | drop(lock); 62 | self.inner.cvar.notify_one(); 63 | Ok(()) 64 | } 65 | 66 | pub fn string_arg(&self) -> String { 67 | panic!( 68 | "On this platform there is no cross process jobserver support, 69 | so Client::configure is not supported." 70 | ); 71 | } 72 | 73 | pub fn available(&self) -> io::Result { 74 | let lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner()); 75 | Ok(*lock) 76 | } 77 | 78 | pub fn configure(&self, _cmd: &mut Command) { 79 | unreachable!(); 80 | } 81 | } 82 | 83 | #[derive(Debug)] 84 | pub struct Helper { 85 | thread: JoinHandle<()>, 86 | } 87 | 88 | pub(crate) fn spawn_helper( 89 | client: crate::Client, 90 | state: Arc, 91 | mut f: Box) + Send>, 92 | ) -> io::Result { 93 | let thread = Builder::new().spawn(move || { 94 | state.for_each_request(|_| f(client.acquire())); 95 | })?; 96 | 97 | Ok(Helper { thread }) 98 | } 99 | 100 | impl Helper { 101 | pub fn join(self) { 102 | // TODO: this is not correct if the thread is blocked in 103 | // `client.acquire()`. 104 | drop(self.thread.join()); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/windows.rs: -------------------------------------------------------------------------------- 1 | use crate::FromEnvErrorInner; 2 | use std::ffi::CString; 3 | use std::io; 4 | use std::process::Command; 5 | use std::ptr; 6 | use std::sync::Arc; 7 | use std::thread::{Builder, JoinHandle}; 8 | 9 | #[derive(Debug)] 10 | pub struct Client { 11 | sem: Handle, 12 | name: String, 13 | } 14 | 15 | #[derive(Debug)] 16 | pub struct Acquired; 17 | 18 | #[allow(clippy::upper_case_acronyms)] 19 | type BOOL = i32; 20 | #[allow(clippy::upper_case_acronyms)] 21 | type DWORD = u32; 22 | #[allow(clippy::upper_case_acronyms)] 23 | type HANDLE = *mut u8; 24 | #[allow(clippy::upper_case_acronyms)] 25 | type LONG = i32; 26 | 27 | const ERROR_ALREADY_EXISTS: DWORD = 183; 28 | const FALSE: BOOL = 0; 29 | const INFINITE: DWORD = 0xffffffff; 30 | const SEMAPHORE_MODIFY_STATE: DWORD = 0x2; 31 | const SYNCHRONIZE: DWORD = 0x00100000; 32 | const TRUE: BOOL = 1; 33 | 34 | const WAIT_ABANDONED: DWORD = 128u32; 35 | const WAIT_FAILED: DWORD = 4294967295u32; 36 | const WAIT_OBJECT_0: DWORD = 0u32; 37 | const WAIT_TIMEOUT: DWORD = 258u32; 38 | 39 | #[link(name = "kernel32")] 40 | extern "system" { 41 | fn CloseHandle(handle: HANDLE) -> BOOL; 42 | fn SetEvent(hEvent: HANDLE) -> BOOL; 43 | fn WaitForMultipleObjects( 44 | ncount: DWORD, 45 | lpHandles: *const HANDLE, 46 | bWaitAll: BOOL, 47 | dwMilliseconds: DWORD, 48 | ) -> DWORD; 49 | fn CreateEventA( 50 | lpEventAttributes: *mut u8, 51 | bManualReset: BOOL, 52 | bInitialState: BOOL, 53 | lpName: *const i8, 54 | ) -> HANDLE; 55 | fn ReleaseSemaphore( 56 | hSemaphore: HANDLE, 57 | lReleaseCount: LONG, 58 | lpPreviousCount: *mut LONG, 59 | ) -> BOOL; 60 | fn CreateSemaphoreA( 61 | lpEventAttributes: *mut u8, 62 | lInitialCount: LONG, 63 | lMaximumCount: LONG, 64 | lpName: *const i8, 65 | ) -> HANDLE; 66 | fn OpenSemaphoreA(dwDesiredAccess: DWORD, bInheritHandle: BOOL, lpName: *const i8) -> HANDLE; 67 | fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD; 68 | } 69 | 70 | impl Client { 71 | pub fn new(limit: usize) -> io::Result { 72 | // Try a bunch of random semaphore names until we get a unique one, 73 | // but don't try for too long. 74 | // 75 | // Note that `limit == 0` is a valid argument above but Windows 76 | // won't let us create a semaphore with 0 slots available to it. Get 77 | // `limit == 0` working by creating a semaphore instead with one 78 | // slot and then immediately acquire it (without ever releaseing it 79 | // back). 80 | for _ in 0..100 { 81 | let bytes = getrandom::u32()?; 82 | let mut name = format!("__rust_jobserver_semaphore_{}\0", bytes); 83 | unsafe { 84 | let create_limit = if limit == 0 { 1 } else { limit }; 85 | let r = CreateSemaphoreA( 86 | ptr::null_mut(), 87 | create_limit as LONG, 88 | create_limit as LONG, 89 | name.as_ptr() as *const _, 90 | ); 91 | if r.is_null() { 92 | return Err(io::Error::last_os_error()); 93 | } 94 | let handle = Handle(r); 95 | 96 | let err = io::Error::last_os_error(); 97 | if err.raw_os_error() == Some(ERROR_ALREADY_EXISTS as i32) { 98 | continue; 99 | } 100 | name.pop(); // chop off the trailing nul 101 | let client = Client { sem: handle, name }; 102 | if create_limit != limit { 103 | client.acquire()?; 104 | } 105 | return Ok(client); 106 | } 107 | } 108 | 109 | Err(io::Error::new( 110 | io::ErrorKind::Other, 111 | "failed to find a unique name for a semaphore", 112 | )) 113 | } 114 | 115 | pub(crate) unsafe fn open(s: &str, _check_pipe: bool) -> Result { 116 | let name = match CString::new(s) { 117 | Ok(s) => s, 118 | Err(e) => return Err(FromEnvErrorInner::CannotParse(e.to_string())), 119 | }; 120 | 121 | let sem = OpenSemaphoreA(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE, FALSE, name.as_ptr()); 122 | if sem.is_null() { 123 | Err(FromEnvErrorInner::CannotOpenPath( 124 | s.to_string(), 125 | io::Error::last_os_error(), 126 | )) 127 | } else { 128 | Ok(Client { 129 | sem: Handle(sem), 130 | name: s.to_string(), 131 | }) 132 | } 133 | } 134 | 135 | pub fn acquire(&self) -> io::Result { 136 | unsafe { 137 | let r = WaitForSingleObject(self.sem.0, INFINITE); 138 | if r == WAIT_OBJECT_0 { 139 | Ok(Acquired) 140 | } else { 141 | Err(io::Error::last_os_error()) 142 | } 143 | } 144 | } 145 | 146 | pub fn try_acquire(&self) -> io::Result> { 147 | match unsafe { WaitForSingleObject(self.sem.0, 0) } { 148 | WAIT_OBJECT_0 => Ok(Some(Acquired)), 149 | WAIT_TIMEOUT => Ok(None), 150 | WAIT_FAILED => Err(io::Error::last_os_error()), 151 | // We believe this should be impossible for a semaphore, but still 152 | // check the error code just in case it happens. 153 | WAIT_ABANDONED => Err(io::Error::new( 154 | io::ErrorKind::Other, 155 | "Wait on jobserver semaphore returned WAIT_ABANDONED", 156 | )), 157 | _ => unreachable!("Unexpected return value from WaitForSingleObject"), 158 | } 159 | } 160 | 161 | pub fn release(&self, _data: Option<&Acquired>) -> io::Result<()> { 162 | unsafe { 163 | let r = ReleaseSemaphore(self.sem.0, 1, ptr::null_mut()); 164 | if r != 0 { 165 | Ok(()) 166 | } else { 167 | Err(io::Error::last_os_error()) 168 | } 169 | } 170 | } 171 | 172 | pub fn string_arg(&self) -> String { 173 | self.name.clone() 174 | } 175 | 176 | pub fn available(&self) -> io::Result { 177 | // Can't read value of a semaphore on Windows, so 178 | // try to acquire without sleeping, since we can find out the 179 | // old value on release. If acquisiton fails, then available is 0. 180 | unsafe { 181 | let r = WaitForSingleObject(self.sem.0, 0); 182 | if r != WAIT_OBJECT_0 { 183 | Ok(0) 184 | } else { 185 | let mut prev: LONG = 0; 186 | let r = ReleaseSemaphore(self.sem.0, 1, &mut prev); 187 | if r != 0 { 188 | Ok(prev as usize + 1) 189 | } else { 190 | Err(io::Error::last_os_error()) 191 | } 192 | } 193 | } 194 | } 195 | 196 | pub fn configure(&self, _cmd: &mut Command) { 197 | // nothing to do here, we gave the name of our semaphore to the 198 | // child above 199 | } 200 | } 201 | 202 | #[derive(Debug)] 203 | struct Handle(HANDLE); 204 | // HANDLE is a raw ptr, but we're send/sync 205 | unsafe impl Sync for Handle {} 206 | unsafe impl Send for Handle {} 207 | 208 | impl Drop for Handle { 209 | fn drop(&mut self) { 210 | unsafe { 211 | CloseHandle(self.0); 212 | } 213 | } 214 | } 215 | 216 | #[derive(Debug)] 217 | pub struct Helper { 218 | event: Arc, 219 | thread: JoinHandle<()>, 220 | } 221 | 222 | pub(crate) fn spawn_helper( 223 | client: crate::Client, 224 | state: Arc, 225 | mut f: Box) + Send>, 226 | ) -> io::Result { 227 | let event = unsafe { 228 | let r = CreateEventA(ptr::null_mut(), TRUE, FALSE, ptr::null()); 229 | if r.is_null() { 230 | return Err(io::Error::last_os_error()); 231 | } else { 232 | Handle(r) 233 | } 234 | }; 235 | let event = Arc::new(event); 236 | let event2 = event.clone(); 237 | let thread = Builder::new().spawn(move || { 238 | let objects = [event2.0, client.inner.sem.0]; 239 | state.for_each_request(|_| { 240 | const WAIT_OBJECT_1: u32 = WAIT_OBJECT_0 + 1; 241 | match unsafe { WaitForMultipleObjects(2, objects.as_ptr(), FALSE, INFINITE) } { 242 | WAIT_OBJECT_0 => {} 243 | WAIT_OBJECT_1 => f(Ok(crate::Acquired { 244 | client: client.inner.clone(), 245 | data: Acquired, 246 | disabled: false, 247 | })), 248 | _ => f(Err(io::Error::last_os_error())), 249 | } 250 | }); 251 | })?; 252 | Ok(Helper { thread, event }) 253 | } 254 | 255 | impl Helper { 256 | pub fn join(self) { 257 | // Unlike unix this logic is much easier. If our thread was blocked 258 | // in waiting for requests it should already be woken up and 259 | // exiting. Otherwise it's waiting for a token, so we wake it up 260 | // with a different event that it's also waiting on here. After 261 | // these two we should be guaranteed the thread is on its way out, 262 | // so we can safely `join`. 263 | let r = unsafe { SetEvent(self.event.0) }; 264 | if r == 0 { 265 | panic!("failed to set event: {}", io::Error::last_os_error()); 266 | } 267 | drop(self.thread.join()); 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /tests/client-of-myself.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::io::prelude::*; 3 | use std::io::BufReader; 4 | use std::process::{Command, Stdio}; 5 | use std::sync::mpsc; 6 | use std::thread; 7 | 8 | use jobserver::Client; 9 | 10 | macro_rules! t { 11 | ($e:expr) => { 12 | match $e { 13 | Ok(e) => e, 14 | Err(e) => panic!("{} failed with {}", stringify!($e), e), 15 | } 16 | }; 17 | } 18 | 19 | fn main() { 20 | if env::var("I_AM_THE_CLIENT").is_ok() { 21 | client(); 22 | } else { 23 | server(); 24 | } 25 | } 26 | 27 | fn server() { 28 | let me = t!(env::current_exe()); 29 | let client = t!(Client::new(1)); 30 | let mut cmd = Command::new(me); 31 | cmd.env("I_AM_THE_CLIENT", "1").stdout(Stdio::piped()); 32 | client.configure(&mut cmd); 33 | let acq = client.acquire().unwrap(); 34 | let mut child = t!(cmd.spawn()); 35 | let stdout = child.stdout.take().unwrap(); 36 | let (tx, rx) = mpsc::channel(); 37 | let t = thread::spawn(move || { 38 | for line in BufReader::new(stdout).lines() { 39 | tx.send(t!(line)).unwrap(); 40 | } 41 | }); 42 | 43 | for _ in 0..100 { 44 | assert!(rx.try_recv().is_err()); 45 | } 46 | 47 | drop(acq); 48 | assert_eq!(rx.recv().unwrap(), "hello!"); 49 | t.join().unwrap(); 50 | assert!(rx.recv().is_err()); 51 | client.acquire().unwrap(); 52 | } 53 | 54 | fn client() { 55 | let client = unsafe { Client::from_env().unwrap() }; 56 | let acq = client.acquire().unwrap(); 57 | println!("hello!"); 58 | drop(acq); 59 | } 60 | -------------------------------------------------------------------------------- /tests/client.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::fs::File; 3 | use std::io::Write; 4 | use std::process::Command; 5 | use std::sync::atomic::{AtomicBool, Ordering}; 6 | use std::sync::mpsc; 7 | use std::sync::Arc; 8 | use std::thread; 9 | 10 | use jobserver::Client; 11 | 12 | macro_rules! t { 13 | ($e:expr) => { 14 | match $e { 15 | Ok(e) => e, 16 | Err(e) => panic!("{} failed with {}", stringify!($e), e), 17 | } 18 | }; 19 | } 20 | 21 | struct Test { 22 | name: &'static str, 23 | f: &'static (dyn Fn() + Send + Sync), 24 | make_args: &'static [&'static str], 25 | rule: &'static (dyn Fn(&str) -> String + Send + Sync), 26 | } 27 | 28 | const TESTS: &[Test] = &[ 29 | Test { 30 | name: "no j args", 31 | make_args: &[], 32 | rule: &|me| me.to_string(), 33 | f: &|| { 34 | assert!(unsafe { Client::from_env().is_none() }); 35 | }, 36 | }, 37 | Test { 38 | name: "no j args with plus", 39 | make_args: &[], 40 | rule: &|me| format!("+{}", me), 41 | f: &|| { 42 | assert!(unsafe { Client::from_env().is_none() }); 43 | }, 44 | }, 45 | Test { 46 | name: "j args with plus", 47 | make_args: &["-j2"], 48 | rule: &|me| format!("+{}", me), 49 | f: &|| { 50 | assert!(unsafe { Client::from_env().is_some() }); 51 | }, 52 | }, 53 | Test { 54 | name: "acquire", 55 | make_args: &["-j2"], 56 | rule: &|me| format!("+{}", me), 57 | f: &|| { 58 | let c = unsafe { Client::from_env().unwrap() }; 59 | drop(c.acquire().unwrap()); 60 | drop(c.acquire().unwrap()); 61 | }, 62 | }, 63 | Test { 64 | name: "acquire3", 65 | make_args: &["-j3"], 66 | rule: &|me| format!("+{}", me), 67 | f: &|| { 68 | let c = unsafe { Client::from_env().unwrap() }; 69 | let a = c.acquire().unwrap(); 70 | let b = c.acquire().unwrap(); 71 | drop((a, b)); 72 | }, 73 | }, 74 | Test { 75 | name: "acquire blocks", 76 | make_args: &["-j2"], 77 | rule: &|me| format!("+{}", me), 78 | f: &|| { 79 | let c = unsafe { Client::from_env().unwrap() }; 80 | let a = c.acquire().unwrap(); 81 | let hit = Arc::new(AtomicBool::new(false)); 82 | let hit2 = hit.clone(); 83 | let (tx, rx) = mpsc::channel(); 84 | let t = thread::spawn(move || { 85 | tx.send(()).unwrap(); 86 | let _b = c.acquire().unwrap(); 87 | hit2.store(true, Ordering::SeqCst); 88 | }); 89 | rx.recv().unwrap(); 90 | assert!(!hit.load(Ordering::SeqCst)); 91 | drop(a); 92 | t.join().unwrap(); 93 | assert!(hit.load(Ordering::SeqCst)); 94 | }, 95 | }, 96 | Test { 97 | name: "acquire_raw", 98 | make_args: &["-j2"], 99 | rule: &|me| format!("+{}", me), 100 | f: &|| { 101 | let c = unsafe { Client::from_env().unwrap() }; 102 | c.acquire_raw().unwrap(); 103 | c.release_raw().unwrap(); 104 | }, 105 | }, 106 | ]; 107 | 108 | fn main() { 109 | if let Ok(test) = env::var("TEST_TO_RUN") { 110 | return (TESTS.iter().find(|t| t.name == test).unwrap().f)(); 111 | } 112 | 113 | let me = t!(env::current_exe()); 114 | let me = me.to_str().unwrap(); 115 | let filter = env::args().nth(1); 116 | 117 | let join_handles = TESTS 118 | .iter() 119 | .filter(|test| match filter { 120 | Some(ref s) => test.name.contains(s), 121 | None => true, 122 | }) 123 | .map(|test| { 124 | let td = t!(tempfile::tempdir()); 125 | let makefile = format!( 126 | "\ 127 | all: export TEST_TO_RUN={} 128 | all: 129 | \t{} 130 | ", 131 | test.name, 132 | (test.rule)(me) 133 | ); 134 | t!(t!(File::create(td.path().join("Makefile"))).write_all(makefile.as_bytes())); 135 | thread::spawn(move || { 136 | let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string()); 137 | let mut cmd = Command::new(prog); 138 | cmd.args(test.make_args); 139 | cmd.current_dir(td.path()); 140 | 141 | (test, cmd.output().unwrap()) 142 | }) 143 | }) 144 | .collect::>(); 145 | 146 | println!("\nrunning {} tests\n", join_handles.len()); 147 | 148 | let failures = join_handles 149 | .into_iter() 150 | .filter_map(|join_handle| { 151 | let (test, output) = join_handle.join().unwrap(); 152 | 153 | if output.status.success() { 154 | println!("test {} ... ok", test.name); 155 | None 156 | } else { 157 | println!("test {} ... FAIL", test.name); 158 | Some((test, output)) 159 | } 160 | }) 161 | .collect::>(); 162 | 163 | if failures.is_empty() { 164 | println!("\ntest result: ok\n"); 165 | return; 166 | } 167 | 168 | println!("\n----------- failures"); 169 | 170 | for (test, output) in failures { 171 | println!("test {}", test.name); 172 | let stdout = String::from_utf8_lossy(&output.stdout); 173 | let stderr = String::from_utf8_lossy(&output.stderr); 174 | 175 | println!("\texit status: {}", output.status); 176 | if !stdout.is_empty() { 177 | println!("\tstdout ==="); 178 | for line in stdout.lines() { 179 | println!("\t\t{}", line); 180 | } 181 | } 182 | 183 | if !stderr.is_empty() { 184 | println!("\tstderr ==="); 185 | for line in stderr.lines() { 186 | println!("\t\t{}", line); 187 | } 188 | } 189 | } 190 | 191 | std::process::exit(4); 192 | } 193 | -------------------------------------------------------------------------------- /tests/helper.rs: -------------------------------------------------------------------------------- 1 | use jobserver::Client; 2 | use std::sync::atomic::*; 3 | use std::sync::*; 4 | 5 | macro_rules! t { 6 | ($e:expr) => { 7 | match $e { 8 | Ok(e) => e, 9 | Err(e) => panic!("{} failed with {}", stringify!($e), e), 10 | } 11 | }; 12 | } 13 | 14 | #[test] 15 | fn helper_smoke() { 16 | let client = t!(Client::new(1)); 17 | drop(client.clone().into_helper_thread(|_| ()).unwrap()); 18 | drop(client.clone().into_helper_thread(|_| ()).unwrap()); 19 | drop(client.clone().into_helper_thread(|_| ()).unwrap()); 20 | drop(client.clone().into_helper_thread(|_| ()).unwrap()); 21 | drop(client.clone().into_helper_thread(|_| ()).unwrap()); 22 | drop(client.into_helper_thread(|_| ()).unwrap()); 23 | } 24 | 25 | #[test] 26 | fn acquire() { 27 | let (tx, rx) = mpsc::channel(); 28 | let client = t!(Client::new(1)); 29 | let helper = client 30 | .into_helper_thread(move |a| drop(tx.send(a))) 31 | .unwrap(); 32 | assert!(rx.try_recv().is_err()); 33 | helper.request_token(); 34 | rx.recv().unwrap().unwrap(); 35 | helper.request_token(); 36 | rx.recv().unwrap().unwrap(); 37 | 38 | helper.request_token(); 39 | helper.request_token(); 40 | rx.recv().unwrap().unwrap(); 41 | rx.recv().unwrap().unwrap(); 42 | 43 | helper.request_token(); 44 | helper.request_token(); 45 | drop(helper); 46 | } 47 | 48 | #[test] 49 | fn prompt_shutdown() { 50 | for _ in 0..100 { 51 | let client = jobserver::Client::new(4).unwrap(); 52 | let count = Arc::new(AtomicU32::new(0)); 53 | let count2 = count.clone(); 54 | let tokens = Arc::new(Mutex::new(Vec::new())); 55 | let helper = client 56 | .into_helper_thread(move |token| { 57 | tokens.lock().unwrap().push(token); 58 | count2.fetch_add(1, Ordering::SeqCst); 59 | }) 60 | .unwrap(); 61 | 62 | // Request more tokens than what are available. 63 | for _ in 0..5 { 64 | helper.request_token(); 65 | } 66 | // Wait for at least some of the requests to finish. 67 | while count.load(Ordering::SeqCst) < 3 { 68 | std::thread::yield_now(); 69 | } 70 | // Drop helper 71 | let t = std::time::Instant::now(); 72 | drop(helper); 73 | let d = t.elapsed(); 74 | assert!(d.as_secs_f64() < 0.5); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/make-as-a-client.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::fs::File; 3 | use std::io::prelude::*; 4 | use std::net::{TcpListener, TcpStream}; 5 | use std::process::Command; 6 | 7 | use jobserver::Client; 8 | 9 | macro_rules! t { 10 | ($e:expr) => { 11 | match $e { 12 | Ok(e) => e, 13 | Err(e) => panic!("{} failed with {}", stringify!($e), e), 14 | } 15 | }; 16 | } 17 | 18 | fn main() { 19 | if env::var("_DO_THE_TEST").is_ok() { 20 | std::process::exit( 21 | Command::new(env::var_os("MAKE").unwrap()) 22 | .env("MAKEFLAGS", env::var_os("CARGO_MAKEFLAGS").unwrap()) 23 | .env_remove("_DO_THE_TEST") 24 | .args(&env::args_os().skip(1).collect::>()) 25 | .status() 26 | .unwrap() 27 | .code() 28 | .unwrap_or(1), 29 | ); 30 | } 31 | 32 | if let Ok(s) = env::var("TEST_ADDR") { 33 | let mut contents = Vec::new(); 34 | t!(t!(TcpStream::connect(&s)).read_to_end(&mut contents)); 35 | return; 36 | } 37 | 38 | let c = t!(Client::new(1)); 39 | let td = tempfile::tempdir().unwrap(); 40 | 41 | let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string()); 42 | 43 | let me = t!(env::current_exe()); 44 | let me = me.to_str().unwrap(); 45 | 46 | let mut cmd = Command::new(&me); 47 | cmd.current_dir(td.path()); 48 | cmd.env("MAKE", prog); 49 | cmd.env("_DO_THE_TEST", "1"); 50 | 51 | t!(t!(File::create(td.path().join("Makefile"))).write_all( 52 | format!( 53 | "\ 54 | all: foo bar 55 | foo: 56 | \t{0} 57 | bar: 58 | \t{0} 59 | ", 60 | me 61 | ) 62 | .as_bytes() 63 | )); 64 | 65 | // We're leaking one extra token to `make` sort of violating the makefile 66 | // jobserver protocol. It has the desired effect though. 67 | c.configure(&mut cmd); 68 | 69 | let listener = t!(TcpListener::bind("127.0.0.1:0")); 70 | let addr = t!(listener.local_addr()); 71 | cmd.env("TEST_ADDR", addr.to_string()); 72 | let mut child = t!(cmd.spawn()); 73 | 74 | // We should get both connections as the two programs should be run 75 | // concurrently. 76 | let a = t!(listener.accept()); 77 | let b = t!(listener.accept()); 78 | drop((a, b)); 79 | 80 | assert!(t!(child.wait()).success()); 81 | } 82 | -------------------------------------------------------------------------------- /tests/server.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::fs::File; 3 | use std::io::prelude::*; 4 | use std::process::Command; 5 | use std::sync::atomic::{AtomicBool, Ordering}; 6 | use std::sync::mpsc; 7 | use std::sync::Arc; 8 | use std::thread; 9 | 10 | use jobserver::Client; 11 | 12 | macro_rules! t { 13 | ($e:expr) => { 14 | match $e { 15 | Ok(e) => e, 16 | Err(e) => panic!("{} failed with {}", stringify!($e), e), 17 | } 18 | }; 19 | } 20 | 21 | #[test] 22 | fn server_smoke() { 23 | let c = t!(Client::new(1)); 24 | drop(c.acquire().unwrap()); 25 | drop(c.acquire().unwrap()); 26 | } 27 | 28 | #[test] 29 | fn server_multiple() { 30 | let c = t!(Client::new(2)); 31 | let a = c.acquire().unwrap(); 32 | let b = c.acquire().unwrap(); 33 | drop((a, b)); 34 | } 35 | 36 | #[test] 37 | fn server_available() { 38 | let c = t!(Client::new(10)); 39 | assert_eq!(c.available().unwrap(), 10); 40 | let a = c.acquire().unwrap(); 41 | assert_eq!(c.available().unwrap(), 9); 42 | drop(a); 43 | assert_eq!(c.available().unwrap(), 10); 44 | } 45 | 46 | #[test] 47 | fn server_none_available() { 48 | let c = t!(Client::new(2)); 49 | assert_eq!(c.available().unwrap(), 2); 50 | let a = c.acquire().unwrap(); 51 | assert_eq!(c.available().unwrap(), 1); 52 | let b = c.acquire().unwrap(); 53 | assert_eq!(c.available().unwrap(), 0); 54 | drop(a); 55 | assert_eq!(c.available().unwrap(), 1); 56 | drop(b); 57 | assert_eq!(c.available().unwrap(), 2); 58 | } 59 | 60 | #[test] 61 | fn server_blocks() { 62 | let c = t!(Client::new(1)); 63 | let a = c.acquire().unwrap(); 64 | let hit = Arc::new(AtomicBool::new(false)); 65 | let hit2 = hit.clone(); 66 | let (tx, rx) = mpsc::channel(); 67 | let t = thread::spawn(move || { 68 | tx.send(()).unwrap(); 69 | let _b = c.acquire().unwrap(); 70 | hit2.store(true, Ordering::SeqCst); 71 | }); 72 | rx.recv().unwrap(); 73 | assert!(!hit.load(Ordering::SeqCst)); 74 | drop(a); 75 | t.join().unwrap(); 76 | assert!(hit.load(Ordering::SeqCst)); 77 | } 78 | 79 | #[test] 80 | fn make_as_a_single_thread_client() { 81 | let c = t!(Client::new(1)); 82 | let td = tempfile::tempdir().unwrap(); 83 | 84 | let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string()); 85 | let mut cmd = Command::new(prog); 86 | cmd.current_dir(td.path()); 87 | 88 | t!(t!(File::create(td.path().join("Makefile"))).write_all( 89 | b" 90 | all: foo bar 91 | foo: 92 | \techo foo 93 | bar: 94 | \techo bar 95 | " 96 | )); 97 | 98 | // The jobserver protocol means that the `make` process itself "runs with a 99 | // token", so we acquire our one token to drain the jobserver, and this 100 | // should mean that `make` itself never has a second token available to it. 101 | let _a = c.acquire(); 102 | c.configure(&mut cmd); 103 | let output = t!(cmd.output()); 104 | println!( 105 | "\n\t=== stderr\n\t\t{}", 106 | String::from_utf8_lossy(&output.stderr).replace("\n", "\n\t\t") 107 | ); 108 | println!( 109 | "\t=== stdout\n\t\t{}", 110 | String::from_utf8_lossy(&output.stdout).replace("\n", "\n\t\t") 111 | ); 112 | 113 | assert!(output.status.success()); 114 | assert!(output.stderr.is_empty()); 115 | 116 | let stdout = String::from_utf8_lossy(&output.stdout).replace("\r\n", "\n"); 117 | let a = "\ 118 | echo foo 119 | foo 120 | echo bar 121 | bar 122 | "; 123 | let b = "\ 124 | echo bar 125 | bar 126 | echo foo 127 | foo 128 | "; 129 | 130 | assert!(stdout == a || stdout == b); 131 | } 132 | 133 | #[test] 134 | fn make_as_a_multi_thread_client() { 135 | let c = t!(Client::new(1)); 136 | let td = tempfile::tempdir().unwrap(); 137 | 138 | let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string()); 139 | let mut cmd = Command::new(prog); 140 | cmd.current_dir(td.path()); 141 | 142 | t!(t!(File::create(td.path().join("Makefile"))).write_all( 143 | b" 144 | all: foo bar 145 | foo: 146 | \techo foo 147 | bar: 148 | \techo bar 149 | " 150 | )); 151 | 152 | // We're leaking one extra token to `make` sort of violating the makefile 153 | // jobserver protocol. It has the desired effect though. 154 | c.configure(&mut cmd); 155 | let output = t!(cmd.output()); 156 | println!( 157 | "\n\t=== stderr\n\t\t{}", 158 | String::from_utf8_lossy(&output.stderr).replace("\n", "\n\t\t") 159 | ); 160 | println!( 161 | "\t=== stdout\n\t\t{}", 162 | String::from_utf8_lossy(&output.stdout).replace("\n", "\n\t\t") 163 | ); 164 | 165 | assert!(output.status.success()); 166 | } 167 | 168 | #[test] 169 | fn zero_client() { 170 | let client = t!(Client::new(0)); 171 | let (tx, rx) = mpsc::channel(); 172 | let helper = client 173 | .into_helper_thread(move |a| drop(tx.send(a))) 174 | .unwrap(); 175 | helper.request_token(); 176 | helper.request_token(); 177 | 178 | for _ in 0..1000 { 179 | assert!(rx.try_recv().is_err()); 180 | } 181 | } 182 | --------------------------------------------------------------------------------