michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef SANDBOX_SRC_WIN2K_THREADPOOL_H_ michael@0: #define SANDBOX_SRC_WIN2K_THREADPOOL_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include "sandbox/win/src/crosscall_server.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: // Win2kThreadPool a simple implementation of a thread provider as required michael@0: // for the sandbox IPC subsystem. See sandbox\crosscall_server.h for the details michael@0: // and requirements of this interface. michael@0: // michael@0: // Implementing the thread provider as a thread pool is desirable in the case michael@0: // of shared memory IPC because it can generate a large number of waitable michael@0: // events: as many as channels. A thread pool does not create a thread per michael@0: // event, instead maintains a few idle threads but can create more if the need michael@0: // arises. michael@0: // michael@0: // This implementation simply thunks to the nice thread pool API of win2k. michael@0: class Win2kThreadPool : public ThreadProvider { michael@0: public: michael@0: Win2kThreadPool() { michael@0: ::InitializeCriticalSection(&lock_); michael@0: } michael@0: virtual ~Win2kThreadPool(); michael@0: michael@0: virtual bool RegisterWait(const void* cookie, HANDLE waitable_object, michael@0: CrossCallIPCCallback callback, michael@0: void* context); michael@0: michael@0: virtual bool UnRegisterWaits(void* cookie); michael@0: michael@0: // Returns the total number of wait objects associated with michael@0: // the thread pool. michael@0: size_t OutstandingWaits(); michael@0: michael@0: private: michael@0: // record to keep track of a wait and its associated cookie. michael@0: struct PoolObject { michael@0: const void* cookie; michael@0: HANDLE wait; michael@0: }; michael@0: // The list of pool wait objects. michael@0: typedef std::list PoolObjects; michael@0: PoolObjects pool_objects_; michael@0: // This lock protects the list of pool wait objects. michael@0: CRITICAL_SECTION lock_; michael@0: DISALLOW_COPY_AND_ASSIGN(Win2kThreadPool); michael@0: }; michael@0: michael@0: } // namespace sandbox michael@0: michael@0: #endif // SANDBOX_SRC_WIN2K_THREADPOOL_H_