]> Cypherpunks.ru repositories - goredo.git/blobdiff - js.go
Download link for 2.6.2 release
[goredo.git] / js.go
diff --git a/js.go b/js.go
index 1e452ee51a02725bcae747bdc501b72d3fffe52b..0aa43d9393cbc07cbc0adc98ca56ef87303578ff 100644 (file)
--- a/js.go
+++ b/js.go
@@ -1,19 +1,17 @@
-/*
-goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+// goredo -- djb's redo implementation on pure Go
+// Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 // Jobserver
 
@@ -24,11 +22,9 @@ import (
        "fmt"
        "log"
        "os"
-       "os/signal"
        "regexp"
        "strconv"
        "sync"
-       "syscall"
 )
 
 const (
@@ -39,7 +35,7 @@ const (
        EnvJSToken = "REDO_JS_TOKEN"
        EnvMake    = "REDO_MAKE"
 
-       MakeTypeNone = "none"
+       MakeTypeNone  = "none"
        MakeTypeBmake = "bmake"
        MakeTypeGmake = "gmake"
 )
@@ -69,17 +65,27 @@ var (
        jsTokens  map[byte]int
        jsTokensM sync.Mutex
 
-       flagJobs = flag.Int("j", -1, fmt.Sprintf("number of parallel jobs (0=inf, <0=1) (%s)", EnvJobs))
+       flagJobs *int
 )
 
+func init() {
+       cmdName := CmdName()
+       if !(cmdName == CmdNameRedo || cmdName == CmdNameRedoIfchange) {
+               return
+       }
+       flagJobs = flag.Int("j", -1,
+               fmt.Sprintf("number of parallel jobs (0=inf, <0=1) (%s)", EnvJobs))
+}
+
 func jsStart(jobsEnv string) {
        jobs := uint64(1)
        var err error
-       if *flagJobs == 0 {
+       switch {
+       case *flagJobs == 0:
                jobs = 0
-       } else if *flagJobs > 0 {
+       case *flagJobs > 0:
                jobs = uint64(*flagJobs)
-       } else if jobsEnv != "" {
+       case jobsEnv != "":
                jobs, err = strconv.ParseUint(jobsEnv, 10, 64)
                if err != nil {
                        log.Fatalln("can not parse", EnvJobs, err)
@@ -91,9 +97,9 @@ func jsStart(jobsEnv string) {
        }
        JSR, JSW, err = os.Pipe()
        if err != nil {
-               log.Fatalln(err)
+               log.Fatal(err)
        }
-       trace(CJS, "initial fill with %d", jobs)
+       tracef(CJS, "initial fill with %d", jobs)
        jsTokens[BMakeGoodToken] = int(jobs)
        for ; jobs > 0; jobs-- {
                jsReleaseNoLock(BMakeGoodToken)
@@ -145,7 +151,7 @@ func jsInit() {
        func() {
                defer func() {
                        if err := recover(); err != nil {
-                               log.Fatalln(err)
+                               log.Fatal(err)
                        }
                }()
                JSR = mustParseFd(match[2], "JSR")
@@ -161,20 +167,6 @@ func jsInit() {
                jsTokens[jsToken]++
                jsRelease("ifchange entered", jsToken)
        }
-
-       killed := make(chan os.Signal, 0)
-       signal.Notify(killed, syscall.SIGTERM, syscall.SIGINT)
-       go func() {
-               <-killed
-               jsTokensM.Lock()
-               for token, i := range jsTokens {
-                       for ; i > 0; i-- {
-                               jsReleaseNoLock(token)
-                       }
-               }
-               jsTokensM.Unlock()
-               os.Exit(1)
-       }()
 }
 
 func jsReleaseNoLock(token byte) {
@@ -187,18 +179,28 @@ func jsRelease(ctx string, token byte) {
        if JSW == nil {
                return
        }
-       trace(CJS, "release from %s", ctx)
+       tracef(CJS, "release from %s", ctx)
        jsTokensM.Lock()
        jsTokens[token]--
        jsReleaseNoLock(token)
        jsTokensM.Unlock()
 }
 
+func jsReleaseAll() {
+       jsTokensM.Lock()
+       for token, i := range jsTokens {
+               for ; i > 0; i-- {
+                       jsReleaseNoLock(token)
+               }
+       }
+       jsTokensM.Unlock()
+}
+
 func jsAcquire(ctx string) byte {
        if JSR == nil {
                return BMakeGoodToken
        }
-       trace(CJS, "acquire for %s", ctx)
+       tracef(CJS, "acquire for %s", ctx)
        token := []byte{0}
        if n, err := JSR.Read(token); err != nil || n != 1 {
                log.Fatalln("can not read JSR:", err)
@@ -206,6 +208,6 @@ func jsAcquire(ctx string) byte {
        jsTokensM.Lock()
        jsTokens[token[0]]++
        jsTokensM.Unlock()
-       trace(CJS, "acquired for %s", ctx)
+       tracef(CJS, "acquired for %s", ctx)
        return token[0]
 }