Merge changes I2e0d0daa,I0b66fbb8 into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Mutex.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
18 #include <iostream>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using Dali::Mutex;
25
26 int UtcDaliMutexSingleThread(void)
27 {
28   tet_infoline("Testing Dali::Mutex in a single thread");
29
30   {
31     Mutex mutex1;
32     DALI_TEST_EQUALS( false, mutex1.IsLocked(), TEST_LOCATION );
33   }
34
35   {
36     Mutex mutex2;
37     Mutex::ScopedLock lock( mutex2 );
38     DALI_TEST_EQUALS( true, mutex2.IsLocked(), TEST_LOCATION );
39   }
40
41   Mutex mutex3;
42   {
43     Mutex::ScopedLock lock( mutex3 );
44   }
45   DALI_TEST_EQUALS( false, mutex3.IsLocked(), TEST_LOCATION );
46
47   END_TEST;
48 }
49
50 namespace // for local variables to avoid name clashes
51 {
52 int gGlobalValue = 0;
53 Mutex* gGlobalValueMutex;
54 bool gWorkerThreadWait = true;
55 enum ThreadState { INIT, RUN, LOCKING, TERMINATE } gWorkerThreadState = INIT;
56 }
57 void* WorkerThread1( void* ptr )
58 {
59   gWorkerThreadState = RUN;
60   {
61     Mutex::ScopedLock lock( *gGlobalValueMutex );
62     gWorkerThreadState = LOCKING;
63     gGlobalValue = -1;
64     while( gWorkerThreadWait ) // wait till we can exit
65     {
66       usleep( 1 ); // 1 microsecond
67     }
68   }
69   gWorkerThreadState = TERMINATE;
70   return NULL;
71 }
72
73 int UtcDaliMutexMultiThread(void)
74 {
75   tet_infoline("Testing Dali::Mutex multithreaded");
76
77   gGlobalValueMutex = new Dali::Mutex;
78
79   pthread_t thread1;
80   // initialize values
81   gGlobalValue = 0;
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 );
86
87   // lock the mutex
88   {
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 )
94     {
95       usleep( 1 ); // 1 microsecond
96     }
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
101   }
102   // now child thread is allowed to change the value
103   // wait till the thread is in locking state
104   while( LOCKING != gWorkerThreadState )
105   {
106     usleep( 1 ); // 1 microsecond
107   }
108   // mutex is locked, but not by us, by the child thread
109   DALI_TEST_EQUALS( true, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
110   // value is changed
111   DALI_TEST_EQUALS( -1, gGlobalValue, TEST_LOCATION );
112   // let worker finish
113   gWorkerThreadWait = false;
114   // wait till the thread is terminated state
115   while( TERMINATE != gWorkerThreadState )
116   {
117     usleep( 1 ); // 1 microsecond
118   }
119   DALI_TEST_EQUALS( false, gGlobalValueMutex->IsLocked(), TEST_LOCATION );
120   void* exitValue;
121   pthread_join( thread1, &exitValue );
122
123   END_TEST;
124 }
125
126 int UtcDaliMutexNonCopyable(void)
127 {
128   // we want to make sure that mutex is not copyable (its copy constructor is not defined)
129   // this test will stop compiling if Mutex has compiler generated copy constructor
130   DALI_COMPILE_TIME_ASSERT( !__has_trivial_copy( Mutex ) );
131
132   DALI_TEST_CHECK( true );
133   END_TEST;
134 }
135
136