Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/_semaphore_example_8cpp_source.html
Initial contribution of the Adaptation Documentation.
<!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/SemaphoreExample/src/SemaphoreExample.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/SemaphoreExample/src/SemaphoreExample.cpp</h1><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">// Contains the E32Main() function, which executes the example. The macro USE_SEMAPHORE is used at various</span>
<a name="l00015"></a>00015 <span class="comment">// places to show synchronization mechanism using semaphores.</span>
<a name="l00016"></a>00016
<a name="l00017"></a>00017
<a name="l00018"></a>00018 <span class="preprocessor">#include "SemaphoreExample.h"</span>
<a name="l00019"></a>00019
<a name="l00023"></a><a class="code" href="class_c_semaphore_example.html#aa1bde5ce9ac1befd3249c4ea56a2d214">00023</a> <a class="code" href="class_c_semaphore_example.html#aa1bde5ce9ac1befd3249c4ea56a2d214">CSemaphoreExample::~CSemaphoreExample</a>()
<a name="l00024"></a>00024 {
<a name="l00025"></a>00025 iReadThread.Close();<span class="comment">//Reader thread closed. </span>
<a name="l00026"></a>00026 iWriteThread.Close();<span class="comment">//Writer thread closed. </span>
<a name="l00027"></a>00027 <span class="keyword">delete</span> iConsole;
<a name="l00028"></a>00028 }
<a name="l00029"></a>00029
<a name="l00033"></a><a class="code" href="class_c_semaphore_example.html#a12ed690de78daccf113004ba4ba02f42">00033</a> <a class="code" href="class_c_semaphore_example.html">CSemaphoreExample</a>* <a class="code" href="class_c_semaphore_example.html#a12ed690de78daccf113004ba4ba02f42">CSemaphoreExample::NewL</a>()
<a name="l00034"></a>00034 {
<a name="l00035"></a>00035 <a class="code" href="class_c_semaphore_example.html">CSemaphoreExample</a>* <span class="keyword">self</span>=<span class="keyword">new</span>(ELeave)<a class="code" href="class_c_semaphore_example.html">CSemaphoreExample</a>();
<a name="l00036"></a>00036 CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00037"></a>00037 <span class="keyword">self</span>->ConstructL();
<a name="l00038"></a>00038 CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00039"></a>00039 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00040"></a>00040 }
<a name="l00041"></a>00041
<a name="l00046"></a><a class="code" href="class_c_semaphore_example.html#a206ff3d040178008062e201ecf67577f">00046</a> <span class="keywordtype">void</span> <a class="code" href="class_c_semaphore_example.html#a206ff3d040178008062e201ecf67577f">CSemaphoreExample::ConstructL</a>()
<a name="l00047"></a>00047 {
<a name="l00048"></a>00048 <span class="comment">//Create a reader thread to read the database.</span>
<a name="l00049"></a>00049 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KReader,<span class="stringliteral">"ReaderThread"</span>);
<a name="l00050"></a>00050 User::LeaveIfError(iReadThread.Create(KReader,<a class="code" href="class_c_semaphore_example.html#a9a32bdc9ccf1e50c38b4accb4d7dfa56">ReadThreadFuncL</a>,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,<span class="keyword">this</span>,EOwnerProcess));
<a name="l00051"></a>00051
<a name="l00052"></a>00052 <span class="comment">//Create a writer thread to write into the database. </span>
<a name="l00053"></a>00053 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KWriter,<span class="stringliteral">"WriterThread"</span>);
<a name="l00054"></a>00054 User::LeaveIfError(iWriteThread.Create(KWriter,<a class="code" href="class_c_semaphore_example.html#a38d9e05692b6122e501d04a4c481bdeb">WriteThreadFuncL</a>,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,<span class="keyword">this</span>,EOwnerProcess));
<a name="l00055"></a>00055
<a name="l00056"></a>00056 <span class="comment">//Sets the priorities of the threads when not using a semaphore, but this is not a good practice to achieve serialized execution; the preferred way</span>
<a name="l00057"></a>00057 <span class="comment">//should be using semaphores. If USE_SEMAPHORE is used, create a global semaphore. </span>
<a name="l00058"></a>00058 <span class="preprocessor"> #ifndef USE_SEMAPHORE</span>
<a name="l00059"></a>00059 <span class="preprocessor"></span> iWriteThread.SetPriority(EPriorityMore);
<a name="l00060"></a>00060 iReadThread.SetPriority(EPriorityLess);
<a name="l00061"></a>00061 <span class="preprocessor"> #else</span>
<a name="l00062"></a>00062 <span class="preprocessor"></span> <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSemaphoreWrite, <span class="stringliteral">"WriteSemaphore"</span>);
<a name="l00063"></a>00063 User::LeaveIfError(iSem.CreateGlobal(KSemaphoreWrite, 0, EOwnerProcess));
<a name="l00064"></a>00064 <span class="preprocessor"> #endif</span>
<a name="l00065"></a>00065 <span class="preprocessor"></span> }
<a name="l00066"></a>00066
<a name="l00070"></a><a class="code" href="class_c_semaphore_example.html#a518cb178a863033b0eb4899035802924">00070</a> <span class="keywordtype">void</span> <a class="code" href="class_c_semaphore_example.html#a518cb178a863033b0eb4899035802924">CSemaphoreExample::StartThreads</a>()
<a name="l00071"></a>00071 {
<a name="l00072"></a>00072 TRequestStatus status1, status2;
<a name="l00073"></a>00073
<a name="l00074"></a>00074 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPressKey, <span class="stringliteral">"\nPress any key to start writing to and reading from the database.\n"</span>);
<a name="l00075"></a>00075 iConsole->Printf(KPressKey);
<a name="l00076"></a>00076 iConsole->Getch();
<a name="l00077"></a>00077 <span class="comment">//Request a notification for iReadThread to terminate. </span>
<a name="l00078"></a>00078 iReadThread.Logon(status2);
<a name="l00079"></a>00079 iReadThread.Resume();
<a name="l00080"></a>00080
<a name="l00081"></a>00081 <span class="comment">//Request a notification for iWriteThread to terminate. </span>
<a name="l00082"></a>00082 iWriteThread.Logon(status1);
<a name="l00083"></a>00083 iWriteThread.Resume();
<a name="l00084"></a>00084
<a name="l00085"></a>00085 <span class="comment">//Control returns to the main thread when the two threads terminate. </span>
<a name="l00086"></a>00086 User::WaitForRequest(status1);
<a name="l00087"></a>00087 User::WaitForRequest(status2);
<a name="l00088"></a>00088 }
<a name="l00089"></a>00089
<a name="l00090"></a>00090 <span class="keywordtype">void</span> CSemaphoreExample::PrintMessage()
<a name="l00091"></a>00091 {
<a name="l00092"></a>00092 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextConsoleTitle, <span class="stringliteral">"SemaphoreExample"</span>);
<a name="l00093"></a>00093 iConsole = <a class="code" href="class_c_semaphore_example.html#a12ed690de78daccf113004ba4ba02f42">Console::NewL</a>(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen));
<a name="l00094"></a>00094 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KWelcome, <span class="stringliteral">"Welcome to the SemaphoreExample.\n"</span>);
<a name="l00095"></a>00095
<a name="l00096"></a>00096 iConsole->Printf(KWelcome);
<a name="l00097"></a>00097 <span class="preprocessor"> #ifdef USE_SEMAPHORE </span>
<a name="l00098"></a>00098 <span class="preprocessor"></span> <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSemDefined, <span class="stringliteral">"The USE_SEMAPHORE macro is defined.\n"</span>);
<a name="l00099"></a>00099 iConsole->Printf(KSemDefined);
<a name="l00100"></a>00100 <span class="preprocessor"> #else </span>
<a name="l00101"></a>00101 <span class="preprocessor"></span> <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSemUnDefined, <span class="stringliteral">"The USE_SEMAPHORE macro is undefined.\n"</span>);
<a name="l00102"></a>00102 iConsole->Printf(KSemUnDefined);
<a name="l00103"></a>00103 <span class="preprocessor"> #endif</span>
<a name="l00104"></a>00104 <span class="preprocessor"></span>
<a name="l00105"></a>00105 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPurposel1, <span class="stringliteral">"There are two threads running in the example: WriterThread calls a periodic function\n"</span>);
<a name="l00106"></a>00106 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPurposel2, <span class="stringliteral">"which adds rows of integers to a database at an interval of 1 second.\n"</span>);
<a name="l00107"></a>00107
<a name="l00108"></a>00108 iConsole->Printf(KPurposel1);
<a name="l00109"></a>00109 iConsole->Printf(KPurposel2);
<a name="l00110"></a>00110
<a name="l00111"></a>00111
<a name="l00112"></a>00112 <span class="preprocessor"> #ifdef USE_SEMAPHORE </span>
<a name="l00113"></a>00113 <span class="preprocessor"></span>
<a name="l00114"></a>00114 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPurposel4, <span class="stringliteral">"ReaderThread waits for the WriterThread to complete inserting one row of integers\n"</span>);
<a name="l00115"></a>00115 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPurposel5, <span class="stringliteral">"and then reads the data just written.\n"</span>);
<a name="l00116"></a>00116
<a name="l00117"></a>00117 iConsole->Printf(KPurposel4);
<a name="l00118"></a>00118 iConsole->Printf(KPurposel5);
<a name="l00119"></a>00119 <span class="preprocessor"> #else </span>
<a name="l00120"></a>00120 <span class="preprocessor"></span> <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPurposel31, <span class="stringliteral">"ReaderThread also calls a periodic function to read each row of integers\n"</span>);
<a name="l00121"></a>00121 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPurposel32, <span class="stringliteral">"and displays them in the console.\n"</span>);
<a name="l00122"></a>00122 iConsole->Printf(KPurposel31);
<a name="l00123"></a>00123 iConsole->Printf(KPurposel32);
<a name="l00124"></a>00124 <span class="preprocessor"> #endif</span>
<a name="l00125"></a>00125 <span class="preprocessor"></span>
<a name="l00126"></a>00126 <span class="preprocessor"> #ifdef USE_SEMAPHORE </span>
<a name="l00127"></a>00127 <span class="preprocessor"></span> <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSemaphoreNote, <span class="stringliteral">"RSemaphore is used to synchronize the two threads.\n"</span>);
<a name="l00128"></a>00128 iConsole->Printf(KSemaphoreNote);
<a name="l00129"></a>00129 <span class="preprocessor"> #else</span>
<a name="l00130"></a>00130 <span class="preprocessor"></span> <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KNoSemaphoreNotel, <span class="stringliteral">"This implementation does not use Semaphores, instead it uses thread priorities and doesn't\n"</span>);
<a name="l00131"></a>00131 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KNoSemaphoreNote2, <span class="stringliteral">"guarantee correct thread synchronization.\n"</span>);
<a name="l00132"></a>00132 iConsole->Printf(KNoSemaphoreNotel);
<a name="l00133"></a>00133 iConsole->Printf(KNoSemaphoreNote2);
<a name="l00134"></a>00134 <span class="preprocessor"> #endif</span>
<a name="l00135"></a>00135 <span class="preprocessor"></span> }
<a name="l00136"></a>00136
<a name="l00141"></a>00141 <span class="keyword">static</span> <span class="keywordtype">void</span> ReadDbFuncL()
<a name="l00142"></a>00142 {
<a name="l00143"></a>00143 CActiveScheduler* scheduler = <span class="keyword">new</span> (ELeave) CActiveScheduler();
<a name="l00144"></a>00144 CleanupStack::PushL(scheduler);
<a name="l00145"></a>00145 CActiveScheduler::Install(scheduler);
<a name="l00146"></a>00146
<a name="l00147"></a>00147 <a class="code" href="class_c_database.html">CDatabase</a>* db = <a class="code" href="class_c_database.html#aab355b9be127debd7a5d54e5ac8f9021">CDatabase::NewL</a>();
<a name="l00148"></a>00148 CleanupStack::PushL(db);
<a name="l00149"></a>00149
<a name="l00150"></a>00150 <span class="comment">//Start reading the database. </span>
<a name="l00151"></a>00151 db-><a class="code" href="class_c_database.html#ae9fea33b26bf2ba7cdad59361ee45221">ReadfromDatabaseL</a>();
<a name="l00152"></a>00152
<a name="l00153"></a>00153 CleanupStack::PopAndDestroy(db);
<a name="l00154"></a>00154 CleanupStack::PopAndDestroy(scheduler);
<a name="l00155"></a>00155 }
<a name="l00156"></a>00156
<a name="l00161"></a><a class="code" href="class_c_semaphore_example.html#a9a32bdc9ccf1e50c38b4accb4d7dfa56">00161</a> TInt <a class="code" href="class_c_semaphore_example.html#a9a32bdc9ccf1e50c38b4accb4d7dfa56">CSemaphoreExample::ReadThreadFuncL</a>(TAny* <span class="comment">/*aPtr*/</span>)
<a name="l00162"></a>00162 {
<a name="l00163"></a>00163 __UHEAP_MARK;
<a name="l00164"></a>00164 <span class="comment">//Create cleanup stack.</span>
<a name="l00165"></a>00165 CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00166"></a>00166 <span class="keywordflow">if</span>(!cleanup)
<a name="l00167"></a>00167 {
<a name="l00168"></a>00168 <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00169"></a>00169 }
<a name="l00170"></a>00170
<a name="l00171"></a>00171 TRAPD(error, ReadDbFuncL());
<a name="l00172"></a>00172 <span class="keywordflow">if</span>(error != KErrNone)
<a name="l00173"></a>00173 {
<a name="l00174"></a>00174 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic,<span class="stringliteral">"Failed"</span>);
<a name="l00175"></a>00175 User::Panic(KUserPanic, error);
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177 <span class="keyword">delete</span> cleanup;
<a name="l00178"></a>00178 __UHEAP_MARKEND;
<a name="l00179"></a>00179 <span class="keywordflow">return</span> KErrNone;
<a name="l00180"></a>00180 }
<a name="l00181"></a>00181
<a name="l00186"></a>00186 <span class="keyword">static</span> <span class="keywordtype">void</span> WriteDbFuncL()
<a name="l00187"></a>00187 {
<a name="l00188"></a>00188 CActiveScheduler* scheduler = <span class="keyword">new</span> (ELeave) CActiveScheduler();
<a name="l00189"></a>00189 CleanupStack::PushL(scheduler);
<a name="l00190"></a>00190 CActiveScheduler::Install(scheduler);
<a name="l00191"></a>00191
<a name="l00192"></a>00192 <a class="code" href="class_c_database.html">CDatabase</a>* db = <a class="code" href="class_c_database.html#aab355b9be127debd7a5d54e5ac8f9021">CDatabase::NewL</a>();
<a name="l00193"></a>00193 CleanupStack::PushL(db);
<a name="l00194"></a>00194 <span class="comment">//Create a database.</span>
<a name="l00195"></a>00195 db-><a class="code" href="class_c_database.html#a89047671e6c027e4551a8b7a276add96">CreateDatabaseL</a>();
<a name="l00196"></a>00196
<a name="l00197"></a>00197 <span class="comment">//Write into the database. </span>
<a name="l00198"></a>00198 db-><a class="code" href="class_c_database.html#a5df7d7393cfb36c850d562df4f35a0f6">WritetoDatabaseL</a>();
<a name="l00199"></a>00199
<a name="l00200"></a>00200 CleanupStack::PopAndDestroy(db);
<a name="l00201"></a>00201 CleanupStack::PopAndDestroy(scheduler);
<a name="l00202"></a>00202 }
<a name="l00203"></a>00203
<a name="l00208"></a><a class="code" href="class_c_semaphore_example.html#a38d9e05692b6122e501d04a4c481bdeb">00208</a> TInt <a class="code" href="class_c_semaphore_example.html#a38d9e05692b6122e501d04a4c481bdeb">CSemaphoreExample::WriteThreadFuncL</a>(TAny* <span class="comment">/*aPtr*/</span>)
<a name="l00209"></a>00209 {
<a name="l00210"></a>00210 __UHEAP_MARK;
<a name="l00211"></a>00211 <span class="comment">//Create cleanup stack.</span>
<a name="l00212"></a>00212 CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00213"></a>00213 <span class="keywordflow">if</span>(!cleanup)
<a name="l00214"></a>00214 {
<a name="l00215"></a>00215 <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00216"></a>00216 }
<a name="l00217"></a>00217
<a name="l00218"></a>00218 TRAPD(err, WriteDbFuncL());
<a name="l00219"></a>00219 <span class="keywordflow">if</span>(err != KErrNone)
<a name="l00220"></a>00220 {
<a name="l00221"></a>00221 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic,<span class="stringliteral">"Failed"</span>);
<a name="l00222"></a>00222 User::Panic(KUserPanic, err);
<a name="l00223"></a>00223 }
<a name="l00224"></a>00224 <span class="keyword">delete</span> cleanup;
<a name="l00225"></a>00225 __UHEAP_MARKEND;
<a name="l00226"></a>00226 <span class="keywordflow">return</span> KErrNone;
<a name="l00227"></a>00227 }
<a name="l00228"></a>00228
<a name="l00232"></a>00232 <span class="keyword">static</span> <span class="keywordtype">void</span> MainL()
<a name="l00233"></a>00233 {
<a name="l00234"></a>00234 <a class="code" href="class_c_semaphore_example.html">CSemaphoreExample</a>* semExample = <a class="code" href="class_c_semaphore_example.html#a12ed690de78daccf113004ba4ba02f42">CSemaphoreExample::NewL</a>();
<a name="l00235"></a>00235 CleanupStack::PushL(semExample);
<a name="l00236"></a>00236 semExample->PrintMessage();
<a name="l00237"></a>00237 semExample-><a class="code" href="class_c_semaphore_example.html#a518cb178a863033b0eb4899035802924">StartThreads</a>();
<a name="l00238"></a>00238 CleanupStack::PopAndDestroy(semExample);
<a name="l00239"></a>00239 }
<a name="l00240"></a>00240
<a name="l00244"></a>00244 <span class="keyword">extern</span> TInt E32Main()
<a name="l00245"></a>00245 {
<a name="l00246"></a>00246 <span class="comment">//Create cleanup stack.</span>
<a name="l00247"></a>00247 __UHEAP_MARK;
<a name="l00248"></a>00248 CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00249"></a>00249 <span class="keywordflow">if</span>(!cleanup)
<a name="l00250"></a>00250 {
<a name="l00251"></a>00251 <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00252"></a>00252 }
<a name="l00253"></a>00253 <span class="comment">//Run application code inside a TRAP harness.</span>
<a name="l00254"></a>00254 TRAPD(mainError, MainL());
<a name="l00255"></a>00255 <span class="keywordflow">if</span>(mainError != KErrNone)
<a name="l00256"></a>00256 {
<a name="l00257"></a>00257 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic,<span class="stringliteral">"Failed"</span>);
<a name="l00258"></a>00258 User::Panic(KUserPanic, mainError);
<a name="l00259"></a>00259 }
<a name="l00260"></a>00260 <span class="keyword">delete</span> cleanup;
<a name="l00261"></a>00261 __UHEAP_MARKEND;
<a name="l00262"></a>00262 <span class="keywordflow">return</span> KErrNone;
<a name="l00263"></a>00263 }
</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>