1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2/* vim:set ts=2 sw=2 sts=2 et cindent: */ 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 "nsIEventTarget.idl" 8 9[uuid(ef194cab-3f86-4b61-b132-e5e96a79e5d1)] 10interface nsIThreadPoolListener : nsISupports 11{ 12 /** 13 * Called when a new thread is created by the thread pool. The notification 14 * happens on the newly-created thread. 15 */ 16 void onThreadCreated(); 17 18 /** 19 * Called when a thread is about to be destroyed by the thread pool. The 20 * notification happens on the thread that is about to be destroyed. 21 */ 22 void onThreadShuttingDown(); 23}; 24 25/** 26 * An interface to a thread pool. A thread pool creates a limited number of 27 * anonymous (unnamed) worker threads. An event dispatched to the thread pool 28 * will be run on the next available worker thread. 29 */ 30[uuid(76ce99c9-8e43-489a-9789-f27cc4424965)] 31interface nsIThreadPool : nsIEventTarget 32{ 33 /** 34 * Shutdown the thread pool. This method may not be executed from any thread 35 * in the thread pool. Instead, it is meant to be executed from another 36 * thread (usually the thread that created this thread pool). When this 37 * function returns, the thread pool and all of its threads will be shutdown, 38 * and it will no longer be possible to dispatch tasks to the thread pool. 39 * 40 * As a side effect, events on the current thread will be processed. 41 */ 42 void shutdown(); 43 44 /** 45 * Shutdown the thread pool, but only wait for aTimeoutMs. After the timeout 46 * expires, any threads that have not shutdown yet are leaked and will not 47 * block shutdown. 48 * 49 * This method should only be used at during shutdown to cleanup threads that 50 * made blocking calls to code outside our control, and can't be safely 51 * terminated. We choose to leak them intentionally to avoid a shutdown hang. 52 */ 53 [noscript] void shutdownWithTimeout(in long aTimeoutMs); 54 55 /** 56 * Get/set the maximum number of threads allowed at one time in this pool. 57 */ 58 attribute unsigned long threadLimit; 59 60 /** 61 * Get/set the maximum number of idle threads kept alive. 62 */ 63 attribute unsigned long idleThreadLimit; 64 65 /** 66 * Get/set the amount of time in milliseconds before an idle thread is 67 * destroyed. 68 */ 69 attribute unsigned long idleThreadTimeout; 70 71 /** 72 * If set to true the idle timeout will be calculated as idleThreadTimeout 73 * divideded by the number of idle threads at the moment. This may help 74 * save memory allocations but still keep reasonable amount of idle threads. 75 * Default is false, use |idleThreadTimeout| for all threads. 76 */ 77 attribute boolean idleThreadTimeoutRegressive; 78 79 /** 80 * Get/set the number of bytes reserved for the stack of all threads in 81 * the pool. By default this is nsIThreadManager::DEFAULT_STACK_SIZE. 82 */ 83 attribute unsigned long threadStackSize; 84 85 /** 86 * An optional listener that will be notified when a thread is created or 87 * destroyed in the course of the thread pool's operation. 88 * 89 * A listener will only receive notifications about threads created after the 90 * listener is set so it is recommended that the consumer set the listener 91 * before dispatching the first event. A listener that receives an 92 * onThreadCreated() notification is guaranteed to always receive the 93 * corresponding onThreadShuttingDown() notification. 94 * 95 * The thread pool takes ownership of the listener and releases it when the 96 * shutdown() method is called. Threads created after the listener is set will 97 * also take ownership of the listener so that the listener will be kept alive 98 * long enough to receive the guaranteed onThreadShuttingDown() notification. 99 */ 100 attribute nsIThreadPoolListener listener; 101 102 /** 103 * Set the label for threads in the pool. All threads will be named 104 * "<aName> #<n>", where <n> is a serial number. 105 */ 106 void setName(in ACString aName); 107}; 108