| // 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 app |
| |
| import ( |
| "os" |
| "os/signal" |
| "time" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| "android.googlesource.com/platform/tools/gpu/framework/task" |
| ) |
| |
| var ( |
| // CleanupTimeout is the time to wait for all cleanup signals to fire when shutting down. |
| CleanupTimeout = time.Second |
| |
| events = task.Events{} |
| ) |
| |
| // AddCleanupSignal adds a signal the app should wait on when shutting down. |
| // The signal will automatically be dropped when it is fired, no need unregister it. |
| func AddCleanupSignal(s ...task.Event) { |
| events.Add(s...) |
| } |
| |
| // WaitForCleanup waits for all the cleanup signals to fire, or the cleanup timeout to expire, |
| // whichever comes first. |
| func WaitForCleanup(ctx log.Context) bool { |
| return events.TryWait(ctx, CleanupTimeout) |
| } |
| |
| func handleAbortSignals(cancel task.CancelFunc) { |
| // register a signal handler for exits |
| sigchan := make(chan os.Signal) |
| // Enable signal interception, no-op if already enabled. |
| // Note: for Unix, these signals translate to SIGINT and SIGKILL. |
| signal.Notify(sigchan, os.Interrupt, os.Kill) |
| // Run a goroutine that calls the cancel func if the signal is received |
| go func() { |
| <-sigchan |
| cancel() |
| }() |
| } |