Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/threads/nsThreadSyncDispatch.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsThreadSyncDispatch_h_
8
#define nsThreadSyncDispatch_h_
9
10
#include "mozilla/Atomics.h"
11
#include "mozilla/DebugOnly.h"
12
13
#include "nsThreadUtils.h"
14
#include "LeakRefPtr.h"
15
16
class nsThreadSyncDispatch : public mozilla::Runnable
17
{
18
public:
19
  nsThreadSyncDispatch(already_AddRefed<nsIEventTarget> aOrigin, already_AddRefed<nsIRunnable>&& aTask)
20
    : Runnable("nsThreadSyncDispatch")
21
    , mOrigin(aOrigin)
22
    , mSyncTask(std::move(aTask))
23
    , mIsPending(true)
24
0
  {
25
0
  }
26
27
  bool IsPending()
28
0
  {
29
0
    // This is an atomic acquire on the origin thread.
30
0
    return mIsPending;
31
0
  }
32
33
private:
34
  NS_IMETHOD Run() override
35
0
  {
36
0
    if (nsCOMPtr<nsIRunnable> task = mSyncTask.take()) {
37
0
      MOZ_ASSERT(!mSyncTask);
38
0
39
0
      mozilla::DebugOnly<nsresult> result = task->Run();
40
0
      MOZ_ASSERT(NS_SUCCEEDED(result),
41
0
                 "task in sync dispatch should not fail");
42
0
43
0
      // We must release the task here to ensure that when the original
44
0
      // thread is unblocked, this task has been released.
45
0
      task = nullptr;
46
0
47
0
      // This is an atomic release on the target thread.
48
0
      mIsPending = false;
49
0
50
0
      // unblock the origin thread
51
0
      mOrigin->Dispatch(this, NS_DISPATCH_NORMAL);
52
0
    }
53
0
54
0
    return NS_OK;
55
0
  }
56
57
  nsCOMPtr<nsIEventTarget> mOrigin;
58
  // The task is leaked by default when Run() is not called, because
59
  // otherwise we may release it in an incorrect thread.
60
  mozilla::LeakRefPtr<nsIRunnable> mSyncTask;
61
  mozilla::Atomic<bool, mozilla::ReleaseAcquire> mIsPending;
62
};
63
64
#endif // nsThreadSyncDispatch_h_