xpcom/tests/TestThreadPool.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <stdio.h>
michael@0 8 #include <stdlib.h>
michael@0 9 #include "nsXPCOM.h"
michael@0 10 #include "nsXPCOMCIDInternal.h"
michael@0 11 #include "nsIThreadPool.h"
michael@0 12 #include "nsComponentManagerUtils.h"
michael@0 13 #include "nsCOMPtr.h"
michael@0 14 #include "nsIRunnable.h"
michael@0 15
michael@0 16 class Task : public nsIRunnable
michael@0 17 {
michael@0 18 public:
michael@0 19 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 20
michael@0 21 Task(int i) : mIndex(i) {}
michael@0 22
michael@0 23 NS_IMETHOD Run()
michael@0 24 {
michael@0 25 printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
michael@0 26 int r = (int) ((float) rand() * 200 / RAND_MAX);
michael@0 27 PR_Sleep(PR_MillisecondsToInterval(r));
michael@0 28 printf("###(%d) exiting from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
michael@0 29 return NS_OK;
michael@0 30 }
michael@0 31
michael@0 32 private:
michael@0 33 int mIndex;
michael@0 34 };
michael@0 35 NS_IMPL_ISUPPORTS(Task, nsIRunnable)
michael@0 36
michael@0 37 static nsresult
michael@0 38 RunTests()
michael@0 39 {
michael@0 40 nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
michael@0 41 NS_ENSURE_STATE(pool);
michael@0 42
michael@0 43 for (int i = 0; i < 100; ++i) {
michael@0 44 nsCOMPtr<nsIRunnable> task = new Task(i);
michael@0 45 NS_ENSURE_TRUE(task, NS_ERROR_OUT_OF_MEMORY);
michael@0 46
michael@0 47 pool->Dispatch(task, NS_DISPATCH_NORMAL);
michael@0 48 }
michael@0 49
michael@0 50 pool->Shutdown();
michael@0 51 return NS_OK;
michael@0 52 }
michael@0 53
michael@0 54 int
michael@0 55 main(int argc, char **argv)
michael@0 56 {
michael@0 57 if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
michael@0 58 return -1;
michael@0 59 RunTests();
michael@0 60 NS_ShutdownXPCOM(nullptr);
michael@0 61 return 0;
michael@0 62 }

mercurial