|
1 // spinlock.cpp |
|
2 // |
|
3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "Eclipse Public License v1.0" |
|
6 // which accompanies this distribution, and is available |
|
7 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 // |
|
9 // Initial Contributors: |
|
10 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include <fshell/memoryaccesscmd.h> |
|
14 #include <fshell/ioutils.h> |
|
15 #include <HAL.H> |
|
16 |
|
17 using namespace IoUtils; |
|
18 |
|
19 class CCmdSpinlock : public CMemoryAccessCommandBase |
|
20 { |
|
21 public: |
|
22 static CCommandBase* NewLC(); |
|
23 ~CCmdSpinlock(); |
|
24 private: |
|
25 CCmdSpinlock(); |
|
26 private: // From CCommandBase. |
|
27 virtual const TDesC& Name() const; |
|
28 virtual void DoRunL(); |
|
29 virtual void ArgumentsL(RCommandArgumentList& aArguments); |
|
30 virtual void OptionsL(RCommandOptionList& aOptions); |
|
31 private: |
|
32 TInt iStartIn; |
|
33 TInt iDuration; |
|
34 TInt iLoad; |
|
35 TInt iPriority; |
|
36 volatile TInt iCounter; |
|
37 }; |
|
38 |
|
39 |
|
40 CCommandBase* CCmdSpinlock::NewLC() |
|
41 { |
|
42 CCmdSpinlock* self = new(ELeave) CCmdSpinlock(); |
|
43 CleanupStack::PushL(self); |
|
44 self->BaseConstructL(); |
|
45 return self; |
|
46 } |
|
47 |
|
48 CCmdSpinlock::~CCmdSpinlock() |
|
49 { |
|
50 } |
|
51 |
|
52 CCmdSpinlock::CCmdSpinlock() |
|
53 { |
|
54 } |
|
55 |
|
56 const TDesC& CCmdSpinlock::Name() const |
|
57 { |
|
58 _LIT(KName, "spinlock"); |
|
59 return KName; |
|
60 } |
|
61 |
|
62 void CCmdSpinlock::ArgumentsL(RCommandArgumentList& aArguments) |
|
63 { |
|
64 aArguments.AppendIntL(iLoad, _L("load")); |
|
65 } |
|
66 |
|
67 void CCmdSpinlock::OptionsL(RCommandOptionList& aOptions) |
|
68 { |
|
69 aOptions.AppendIntL(iStartIn, _L("start-after")); |
|
70 aOptions.AppendIntL(iDuration, _L("duration")); |
|
71 aOptions.AppendIntL(iPriority, _L("priority")); |
|
72 } |
|
73 |
|
74 EXE_BOILER_PLATE(CCmdSpinlock) |
|
75 |
|
76 void CCmdSpinlock::DoRunL() |
|
77 { |
|
78 if (iPriority) |
|
79 { |
|
80 #ifdef FSHELL_MEMORY_ACCESS_SUPPORT |
|
81 LoadMemoryAccessL(); |
|
82 |
|
83 RThread me; |
|
84 CleanupClosePushL(me); |
|
85 LeaveIfErr(me.Open(me.Id()), _L("Couldn't open myself (!?!)")); |
|
86 LeaveIfErr(iMemAccess.SetThreadPriority(me, iPriority), _L("Couldn't set priority")); |
|
87 CleanupStack::PopAndDestroy(&me); |
|
88 #else |
|
89 PrintWarning(_L("--priority is not supported if memoryaccess is not available.")); |
|
90 #endif |
|
91 } |
|
92 |
|
93 if (iStartIn) |
|
94 { |
|
95 Printf(_L("Starting in %d seconds...\r\n"), iStartIn); |
|
96 User::After(iStartIn * 1000000); |
|
97 } |
|
98 |
|
99 if (iDuration) |
|
100 { |
|
101 Printf(_L("Starting to spin for %d seconds... (Press CTRL-C to exit earlier)\r\n"), iDuration); |
|
102 } |
|
103 else |
|
104 { |
|
105 Printf(_L("Starting to spin... (Press CTRL-C to exit)\r\n")); |
|
106 } |
|
107 |
|
108 // We have ~300,000 cycles per millisecond to play with. We will do some amount of work then wait a millisecond. |
|
109 // This is pretty arbitrary. |
|
110 |
|
111 TInt tickPeriod; |
|
112 HAL::Get(HAL::ENanoTickPeriod, tickPeriod); |
|
113 |
|
114 TInt endTime; // in nkern ticks |
|
115 if (iDuration) |
|
116 { |
|
117 endTime = User::NTickCount() + (iDuration * 1000000)/tickPeriod; |
|
118 } |
|
119 else |
|
120 { |
|
121 endTime = KMaxTInt; |
|
122 } |
|
123 |
|
124 if (iLoad) |
|
125 { |
|
126 //TODO this loop needs sorting out. The User::After is a bit broken. |
|
127 TInt n = iLoad * 1000; |
|
128 while (User::NTickCount() < (TUint)endTime) |
|
129 { |
|
130 for (iCounter = 0; iCounter < n; iCounter++) |
|
131 { |
|
132 // Let's do an exec call just for fun |
|
133 UserSvr::DebugMask(0); |
|
134 } |
|
135 |
|
136 User::After(1000); // Wait 1 ms |
|
137 } |
|
138 } |
|
139 else |
|
140 { |
|
141 // Just spin |
|
142 while (User::NTickCount() < (TUint)endTime) |
|
143 { |
|
144 } |
|
145 } |
|
146 } |