/* Copyright (c) 2005-2021 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ //! \file test_tbb_fork.cpp //! \brief Test for [sched.global_control] specification #define TBB_PREVIEW_WAITING_FOR_WORKERS 1 #include "tbb/global_control.h" #include "tbb/blocked_range.h" #include "tbb/cache_aligned_allocator.h" #include "tbb/parallel_for.h" static const int MinThread = 1; static const int MaxThread = 4; // Doctest is not used here, but placed just to prevent compiler errors for bad headers design #define DOCTEST_CONFIG_IMPLEMENT #include "common/test.h" #include "common/utils.h" #include "common/utils_assert.h" #if _WIN32||_WIN64 #include "tbb/concurrent_hash_map.h" HANDLE getCurrentThreadHandle() { HANDLE hProc = GetCurrentProcess(), hThr = INVALID_HANDLE_VALUE; #if TBB_USE_ASSERT BOOL res = #endif DuplicateHandle( hProc, GetCurrentThread(), hProc, &hThr, 0, FALSE, DUPLICATE_SAME_ACCESS ); __TBB_ASSERT( res, "Retrieving current thread handle failed" ); return hThr; } bool threadTerminated(HANDLE h) { DWORD ret = WaitForSingleObjectEx(h, 0, FALSE); return WAIT_OBJECT_0 == ret; } struct Data { HANDLE h; }; typedef tbb::concurrent_hash_map TidTableType; static TidTableType tidTable; #else #if __sun || __SUNPRO_CC #define _POSIX_PTHREAD_SEMANTICS 1 // to get standard-conforming sigwait(2) #endif #include #include #include #include #include #include "tbb/tick_count.h" void SigHandler(int) { } #endif // _WIN32||_WIN64 class AllocTask { public: void operator() (const tbb::blocked_range &r) const { #if _WIN32||_WIN64 HANDLE h = getCurrentThreadHandle(); DWORD tid = GetCurrentThreadId(); { TidTableType::accessor acc; if (tidTable.insert(acc, tid)) { acc->second.h = h; } } #endif for (int y = r.begin(); y != r.end(); ++y) { void *p = tbb::detail::r1::cache_aligned_allocate(7000); tbb::detail::r1::cache_aligned_deallocate(p); } } AllocTask() {} }; void CallParallelFor() { tbb::parallel_for(tbb::blocked_range(0, 10000, 1), AllocTask(), tbb::simple_partitioner()); } /* Regression test against data race between termination of workers and setting blocking termination mode in main thread. */ class RunWorkersBody : utils::NoAssign { bool wait_workers; public: RunWorkersBody(bool waitWorkers) : wait_workers(waitWorkers) {} void operator()(const int /*threadID*/) const { tbb::task_scheduler_handle tsi = tbb::task_scheduler_handle::get(); CallParallelFor(); if (wait_workers) { bool ok = tbb::finalize(tsi, std::nothrow); ASSERT(ok, NULL); } else { tbb::task_scheduler_handle::release(tsi); } } }; void TestBlockNonblock() { for (int i=0; i<100; i++) { utils::NativeParallelFor(4, RunWorkersBody(/*wait_workers=*/false)); RunWorkersBody(/*wait_workers=*/true)(0); } } class RunInNativeThread : utils::NoAssign { bool blocking; public: RunInNativeThread(bool blocking_) : blocking(blocking_) {} void operator()(const int /*threadID*/) const { tbb::task_scheduler_handle tsi = tbb::task_scheduler_handle::get(); CallParallelFor(); if (blocking) { bool ok = tbb::finalize(tsi, std::nothrow); ASSERT(!ok, "Nested blocking terminate must fail."); } else { tbb::task_scheduler_handle::release(tsi); } } }; void TestTasksInThread() { tbb::task_scheduler_handle sch = tbb::task_scheduler_handle::get(); CallParallelFor(); utils::NativeParallelFor(2, RunInNativeThread(/*blocking=*/false)); bool ok = tbb::finalize(sch, std::nothrow); ASSERT(ok, NULL); } #if TBB_REVAMP_TODO #include "common/memory_usage.h" // check for memory leak during TBB task scheduler init/terminate life cycle // TODO: move to test_task_scheduler_init after workers waiting productization void TestSchedulerMemLeaks() { const int ITERS = 10; int it; for (it=0; itfirst) { ASSERT(threadTerminated(it->second.h), NULL); } } tidTable.clear(); #else // _WIN32||_WIN64 if (child) exit(0); else { pid_t pid = fork(); if (!pid) { i = -1; child = true; } else { int sig; pid_t w_ret = 0; // wait for SIGCHLD up to timeout alarm(30); if (0 != sigwait(&sig_set, &sig)) ASSERT(0, "sigwait failed"); alarm(0); w_ret = waitpid(pid, NULL, WNOHANG); ASSERT(w_ret>=0, "waitpid failed"); if (!w_ret) { ASSERT(!kill(pid, SIGKILL), NULL); w_ret = waitpid(pid, NULL, 0); ASSERT(w_ret!=-1, "waitpid failed"); ASSERT(0, "Hang after fork"); } // clean pending signals (if any occurs since sigwait) sigset_t p_mask; for (;;) { sigemptyset(&p_mask); sigpending(&p_mask); if (sigismember(&p_mask, SIGALRM) || sigismember(&p_mask, SIGCHLD)) { if (0 != sigwait(&p_mask, &sig)) ASSERT(0, "sigwait failed"); } else break; } } } #endif // _WIN32||_WIN64 } } // auto initialization at this point TestAutoInit(); return 0; }