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: #include "sandbox/win/src/win2k_threadpool.h" michael@0: michael@0: #include "sandbox/win/src/win_utils.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: bool Win2kThreadPool::RegisterWait(const void* cookie, HANDLE waitable_object, michael@0: CrossCallIPCCallback callback, michael@0: void* context) { michael@0: if (0 == cookie) { michael@0: return false; michael@0: } michael@0: HANDLE pool_object = NULL; michael@0: // create a wait for a kernel object, with no timeout michael@0: if (!::RegisterWaitForSingleObject(&pool_object, waitable_object, callback, michael@0: context, INFINITE, WT_EXECUTEDEFAULT)) { michael@0: return false; michael@0: } michael@0: PoolObject pool_obj = {cookie, pool_object}; michael@0: AutoLock lock(&lock_); michael@0: pool_objects_.push_back(pool_obj); michael@0: return true; michael@0: } michael@0: michael@0: bool Win2kThreadPool::UnRegisterWaits(void* cookie) { michael@0: if (0 == cookie) { michael@0: return false; michael@0: } michael@0: AutoLock lock(&lock_); michael@0: bool success = true; michael@0: PoolObjects::iterator it = pool_objects_.begin(); michael@0: while (it != pool_objects_.end()) { michael@0: if (it->cookie == cookie) { michael@0: HANDLE wait = it->wait; michael@0: it = pool_objects_.erase(it); michael@0: success &= (::UnregisterWaitEx(wait, INVALID_HANDLE_VALUE) != 0); michael@0: } else { michael@0: ++it; michael@0: } michael@0: } michael@0: return success; michael@0: } michael@0: michael@0: size_t Win2kThreadPool::OutstandingWaits() { michael@0: AutoLock lock(&lock_); michael@0: return pool_objects_.size(); michael@0: } michael@0: michael@0: Win2kThreadPool::~Win2kThreadPool() { michael@0: // Here we used to unregister all the pool wait handles. Now, following the michael@0: // rest of the code we avoid lengthy or blocking calls given that the process michael@0: // is being torn down. michael@0: ::DeleteCriticalSection(&lock_); michael@0: } michael@0: michael@0: } // namespace sandbox