2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of once tests
22 #include <dpl/test/test_runner.h>
24 #include <dpl/waitable_event.h>
25 #include <dpl/waitable_handle.h>
26 #include <dpl/thread.h>
27 #include <dpl/atomic.h>
30 RUNNER_TEST_GROUP_INIT(DPL)
32 namespace // anonymous
40 } // namespace anonymous
42 RUNNER_TEST(Once_DoubleCall)
51 RUNNER_ASSERT_MSG(g_counter == 1, "Counter value is: " << g_counter);
58 virtual int ThreadEntry()
60 DPL::WaitForSingleHandle(m_event->GetHandle());
61 m_once->Call(DPL::Once::Delegate(this, &MyThread::Call));
71 MyThread(DPL::WaitableEvent *event, DPL::Once *once, DPL::Atomic *atom) :
72 m_event(event), m_once(once), m_atom(atom)
76 DPL::WaitableEvent *m_event;
82 Name: Once_MultiThreadCall
83 Description: tests once call wrapper for use by multiple threads
84 Expected: function should be called just once from one of running threads
86 RUNNER_TEST(Once_MultiThreadCall)
88 const size_t NUM_THREADS = 20;
89 typedef std::shared_ptr<MyThread> ThreadPtr;
91 ThreadPtr threads[NUM_THREADS];
92 DPL::WaitableEvent event;
96 for (size_t i = 0; i < NUM_THREADS; ++i) {
97 (threads[i] = ThreadPtr(new MyThread(&event, &once, &atom)))->Run();
102 for (size_t i = 0; i < NUM_THREADS; ++i) {
106 RUNNER_ASSERT_MSG(atom == 1, "Atom value is: " << atom);