@ -5,7 +5,7 @@
# ifndef V8_CANCELABLE_TASK_H_
# ifndef V8_CANCELABLE_TASK_H_
# define V8_CANCELABLE_TASK_H_
# define V8_CANCELABLE_TASK_H_
# include <map>
# include <unordered_ map>
# include "include/v8-platform.h"
# include "include/v8-platform.h"
# include "src/base/atomic-utils.h"
# include "src/base/atomic-utils.h"
@ -24,12 +24,14 @@ class Isolate;
// from any fore- and background task/thread.
// from any fore- and background task/thread.
class V8_EXPORT_PRIVATE CancelableTaskManager {
class V8_EXPORT_PRIVATE CancelableTaskManager {
public :
public :
using Id = uint64_t ;
CancelableTaskManager ( ) ;
CancelableTaskManager ( ) ;
// Registers a new cancelable {task}. Returns the unique {id} of the task that
// Registers a new cancelable {task}. Returns the unique {id} of the task that
// can be used to try to abort a task by calling {Abort}.
// can be used to try to abort a task by calling {Abort}.
// Must not be called after CancelAndWait.
// Must not be called after CancelAndWait.
uint32_t Register ( Cancelable * task ) ;
Id Register ( Cancelable * task ) ;
// Try to abort running a task identified by {id}. The possible outcomes are:
// Try to abort running a task identified by {id}. The possible outcomes are:
// (1) The task is already finished running or was canceled before and
// (1) The task is already finished running or was canceled before and
@ -39,7 +41,7 @@ class V8_EXPORT_PRIVATE CancelableTaskManager {
// removed.
// removed.
//
//
enum TryAbortResult { kTaskRemoved , kTaskRunning , kTaskAborted } ;
enum TryAbortResult { kTaskRemoved , kTaskRunning , kTaskAborted } ;
TryAbortResult TryAbort ( uint32_t id ) ;
TryAbortResult TryAbort ( Id id ) ;
// Cancels all remaining registered tasks and waits for tasks that are
// Cancels all remaining registered tasks and waits for tasks that are
// already running. This disallows subsequent Register calls.
// already running. This disallows subsequent Register calls.
@ -59,13 +61,13 @@ class V8_EXPORT_PRIVATE CancelableTaskManager {
private :
private :
// Only called by {Cancelable} destructor. The task is done with executing,
// Only called by {Cancelable} destructor. The task is done with executing,
// but needs to be removed.
// but needs to be removed.
void RemoveFinishedTask ( uint32_t id ) ;
void RemoveFinishedTask ( Id id ) ;
// To mitigate the ABA problem, the api refers to tasks through an id.
// To mitigate the ABA problem, the api refers to tasks through an id.
uint32_t task_id_counter_ ;
Id task_id_counter_ ;
// A set of cancelable tasks that are currently registered.
// A set of cancelable tasks that are currently registered.
std : : map < uint32_t , Cancelable * > cancelable_tasks_ ;
std : : unordered_ map< Id , Cancelable * > cancelable_tasks_ ;
// Mutex and condition variable enabling concurrent register and removing, as
// Mutex and condition variable enabling concurrent register and removing, as
// well as waiting for background tasks on {CancelAndWait}.
// well as waiting for background tasks on {CancelAndWait}.
@ -89,7 +91,7 @@ class V8_EXPORT_PRIVATE Cancelable {
// a platform. This step transfers ownership to the platform, which destroys
// a platform. This step transfers ownership to the platform, which destroys
// the task after running it. Since the exact time is not known, we cannot
// the task after running it. Since the exact time is not known, we cannot
// access the object after handing it to a platform.
// access the object after handing it to a platform.
uint32_t id ( ) { return id_ ; }
CancelableTaskManager : : Id id ( ) { return id_ ; }
protected :
protected :
bool TryRun ( ) { return status_ . TrySetValue ( kWaiting , kRunning ) ; }
bool TryRun ( ) { return status_ . TrySetValue ( kWaiting , kRunning ) ; }
@ -120,7 +122,7 @@ class V8_EXPORT_PRIVATE Cancelable {
CancelableTaskManager * parent_ ;
CancelableTaskManager * parent_ ;
base : : AtomicValue < Status > status_ ;
base : : AtomicValue < Status > status_ ;
uint32_t id_ ;
CancelableTaskManager : : Id id_ ;
// The counter is incremented for failing tries to cancel a task. This can be
// The counter is incremented for failing tries to cancel a task. This can be
// used by the task itself as an indication how often external entities tried
// used by the task itself as an indication how often external entities tried