Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/_i_p_c_2condvar_2condvarglobal_2src_2adder_8cpp_source.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/Base/IPC/condvar/condvarglobal/src/adder.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/Base/IPC/condvar/condvarglobal/src/adder.cpp</h1><a href="_i_p_c_2condvar_2condvarglobal_2src_2adder_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00004"></a>00004 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span>
<a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00006"></a>00006 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
<a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// Description:</span>
<a name="l00014"></a>00014 <span class="comment">// Implements the write operation on a shared memory block.</span>
<a name="l00015"></a>00015 <span class="comment">//</span>
<a name="l00016"></a>00016
<a name="l00017"></a>00017
<a name="l00018"></a>00018
<a name="l00022"></a>00022 <span class="preprocessor">#include "<a class="code" href="sharedmem_8h.html">sharedmem.h</a>"</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="_i_p_c_2condvar_2condvarglobal_2inc_2adder_8h.html">adder.h</a>"</span>
<a name="l00024"></a>00024
<a name="l00030"></a><a class="code" href="class_c_adder.html#ac207184f306dec25c5430f2c510f5c3c">00030</a> <a class="code" href="class_c_adder.html">CAdder</a>* <a class="code" href="class_c_adder.html#aa703f5386236c2fa9ad3b750f521200a">CAdder::NewL</a>(CConsoleBase* aConsole)
<a name="l00031"></a>00031 {
<a name="l00032"></a>00032 <a class="code" href="class_c_adder.html">CAdder</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave) <a class="code" href="class_c_adder.html">CAdder</a>;
<a name="l00033"></a>00033 CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00034"></a>00034 <span class="keyword">self</span>->ConstructL(aConsole);
<a name="l00035"></a>00035 CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00036"></a>00036 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00037"></a>00037 }
<a name="l00038"></a>00038
<a name="l00043"></a><a class="code" href="class_c_adder.html#a258412cf6a78091b2b9f641dadb8e41b">00043</a> <span class="keywordtype">void</span> <a class="code" href="class_c_adder.html#a258412cf6a78091b2b9f641dadb8e41b">CAdder::RunL</a>()
<a name="l00044"></a>00044 {
<a name="l00045"></a>00045 <span class="comment">// Get the key code.</span>
<a name="l00046"></a>00046 TUint8 option = iConsole->KeyCode();
<a name="l00047"></a>00047 <span class="comment">// Print the selected option.</span>
<a name="l00048"></a>00048 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextFormat,<span class="stringliteral">"%c\n"</span>);
<a name="l00049"></a>00049 iConsole->Printf(KTextFormat,option);
<a name="l00050"></a>00050 <span class="comment">// Stop the timer and the active scheduler.</span>
<a name="l00051"></a>00051 <a class="code" href="class_c_adder.html#a5d5d56b9ce85c971c2e48b575f792d9d">StopTimer</a>();
<a name="l00052"></a>00052 CActiveScheduler::Stop();
<a name="l00053"></a>00053 }
<a name="l00054"></a>00054
<a name="l00058"></a><a class="code" href="class_c_adder.html#a73d41c81991e4cf7b65e3644aba926cc">00058</a> <span class="keywordtype">void</span> <a class="code" href="class_c_adder.html#a73d41c81991e4cf7b65e3644aba926cc">CAdder::DoCancel</a>()
<a name="l00059"></a>00059 {
<a name="l00060"></a>00060 <span class="keywordflow">if</span>(IsActive())
<a name="l00061"></a>00061 {
<a name="l00062"></a>00062 <span class="comment">// Cancel any outstanding read requests.</span>
<a name="l00063"></a>00063 iConsole->ReadCancel();
<a name="l00064"></a>00064 }
<a name="l00065"></a>00065 }
<a name="l00066"></a>00066
<a name="l00070"></a>00070 <a class="code" href="class_c_adder.html#a76003b0896145aa8d396f596bb8fbe31">CAdder::~CAdder</a>()
<a name="l00071"></a>00071 {
<a name="l00072"></a>00072 <span class="comment">// Cancel all outstanding requests.</span>
<a name="l00073"></a>00073 <a class="code" href="class_c_adder.html#a73d41c81991e4cf7b65e3644aba926cc">DoCancel</a>();
<a name="l00074"></a>00074
<a name="l00075"></a>00075 <span class="comment">// Delete the timer object.</span>
<a name="l00076"></a>00076 iPeriodic->Cancel();
<a name="l00077"></a>00077 <span class="keyword">delete</span> iPeriodic;
<a name="l00078"></a>00078
<a name="l00079"></a>00079 <span class="comment">// Close all resource handles.</span>
<a name="l00080"></a>00080 iChunk.Close();
<a name="l00081"></a>00081 iMutex.Close();
<a name="l00082"></a>00082 iCondVar.Close();
<a name="l00083"></a>00083 }
<a name="l00084"></a>00084
<a name="l00088"></a>00088 CAdder::CAdder():CActive(EPriorityUserInput)
<a name="l00089"></a>00089 {
<a name="l00090"></a>00090 }
<a name="l00091"></a>00091
<a name="l00101"></a>00101 <span class="keywordtype">void</span> CAdder::ConstructL(CConsoleBase* aConsole)
<a name="l00102"></a>00102 {
<a name="l00103"></a>00103 <span class="comment">// Create the global condition variable.</span>
<a name="l00104"></a>00104 User::LeaveIfError(iCondVar.CreateGlobal(KCondVarName));
<a name="l00105"></a>00105 <span class="comment">// Create the global chunk.</span>
<a name="l00106"></a>00106 User::LeaveIfError(iChunk.CreateGlobal(KChunkName,<a class="code" href="sharedmem_8h.html#a1006d8c149c84d799a92f7de74ebcf4c">KChunkSize</a>,<a class="code" href="sharedmem_8h.html#a1006d8c149c84d799a92f7de74ebcf4c">KChunkSize</a>));
<a name="l00107"></a>00107 <span class="comment">// Create the global mutex variable.</span>
<a name="l00108"></a>00108 User::LeaveIfError(iMutex.CreateGlobal(KMutexName));
<a name="l00109"></a>00109
<a name="l00110"></a>00110 <span class="comment">// Create the CPeriodic object.</span>
<a name="l00111"></a>00111 iPeriodic = <a class="code" href="class_c_adder.html#aa703f5386236c2fa9ad3b750f521200a">CPeriodic::NewL</a>(CActive::EPriorityUserInput);
<a name="l00112"></a>00112 iConsole = aConsole;
<a name="l00113"></a>00113
<a name="l00114"></a>00114 <span class="comment">// Get the address of the chunk.</span>
<a name="l00115"></a>00115 TUint8 *ptr = iChunk.Base();
<a name="l00116"></a>00116 <span class="comment">// Initialise the value of the chunk to 0.</span>
<a name="l00117"></a>00117 *ptr = 0;
<a name="l00118"></a>00118
<a name="l00119"></a>00119 <span class="comment">// Add the object to the active scheduler.</span>
<a name="l00120"></a>00120 <a class="code" href="class_c_adder.html#a1a7d0002967f1aa090a6beb84a433d4c">CActiveScheduler::Add</a>(<span class="keyword">this</span>);
<a name="l00121"></a>00121 }
<a name="l00122"></a>00122
<a name="l00126"></a><a class="code" href="class_c_adder.html#a800f42785c37c746dadd65c90b7e0dfa">00126</a> <span class="keywordtype">void</span> <a class="code" href="class_c_adder.html#a800f42785c37c746dadd65c90b7e0dfa">CAdder::ReadFunction</a>()
<a name="l00127"></a>00127 {
<a name="l00128"></a>00128 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextMessage,<span class="stringliteral">"Press a key to exit...\n"</span>);
<a name="l00129"></a>00129 iConsole->Printf(KTextMessage);
<a name="l00130"></a>00130 <span class="comment">// Wait for a key press event.</span>
<a name="l00131"></a>00131 iConsole->Read(iStatus);
<a name="l00132"></a>00132 SetActive();
<a name="l00133"></a>00133 }
<a name="l00134"></a>00134
<a name="l00139"></a><a class="code" href="class_c_adder.html#a004d5ec0fb8626310fdf85843c87ec67">00139</a> <span class="keywordtype">void</span> <a class="code" href="class_c_adder.html#a004d5ec0fb8626310fdf85843c87ec67">CAdder::StartTimer</a>()
<a name="l00140"></a>00140 {
<a name="l00141"></a>00141 <span class="comment">// Start generating periodic events.</span>
<a name="l00142"></a>00142 iPeriodic->Start(0,3000000,TCallBack(<a class="code" href="class_c_adder.html#a6e524c7466f89ca4ab9896f5bae13f3e">AddFunction</a>,<span class="keyword">this</span>));
<a name="l00143"></a>00143 }
<a name="l00144"></a>00144
<a name="l00148"></a><a class="code" href="class_c_adder.html#a5d5d56b9ce85c971c2e48b575f792d9d">00148</a> <span class="keywordtype">void</span> <a class="code" href="class_c_adder.html#a5d5d56b9ce85c971c2e48b575f792d9d">CAdder::StopTimer</a>()
<a name="l00149"></a>00149 {
<a name="l00150"></a>00150 <span class="comment">// Cancel the outstanding request.</span>
<a name="l00151"></a>00151 iPeriodic->Cancel();
<a name="l00152"></a>00152 }
<a name="l00153"></a>00153
<a name="l00159"></a><a class="code" href="class_c_adder.html#a6e524c7466f89ca4ab9896f5bae13f3e">00159</a> TInt <a class="code" href="class_c_adder.html#a6e524c7466f89ca4ab9896f5bae13f3e">CAdder::AddFunction</a>(TAny* aPtr)
<a name="l00160"></a>00160 {
<a name="l00161"></a>00161 <a class="code" href="class_c_adder.html">CAdder</a>* ptr = <span class="keyword">static_cast<</span><a class="code" href="class_c_adder.html">CAdder</a>*<span class="keyword">></span> (aPtr);
<a name="l00162"></a>00162 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTxtPanic,<span class="stringliteral">"Pointer is NULL"</span>);
<a name="l00163"></a>00163 __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
<a name="l00164"></a>00164 <span class="comment">// Invoke the Add() function.</span>
<a name="l00165"></a>00165 ptr-><a class="code" href="class_c_adder.html#a1a7d0002967f1aa090a6beb84a433d4c">Add</a>();
<a name="l00166"></a>00166 <span class="keywordflow">return</span> KErrNone;
<a name="l00167"></a>00167 }
<a name="l00168"></a>00168
<a name="l00172"></a>00172 <span class="keywordtype">void</span> <a class="code" href="class_c_adder.html#a1a7d0002967f1aa090a6beb84a433d4c">CAdder::Add</a>()
<a name="l00173"></a>00173 {
<a name="l00174"></a>00174 <span class="comment">// Acquire the mutex.</span>
<a name="l00175"></a>00175 iMutex.Wait();
<a name="l00176"></a>00176
<a name="l00177"></a>00177 <span class="comment">// Get a random number.</span>
<a name="l00178"></a>00178 TInt randVal = Math::Random() % 10;
<a name="l00179"></a>00179
<a name="l00180"></a>00180 <span class="comment">// Get the address of the chunk.</span>
<a name="l00181"></a>00181 TUint8 *ptr = iChunk.Base();
<a name="l00182"></a>00182
<a name="l00183"></a>00183 <span class="comment">// Print the value of the chunk before addition.</span>
<a name="l00184"></a>00184 iConsole->Printf(_L(<span class="stringliteral">"Value read from the shared memory: %d\n"</span>),*ptr);
<a name="l00185"></a>00185
<a name="l00186"></a>00186 <span class="comment">// Add the random value to the shared memory variable.</span>
<a name="l00187"></a>00187 *ptr += randVal;
<a name="l00188"></a>00188
<a name="l00189"></a>00189 <span class="keywordflow">while</span>(*ptr > <a class="code" href="sharedmem_8h.html#a0f2ae9d486d80c446844be5480b7bbc2">KUpperThreshold</a>)
<a name="l00190"></a>00190 {
<a name="l00191"></a>00191 <span class="comment">// Wait on the condition variable if the result is greater than 100.</span>
<a name="l00192"></a>00192 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KBlockMessage,<span class="stringliteral">"Adder blocked by condVar until Subtractor signals that value has been decreased.\nIntermediate value of the chunk = %d\n"</span>);
<a name="l00193"></a>00193 iConsole->Printf(KBlockMessage,*ptr);
<a name="l00194"></a>00194 iCondVar.Wait(iMutex);
<a name="l00195"></a>00195 }
<a name="l00196"></a>00196 <span class="comment">// Print the updated value of the chunk.</span>
<a name="l00197"></a>00197 iConsole->Printf(_L(<span class="stringliteral">"Value of the shared memory increased to : %d\n"</span>),*ptr);
<a name="l00198"></a>00198
<a name="l00199"></a>00199 <span class="comment">// Signal the mutex and the condition variable.</span>
<a name="l00200"></a>00200 <span class="keywordflow">if</span>(*ptr > <a class="code" href="sharedmem_8h.html#a817ce7a9d8ca9ead92509953f8dcdcad">KLowerThreshold</a>)
<a name="l00201"></a>00201 {
<a name="l00202"></a>00202 <span class="comment">// Signal that the level is safe for subtraction to (re)start.</span>
<a name="l00203"></a>00203 iCondVar.Signal();
<a name="l00204"></a>00204 }
<a name="l00205"></a>00205 iMutex.Signal();
<a name="l00206"></a>00206 }
<a name="l00207"></a>00207
<a name="l00208"></a>00208 LOCAL_D CConsoleBase* console;
<a name="l00209"></a>00209 LOCAL_C <span class="keywordtype">void</span> DoExampleL();
<a name="l00210"></a>00210 LOCAL_C <span class="keywordtype">void</span> callExampleL();
<a name="l00211"></a>00211
<a name="l00212"></a>00212 LOCAL_C <span class="keywordtype">void</span> DoExampleL()
<a name="l00213"></a>00213 {
<a name="l00214"></a>00214 <span class="comment">// Create and install the active scheduler.</span>
<a name="l00215"></a>00215 CActiveScheduler* scheduler = <span class="keyword">new</span> (ELeave) CActiveScheduler();
<a name="l00216"></a>00216 CleanupStack::PushL(scheduler);
<a name="l00217"></a>00217 CActiveScheduler::Install(scheduler);
<a name="l00218"></a>00218
<a name="l00219"></a>00219 <span class="comment">// Create the CAdder object.</span>
<a name="l00220"></a>00220 <a class="code" href="class_c_adder.html">CAdder</a>* adder = <a class="code" href="class_c_adder.html#aa703f5386236c2fa9ad3b750f521200a">CAdder::NewL</a>(console);
<a name="l00221"></a>00221 CleanupStack::PushL(adder);
<a name="l00222"></a>00222
<a name="l00223"></a>00223 <span class="comment">// Start the timer of the CAdder object.</span>
<a name="l00224"></a>00224 adder-><a class="code" href="class_c_adder.html#a004d5ec0fb8626310fdf85843c87ec67">StartTimer</a>();
<a name="l00225"></a>00225 <span class="comment">// Issue an asynchronous read request.</span>
<a name="l00226"></a>00226 adder-><a class="code" href="class_c_adder.html#a800f42785c37c746dadd65c90b7e0dfa">ReadFunction</a>();
<a name="l00227"></a>00227 <span class="comment">// Start the active scheduler.</span>
<a name="l00228"></a>00228 CActiveScheduler::Start();
<a name="l00229"></a>00229
<a name="l00230"></a>00230 CleanupStack::PopAndDestroy(2,scheduler);
<a name="l00231"></a>00231 }
<a name="l00232"></a>00232
<a name="l00233"></a><a class="code" href="_i_p_c_2condvar_2condvarglobal_2src_2adder_8cpp.html#a0f358e9c4355138f629b8c4f37310295">00233</a> GLDEF_C TInt E32Main() <span class="comment">// main function called by E32</span>
<a name="l00234"></a>00234 {
<a name="l00235"></a>00235 __UHEAP_MARK;
<a name="l00236"></a>00236 CTrapCleanup* cleanup=CTrapCleanup::New(); <span class="comment">// get clean-up stack</span>
<a name="l00237"></a>00237 TRAPD(error,callExampleL()); <span class="comment">// more initializations, then do example</span>
<a name="l00238"></a>00238 <span class="keyword">delete</span> cleanup; <span class="comment">// destroy clean-up stack</span>
<a name="l00239"></a>00239 __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
<a name="l00240"></a>00240 __UHEAP_MARKEND;
<a name="l00241"></a>00241 <span class="keywordflow">return</span> 0; <span class="comment">// and return</span>
<a name="l00242"></a>00242 }
<a name="l00243"></a>00243
<a name="l00244"></a>00244 LOCAL_C <span class="keywordtype">void</span> callExampleL() <span class="comment">// initialise and call example code under cleanup stack</span>
<a name="l00245"></a>00245 {
<a name="l00246"></a>00246 console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
<a name="l00247"></a>00247 CleanupStack::PushL(console);
<a name="l00248"></a>00248 TRAPD(error,DoExampleL()); <span class="comment">// perform example function</span>
<a name="l00249"></a>00249 <span class="keywordflow">if</span> (error)
<a name="l00250"></a>00250 console->Printf(KFormatFailed, error);
<a name="l00251"></a>00251 <span class="keywordflow">else</span>
<a name="l00252"></a>00252 console->Printf(KTxtOK);
<a name="l00253"></a>00253 console->Printf(KTxtPressAnyKey);
<a name="l00254"></a>00254 console->Getch(); <span class="comment">// get and ignore character</span>
<a name="l00255"></a>00255 CleanupStack::PopAndDestroy(); <span class="comment">// close console</span>
<a name="l00256"></a>00256 }
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>