Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestThreadPool.cpp
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
#include <stdio.h>
8
#include <stdlib.h>
9
#include "nsXPCOM.h"
10
#include "nsXPCOMCIDInternal.h"
11
#include "nsThreadPool.h"
12
#include "nsComponentManagerUtils.h"
13
#include "nsCOMPtr.h"
14
#include "nsIRunnable.h"
15
#include "nsThreadUtils.h"
16
#include "mozilla/Atomics.h"
17
#include "mozilla/Monitor.h"
18
#include "gtest/gtest.h"
19
20
using namespace mozilla;
21
22
class Task final : public nsIRunnable
23
{
24
public:
25
  NS_DECL_THREADSAFE_ISUPPORTS
26
27
0
  explicit Task(int i) : mIndex(i) {}
28
29
  NS_IMETHOD Run() override
30
0
  {
31
0
    printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
32
0
    int r = (int) ((float) rand() * 200 / RAND_MAX);
33
0
    PR_Sleep(PR_MillisecondsToInterval(r));
34
0
    printf("###(%d) exiting from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
35
0
    ++sCount;
36
0
    return NS_OK;
37
0
  }
38
39
  static mozilla::Atomic<int> sCount;
40
41
private:
42
0
  ~Task() {}
43
44
  int mIndex;
45
};
46
NS_IMPL_ISUPPORTS(Task, nsIRunnable)
47
48
mozilla::Atomic<int> Task::sCount;
49
50
TEST(ThreadPool, Main)
51
0
{
52
0
  nsCOMPtr<nsIThreadPool> pool = new nsThreadPool();
53
0
54
0
  for (int i = 0; i < 100; ++i) {
55
0
    nsCOMPtr<nsIRunnable> task = new Task(i);
56
0
    EXPECT_TRUE(task);
57
0
58
0
    pool->Dispatch(task, NS_DISPATCH_NORMAL);
59
0
  }
60
0
61
0
  pool->Shutdown();
62
0
  EXPECT_EQ(Task::sCount, 100);
63
0
}
64
65
TEST(ThreadPool, Parallelism)
66
0
{
67
0
  nsCOMPtr<nsIThreadPool> pool = new nsThreadPool();
68
0
69
0
  // Dispatch and sleep to ensure we have an idle thread
70
0
  nsCOMPtr<nsIRunnable> r0 = new Runnable("TestRunnable");
71
0
  pool->Dispatch(r0, NS_DISPATCH_SYNC);
72
0
  PR_Sleep(PR_SecondsToInterval(2));
73
0
74
0
  class Runnable1 : public Runnable {
75
0
  public:
76
0
    Runnable1(Monitor& aMonitor, bool& aDone)
77
0
      : mozilla::Runnable("Runnable1")
78
0
      , mMonitor(aMonitor)
79
0
      , mDone(aDone)
80
0
    {
81
0
    }
82
0
83
0
    NS_IMETHOD Run() override {
84
0
      MonitorAutoLock mon(mMonitor);
85
0
      if (!mDone) {
86
0
        // Wait for a reasonable timeout since we don't want to block gtests
87
0
        // forever should any regression happen.
88
0
        mon.Wait(TimeDuration::FromSeconds(300));
89
0
      }
90
0
      EXPECT_TRUE(mDone);
91
0
      return NS_OK;
92
0
    }
93
0
  private:
94
0
    Monitor& mMonitor;
95
0
    bool& mDone;
96
0
  };
97
0
98
0
  class Runnable2 : public Runnable {
99
0
  public:
100
0
    Runnable2(Monitor& aMonitor, bool& aDone)
101
0
      : mozilla::Runnable("Runnable2")
102
0
      , mMonitor(aMonitor)
103
0
      , mDone(aDone)
104
0
    {
105
0
    }
106
0
107
0
    NS_IMETHOD Run() override {
108
0
      MonitorAutoLock mon(mMonitor);
109
0
      mDone = true;
110
0
      mon.NotifyAll();
111
0
      return NS_OK;
112
0
    }
113
0
  private:
114
0
    Monitor& mMonitor;
115
0
    bool& mDone;
116
0
  };
117
0
118
0
  // Dispatch 2 events in a row. Since we are still within the thread limit,
119
0
  // We should wake up the idle thread and spawn a new thread so these 2 events
120
0
  // can run in parallel. We will time out if r1 and r2 run in sequence for r1
121
0
  // won't finish until r2 finishes.
122
0
  Monitor mon("ThreadPool::Parallelism");
123
0
  bool done = false;
124
0
  nsCOMPtr<nsIRunnable> r1 = new Runnable1(mon, done);
125
0
  nsCOMPtr<nsIRunnable> r2 = new Runnable2(mon, done);
126
0
  pool->Dispatch(r1, NS_DISPATCH_NORMAL);
127
0
  pool->Dispatch(r2, NS_DISPATCH_NORMAL);
128
0
129
0
  pool->Shutdown();
130
0
}