|
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
|
3 <title>TB10.1 Example Applications: examples/Base/SmpExample/src/SmpExample.cpp Source File</title> |
|
4 <link href="doxygen.css" rel="stylesheet" type="text/css"> |
|
5 <link href="tabs.css" rel="stylesheet" type="text/css"> |
|
6 </head><body> |
|
7 <!-- Generated by Doxygen 1.5.3 --> |
|
8 <h1>examples/Base/SmpExample/src/SmpExample.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> |
|
9 <a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span> |
|
10 <a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span> |
|
11 <a name="l00004"></a>00004 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span> |
|
12 <a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span> |
|
13 <a name="l00006"></a>00006 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span> |
|
14 <a name="l00007"></a>00007 <span class="comment">//</span> |
|
15 <a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span> |
|
16 <a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span> |
|
17 <a name="l00010"></a>00010 <span class="comment">//</span> |
|
18 <a name="l00011"></a>00011 <span class="comment">// Contributors:</span> |
|
19 <a name="l00012"></a>00012 <span class="comment">//</span> |
|
20 <a name="l00013"></a>00013 |
|
21 <a name="l00014"></a>00014 <span class="preprocessor">#include <d32dbms.h></span> <span class="comment">//Used for RDbDatabase class. </span> |
|
22 <a name="l00015"></a>00015 <span class="preprocessor">#include "SmpExample.h"</span> |
|
23 <a name="l00016"></a>00016 _LIT(KDatabase1, <span class="stringliteral">"C:\\DBforSMP1.db"</span>); |
|
24 <a name="l00017"></a>00017 _LIT(KDatabase2, <span class="stringliteral">"C:\\DBforSMP2.db"</span>); |
|
25 <a name="l00018"></a>00018 |
|
26 <a name="l00022"></a><a class="code" href="class_c_smp_example.html#4f902855860346e1529c8aad989100f0">00022</a> <a class="code" href="class_c_smp_example.html#4f902855860346e1529c8aad989100f0">CSmpExample::~CSmpExample</a>() |
|
27 <a name="l00023"></a>00023 { |
|
28 <a name="l00024"></a>00024 iReadThread.Close(); <span class="comment">//Reader thread closed. </span> |
|
29 <a name="l00025"></a>00025 iWriteThread1.Close(); <span class="comment">//Writer thread closed. </span> |
|
30 <a name="l00026"></a>00026 iWriteThread2.Close(); <span class="comment">//Writer thread closed.</span> |
|
31 <a name="l00027"></a>00027 <span class="keyword">delete</span> iConsole; |
|
32 <a name="l00028"></a>00028 } |
|
33 <a name="l00029"></a>00029 |
|
34 <a name="l00033"></a><a class="code" href="class_c_smp_example.html#af2dab332a98167a1dde6309732a2460">00033</a> <a class="code" href="class_c_smp_example.html">CSmpExample</a>* <a class="code" href="class_c_smp_example.html#af2dab332a98167a1dde6309732a2460">CSmpExample::NewL</a>() |
|
35 <a name="l00034"></a>00034 { |
|
36 <a name="l00035"></a>00035 <a class="code" href="class_c_smp_example.html">CSmpExample</a>* <span class="keyword">self</span> = <span class="keyword">new</span>(ELeave)<a class="code" href="class_c_smp_example.html">CSmpExample</a>(); |
|
37 <a name="l00036"></a>00036 CleanupStack::PushL(<span class="keyword">self</span>); |
|
38 <a name="l00037"></a>00037 <span class="keyword">self</span>->ConstructL(); |
|
39 <a name="l00038"></a>00038 CleanupStack::Pop(<span class="keyword">self</span>); |
|
40 <a name="l00039"></a>00039 <span class="keywordflow">return</span> <span class="keyword">self</span>; |
|
41 <a name="l00040"></a>00040 } |
|
42 <a name="l00041"></a>00041 |
|
43 <a name="l00045"></a><a class="code" href="class_c_smp_example.html#a9bbdb0f3c1cb00236cf6eedc2fe0840">00045</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#a9bbdb0f3c1cb00236cf6eedc2fe0840">CSmpExample::ConstructL</a>() |
|
44 <a name="l00046"></a>00046 { |
|
45 <a name="l00047"></a>00047 <a class="code" href="class_c_smp_example.html#6ed7d09453967159b3cb9cb8c271aeab">CreateDatabaseL</a>(KDatabase1); |
|
46 <a name="l00048"></a>00048 <a class="code" href="class_c_smp_example.html#6ed7d09453967159b3cb9cb8c271aeab">CreateDatabaseL</a>(KDatabase2); |
|
47 <a name="l00049"></a>00049 |
|
48 <a name="l00050"></a>00050 _LIT(KTextConsoleTitle, <span class="stringliteral">"SmpExample"</span>); |
|
49 <a name="l00051"></a>00051 iConsole = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen, KConsFullScreen)); |
|
50 <a name="l00052"></a>00052 |
|
51 <a name="l00053"></a>00053 <span class="comment">//Creates a reader thread to read the databases.</span> |
|
52 <a name="l00054"></a>00054 _LIT(KReader, <span class="stringliteral">"ReaderThread"</span>); |
|
53 <a name="l00055"></a>00055 User::LeaveIfError(iReadThread.Create(KReader, <a class="code" href="class_c_smp_example.html#c56a561b32603f3d9fccb186b90ee28b">ReadThreadFuncL</a>, KDefaultStackSize, KMinHeapSize, 256*KMinHeapSize, <span class="keyword">this</span>, EOwnerProcess)); |
|
54 <a name="l00056"></a>00056 |
|
55 <a name="l00057"></a>00057 <span class="comment">//Creates a writer thread to write to the DBforSMP1.db database. </span> |
|
56 <a name="l00058"></a>00058 _LIT(KWriter1, <span class="stringliteral">"WriterThread1"</span>); |
|
57 <a name="l00059"></a>00059 User::LeaveIfError(iWriteThread1.Create(KWriter1, <a class="code" href="class_c_smp_example.html#3a99e9be00e57c1d8489de6f62d00a2b">WriteThread1FuncL</a>, KDefaultStackSize, KMinHeapSize, 256*KMinHeapSize, <span class="keyword">this</span>, EOwnerProcess)); |
|
58 <a name="l00060"></a>00060 |
|
59 <a name="l00061"></a>00061 <span class="comment">//Creates a writer thread to write to the DBforSMP2.db database. </span> |
|
60 <a name="l00062"></a>00062 _LIT(KWriter2, <span class="stringliteral">"WriterThread2"</span>); |
|
61 <a name="l00063"></a>00063 User::LeaveIfError(iWriteThread2.Create(KWriter2, <a class="code" href="class_c_smp_example.html#41ccf5be445f2ad57cee4fa93b8830c6">WriteThread2FuncL</a>, KDefaultStackSize, KMinHeapSize, 256*KMinHeapSize, <span class="keyword">this</span>, EOwnerProcess)); |
|
62 <a name="l00064"></a>00064 |
|
63 <a name="l00065"></a>00065 |
|
64 <a name="l00066"></a>00066 <span class="comment">//Sets priority to the threads. </span> |
|
65 <a name="l00067"></a>00067 iWriteThread1.SetPriority(EPriorityMuchLess); |
|
66 <a name="l00068"></a>00068 iWriteThread2.SetPriority(EPriorityMore); |
|
67 <a name="l00069"></a>00069 iReadThread.SetPriority(EPriorityNormal); |
|
68 <a name="l00070"></a>00070 } |
|
69 <a name="l00071"></a>00071 |
|
70 <a name="l00075"></a><a class="code" href="class_c_smp_example.html#fdb87a66a6b0a41cbea465aa2495aa1c">00075</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#fdb87a66a6b0a41cbea465aa2495aa1c">CSmpExample::StartThreads</a>() |
|
71 <a name="l00076"></a>00076 { |
|
72 <a name="l00077"></a>00077 TRequestStatus writerThread1Status; |
|
73 <a name="l00078"></a>00078 TRequestStatus readerThreadStatus; |
|
74 <a name="l00079"></a>00079 TRequestStatus writerThread2Status; |
|
75 <a name="l00080"></a>00080 |
|
76 <a name="l00081"></a>00081 _LIT(KTextPressKey, <span class="stringliteral">"\nPress any key to start writing to and reading from the database\n"</span>); |
|
77 <a name="l00082"></a>00082 iConsole->Printf(KTextPressKey); |
|
78 <a name="l00083"></a>00083 iConsole->Getch(); |
|
79 <a name="l00084"></a>00084 |
|
80 <a name="l00085"></a>00085 <span class="comment">//Requests a notification for the ReaderThread to terminate. </span> |
|
81 <a name="l00086"></a>00086 iReadThread.Logon(readerThreadStatus); |
|
82 <a name="l00087"></a>00087 iReadThread.Resume(); |
|
83 <a name="l00088"></a>00088 |
|
84 <a name="l00089"></a>00089 <span class="comment">//Requests a notification for the WriterThread1 to terminate. </span> |
|
85 <a name="l00090"></a>00090 iWriteThread1.Logon(writerThread1Status); |
|
86 <a name="l00091"></a>00091 iWriteThread1.Resume(); |
|
87 <a name="l00092"></a>00092 |
|
88 <a name="l00093"></a>00093 <span class="comment">//Requests a notification for the WriterThread2 to terminate. </span> |
|
89 <a name="l00094"></a>00094 iWriteThread2.Logon(writerThread2Status); |
|
90 <a name="l00095"></a>00095 iWriteThread2.Resume(); |
|
91 <a name="l00096"></a>00096 |
|
92 <a name="l00097"></a>00097 <span class="comment">//Control returns to the main thread when the all the threads terminate. </span> |
|
93 <a name="l00098"></a>00098 User::WaitForRequest(writerThread1Status); |
|
94 <a name="l00099"></a>00099 User::WaitForRequest(readerThreadStatus); |
|
95 <a name="l00100"></a>00100 User::WaitForRequest(writerThread2Status); |
|
96 <a name="l00101"></a>00101 } |
|
97 <a name="l00102"></a>00102 |
|
98 <a name="l00106"></a><a class="code" href="class_c_smp_example.html#8eaf4cbbf9c4b69d0854140963a22e51">00106</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#8eaf4cbbf9c4b69d0854140963a22e51">CSmpExample::PrintMessage</a>() |
|
99 <a name="l00107"></a>00107 { |
|
100 <a name="l00108"></a>00108 _LIT(KTextWelcome, <span class="stringliteral">"Welcome to the SmpExample.\n"</span>); |
|
101 <a name="l00109"></a>00109 _LIT(KTextPurposel1, <span class="stringliteral">"There are three threads running in the example: WriterThread1 has the lowest priority,\n"</span>); |
|
102 <a name="l00110"></a>00110 _LIT(KTextPurposel2, <span class="stringliteral">"ReaderThread has normal priority and WriterThread2 has maximum priority.\n"</span>); |
|
103 <a name="l00111"></a>00111 _LIT(KTextPurposel3, <span class="stringliteral">"The two WriterThreads open two different databases and write some integers to them.\n"</span>); |
|
104 <a name="l00112"></a>00112 _LIT(KTextPurposel4, <span class="stringliteral">"ReaderThread reads the two databases and prints the output to the console.\n"</span>); |
|
105 <a name="l00113"></a>00113 |
|
106 <a name="l00114"></a>00114 _LIT(KTextPurposel5, <span class="stringliteral">"In a unicore environment, WriterThread1 will be scheduled to run last and would not have\n"</span>); |
|
107 <a name="l00115"></a>00115 _LIT(KTextPurposel6, <span class="stringliteral">"written to the database when ReaderThread starts reading the databases.\n"</span>); |
|
108 <a name="l00116"></a>00116 _LIT(KTextPurposel7, <span class="stringliteral">"But in an SMP environment, both the WriterThreads write to their respective databases simultaneously.\n"</span>); |
|
109 <a name="l00117"></a>00117 |
|
110 <a name="l00118"></a>00118 iConsole->Printf(KTextWelcome); |
|
111 <a name="l00119"></a>00119 iConsole->Printf(KTextPurposel1); |
|
112 <a name="l00120"></a>00120 iConsole->Printf(KTextPurposel2); |
|
113 <a name="l00121"></a>00121 iConsole->Printf(KTextPurposel3); |
|
114 <a name="l00122"></a>00122 iConsole->Printf(KTextPurposel4); |
|
115 <a name="l00123"></a>00123 iConsole->Printf(KTextPurposel5); |
|
116 <a name="l00124"></a>00124 iConsole->Printf(KTextPurposel6); |
|
117 <a name="l00125"></a>00125 iConsole->Printf(KTextPurposel7); |
|
118 <a name="l00126"></a>00126 } |
|
119 <a name="l00127"></a>00127 |
|
120 <a name="l00131"></a><a class="code" href="class_c_smp_example.html#3b589411b99d71c460442290516e04ec">00131</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#3b589411b99d71c460442290516e04ec">CSmpExample::ReadDatabaseL</a>(<span class="keyword">const</span> TDesC& aDbName, CConsoleBase& <a class="code" href="dbllist_8cpp.html#d789d40251baeb2f60e118de4856583d">console</a>) |
|
121 <a name="l00132"></a>00132 { |
|
122 <a name="l00133"></a>00133 <span class="comment">//Creates a file server session object before any file system manipulation. </span> |
|
123 <a name="l00134"></a>00134 RFs fsSession; |
|
124 <a name="l00135"></a>00135 CleanupClosePushL(fsSession); |
|
125 <a name="l00136"></a>00136 fsSession.Connect(); |
|
126 <a name="l00137"></a>00137 |
|
127 <a name="l00138"></a>00138 <span class="comment">//Creates Rdbs object</span> |
|
128 <a name="l00139"></a>00139 RDbs dbs; |
|
129 <a name="l00140"></a>00140 CleanupClosePushL(dbs); |
|
130 <a name="l00141"></a>00141 dbs.Connect(); |
|
131 <a name="l00142"></a>00142 |
|
132 <a name="l00143"></a>00143 <span class="comment">//Opens the named database using the RDbs object</span> |
|
133 <a name="l00144"></a>00144 RDbNamedDatabase database; |
|
134 <a name="l00145"></a>00145 CleanupClosePushL(database); |
|
135 <a name="l00146"></a>00146 User::LeaveIfError(database.Open(dbs, aDbName)); |
|
136 <a name="l00147"></a>00147 |
|
137 <a name="l00148"></a>00148 <span class="comment">//Locks the database.</span> |
|
138 <a name="l00149"></a>00149 database.Begin(); |
|
139 <a name="l00150"></a>00150 <span class="comment">//Prepare an SQL query to read one row of numbers from the database. </span> |
|
140 <a name="l00151"></a>00151 _LIT(KSQLStatement, <span class="stringliteral">"Select Number1, Number2, Number3 from Numbers order by Number1, Number2, Number3"</span>); |
|
141 <a name="l00152"></a>00152 |
|
142 <a name="l00153"></a>00153 <span class="comment">//Creates a view on the database to read it.</span> |
|
143 <a name="l00154"></a>00154 RDbView view; |
|
144 <a name="l00155"></a>00155 CleanupClosePushL(view); |
|
145 <a name="l00156"></a>00156 |
|
146 <a name="l00157"></a>00157 User::LeaveIfError(view.Prepare(database, TDbQuery(KSQLStatement, EDbCompareNormal))); |
|
147 <a name="l00158"></a>00158 User::LeaveIfError(view.EvaluateAll()); |
|
148 <a name="l00159"></a>00159 |
|
149 <a name="l00160"></a>00160 _LIT(KTextReading, <span class="stringliteral">"Reading the database\t %S.\n"</span>); |
|
150 <a name="l00161"></a>00161 console.Printf(KTextReading, &aDbName); |
|
151 <a name="l00162"></a>00162 |
|
152 <a name="l00163"></a>00163 <span class="comment">//Boolean variable to check whether the database is empty.</span> |
|
153 <a name="l00164"></a>00164 TBool isDbEmpty= EFalse; |
|
154 <a name="l00165"></a>00165 <span class="comment">//Iterates through the database till the last row is read. </span> |
|
155 <a name="l00166"></a>00166 <span class="keywordflow">for</span> (view.FirstL(); view.AtRow(); view.NextL()) |
|
156 <a name="l00167"></a>00167 { |
|
157 <a name="l00168"></a>00168 view.GetL(); |
|
158 <a name="l00169"></a>00169 TInt number1 = view.ColInt(1); |
|
159 <a name="l00170"></a>00170 TInt number2 = view.ColInt(2); |
|
160 <a name="l00171"></a>00171 TInt number3 = view.ColInt(3); |
|
161 <a name="l00172"></a>00172 <span class="comment">//Prepare a row formatter to print numbers in the console. </span> |
|
162 <a name="l00173"></a>00173 _LIT(KRowFormatter, <span class="stringliteral">"Reading %d \t%d\t%d\n"</span>); |
|
163 <a name="l00174"></a>00174 <span class="comment">//Reads the integers from the view and display them in the console.</span> |
|
164 <a name="l00175"></a>00175 console.Printf(KRowFormatter, number1, number2, number3); |
|
165 <a name="l00176"></a>00176 isDbEmpty= ETrue; |
|
166 <a name="l00177"></a>00177 } |
|
167 <a name="l00178"></a>00178 |
|
168 <a name="l00179"></a>00179 <span class="keywordflow">if</span>(isDbEmpty== EFalse) |
|
169 <a name="l00180"></a>00180 { |
|
170 <a name="l00181"></a>00181 _LIT(KTextDbEmpty, <span class="stringliteral">"Database is empty.\n\n"</span>); |
|
171 <a name="l00182"></a>00182 console.Printf(KTextDbEmpty); |
|
172 <a name="l00183"></a>00183 } |
|
173 <a name="l00184"></a>00184 <span class="keywordflow">else</span> |
|
174 <a name="l00185"></a>00185 { |
|
175 <a name="l00186"></a>00186 _LIT(KTextAllRead, <span class="stringliteral">"All the numbers in the database have been read.\n\n"</span>); |
|
176 <a name="l00187"></a>00187 console.Printf(KTextAllRead); |
|
177 <a name="l00188"></a>00188 } |
|
178 <a name="l00189"></a>00189 |
|
179 <a name="l00190"></a>00190 CleanupStack::PopAndDestroy(&view); |
|
180 <a name="l00191"></a>00191 CleanupStack::PopAndDestroy(&database); |
|
181 <a name="l00192"></a>00192 CleanupStack::PopAndDestroy(&dbs); |
|
182 <a name="l00193"></a>00193 CleanupStack::PopAndDestroy(&fsSession); |
|
183 <a name="l00194"></a>00194 } |
|
184 <a name="l00195"></a>00195 |
|
185 <a name="l00199"></a><a class="code" href="class_c_smp_example.html#70e2ac4866c0bea841d38203ae2f6194">00199</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#70e2ac4866c0bea841d38203ae2f6194">CSmpExample::ReadBothDatabasesL</a>() |
|
186 <a name="l00200"></a>00200 { |
|
187 <a name="l00201"></a>00201 _LIT(KTextConsoleTitle, <span class="stringliteral">"ReaderThread"</span>); |
|
188 <a name="l00202"></a>00202 CConsoleBase* <a class="code" href="dbllist_8cpp.html#d789d40251baeb2f60e118de4856583d">console</a>= Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen, KConsFullScreen)); |
|
189 <a name="l00203"></a>00203 CleanupStack::PushL(console); |
|
190 <a name="l00204"></a>00204 |
|
191 <a name="l00205"></a>00205 <span class="comment">//Reads DBforSMP1.db and prints the output to the console. </span> |
|
192 <a name="l00206"></a>00206 <a class="code" href="class_c_smp_example.html#3b589411b99d71c460442290516e04ec">ReadDatabaseL</a>(KDatabase1, *console); |
|
193 <a name="l00207"></a>00207 <span class="comment">//Reads DBforSMP2.db and prints the output to the console.</span> |
|
194 <a name="l00208"></a>00208 <a class="code" href="class_c_smp_example.html#3b589411b99d71c460442290516e04ec">ReadDatabaseL</a>(KDatabase2, *console); |
|
195 <a name="l00209"></a>00209 |
|
196 <a name="l00210"></a>00210 _LIT(KExit, <span class="stringliteral">"Press any key to exit\n"</span>); |
|
197 <a name="l00211"></a>00211 console->Printf(KExit); |
|
198 <a name="l00212"></a>00212 console->Getch(); |
|
199 <a name="l00213"></a>00213 CleanupStack::PopAndDestroy(console); |
|
200 <a name="l00214"></a>00214 } |
|
201 <a name="l00215"></a>00215 |
|
202 <a name="l00220"></a><a class="code" href="class_c_smp_example.html#c56a561b32603f3d9fccb186b90ee28b">00220</a> TInt <a class="code" href="class_c_smp_example.html#c56a561b32603f3d9fccb186b90ee28b">CSmpExample::ReadThreadFuncL</a>(TAny* <span class="comment">/*aPtr*/</span>) |
|
203 <a name="l00221"></a>00221 { |
|
204 <a name="l00222"></a>00222 __UHEAP_MARK; |
|
205 <a name="l00223"></a>00223 <span class="comment">//Creates cleanup stack.</span> |
|
206 <a name="l00224"></a>00224 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
207 <a name="l00225"></a>00225 <span class="keywordflow">if</span>(!cleanup) |
|
208 <a name="l00226"></a>00226 { |
|
209 <a name="l00227"></a>00227 <span class="keywordflow">return</span> KErrNoMemory; |
|
210 <a name="l00228"></a>00228 } |
|
211 <a name="l00229"></a>00229 |
|
212 <a name="l00230"></a>00230 TRAPD(<a class="code" href="_camera___app_ui_8cpp.html#711e7d3cb49b7e0de7880fed269134d3">error</a>, <a class="code" href="class_c_smp_example.html#70e2ac4866c0bea841d38203ae2f6194">ReadBothDatabasesL</a>()); |
|
213 <a name="l00231"></a>00231 <span class="keywordflow">if</span>(<a class="code" href="_camera___app_ui_8cpp.html#711e7d3cb49b7e0de7880fed269134d3">error</a> != KErrNone) |
|
214 <a name="l00232"></a>00232 { |
|
215 <a name="l00233"></a>00233 _LIT(KUserPanic, <span class="stringliteral">"DB Read Failed"</span>); |
|
216 <a name="l00234"></a>00234 User::Panic(KUserPanic, <a class="code" href="_camera___app_ui_8cpp.html#711e7d3cb49b7e0de7880fed269134d3">error</a>); |
|
217 <a name="l00235"></a>00235 } |
|
218 <a name="l00236"></a>00236 <span class="keyword">delete</span> cleanup; |
|
219 <a name="l00237"></a>00237 __UHEAP_MARKEND; |
|
220 <a name="l00238"></a>00238 <span class="keywordflow">return</span> KErrNone; |
|
221 <a name="l00239"></a>00239 } |
|
222 <a name="l00240"></a>00240 |
|
223 <a name="l00245"></a><a class="code" href="class_c_smp_example.html#26abdb02599626bb596e73e90a145037">00245</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#26abdb02599626bb596e73e90a145037">CSmpExample::WriteDbFuncL</a>(<span class="keyword">const</span> TDesC& aDbName, TInt aNum) |
|
224 <a name="l00246"></a>00246 { |
|
225 <a name="l00247"></a>00247 <span class="comment">//Creates a file server session object before any file system manipulation. </span> |
|
226 <a name="l00248"></a>00248 RFs fsSession; |
|
227 <a name="l00249"></a>00249 CleanupClosePushL(fsSession); |
|
228 <a name="l00250"></a>00250 fsSession.Connect(); |
|
229 <a name="l00251"></a>00251 |
|
230 <a name="l00252"></a>00252 |
|
231 <a name="l00253"></a>00253 <span class="comment">//Creates Rdbs object </span> |
|
232 <a name="l00254"></a>00254 RDbs dbs; |
|
233 <a name="l00255"></a>00255 CleanupClosePushL(dbs); |
|
234 <a name="l00256"></a>00256 dbs.Connect(); |
|
235 <a name="l00257"></a>00257 |
|
236 <a name="l00258"></a>00258 <span class="comment">//Opens the database using the RDbs object</span> |
|
237 <a name="l00259"></a>00259 RDbNamedDatabase database; |
|
238 <a name="l00260"></a>00260 CleanupClosePushL(database); |
|
239 <a name="l00261"></a>00261 User::LeaveIfError(database.Open(dbs, aDbName)); |
|
240 <a name="l00262"></a>00262 |
|
241 <a name="l00263"></a>00263 <span class="comment">//Locks the database.</span> |
|
242 <a name="l00264"></a>00264 database.Begin(); |
|
243 <a name="l00265"></a>00265 |
|
244 <a name="l00266"></a>00266 <span class="comment">//Creates a view on the database. </span> |
|
245 <a name="l00267"></a>00267 RDbView view; |
|
246 <a name="l00268"></a>00268 CleanupClosePushL(view); |
|
247 <a name="l00269"></a>00269 _LIT(KSQLStatement, <span class="stringliteral">"Select Number1, Number2, Number3 from Numbers order by Number1, Number2, Number3"</span>); |
|
248 <a name="l00270"></a>00270 User::LeaveIfError(view.Prepare(database, TDbQuery(KSQLStatement, EDbCompareNormal))); |
|
249 <a name="l00271"></a>00271 User::LeaveIfError(view.EvaluateAll()); |
|
250 <a name="l00272"></a>00272 |
|
251 <a name="l00273"></a>00273 <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; i<5; i++) |
|
252 <a name="l00274"></a>00274 { |
|
253 <a name="l00275"></a>00275 <span class="comment">//Inserts a new row at the end of the database. </span> |
|
254 <a name="l00276"></a>00276 view.InsertL(); |
|
255 <a name="l00277"></a>00277 <span class="comment">//Fills three columns with numbers.</span> |
|
256 <a name="l00278"></a>00278 view.SetColL(1, aNum++); |
|
257 <a name="l00279"></a>00279 view.SetColL(2, aNum++); |
|
258 <a name="l00280"></a>00280 view.SetColL(3, aNum++); |
|
259 <a name="l00281"></a>00281 view.PutL(); |
|
260 <a name="l00282"></a>00282 } |
|
261 <a name="l00283"></a>00283 |
|
262 <a name="l00284"></a>00284 CleanupStack::PopAndDestroy(&view); |
|
263 <a name="l00285"></a>00285 <span class="comment">//Commits the database and unlocks it. </span> |
|
264 <a name="l00286"></a>00286 database.Commit(); |
|
265 <a name="l00287"></a>00287 CleanupStack::PopAndDestroy(&database); |
|
266 <a name="l00288"></a>00288 CleanupStack::PopAndDestroy(&dbs); |
|
267 <a name="l00289"></a>00289 CleanupStack::PopAndDestroy(&fsSession); |
|
268 <a name="l00290"></a>00290 } |
|
269 <a name="l00291"></a>00291 |
|
270 <a name="l00292"></a>00292 |
|
271 <a name="l00297"></a><a class="code" href="class_c_smp_example.html#41ccf5be445f2ad57cee4fa93b8830c6">00297</a> TInt <a class="code" href="class_c_smp_example.html#41ccf5be445f2ad57cee4fa93b8830c6">CSmpExample::WriteThread2FuncL</a>(TAny* <span class="comment">/*aPtr*/</span>) |
|
272 <a name="l00298"></a>00298 { |
|
273 <a name="l00299"></a>00299 __UHEAP_MARK; |
|
274 <a name="l00300"></a>00300 <span class="comment">//Creates cleanup stack.</span> |
|
275 <a name="l00301"></a>00301 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
276 <a name="l00302"></a>00302 <span class="keywordflow">if</span>(!cleanup) |
|
277 <a name="l00303"></a>00303 { |
|
278 <a name="l00304"></a>00304 <span class="keywordflow">return</span> KErrNoMemory; |
|
279 <a name="l00305"></a>00305 } |
|
280 <a name="l00306"></a>00306 |
|
281 <a name="l00307"></a>00307 <span class="comment">//First number to be written to the database. </span> |
|
282 <a name="l00308"></a>00308 <span class="keyword">const</span> <span class="keywordtype">int</span> KFirstNumforDb2 = 500; |
|
283 <a name="l00309"></a>00309 TRAPD(err, <a class="code" href="class_c_smp_example.html#26abdb02599626bb596e73e90a145037">WriteDbFuncL</a>(KDatabase2, KFirstNumforDb2)); |
|
284 <a name="l00310"></a>00310 <span class="keywordflow">if</span>(err != KErrNone) |
|
285 <a name="l00311"></a>00311 { |
|
286 <a name="l00312"></a>00312 _LIT(KUserPanic, <span class="stringliteral">"DB Write Failed"</span>); |
|
287 <a name="l00313"></a>00313 User::Panic(KUserPanic, err); |
|
288 <a name="l00314"></a>00314 } |
|
289 <a name="l00315"></a>00315 <span class="keyword">delete</span> cleanup; |
|
290 <a name="l00316"></a>00316 __UHEAP_MARKEND; |
|
291 <a name="l00317"></a>00317 <span class="keywordflow">return</span> KErrNone; |
|
292 <a name="l00318"></a>00318 } |
|
293 <a name="l00319"></a>00319 |
|
294 <a name="l00324"></a><a class="code" href="class_c_smp_example.html#3a99e9be00e57c1d8489de6f62d00a2b">00324</a> TInt <a class="code" href="class_c_smp_example.html#3a99e9be00e57c1d8489de6f62d00a2b">CSmpExample::WriteThread1FuncL</a>(TAny* <span class="comment">/*aPtr*/</span>) |
|
295 <a name="l00325"></a>00325 { |
|
296 <a name="l00326"></a>00326 __UHEAP_MARK; |
|
297 <a name="l00327"></a>00327 <span class="comment">//Creates cleanup stack.</span> |
|
298 <a name="l00328"></a>00328 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
299 <a name="l00329"></a>00329 <span class="keywordflow">if</span>(!cleanup) |
|
300 <a name="l00330"></a>00330 { |
|
301 <a name="l00331"></a>00331 <span class="keywordflow">return</span> KErrNoMemory; |
|
302 <a name="l00332"></a>00332 } |
|
303 <a name="l00333"></a>00333 |
|
304 <a name="l00334"></a>00334 <span class="comment">//First number to be written to the database. </span> |
|
305 <a name="l00335"></a>00335 <span class="keyword">const</span> <span class="keywordtype">int</span> KFirstNumforDb1 = 0; |
|
306 <a name="l00336"></a>00336 TRAPD(err, <a class="code" href="class_c_smp_example.html#26abdb02599626bb596e73e90a145037">WriteDbFuncL</a>(KDatabase1, KFirstNumforDb1)); |
|
307 <a name="l00337"></a>00337 <span class="keywordflow">if</span>(err != KErrNone) |
|
308 <a name="l00338"></a>00338 { |
|
309 <a name="l00339"></a>00339 _LIT(KUserPanic, <span class="stringliteral">"DB Write Failed"</span>); |
|
310 <a name="l00340"></a>00340 User::Panic(KUserPanic, err); |
|
311 <a name="l00341"></a>00341 } |
|
312 <a name="l00342"></a>00342 <span class="keyword">delete</span> cleanup; |
|
313 <a name="l00343"></a>00343 __UHEAP_MARKEND; |
|
314 <a name="l00344"></a>00344 <span class="keywordflow">return</span> KErrNone; |
|
315 <a name="l00345"></a>00345 } |
|
316 <a name="l00346"></a>00346 |
|
317 <a name="l00351"></a><a class="code" href="class_c_smp_example.html#6ed7d09453967159b3cb9cb8c271aeab">00351</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#6ed7d09453967159b3cb9cb8c271aeab">CSmpExample::CreateDatabaseL</a>(<span class="keyword">const</span> TDesC& aDbName) |
|
318 <a name="l00352"></a>00352 { |
|
319 <a name="l00353"></a>00353 <span class="comment">//Creates a file server session object before any file system manipulation. </span> |
|
320 <a name="l00354"></a>00354 RFs fsSession; |
|
321 <a name="l00355"></a>00355 CleanupClosePushL(fsSession); |
|
322 <a name="l00356"></a>00356 fsSession.Connect(); |
|
323 <a name="l00357"></a>00357 |
|
324 <a name="l00358"></a>00358 <span class="comment">//Creates Rdbs object</span> |
|
325 <a name="l00359"></a>00359 RDbs dbs; |
|
326 <a name="l00360"></a>00360 CleanupClosePushL(dbs); |
|
327 <a name="l00361"></a>00361 dbs.Connect(); |
|
328 <a name="l00362"></a>00362 |
|
329 <a name="l00363"></a>00363 <span class="comment">//Creates a new database with the name specified in the parameter aDbName, </span> |
|
330 <a name="l00364"></a>00364 <span class="comment">//if database with same name is present, it will be replaced. </span> |
|
331 <a name="l00365"></a>00365 RDbNamedDatabase database; |
|
332 <a name="l00366"></a>00366 CleanupClosePushL(database); |
|
333 <a name="l00367"></a>00367 User::LeaveIfError(database.Replace(fsSession, aDbName)); |
|
334 <a name="l00368"></a>00368 database.Close(); |
|
335 <a name="l00369"></a>00369 |
|
336 <a name="l00370"></a>00370 <span class="comment">//Opens the database using the RDbs object</span> |
|
337 <a name="l00371"></a>00371 User::LeaveIfError(database.Open(dbs, aDbName)); |
|
338 <a name="l00372"></a>00372 <span class="comment">//Creates a table definition.</span> |
|
339 <a name="l00373"></a>00373 CDbColSet* columns=CDbColSet::NewLC(); |
|
340 <a name="l00374"></a>00374 |
|
341 <a name="l00375"></a>00375 <span class="comment">//Adds three columns each containing Int32 values. </span> |
|
342 <a name="l00376"></a>00376 _LIT(KCol1, <span class="stringliteral">"Number1"</span>); |
|
343 <a name="l00377"></a>00377 _LIT(KCol2, <span class="stringliteral">"Number2"</span>); |
|
344 <a name="l00378"></a>00378 _LIT(KCol3, <span class="stringliteral">"Number3"</span>); |
|
345 <a name="l00379"></a>00379 columns->AddL(TDbCol(KCol1, EDbColInt32)); |
|
346 <a name="l00380"></a>00380 columns->AddL(TDbCol(KCol2, EDbColInt32)); |
|
347 <a name="l00381"></a>00381 columns->AddL(TDbCol(KCol3, EDbColInt32)); |
|
348 <a name="l00382"></a>00382 |
|
349 <a name="l00383"></a>00383 |
|
350 <a name="l00384"></a>00384 <span class="comment">//Creates the table, table name is "Numbers" and add the columns to it.</span> |
|
351 <a name="l00385"></a>00385 _LIT(KTable, <span class="stringliteral">"Numbers"</span>); |
|
352 <a name="l00386"></a>00386 User::LeaveIfError(database.CreateTable(KTable, *columns)); |
|
353 <a name="l00387"></a>00387 |
|
354 <a name="l00388"></a>00388 CDbKey* key=CDbKey::NewLC(); |
|
355 <a name="l00389"></a>00389 |
|
356 <a name="l00390"></a>00390 <span class="comment">//Add the key columns.</span> |
|
357 <a name="l00391"></a>00391 TDbKeyCol number1(KCol1); |
|
358 <a name="l00392"></a>00392 key->AddL(number1); |
|
359 <a name="l00393"></a>00393 TDbKeyCol number2(KCol2); |
|
360 <a name="l00394"></a>00394 key->AddL(number2); |
|
361 <a name="l00395"></a>00395 TDbKeyCol number3(KCol3); |
|
362 <a name="l00396"></a>00396 key->AddL(number3); |
|
363 <a name="l00397"></a>00397 User::LeaveIfError(database.CreateIndex(KTable, KTable, *key)); |
|
364 <a name="l00398"></a>00398 |
|
365 <a name="l00399"></a>00399 <span class="comment">//Cleans up the column set. </span> |
|
366 <a name="l00400"></a>00400 CleanupStack::PopAndDestroy(key); |
|
367 <a name="l00401"></a>00401 CleanupStack::PopAndDestroy(columns); |
|
368 <a name="l00402"></a>00402 CleanupStack::PopAndDestroy(&database); |
|
369 <a name="l00403"></a>00403 CleanupStack::PopAndDestroy(&dbs); |
|
370 <a name="l00404"></a>00404 CleanupStack::PopAndDestroy(&fsSession); |
|
371 <a name="l00405"></a>00405 } |
|
372 <a name="l00406"></a>00406 |
|
373 <a name="l00411"></a>00411 <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="circularbuffer_8cpp.html#5fb473d4ee18739183215b04dcad6e12">MainL</a>() |
|
374 <a name="l00412"></a>00412 { |
|
375 <a name="l00413"></a>00413 <a class="code" href="class_c_smp_example.html">CSmpExample</a>* smpExample = <a class="code" href="class_c_smp_example.html#af2dab332a98167a1dde6309732a2460">CSmpExample::NewL</a>(); |
|
376 <a name="l00414"></a>00414 CleanupStack::PushL(smpExample); |
|
377 <a name="l00415"></a>00415 smpExample-><a class="code" href="class_c_smp_example.html#8eaf4cbbf9c4b69d0854140963a22e51">PrintMessage</a>(); |
|
378 <a name="l00416"></a>00416 smpExample-><a class="code" href="class_c_smp_example.html#fdb87a66a6b0a41cbea465aa2495aa1c">StartThreads</a>(); |
|
379 <a name="l00417"></a>00417 CleanupStack::PopAndDestroy(smpExample); |
|
380 <a name="l00418"></a>00418 } |
|
381 <a name="l00419"></a>00419 |
|
382 <a name="l00423"></a>00423 <span class="keyword">extern</span> TInt <a class="code" href="dbllist_8cpp.html#0f358e9c4355138f629b8c4f37310295">E32Main</a>() |
|
383 <a name="l00424"></a>00424 { |
|
384 <a name="l00425"></a>00425 <span class="comment">//Creates cleanup stack.</span> |
|
385 <a name="l00426"></a>00426 __UHEAP_MARK; |
|
386 <a name="l00427"></a>00427 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
387 <a name="l00428"></a>00428 <span class="keywordflow">if</span>(!cleanup) |
|
388 <a name="l00429"></a>00429 { |
|
389 <a name="l00430"></a>00430 <span class="keywordflow">return</span> KErrNoMemory; |
|
390 <a name="l00431"></a>00431 } |
|
391 <a name="l00432"></a>00432 <span class="comment">//Run application code inside a TRAP harness.</span> |
|
392 <a name="l00433"></a>00433 TRAPD(mainError, <a class="code" href="circularbuffer_8cpp.html#5fb473d4ee18739183215b04dcad6e12">MainL</a>()); |
|
393 <a name="l00434"></a>00434 <span class="keywordflow">if</span>(mainError != KErrNone) |
|
394 <a name="l00435"></a>00435 { |
|
395 <a name="l00436"></a>00436 _LIT(KUserPanic, <span class="stringliteral">"Main Failed"</span>); |
|
396 <a name="l00437"></a>00437 User::Panic(KUserPanic, mainError); |
|
397 <a name="l00438"></a>00438 } |
|
398 <a name="l00439"></a>00439 <span class="keyword">delete</span> cleanup; |
|
399 <a name="l00440"></a>00440 __UHEAP_MARKEND; |
|
400 <a name="l00441"></a>00441 <span class="keywordflow">return</span> KErrNone; |
|
401 <a name="l00442"></a>00442 } |
|
402 <a name="l00443"></a>00443 |
|
403 </pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Thu Jan 21 10:32:57 2010 for TB10.1 Example Applications by |
|
404 <a href="http://www.doxygen.org/index.html"> |
|
405 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address> |
|
406 </body> |
|
407 </html> |