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