blob: 3a483781376b0be8f484bde0267edf4c2beda30c [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package task
import (
"time"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
// Signal is used to notify that a task has completed.
// Nothing is ever sent through a signal, it is closed to indicate signalled.
type Signal <-chan struct{}
// NewSignal builds a new signal, and then returns the signal and a Task that is used to fire the signal.
// The returned fire Task must only be called once.
func NewSignal() (Signal, Task) {
c := make(chan struct{})
return c, func(log.Context) error { close(c); return nil }
}
// Fired returns true if the signal has been fired.
func (s Signal) Fired() bool {
select {
case <-s:
return true
default:
return false
}
}
// Wait blocks until the signal has been fired.
func (s Signal) Wait() {
<-s
}
// TryWait waits for either timeout or the signal to fire, whichever comes first.
// It returns true if the signal is Fired, false if it timed out.
func (s Signal) TryWait(timeout time.Duration) bool {
select {
case <-s:
return true
case <-time.Tick(timeout):
return false
}
}