|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef CWORKER_H_ |
|
19 #define CWORKER_H_ |
|
20 |
|
21 #include <e32base.h> |
|
22 |
|
23 |
|
24 #define KTermDeviation 100 // The bigger this word, the more different terms |
|
25 #define KPrefixDeviation 20 // Pick prefixes more moderately |
|
26 #define KTermCountDeviation 10 // The bigger this word, the more terms per document |
|
27 _LIT( KContentsField, "Contents" ); |
|
28 _LIT( KNormalIndexDirectory, "c:\\data\\indexing\\indexdb\\root\\test\\normal" ); |
|
29 _LIT( KBusyIndexDirectory, "c:\\data\\indexing\\indexdb\\root\\test\\busy"); |
|
30 |
|
31 _LIT( KTestBaseAppClass, "root test" ); |
|
32 _LIT( KNormalBaseAppClass, "root test normal"); |
|
33 _LIT( KBusyBaseAppClass, "root test busy"); |
|
34 |
|
35 _LIT( KNormalVolume, "@df:root test normal"); |
|
36 _LIT( KBusyVolume, "@df:root test busy"); |
|
37 |
|
38 /** |
|
39 * Returns random numbers so, that small numbers |
|
40 * are most common. Used to approximate the distribution |
|
41 * of real world worlds. In the distribution the probability that n is |
|
42 * one of the numbers following i is 9 times as probable as that n is i. |
|
43 */ |
|
44 TInt LogRand( TInt aDeviation ); |
|
45 |
|
46 void SleepRandom( TInt aAverageMs ); |
|
47 |
|
48 /** |
|
49 * Base class for workers. Workers do some kind of activity |
|
50 * (~indexing or searching) in their own slave thread. |
|
51 */ |
|
52 class CWorker : public CBase |
|
53 { |
|
54 public: |
|
55 |
|
56 CWorker(); |
|
57 |
|
58 void ConstructL(); |
|
59 |
|
60 ~CWorker(); |
|
61 |
|
62 public: |
|
63 |
|
64 TInt WorkerId(); |
|
65 |
|
66 public: // Error handling |
|
67 |
|
68 TInt Error(); |
|
69 |
|
70 const TTime& ExitTime(); |
|
71 |
|
72 TBool IsReported(); |
|
73 |
|
74 void SetReported(); |
|
75 |
|
76 public: // Thread life cycle handling |
|
77 |
|
78 /** |
|
79 * Returns true, if TryFinish() has been called successfully |
|
80 */ |
|
81 TBool IsActive(); |
|
82 |
|
83 /** |
|
84 * Starts |
|
85 */ |
|
86 void StartL(); |
|
87 |
|
88 /** |
|
89 * Signals the thread to shut down. The thread will shut down |
|
90 * after finishing its current task. |
|
91 */ |
|
92 void Cancel(); |
|
93 |
|
94 /** |
|
95 * Attempts to 'join' the thread. Returns error code, if |
|
96 * joining failed. NOTE: unlike posix 'join', this method |
|
97 * does not return the thread 'exit-value'. Instead use |
|
98 * Error to see, if an error occurred during thread |
|
99 * execution. |
|
100 * |
|
101 * NOTE: Always call cancel before attempting to join |
|
102 * the thread. |
|
103 */ |
|
104 TInt TryJoin( TInt aMicroSeconds ); |
|
105 |
|
106 /** |
|
107 * Terminates the thread forcefully |
|
108 */ |
|
109 void Terminate(); |
|
110 |
|
111 public: // Lock usage (For accessing the worker private data) |
|
112 |
|
113 void Lock(); |
|
114 |
|
115 TInt TryLock( TInt aMicroSeconds ); |
|
116 |
|
117 void Unlock(); |
|
118 |
|
119 public: |
|
120 |
|
121 virtual const TDesC& Name()= 0; |
|
122 |
|
123 protected: // For internal usage |
|
124 |
|
125 void Wait( TReal aSeconds ); |
|
126 |
|
127 protected: |
|
128 |
|
129 /** |
|
130 * For doing any required preparations in the original thread. |
|
131 */ |
|
132 virtual void DoPrepareL() = 0; |
|
133 virtual void DoCancel() = 0; |
|
134 virtual void DoRunL() = 0; |
|
135 |
|
136 void RunL(); |
|
137 |
|
138 public: |
|
139 |
|
140 static TInt ThreadFunction(void *aThis); |
|
141 |
|
142 private: |
|
143 |
|
144 TInt iWorkerId; |
|
145 TInt iError; |
|
146 TTime iExitTime; |
|
147 TBool iReported; |
|
148 static TInt iNextFreeWorkerId; |
|
149 TBool iActive; |
|
150 RThread iThread; |
|
151 |
|
152 /** |
|
153 * This lock guards the worker thread life cycle. Before |
|
154 * exiting, the worker thread will signal on this semaphore. |
|
155 * |
|
156 * NOTE: Logon could be used instead. |
|
157 */ |
|
158 RSemaphore iSemaphore; |
|
159 |
|
160 /** |
|
161 * This lock guards the internal state variables of this |
|
162 * worker. NOTE: The internal state variables are defined |
|
163 * in the inherited classes. For example the statistics are |
|
164 * guarded by this semaphore. |
|
165 */ |
|
166 RSemaphore iLock; |
|
167 }; |
|
168 |
|
169 |
|
170 #endif /* CWORKER_H_ */ |