|
1 // Copyright (c) 1995-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\prime\t_mutex.cpp |
|
15 // Test mutexes |
|
16 // |
|
17 // |
|
18 |
|
19 #include <e32test.h> |
|
20 #include "u32std.h" |
|
21 #include "../misc/prbs.h" |
|
22 |
|
23 RTest test(_L("T_MUTEX")); |
|
24 |
|
25 TInt Count1=0; |
|
26 TInt Count2=0; |
|
27 TInt TId=-1; |
|
28 RMutex Mutex; |
|
29 |
|
30 void BusyWait(TInt aMicroseconds) |
|
31 { |
|
32 TTime begin; |
|
33 begin.HomeTime(); |
|
34 FOREVER |
|
35 { |
|
36 TTime now; |
|
37 now.HomeTime(); |
|
38 TTimeIntervalMicroSeconds iv=now.MicroSecondsFrom(begin); |
|
39 if (iv.Int64()>=TInt64(aMicroseconds)) |
|
40 return; |
|
41 } |
|
42 } |
|
43 |
|
44 TInt ThreadFunction(TAny* aPtr) |
|
45 { |
|
46 TInt id=(TInt)aPtr; |
|
47 FOREVER |
|
48 { |
|
49 Mutex.Wait(); |
|
50 TId=id; |
|
51 if (Count1!=Count2) |
|
52 { |
|
53 RProcess me; |
|
54 me.Panic(_L("FAIL!"),0); |
|
55 } |
|
56 ++Count1; |
|
57 BusyWait(50000); |
|
58 ++Count2; |
|
59 TId=-1; |
|
60 Mutex.Signal(); |
|
61 } |
|
62 } |
|
63 |
|
64 void CreateThread(RThread& t, TInt aId) |
|
65 { |
|
66 TInt r=t.Create(KNullDesC,ThreadFunction,0x2000,NULL,(TAny*)aId); |
|
67 test(r==KErrNone); |
|
68 t.Resume(); |
|
69 } |
|
70 |
|
71 |
|
72 GLDEF_C TInt E32Main() |
|
73 { |
|
74 test.Title(); |
|
75 test.Start(_L("Create mutex")); |
|
76 TInt r=Mutex.CreateLocal(); |
|
77 test(r==KErrNone); |
|
78 |
|
79 test.Next(_L("Create threads")); |
|
80 RThread().SetPriority(EPriorityMuchMore); |
|
81 RThread t[4]; |
|
82 TInt i; |
|
83 for (i=0; i<4; ++i) |
|
84 CreateThread(t[i],i); |
|
85 |
|
86 TUint seed[2]; |
|
87 seed[0]=0xb17217f8; |
|
88 seed[1]=0; |
|
89 |
|
90 FOREVER |
|
91 { |
|
92 TUint ms=Random(seed); |
|
93 TUint action=(ms>>30); |
|
94 i=(ms>>28)&3; |
|
95 ms&=63; |
|
96 ms+=15; |
|
97 User::AfterHighRes(ms*1000); |
|
98 switch(action) |
|
99 { |
|
100 case 0: |
|
101 { |
|
102 t[i].Suspend(); |
|
103 break; |
|
104 } |
|
105 case 1: |
|
106 { |
|
107 t[i].Resume(); |
|
108 break; |
|
109 } |
|
110 case 2: |
|
111 { |
|
112 TRequestStatus s; |
|
113 t[i].Logon(s); |
|
114 t[i].Kill(0); |
|
115 if (Count1!=Count2) |
|
116 { |
|
117 if (Count1-Count2!=1) |
|
118 { |
|
119 RProcess me; |
|
120 me.Panic(_L("FAIL!"),1); |
|
121 } |
|
122 else if (TId==i) |
|
123 ++Count2; |
|
124 } |
|
125 User::AfterHighRes(50000); |
|
126 User::WaitForRequest(s); |
|
127 t[i].Close(); |
|
128 CreateThread(t[i],i); |
|
129 break; |
|
130 } |
|
131 case 3: |
|
132 { |
|
133 break; |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 // test.End(); |
|
139 } |