1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/TestThreadPool.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,62 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <stdio.h> 1.11 +#include <stdlib.h> 1.12 +#include "nsXPCOM.h" 1.13 +#include "nsXPCOMCIDInternal.h" 1.14 +#include "nsIThreadPool.h" 1.15 +#include "nsComponentManagerUtils.h" 1.16 +#include "nsCOMPtr.h" 1.17 +#include "nsIRunnable.h" 1.18 + 1.19 +class Task : public nsIRunnable 1.20 +{ 1.21 +public: 1.22 + NS_DECL_THREADSAFE_ISUPPORTS 1.23 + 1.24 + Task(int i) : mIndex(i) {} 1.25 + 1.26 + NS_IMETHOD Run() 1.27 + { 1.28 + printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread()); 1.29 + int r = (int) ((float) rand() * 200 / RAND_MAX); 1.30 + PR_Sleep(PR_MillisecondsToInterval(r)); 1.31 + printf("###(%d) exiting from thread: %p\n", mIndex, (void *) PR_GetCurrentThread()); 1.32 + return NS_OK; 1.33 + } 1.34 + 1.35 +private: 1.36 + int mIndex; 1.37 +}; 1.38 +NS_IMPL_ISUPPORTS(Task, nsIRunnable) 1.39 + 1.40 +static nsresult 1.41 +RunTests() 1.42 +{ 1.43 + nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID); 1.44 + NS_ENSURE_STATE(pool); 1.45 + 1.46 + for (int i = 0; i < 100; ++i) { 1.47 + nsCOMPtr<nsIRunnable> task = new Task(i); 1.48 + NS_ENSURE_TRUE(task, NS_ERROR_OUT_OF_MEMORY); 1.49 + 1.50 + pool->Dispatch(task, NS_DISPATCH_NORMAL); 1.51 + } 1.52 + 1.53 + pool->Shutdown(); 1.54 + return NS_OK; 1.55 +} 1.56 + 1.57 +int 1.58 +main(int argc, char **argv) 1.59 +{ 1.60 + if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr))) 1.61 + return -1; 1.62 + RunTests(); 1.63 + NS_ShutdownXPCOM(nullptr); 1.64 + return 0; 1.65 +}