2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
26 int UtcDaliMutexSingleThread(void)
28 tet_infoline("Testing Dali::Mutex in a single thread");
32 DALI_TEST_EQUALS( false, mutex1.IsLocked(), TEST_LOCATION );
37 Mutex::ScopedLock lock( mutex2 );
38 DALI_TEST_EQUALS( true, mutex2.IsLocked(), TEST_LOCATION );
43 Mutex::ScopedLock lock( mutex3 );
45 DALI_TEST_EQUALS( false, mutex3.IsLocked(), TEST_LOCATION );
50 namespace // for local variables to avoid name clashes
53 Mutex* gGlobalValueMutex;
54 bool gWorkerThreadWait = true;
55 enum ThreadState { INIT, RUN, LOCKING, TERMINATE } gWorkerThreadState = INIT;
57 void* WorkerThread1( void* ptr )
59 gWorkerThreadState = RUN;
61 Mutex::ScopedLock lock( *gGlobalValueMutex );
62 gWorkerThreadState = LOCKING;
64 while( gWorkerThreadWait ) // wait till we can exit
66 usleep( 1 ); // 1 microsecond
69 gWorkerThreadState = TERMINATE;
73 int UtcDaliMutexMultiThread(void)
75 tet_infoline("Testing Dali::Mutex multithreaded");
77 gGlobalValueMutex = new Dali::Mutex;
82 gWorkerThreadWait = true;
83 DALI_TEST_EQUALS( INIT, gWorkerThreadState, TEST_LOCATION );
84 DALI_TEST_EQUALS( 0, gGlobalValue, TEST_LOCATION );
85 DALI_TEST_EQUALS( false, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
89 Mutex::ScopedLock lock( *gGlobalValueMutex );
90 DALI_TEST_EQUALS( true, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
91 pthread_create(&thread1, NULL, &WorkerThread1, NULL );
92 // wait till the thread is in run state
93 while( RUN != gWorkerThreadState )
95 usleep( 1 ); // 1 microsecond
97 // now the thread is running and mutex is still locked by this thread so value is not changed
98 DALI_TEST_EQUALS( true, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
99 DALI_TEST_EQUALS( 0, gGlobalValue, TEST_LOCATION );
100 // drop out of scope, releases our lock
102 // now child thread is allowed to change the value
103 // wait till the thread is in locking state
104 while( LOCKING != gWorkerThreadState )
106 usleep( 1 ); // 1 microsecond
108 // mutex is locked, but not by us, by the child thread
109 DALI_TEST_EQUALS( true, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
111 DALI_TEST_EQUALS( -1, gGlobalValue, TEST_LOCATION );
113 gWorkerThreadWait = false;
114 // wait till the thread is terminated state
115 while( TERMINATE != gWorkerThreadState )
117 usleep( 1 ); // 1 microsecond
119 DALI_TEST_EQUALS( false, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
124 int UtcDaliMutexNonCopyable(void)
126 // we want to make sure that mutex is not copyable (its copy constructor is not defined)
127 // this test will stop compiling if Mutex has compiler generated copy constructor
128 DALI_COMPILE_TIME_ASSERT( !__has_trivial_copy( Mutex ) );
130 DALI_TEST_CHECK( true );