Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/subtractor_8cpp_source.html
changeset 6 43e37759235e
equal deleted inserted replaced
5:f345bda72bc4 6:43e37759235e
       
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     2 <html xmlns="http://www.w3.org/1999/xhtml">
       
     3 <head>
       
     4 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
       
     5 <title>TB9.2 Example Applications: examples/Base/IPC/condvar/condvarglobal/src/subtractor.cpp Source File</title>
       
     6 <link href="tabs.css" rel="stylesheet" type="text/css"/>
       
     7 <link href="doxygen.css" rel="stylesheet" type="text/css"/>
       
     8 </head>
       
     9 <body>
       
    10 <!-- Generated by Doxygen 1.6.2 -->
       
    11 <h1>examples/Base/IPC/condvar/condvarglobal/src/subtractor.cpp</h1><a href="subtractor_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>
       
    12 <a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
       
    13 <a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
       
    14 <a name="l00004"></a>00004 <span class="comment">// under the terms of &quot;Eclipse Public License v1.0&quot;</span>
       
    15 <a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
       
    16 <a name="l00006"></a>00006 <span class="comment">// at the URL &quot;http://www.eclipse.org/legal/epl-v10.html&quot;.</span>
       
    17 <a name="l00007"></a>00007 <span class="comment">//</span>
       
    18 <a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
       
    19 <a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
       
    20 <a name="l00010"></a>00010 <span class="comment">//</span>
       
    21 <a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
       
    22 <a name="l00012"></a>00012 <span class="comment">//</span>
       
    23 <a name="l00013"></a>00013 <span class="comment">// Description:</span>
       
    24 <a name="l00014"></a>00014 <span class="comment">// Implements the write operation on a shared memory block.</span>
       
    25 <a name="l00015"></a>00015 <span class="comment">//</span>
       
    26 <a name="l00016"></a>00016 
       
    27 <a name="l00017"></a>00017 
       
    28 <a name="l00018"></a>00018 
       
    29 <a name="l00022"></a>00022 <span class="preprocessor">#include &quot;<a class="code" href="sharedmem_8h.html">sharedmem.h</a>&quot;</span>
       
    30 <a name="l00023"></a>00023 <span class="preprocessor">#include &quot;<a class="code" href="subtractor_8h.html">subtractor.h</a>&quot;</span>
       
    31 <a name="l00024"></a>00024 
       
    32 <a name="l00030"></a><a class="code" href="class_c_subtractor.html#a7d6002b0b8610a3241853c7fb4ef3995">00030</a> <a class="code" href="class_c_subtractor.html">CSubtractor</a>* <a class="code" href="class_c_subtractor.html#a7d6002b0b8610a3241853c7fb4ef3995">CSubtractor::NewL</a>(CConsoleBase* aConsole)
       
    33 <a name="l00031"></a>00031         {
       
    34 <a name="l00032"></a>00032         <a class="code" href="class_c_subtractor.html">CSubtractor</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave) <a class="code" href="class_c_subtractor.html">CSubtractor</a>;
       
    35 <a name="l00033"></a>00033         CleanupStack::PushL(<span class="keyword">self</span>);
       
    36 <a name="l00034"></a>00034         <span class="keyword">self</span>-&gt;ConstructL(aConsole);
       
    37 <a name="l00035"></a>00035         CleanupStack::Pop(<span class="keyword">self</span>);
       
    38 <a name="l00036"></a>00036         <span class="keywordflow">return</span> <span class="keyword">self</span>;
       
    39 <a name="l00037"></a>00037         }
       
    40 <a name="l00038"></a>00038 
       
    41 <a name="l00043"></a><a class="code" href="class_c_subtractor.html#adafc90c62c12777fc2c1670cfc65ff7e">00043</a> <span class="keywordtype">void</span> <a class="code" href="class_c_subtractor.html#adafc90c62c12777fc2c1670cfc65ff7e">CSubtractor::RunL</a>()
       
    42 <a name="l00044"></a>00044         {
       
    43 <a name="l00045"></a>00045         <span class="comment">// Get the key code.</span>
       
    44 <a name="l00046"></a>00046         TUint8 option = iConsole-&gt;KeyCode();
       
    45 <a name="l00047"></a>00047         <span class="comment">// Print the selected option.</span>
       
    46 <a name="l00048"></a>00048         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextFormat,<span class="stringliteral">&quot;%c\n&quot;</span>);
       
    47 <a name="l00049"></a>00049         iConsole-&gt;Printf(KTextFormat,option);
       
    48 <a name="l00050"></a>00050         <span class="comment">// Stop the timer and the active scheduler.</span>
       
    49 <a name="l00051"></a>00051         <a class="code" href="class_c_subtractor.html#a6fccf6a6e4f1b731f1d2105ccac84484">StopTimer</a>();
       
    50 <a name="l00052"></a>00052         CActiveScheduler::Stop();
       
    51 <a name="l00053"></a>00053         }
       
    52 <a name="l00054"></a>00054 
       
    53 <a name="l00058"></a><a class="code" href="class_c_subtractor.html#a8fe681f7232eb2d9a8e5d6b00d56d461">00058</a> <span class="keywordtype">void</span> <a class="code" href="class_c_subtractor.html#a8fe681f7232eb2d9a8e5d6b00d56d461">CSubtractor::DoCancel</a>()
       
    54 <a name="l00059"></a>00059         {
       
    55 <a name="l00060"></a>00060         <span class="keywordflow">if</span>(IsActive())
       
    56 <a name="l00061"></a>00061                 {
       
    57 <a name="l00062"></a>00062                 <span class="comment">// Cancel any outstanding read requests.</span>
       
    58 <a name="l00063"></a>00063                 iConsole-&gt;ReadCancel();
       
    59 <a name="l00064"></a>00064                 }
       
    60 <a name="l00065"></a>00065         }
       
    61 <a name="l00066"></a>00066 
       
    62 <a name="l00070"></a><a class="code" href="class_c_subtractor.html#a6641d546e8cf49ad7295cf4045d6c901">00070</a> <a class="code" href="class_c_subtractor.html#a6641d546e8cf49ad7295cf4045d6c901">CSubtractor::~CSubtractor</a>()
       
    63 <a name="l00071"></a>00071         {
       
    64 <a name="l00072"></a>00072         <span class="comment">// Cancel all outstanding requests.</span>
       
    65 <a name="l00073"></a>00073         <a class="code" href="class_c_subtractor.html#a8fe681f7232eb2d9a8e5d6b00d56d461">DoCancel</a>();
       
    66 <a name="l00074"></a>00074         <span class="comment">// Delete the timer object.</span>
       
    67 <a name="l00075"></a>00075         iPeriodic-&gt;Cancel();
       
    68 <a name="l00076"></a>00076         <span class="keyword">delete</span> iPeriodic;
       
    69 <a name="l00077"></a>00077         }
       
    70 <a name="l00078"></a>00078 
       
    71 <a name="l00082"></a>00082 CSubtractor::CSubtractor():CActive(EPriorityUserInput)
       
    72 <a name="l00083"></a>00083         {
       
    73 <a name="l00084"></a>00084         }
       
    74 <a name="l00085"></a>00085 
       
    75 <a name="l00091"></a>00091 <span class="keywordtype">void</span> CSubtractor::ConstructL(CConsoleBase* aConsole)
       
    76 <a name="l00092"></a>00092         {
       
    77 <a name="l00093"></a>00093         <span class="comment">// Open the global condition variable.</span>
       
    78 <a name="l00094"></a>00094         User::LeaveIfError(iCondVar.OpenGlobal(KCondVarName));
       
    79 <a name="l00095"></a>00095         <span class="comment">// Open the global chunk variable.</span>
       
    80 <a name="l00096"></a>00096         User::LeaveIfError(iChunk.OpenGlobal(KChunkName,EFalse));
       
    81 <a name="l00097"></a>00097         <span class="comment">// Open the global mutex variable.</span>
       
    82 <a name="l00098"></a>00098         User::LeaveIfError(iMutex.OpenGlobal(KMutexName));
       
    83 <a name="l00099"></a>00099 
       
    84 <a name="l00100"></a>00100         <span class="comment">// Create the CPeriodic object.</span>
       
    85 <a name="l00101"></a>00101         iPeriodic = <a class="code" href="class_c_subtractor.html#a7d6002b0b8610a3241853c7fb4ef3995">CPeriodic::NewL</a>(CActive::EPriorityUserInput);
       
    86 <a name="l00102"></a>00102         iConsole = aConsole;
       
    87 <a name="l00103"></a>00103 
       
    88 <a name="l00104"></a>00104         <span class="comment">// Add the object to the active scheduler.</span>
       
    89 <a name="l00105"></a>00105         CActiveScheduler::Add(<span class="keyword">this</span>);
       
    90 <a name="l00106"></a>00106         }
       
    91 <a name="l00107"></a>00107 
       
    92 <a name="l00111"></a><a class="code" href="class_c_subtractor.html#a0d39bfb8170e1923541fa6efd915d206">00111</a> <span class="keywordtype">void</span> <a class="code" href="class_c_subtractor.html#a0d39bfb8170e1923541fa6efd915d206">CSubtractor::ReadFunction</a>()
       
    93 <a name="l00112"></a>00112         {
       
    94 <a name="l00113"></a>00113         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextMessage,<span class="stringliteral">&quot;Press a key to exit...\n&quot;</span>);
       
    95 <a name="l00114"></a>00114         iConsole-&gt;Printf(KTextMessage);
       
    96 <a name="l00115"></a>00115         <span class="comment">// Wait for a key press event.</span>
       
    97 <a name="l00116"></a>00116         iConsole-&gt;Read(iStatus);
       
    98 <a name="l00117"></a>00117         SetActive();
       
    99 <a name="l00118"></a>00118         }
       
   100 <a name="l00119"></a>00119 
       
   101 <a name="l00124"></a><a class="code" href="class_c_subtractor.html#ab5a9d2c973fb9387a6519971ddcb61c2">00124</a> <span class="keywordtype">void</span> <a class="code" href="class_c_subtractor.html#ab5a9d2c973fb9387a6519971ddcb61c2">CSubtractor::StartTimer</a>()
       
   102 <a name="l00125"></a>00125         {
       
   103 <a name="l00126"></a>00126         iPeriodic-&gt;Start(0,3000000,TCallBack(<a class="code" href="class_c_subtractor.html#a44265608e41e7a13f8790fcd68a5589d">SubtractFunction</a>,<span class="keyword">this</span>));
       
   104 <a name="l00127"></a>00127         }
       
   105 <a name="l00128"></a>00128 
       
   106 <a name="l00132"></a><a class="code" href="class_c_subtractor.html#a6fccf6a6e4f1b731f1d2105ccac84484">00132</a> <span class="keywordtype">void</span> <a class="code" href="class_c_subtractor.html#a6fccf6a6e4f1b731f1d2105ccac84484">CSubtractor::StopTimer</a>()
       
   107 <a name="l00133"></a>00133         {
       
   108 <a name="l00134"></a>00134         <span class="comment">// Cancel the  outstanding request.</span>
       
   109 <a name="l00135"></a>00135         iPeriodic-&gt;Cancel();
       
   110 <a name="l00136"></a>00136         }
       
   111 <a name="l00137"></a>00137 
       
   112 <a name="l00143"></a><a class="code" href="class_c_subtractor.html#a44265608e41e7a13f8790fcd68a5589d">00143</a> TInt <a class="code" href="class_c_subtractor.html#a44265608e41e7a13f8790fcd68a5589d">CSubtractor::SubtractFunction</a>(TAny* aPtr)
       
   113 <a name="l00144"></a>00144         {
       
   114 <a name="l00145"></a>00145         <a class="code" href="class_c_subtractor.html">CSubtractor</a>* ptr = <span class="keyword">static_cast&lt;</span><a class="code" href="class_c_subtractor.html">CSubtractor</a>*<span class="keyword">&gt;</span> (aPtr);
       
   115 <a name="l00146"></a>00146         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTxtPanic,<span class="stringliteral">&quot;Unexpected datatype\n&quot;</span>);
       
   116 <a name="l00147"></a>00147         __ASSERT_ALWAYS(ptr,User::Panic(KTxtPanic,-1));
       
   117 <a name="l00148"></a>00148         <span class="comment">// Invoke the Subtract() function.</span>
       
   118 <a name="l00149"></a>00149         ptr-&gt;<a class="code" href="class_c_subtractor.html#aaea4ddea47a64945f5dcd280d9f5adcb">Subtract</a>();
       
   119 <a name="l00150"></a>00150         <span class="keywordflow">return</span> KErrNone;
       
   120 <a name="l00151"></a>00151         }
       
   121 <a name="l00152"></a>00152 
       
   122 <a name="l00156"></a><a class="code" href="class_c_subtractor.html#aaea4ddea47a64945f5dcd280d9f5adcb">00156</a> <span class="keywordtype">void</span> <a class="code" href="class_c_subtractor.html#aaea4ddea47a64945f5dcd280d9f5adcb">CSubtractor::Subtract</a>()
       
   123 <a name="l00157"></a>00157         {
       
   124 <a name="l00158"></a>00158         <span class="comment">// Acquire the mutex.</span>
       
   125 <a name="l00159"></a>00159         iMutex.Wait();
       
   126 <a name="l00160"></a>00160 
       
   127 <a name="l00161"></a>00161         <span class="comment">// Get a random number.</span>
       
   128 <a name="l00162"></a>00162         TInt randVal = Math::Random() % 10;
       
   129 <a name="l00163"></a>00163 
       
   130 <a name="l00164"></a>00164         <span class="comment">// Get the address of the chunk.</span>
       
   131 <a name="l00165"></a>00165         TUint8 *ptr = iChunk.Base();
       
   132 <a name="l00166"></a>00166 
       
   133 <a name="l00167"></a>00167         <span class="comment">// Print the value of the chunk before subtraction.</span>
       
   134 <a name="l00168"></a>00168         iConsole-&gt;Printf(_L(<span class="stringliteral">&quot;Value read from the shared memory: %d\n&quot;</span>),*ptr);
       
   135 <a name="l00169"></a>00169 
       
   136 <a name="l00170"></a>00170         <span class="comment">// Subtract the random value from the shared memory variable.</span>
       
   137 <a name="l00171"></a>00171         *ptr -= randVal;
       
   138 <a name="l00172"></a>00172 
       
   139 <a name="l00173"></a>00173         <span class="keywordflow">while</span>(*ptr &lt; <a class="code" href="sharedmem_8h.html#a817ce7a9d8ca9ead92509953f8dcdcad">KLowerThreshold</a>)
       
   140 <a name="l00174"></a>00174                 {
       
   141 <a name="l00175"></a>00175                 <span class="comment">// Wait on the condition variable if the result is lesser than 0.</span>
       
   142 <a name="l00176"></a>00176                 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KBlockMessage,<span class="stringliteral">&quot;Subtractor blocked by condVar until Adder signals that value has been increased.\nIntermediate value of the chunk = %d\n&quot;</span>);
       
   143 <a name="l00177"></a>00177                 iConsole-&gt;Printf(KBlockMessage,*ptr);
       
   144 <a name="l00178"></a>00178                 iCondVar.Wait(iMutex);
       
   145 <a name="l00179"></a>00179                 }
       
   146 <a name="l00180"></a>00180         <span class="comment">// Print the updated value of the chunk.</span>
       
   147 <a name="l00181"></a>00181         iConsole-&gt;Printf(_L(<span class="stringliteral">&quot;Value of the shared memory decreased to : %d\n&quot;</span>),*ptr);
       
   148 <a name="l00182"></a>00182 
       
   149 <a name="l00183"></a>00183         <span class="comment">// Signal the mutex and the condition variable.</span>
       
   150 <a name="l00184"></a>00184         <span class="keywordflow">if</span>(*ptr &lt; <a class="code" href="sharedmem_8h.html#a0f2ae9d486d80c446844be5480b7bbc2">KUpperThreshold</a>)
       
   151 <a name="l00185"></a>00185                 {
       
   152 <a name="l00186"></a>00186                 <span class="comment">// Signal that the level is safe for addition to (re)start.</span>
       
   153 <a name="l00187"></a>00187                 iCondVar.Signal();
       
   154 <a name="l00188"></a>00188                 }
       
   155 <a name="l00189"></a>00189         iMutex.Signal();
       
   156 <a name="l00190"></a>00190         }
       
   157 <a name="l00191"></a>00191 
       
   158 <a name="l00192"></a>00192 LOCAL_D CConsoleBase* console;
       
   159 <a name="l00193"></a>00193 LOCAL_C <span class="keywordtype">void</span> DoExampleL();
       
   160 <a name="l00194"></a>00194 LOCAL_C <span class="keywordtype">void</span> callExampleL();
       
   161 <a name="l00195"></a>00195 
       
   162 <a name="l00196"></a>00196 LOCAL_C <span class="keywordtype">void</span> DoExampleL()
       
   163 <a name="l00197"></a>00197         {
       
   164 <a name="l00198"></a>00198         <span class="comment">// Create and install the active scheduler.</span>
       
   165 <a name="l00199"></a>00199         CActiveScheduler* scheduler = <span class="keyword">new</span> (ELeave) CActiveScheduler();
       
   166 <a name="l00200"></a>00200         CleanupStack::PushL(scheduler);
       
   167 <a name="l00201"></a>00201         CActiveScheduler::Install(scheduler);
       
   168 <a name="l00202"></a>00202 
       
   169 <a name="l00203"></a>00203         <span class="comment">// Create the CSubtractor object.</span>
       
   170 <a name="l00204"></a>00204         <a class="code" href="class_c_subtractor.html">CSubtractor</a>* subtractor = <a class="code" href="class_c_subtractor.html#a7d6002b0b8610a3241853c7fb4ef3995">CSubtractor::NewL</a>(console);
       
   171 <a name="l00205"></a>00205         CleanupStack::PushL(subtractor);
       
   172 <a name="l00206"></a>00206 
       
   173 <a name="l00207"></a>00207         <span class="comment">// Start the timer of the CSubtractor object.</span>
       
   174 <a name="l00208"></a>00208         subtractor-&gt;<a class="code" href="class_c_subtractor.html#ab5a9d2c973fb9387a6519971ddcb61c2">StartTimer</a>();
       
   175 <a name="l00209"></a>00209         <span class="comment">// Issue an asynchronous read request.</span>
       
   176 <a name="l00210"></a>00210         subtractor-&gt;<a class="code" href="class_c_subtractor.html#a0d39bfb8170e1923541fa6efd915d206">ReadFunction</a>();
       
   177 <a name="l00211"></a>00211         <span class="comment">// Start the active scheduler.</span>
       
   178 <a name="l00212"></a>00212         CActiveScheduler::Start();
       
   179 <a name="l00213"></a>00213 
       
   180 <a name="l00214"></a>00214         CleanupStack::PopAndDestroy(2,scheduler);
       
   181 <a name="l00215"></a>00215         }
       
   182 <a name="l00216"></a>00216 
       
   183 <a name="l00217"></a><a class="code" href="subtractor_8cpp.html#a0f358e9c4355138f629b8c4f37310295">00217</a> GLDEF_C TInt E32Main() <span class="comment">// main function called by E32</span>
       
   184 <a name="l00218"></a>00218     {
       
   185 <a name="l00219"></a>00219         __UHEAP_MARK;
       
   186 <a name="l00220"></a>00220         CTrapCleanup* cleanup=CTrapCleanup::New(); <span class="comment">// get clean-up stack</span>
       
   187 <a name="l00221"></a>00221         TRAPD(error,callExampleL()); <span class="comment">// more initializations, then do example</span>
       
   188 <a name="l00222"></a>00222         <span class="keyword">delete</span> cleanup; <span class="comment">// destroy clean-up stack</span>
       
   189 <a name="l00223"></a>00223         __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
       
   190 <a name="l00224"></a>00224         __UHEAP_MARKEND;
       
   191 <a name="l00225"></a>00225         <span class="keywordflow">return</span> 0; <span class="comment">// and return</span>
       
   192 <a name="l00226"></a>00226     }
       
   193 <a name="l00227"></a>00227 
       
   194 <a name="l00228"></a>00228 LOCAL_C <span class="keywordtype">void</span> callExampleL() <span class="comment">// initialise and call example code under cleanup stack</span>
       
   195 <a name="l00229"></a>00229     {
       
   196 <a name="l00230"></a>00230         console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
       
   197 <a name="l00231"></a>00231         CleanupStack::PushL(console);
       
   198 <a name="l00232"></a>00232         TRAPD(error,DoExampleL()); <span class="comment">// perform example function</span>
       
   199 <a name="l00233"></a>00233         <span class="keywordflow">if</span> (error)
       
   200 <a name="l00234"></a>00234                 console-&gt;Printf(KFormatFailed, error);
       
   201 <a name="l00235"></a>00235         <span class="keywordflow">else</span>
       
   202 <a name="l00236"></a>00236                 console-&gt;Printf(KTxtOK);
       
   203 <a name="l00237"></a>00237         console-&gt;Printf(KTxtPressAnyKey);
       
   204 <a name="l00238"></a>00238         console-&gt;Getch(); <span class="comment">// get and ignore character</span>
       
   205 <a name="l00239"></a>00239         CleanupStack::PopAndDestroy(); <span class="comment">// close console</span>
       
   206 <a name="l00240"></a>00240     }
       
   207 </pre></div></div>
       
   208 <hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
       
   209 <a href="http://www.doxygen.org/index.html">
       
   210 <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
       
   211 </body>
       
   212 </html>