vitess.io/vitess@v0.16.2/go/sync2/semaphore.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sync2
    18  
    19  // What's in a name? Channels have all you need to emulate a counting
    20  // semaphore with a boatload of extra functionality. However, in some
    21  // cases, you just want a familiar API.
    22  
    23  import (
    24  	"context"
    25  	"time"
    26  )
    27  
    28  // Semaphore is a counting semaphore with the option to
    29  // specify a timeout.
    30  type Semaphore struct {
    31  	slots   chan struct{}
    32  	timeout time.Duration
    33  }
    34  
    35  // NewSemaphore creates a Semaphore. The count parameter must be a positive
    36  // number. A timeout of zero means that there is no timeout.
    37  func NewSemaphore(count int, timeout time.Duration) *Semaphore {
    38  	sem := &Semaphore{
    39  		slots:   make(chan struct{}, count),
    40  		timeout: timeout,
    41  	}
    42  	for i := 0; i < count; i++ {
    43  		sem.slots <- struct{}{}
    44  	}
    45  	return sem
    46  }
    47  
    48  // Acquire returns true on successful acquisition, and
    49  // false on a timeout.
    50  func (sem *Semaphore) Acquire() bool {
    51  	if sem.timeout == 0 {
    52  		<-sem.slots
    53  		return true
    54  	}
    55  	tm := time.NewTimer(sem.timeout)
    56  	defer tm.Stop()
    57  	select {
    58  	case <-sem.slots:
    59  		return true
    60  	case <-tm.C:
    61  		return false
    62  	}
    63  }
    64  
    65  // AcquireContext returns true on successful acquisition, and
    66  // false on context expiry. Timeout is ignored.
    67  func (sem *Semaphore) AcquireContext(ctx context.Context) bool {
    68  	select {
    69  	case <-sem.slots:
    70  		return true
    71  	case <-ctx.Done():
    72  		return false
    73  	}
    74  }
    75  
    76  // TryAcquire acquires a semaphore if it's immediately available.
    77  // It returns false otherwise.
    78  func (sem *Semaphore) TryAcquire() bool {
    79  	select {
    80  	case <-sem.slots:
    81  		return true
    82  	default:
    83  		return false
    84  	}
    85  }
    86  
    87  // Release releases the acquired semaphore. You must
    88  // not release more than the number of semaphores you've
    89  // acquired.
    90  func (sem *Semaphore) Release() {
    91  	sem.slots <- struct{}{}
    92  }
    93  
    94  // Size returns the current number of available slots.
    95  func (sem *Semaphore) Size() int {
    96  	return len(sem.slots)
    97  }