michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include "nsXPCOM.h" michael@0: #include "nsXPCOMCIDInternal.h" michael@0: #include "nsIThreadPool.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIRunnable.h" michael@0: michael@0: class Task : public nsIRunnable michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: Task(int i) : mIndex(i) {} michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread()); michael@0: int r = (int) ((float) rand() * 200 / RAND_MAX); michael@0: PR_Sleep(PR_MillisecondsToInterval(r)); michael@0: printf("###(%d) exiting from thread: %p\n", mIndex, (void *) PR_GetCurrentThread()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: int mIndex; michael@0: }; michael@0: NS_IMPL_ISUPPORTS(Task, nsIRunnable) michael@0: michael@0: static nsresult michael@0: RunTests() michael@0: { michael@0: nsCOMPtr pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID); michael@0: NS_ENSURE_STATE(pool); michael@0: michael@0: for (int i = 0; i < 100; ++i) { michael@0: nsCOMPtr task = new Task(i); michael@0: NS_ENSURE_TRUE(task, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: pool->Dispatch(task, NS_DISPATCH_NORMAL); michael@0: } michael@0: michael@0: pool->Shutdown(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr))) michael@0: return -1; michael@0: RunTests(); michael@0: NS_ShutdownXPCOM(nullptr); michael@0: return 0; michael@0: }