1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "multithread_harness.h"
#include <thread>
namespace mt = mir::testing;
namespace
{
void test_func(mt::SynchronizerSpawned* synchronizer, int* data)
{
*data = 1;
synchronizer->child_enter_wait();
*data = 2;
synchronizer->child_enter_wait();
}
}
TEST(Synchronizer, thread_stop_start)
{
int data = 0;
mt::Synchronizer synchronizer;
std::thread t1(test_func, &synchronizer, &data);
synchronizer.ensure_child_is_waiting();
EXPECT_EQ(data, 1);
synchronizer.activate_waiting_child();
synchronizer.ensure_child_is_waiting();
EXPECT_EQ(data, 2);
synchronizer.activate_waiting_child();
t1.join();
}
namespace
{
void test_func_pause (mt::SynchronizerSpawned* synchronizer, int* data) {
bool wait_request;
for(;;)
{
wait_request = synchronizer->child_check_wait_request();
*data = *data+1;
if (wait_request)
{
if (synchronizer->child_enter_wait()) break;
}
std::this_thread::yield();
}
}
}
TEST(Synchronizer, thread_pause_req)
{
int data = 0, old_data = 0;
mt::Synchronizer synchronizer;
std::thread t1(test_func_pause, &synchronizer, &data);
synchronizer.ensure_child_is_waiting();
EXPECT_GT(data, old_data);
old_data = data;
synchronizer.activate_waiting_child();
synchronizer.ensure_child_is_waiting();
EXPECT_GT(data, old_data);
synchronizer.activate_waiting_child();
synchronizer.ensure_child_is_waiting();
synchronizer.kill_thread();
synchronizer.activate_waiting_child();
t1.join();
}
|