231
|
1 |
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32test\system\t_atomicu.cpp
|
|
15 |
// Overview:
|
|
16 |
// Simple test for class User atomic operations
|
|
17 |
// API Information:
|
|
18 |
// User::SafeInc(), User::SafeDec(), User::LockedInc(),
|
|
19 |
// User::LockedDec()
|
|
20 |
// Details:
|
|
21 |
// - Tests SafeInc, SafeDec, LockedInc and LockedDec
|
|
22 |
// functions in single thread and determines that counts
|
|
23 |
// match after finished
|
|
24 |
// - Tests SafeInc, SafeDec, LockedInc and LockedDec
|
|
25 |
// functions in multithreaded configuration and determines
|
|
26 |
// that counts match after finished
|
|
27 |
// Platforms/Drives/Compatibility:
|
|
28 |
// All.
|
|
29 |
// Assumptions/Requirement/Pre-requisites:
|
|
30 |
// Failures and causes:
|
|
31 |
// Base Port information:
|
|
32 |
//
|
|
33 |
//
|
|
34 |
|
|
35 |
#define __E32TEST_EXTENSION__
|
|
36 |
#include <e32test.h>
|
|
37 |
#include <e32svr.h>
|
|
38 |
#include <e32def.h>
|
|
39 |
|
|
40 |
LOCAL_D RTest test(_L("T_ATOMICU"));
|
|
41 |
|
|
42 |
const TInt KMaxOps=20000;
|
|
43 |
#define KNumThreads 20
|
|
44 |
TInt gValue=0;
|
|
45 |
|
|
46 |
void TestSafeIncAndSafeDec()
|
|
47 |
{
|
|
48 |
gValue=0;
|
|
49 |
// increasing when 0, should return 0
|
|
50 |
test(User::SafeInc(gValue)==0);
|
|
51 |
// value also should be 0
|
|
52 |
test(gValue==0);
|
|
53 |
|
|
54 |
gValue=1;
|
|
55 |
TInt expected=0;
|
|
56 |
// gValue should vary only between 1 and 2
|
|
57 |
for (TInt i=0; i<KMaxOps; i++)
|
|
58 |
{
|
|
59 |
expected=User::SafeInc(gValue)+1;
|
|
60 |
test(expected==User::SafeDec(gValue));
|
|
61 |
}
|
|
62 |
|
|
63 |
// after running these, it should be 1
|
|
64 |
test(gValue==1);
|
|
65 |
|
|
66 |
// should stay zero after decreasing from 1 multiple times
|
|
67 |
test(User::SafeDec(gValue)==1);
|
|
68 |
test(User::SafeDec(gValue)==0);
|
|
69 |
test(User::SafeDec(gValue)==0);
|
|
70 |
test(gValue==0);
|
|
71 |
}
|
|
72 |
|
|
73 |
void TestLockedIncAndLockedDec()
|
|
74 |
{
|
|
75 |
gValue=0;
|
|
76 |
// increasing when 0, should return 0 as old value
|
|
77 |
test(User::LockedInc(gValue)==0);
|
|
78 |
// new value should be 1
|
|
79 |
test(gValue==1);
|
|
80 |
|
|
81 |
gValue=-1;
|
|
82 |
|
|
83 |
// gValue should vary only between 1 and 2
|
|
84 |
for (TInt i=0; i<KMaxOps; i++)
|
|
85 |
{
|
|
86 |
test((User::LockedInc(gValue)+1)==User::LockedDec(gValue));
|
|
87 |
}
|
|
88 |
|
|
89 |
// after running these, it should be back in -1
|
|
90 |
test(gValue==-1);
|
|
91 |
}
|
|
92 |
|
|
93 |
TInt MultiThreadSafeIncAndSafeDec_FUNC(TAny*)
|
|
94 |
{
|
|
95 |
for (TInt i=0; i<KMaxOps; i++)
|
|
96 |
{
|
|
97 |
User::SafeInc(gValue);
|
|
98 |
User::SafeDec(gValue);
|
|
99 |
}
|
|
100 |
return KErrNone;
|
|
101 |
}
|
|
102 |
|
|
103 |
TInt MultiThreadLockedIncAndLockedDec_FUNC(TAny*)
|
|
104 |
{
|
|
105 |
for (TInt i=0; i<KMaxOps; i++)
|
|
106 |
{
|
|
107 |
User::LockedInc(gValue);
|
|
108 |
User::LockedDec(gValue);
|
|
109 |
}
|
|
110 |
return KErrNone;
|
|
111 |
}
|
|
112 |
|
|
113 |
void MultiThreadSafeIncAndSafeDec()
|
|
114 |
{
|
|
115 |
gValue=1; // start value 1
|
|
116 |
RThread threads[KNumThreads];
|
|
117 |
TRequestStatus stats[KNumThreads];
|
|
118 |
TInt i;
|
|
119 |
|
|
120 |
for (i=0;i<KNumThreads;i++)
|
|
121 |
{
|
|
122 |
test_KErrNone(threads[i].Create(KNullDesC,MultiThreadSafeIncAndSafeDec_FUNC,KDefaultStackSize,NULL,NULL));
|
|
123 |
threads[i].Logon(stats[i]);
|
|
124 |
}
|
|
125 |
|
|
126 |
//lets increase our priority first, so that all the threads start
|
|
127 |
RThread().SetPriority(EPriorityMore);
|
|
128 |
|
|
129 |
test.Printf(_L("Resuming threads...\n"));
|
|
130 |
for(i=0; i<KNumThreads; i++)
|
|
131 |
threads[i].Resume();
|
|
132 |
|
|
133 |
for(i=0; i<KNumThreads; i++)
|
|
134 |
{
|
|
135 |
User::WaitForRequest(stats[i]);
|
|
136 |
test_KErrNone(stats[i].Int());
|
|
137 |
CLOSE_AND_WAIT(threads[i]);
|
|
138 |
}
|
|
139 |
|
|
140 |
test.Printf(_L("...Threads finished\n"));
|
|
141 |
|
|
142 |
// back to normal
|
|
143 |
RThread().SetPriority(EPriorityNormal);
|
|
144 |
|
|
145 |
// test that we returned to the startvalue
|
|
146 |
test(gValue==1);
|
|
147 |
}
|
|
148 |
|
|
149 |
void MultiThreadLockedIncAndLockedDec()
|
|
150 |
{
|
|
151 |
gValue=-1; // set start value to -1
|
|
152 |
RThread threads[KNumThreads];
|
|
153 |
TRequestStatus stats[KNumThreads];
|
|
154 |
TInt i;
|
|
155 |
|
|
156 |
for (i=0;i<KNumThreads;i++)
|
|
157 |
{
|
|
158 |
test_KErrNone(threads[i].Create(KNullDesC,MultiThreadLockedIncAndLockedDec_FUNC,KDefaultStackSize,NULL,NULL));
|
|
159 |
threads[i].Logon(stats[i]);
|
|
160 |
}
|
|
161 |
|
|
162 |
//lets increase our priority first, so that all the threads start
|
|
163 |
RThread().SetPriority(EPriorityMore);
|
|
164 |
|
|
165 |
test.Printf(_L("Resuming threads...\n"));
|
|
166 |
for(i=0; i<KNumThreads; i++)
|
|
167 |
threads[i].Resume();
|
|
168 |
|
|
169 |
for(i=0; i<KNumThreads; i++)
|
|
170 |
{
|
|
171 |
User::WaitForRequest(stats[i]);
|
|
172 |
test_KErrNone(stats[i].Int());
|
|
173 |
CLOSE_AND_WAIT(threads[i]);
|
|
174 |
}
|
|
175 |
|
|
176 |
test.Printf(_L("...Threads finished\n"));
|
|
177 |
|
|
178 |
// back to normal
|
|
179 |
RThread().SetPriority(EPriorityNormal);
|
|
180 |
|
|
181 |
// test that we returned to the startvalue
|
|
182 |
test(gValue==-1);
|
|
183 |
}
|
|
184 |
|
|
185 |
|
|
186 |
TInt E32Main()
|
|
187 |
{
|
|
188 |
test.Title();
|
|
189 |
|
|
190 |
test.Start(_L("Test single thread User::SafeInc and User::SafeDec"));
|
|
191 |
TestSafeIncAndSafeDec();
|
|
192 |
|
|
193 |
test.Next(_L("Test single thread User::LockedInc and User::LockedDec"));
|
|
194 |
TestLockedIncAndLockedDec();
|
|
195 |
|
|
196 |
test.Next(_L("Test multiple thread User::SafeInc and User::SafeDec"));
|
|
197 |
MultiThreadSafeIncAndSafeDec();
|
|
198 |
|
|
199 |
test.Next(_L("Test multiple thread User::LockedInc and User::LockedDec"));
|
|
200 |
MultiThreadLockedIncAndLockedDec();
|
|
201 |
|
|
202 |
test.End();
|
|
203 |
|
|
204 |
return KErrNone;
|
|
205 |
}
|