13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
53f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#ifndef BASE_SYNCHRONIZATION_CANCELLATION_FLAG_H_
63f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#define BASE_SYNCHRONIZATION_CANCELLATION_FLAG_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h"
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/atomicops.h"
113f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "base/threading/platform_thread.h"
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace base {
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// CancellationFlag allows one thread to cancel jobs executed on some worker
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// thread. Calling Set() from one thread and IsSet() from a number of threads
17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// is thread-safe.
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This class IS NOT intended for synchronization between threads.
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API CancellationFlag {
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  CancellationFlag() : flag_(false) {
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if !defined(NDEBUG)
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    set_on_ = PlatformThread::CurrentId();
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~CancellationFlag() {}
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Set the flag. May only be called on the thread which owns the object.
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Set();
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool IsSet() const;  // Returns true iff the flag was set.
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  base::subtle::Atomic32 flag_;
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if !defined(NDEBUG)
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PlatformThreadId set_on_;
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(CancellationFlag);
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}  // namespace base
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
443f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#endif  // BASE_SYNCHRONIZATION_CANCELLATION_FLAG_H_
45