Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestThreads.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 "nsThreadUtils.h"
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include "nspr.h"
11
#include "nsCOMPtr.h"
12
#include "nsIServiceManager.h"
13
#include "nsXPCOM.h"
14
#include "mozilla/Monitor.h"
15
#include "gtest/gtest.h"
16
17
class nsRunner final : public nsIRunnable {
18
0
  ~nsRunner() {}
19
public:
20
    NS_DECL_THREADSAFE_ISUPPORTS
21
22
0
    NS_IMETHOD Run() override {
23
0
        nsCOMPtr<nsIThread> thread;
24
0
        nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
25
0
        EXPECT_TRUE(NS_SUCCEEDED(rv));
26
0
        printf("running %d on thread %p\n", mNum, (void *)thread.get());
27
0
28
0
        // if we don't do something slow, we'll never see the other
29
0
        // worker threads run
30
0
        PR_Sleep(PR_MillisecondsToInterval(100));
31
0
32
0
        return rv;
33
0
    }
34
35
0
    explicit nsRunner(int num) : mNum(num) {
36
0
    }
37
38
protected:
39
    int mNum;
40
};
41
42
NS_IMPL_ISUPPORTS(nsRunner, nsIRunnable)
43
44
TEST(Threads, Main)
45
0
{
46
0
    nsresult rv;
47
0
48
0
    nsCOMPtr<nsIRunnable> event = new nsRunner(0);
49
0
    EXPECT_TRUE(event);
50
0
51
0
    nsCOMPtr<nsIThread> runner;
52
0
    rv = NS_NewNamedThread("TestThreadsMain", getter_AddRefs(runner), event);
53
0
    EXPECT_TRUE(NS_SUCCEEDED(rv));
54
0
55
0
    nsCOMPtr<nsIThread> thread;
56
0
    rv = NS_GetCurrentThread(getter_AddRefs(thread));
57
0
    EXPECT_TRUE(NS_SUCCEEDED(rv));
58
0
59
0
    rv = runner->Shutdown();     // wait for the runner to die before quitting
60
0
    EXPECT_TRUE(NS_SUCCEEDED(rv));
61
0
62
0
    PR_Sleep(PR_MillisecondsToInterval(100));       // hopefully the runner will quit here
63
0
}
64
65
class nsStressRunner final : public nsIRunnable {
66
public:
67
    NS_DECL_THREADSAFE_ISUPPORTS
68
69
0
    NS_IMETHOD Run() override {
70
0
        EXPECT_FALSE(mWasRun);
71
0
        mWasRun = true;
72
0
        PR_Sleep(1);
73
0
        if (!PR_AtomicDecrement(&gNum)) {
74
0
            printf("   last thread was %d\n", mNum);
75
0
        }
76
0
        return NS_OK;
77
0
    }
78
79
0
    explicit nsStressRunner(int num) : mNum(num), mWasRun(false) {
80
0
        PR_AtomicIncrement(&gNum);
81
0
    }
82
83
0
    static int32_t GetGlobalCount() {return gNum;}
84
85
private:
86
0
    ~nsStressRunner() {
87
0
        EXPECT_TRUE(mWasRun);
88
0
    }
89
90
protected:
91
    static int32_t gNum;
92
    int32_t mNum;
93
    bool mWasRun;
94
};
95
96
int32_t nsStressRunner::gNum = 0;
97
98
NS_IMPL_ISUPPORTS(nsStressRunner, nsIRunnable)
99
100
TEST(Threads, Stress)
101
0
{
102
0
    const int loops = 1000;
103
0
    const int threads = 50;
104
0
105
0
    for (int i = 0; i < loops; i++) {
106
0
        printf("Loop %d of %d\n", i+1, loops);
107
0
108
0
        int k;
109
0
        nsIThread** array = new nsIThread*[threads];
110
0
111
0
        EXPECT_EQ(nsStressRunner::GetGlobalCount(), 0);
112
0
113
0
        for (k = 0; k < threads; k++) {
114
0
            nsCOMPtr<nsIThread> t;
115
0
            nsresult rv = NS_NewNamedThread("StressRunner", getter_AddRefs(t),
116
0
                                            new nsStressRunner(k));
117
0
            EXPECT_TRUE(NS_SUCCEEDED(rv));
118
0
            NS_ADDREF(array[k] = t);
119
0
        }
120
0
121
0
        for (k = threads-1; k >= 0; k--) {
122
0
            array[k]->Shutdown();
123
0
            NS_RELEASE(array[k]);
124
0
        }
125
0
        delete [] array;
126
0
    }
127
0
}
128
129
mozilla::Monitor* gAsyncShutdownReadyMonitor;
130
mozilla::Monitor* gBeginAsyncShutdownMonitor;
131
132
class AsyncShutdownPreparer : public nsIRunnable {
133
public:
134
    NS_DECL_THREADSAFE_ISUPPORTS
135
136
0
    NS_IMETHOD Run() override {
137
0
        EXPECT_FALSE(mWasRun);
138
0
        mWasRun = true;
139
0
140
0
        mozilla::MonitorAutoLock lock(*gAsyncShutdownReadyMonitor);
141
0
        lock.Notify();
142
0
143
0
        return NS_OK;
144
0
    }
145
146
0
    explicit AsyncShutdownPreparer() : mWasRun(false) {}
147
148
private:
149
0
    virtual ~AsyncShutdownPreparer() {
150
0
        EXPECT_TRUE(mWasRun);
151
0
    }
152
153
protected:
154
    bool mWasRun;
155
};
156
157
NS_IMPL_ISUPPORTS(AsyncShutdownPreparer, nsIRunnable)
158
159
class AsyncShutdownWaiter : public nsIRunnable {
160
public:
161
    NS_DECL_THREADSAFE_ISUPPORTS
162
163
0
    NS_IMETHOD Run() override {
164
0
        EXPECT_FALSE(mWasRun);
165
0
        mWasRun = true;
166
0
167
0
        nsCOMPtr<nsIThread> t;
168
0
        nsresult rv;
169
0
170
0
        {
171
0
          mozilla::MonitorAutoLock lock(*gBeginAsyncShutdownMonitor);
172
0
173
0
          rv = NS_NewNamedThread("AsyncShutdownPr", getter_AddRefs(t),
174
0
                                 new AsyncShutdownPreparer());
175
0
          EXPECT_TRUE(NS_SUCCEEDED(rv));
176
0
177
0
          lock.Wait();
178
0
        }
179
0
180
0
        rv = t->AsyncShutdown();
181
0
        EXPECT_TRUE(NS_SUCCEEDED(rv));
182
0
183
0
        return NS_OK;
184
0
    }
185
186
0
    explicit AsyncShutdownWaiter() : mWasRun(false) {}
187
188
private:
189
0
    virtual ~AsyncShutdownWaiter() {
190
0
        EXPECT_TRUE(mWasRun);
191
0
    }
192
193
protected:
194
    bool mWasRun;
195
};
196
197
NS_IMPL_ISUPPORTS(AsyncShutdownWaiter, nsIRunnable)
198
199
class SameThreadSentinel : public nsIRunnable {
200
public:
201
    NS_DECL_ISUPPORTS
202
203
0
    NS_IMETHOD Run() override {
204
0
        mozilla::MonitorAutoLock lock(*gBeginAsyncShutdownMonitor);
205
0
        lock.Notify();
206
0
        return NS_OK;
207
0
    }
208
209
private:
210
0
    virtual ~SameThreadSentinel() {}
211
};
212
213
NS_IMPL_ISUPPORTS(SameThreadSentinel, nsIRunnable)
214
215
TEST(Threads, AsyncShutdown)
216
0
{
217
0
  gAsyncShutdownReadyMonitor = new mozilla::Monitor("gAsyncShutdownReady");
218
0
  gBeginAsyncShutdownMonitor = new mozilla::Monitor("gBeginAsyncShutdown");
219
0
220
0
  nsCOMPtr<nsIThread> t;
221
0
  nsresult rv;
222
0
223
0
  {
224
0
    mozilla::MonitorAutoLock lock(*gAsyncShutdownReadyMonitor);
225
0
226
0
    rv = NS_NewNamedThread("AsyncShutdownWt", getter_AddRefs(t),
227
0
                           new AsyncShutdownWaiter());
228
0
    EXPECT_TRUE(NS_SUCCEEDED(rv));
229
0
230
0
    lock.Wait();
231
0
  }
232
0
233
0
  NS_DispatchToCurrentThread(new SameThreadSentinel());
234
0
  rv = t->Shutdown();
235
0
  EXPECT_TRUE(NS_SUCCEEDED(rv));
236
0
237
0
  delete gAsyncShutdownReadyMonitor;
238
0
  delete gBeginAsyncShutdownMonitor;
239
0
}
240
241
static void threadProc(void *arg)
242
0
{
243
0
    // printf("   running thread %d\n", (int) arg);
244
0
    PR_Sleep(1);
245
0
    EXPECT_EQ(PR_JOINABLE_THREAD, PR_GetThreadState(PR_GetCurrentThread()));
246
0
}
247
248
TEST(Threads, StressNSPR)
249
0
{
250
0
    const int loops = 1000;
251
0
    const int threads = 50;
252
0
253
0
    for (int i = 0; i < loops; i++) {
254
0
        printf("Loop %d of %d\n", i+1, loops);
255
0
256
0
        intptr_t k;
257
0
        PRThread** array = new PRThread*[threads];
258
0
259
0
        for (k = 0; k < threads; k++) {
260
0
            array[k] = PR_CreateThread(PR_USER_THREAD,
261
0
                                       threadProc, (void*) k,
262
0
                                       PR_PRIORITY_NORMAL,
263
0
                                       PR_GLOBAL_THREAD,
264
0
                                       PR_JOINABLE_THREAD,
265
0
                                       0);
266
0
            EXPECT_TRUE(array[k]);
267
0
        }
268
0
269
0
        for (k = 0; k < threads; k++) {
270
0
            EXPECT_EQ(PR_JOINABLE_THREAD, PR_GetThreadState(array[k]));
271
0
        }
272
0
273
0
        for (k = threads-1; k >= 0; k--) {
274
0
            PR_JoinThread(array[k]);
275
0
        }
276
0
        delete [] array;
277
0
    }
278
0
}