1 /* 2 Copyright (c) 2020-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "common/test.h" 18 19 #if (!__TBB_WIN8UI_SUPPORT && !defined(WINAPI_FAMILY) && !__ANDROID__) 20 21 #include "oneapi/tbb/task.h" 22 #include "oneapi/tbb/task_group.h" 23 #include <thread> 24 25 //! \file conformance_resumable_tasks.cpp 26 //! \brief Test for [resumable_tasks] specification 27 28 thread_local bool mLocal = false; 29 30 //! Test asynchronous resume 31 //! \brief \ref interface \ref requirement 32 TEST_CASE("Async test") { 33 CHECK(!mLocal); 34 mLocal = true; 35 bool suspend = false, resume = false; 36 std::thread t; __anon366fea4d0102(oneapi::tbb::task::suspend_point sp) 37 oneapi::tbb::task::suspend([&t, &suspend, &resume](oneapi::tbb::task::suspend_point sp) { 38 suspend = true; 39 t = std::thread([sp, &resume] { 40 resume = true; 41 oneapi::tbb::task::resume(sp); 42 }); 43 }); 44 CHECK(suspend); 45 CHECK(resume); 46 CHECK_MESSAGE(mLocal, "The same thread is expected"); 47 mLocal = false; 48 t.join(); 49 } 50 51 //! Test parallel resume 52 //! \brief \ref interface \ref requirement 53 TEST_CASE("Parallel test") { 54 CHECK(!mLocal); 55 mLocal = true; 56 constexpr int N = 100; 57 std::atomic<int> suspend{}, resume{}; 58 oneapi::tbb::task_group tg; 59 for (int i = 0; i < N; ++i) { __anon366fea4d0302null60 tg.run([&tg, &suspend, &resume] { 61 oneapi::tbb::task::suspend([&tg, &suspend, &resume](oneapi::tbb::task::suspend_point sp) { 62 ++suspend; 63 tg.run([sp, &resume] { 64 ++resume; 65 oneapi::tbb::task::resume(sp); 66 }); 67 }); 68 }); 69 } 70 tg.wait(); 71 CHECK(suspend == N); 72 CHECK(resume == N); 73 CHECK_MESSAGE(mLocal, "The same thread is expected"); 74 } 75 #endif 76