|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Contains the CThreadExec class definition. |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalTechnology |
|
21 */ |
|
22 |
|
23 #include "cthreadexec.h" |
|
24 #include "cctsytestlogging.h" |
|
25 |
|
26 CThreadExec* CThreadExec::NewL() |
|
27 { |
|
28 TEST_FRAMEWORK_LOG1(_L("CThreadExec::NewL")); |
|
29 CThreadExec* self = new(ELeave) CThreadExec(); |
|
30 CleanupStack::PushL(self); |
|
31 self->ConstructL(); |
|
32 CleanupStack::Pop(); |
|
33 return self; |
|
34 } |
|
35 |
|
36 CThreadExec::CThreadExec() |
|
37 { |
|
38 } |
|
39 |
|
40 void CThreadExec::ConstructL() |
|
41 { |
|
42 TEST_FRAMEWORK_LOG1(_L("CThreadExec::ConstructL")); |
|
43 iActiveFnExec = CActiveFnExec::NewL(*this); |
|
44 CActiveScheduler::Add(iActiveFnExec); |
|
45 |
|
46 iExecSemaphore.CreateLocal(0); |
|
47 iActiveFnExec->Start(); |
|
48 } |
|
49 |
|
50 CThreadExec::~CThreadExec() |
|
51 { |
|
52 TEST_FRAMEWORK_LOG1(_L("CThreadExec::~CThreadExec")); |
|
53 iActiveFnExec->Cancel(); |
|
54 delete iActiveFnExec; |
|
55 } |
|
56 |
|
57 TInt CThreadExec::Exec(MFunctor* aFunction) |
|
58 { |
|
59 TEST_FRAMEWORK_LOG1(_L("CThreadExec::Exec")); |
|
60 iFunction = aFunction; |
|
61 iNeedSignal = ETrue; |
|
62 iActiveFnExec->Exec(); |
|
63 // wait for the function to terminate |
|
64 iExecSemaphore.Wait(); |
|
65 return iLatestError; |
|
66 } |
|
67 |
|
68 void CThreadExec::DoExec() |
|
69 { |
|
70 TEST_FRAMEWORK_LOG1(_L("CThreadExec::DoExec")); |
|
71 // run the exec function |
|
72 iLatestError = KErrNone; |
|
73 TRAP(iLatestError, iFunction->ExecL()); |
|
74 // signal called it's done |
|
75 if(iNeedSignal) |
|
76 { |
|
77 iExecSemaphore.Signal(); |
|
78 } |
|
79 } |
|
80 |
|
81 void CThreadExec::AsyncExec(MFunctor* aFunction) |
|
82 /** |
|
83 * Execute a function asynchronously. |
|
84 * This will execute the function in another thread, without waiting for completion (while the Exec will do the same, but the it will wait for the function completion). |
|
85 * @param aFunction the function to execute |
|
86 */ |
|
87 { |
|
88 iFunction = aFunction; |
|
89 iNeedSignal = EFalse; |
|
90 iActiveFnExec->Exec(); |
|
91 } |
|
92 |
|
93 CThreadExec::CActiveFnExec* CThreadExec::CActiveFnExec::NewL(CThreadExec& aOwner) |
|
94 { |
|
95 TEST_FRAMEWORK_LOG1(_L("CThreadExec::CActiveFnExec::NewL")); |
|
96 CActiveFnExec* self = new(ELeave) CActiveFnExec(aOwner); |
|
97 return self; |
|
98 } |
|
99 |
|
100 CThreadExec::CActiveFnExec::CActiveFnExec(CThreadExec& aOwner) |
|
101 : CActive(CActive::EPriorityHigh),iOwner(aOwner) |
|
102 { |
|
103 TEST_FRAMEWORK_LOG1(_L("CThreadExec::CActiveFnExec::CActiveFnExec")); |
|
104 // open a reference to the current thread |
|
105 iThread.Open(RThread().Id()); |
|
106 } |
|
107 |
|
108 void CThreadExec::CActiveFnExec::Start() |
|
109 { |
|
110 TEST_FRAMEWORK_LOG1(_L("CThreadExec::CActiveFnExec::Start")); |
|
111 iStatus = KRequestPending; |
|
112 SetActive(); |
|
113 } |
|
114 |
|
115 void CThreadExec::CActiveFnExec::Exec() |
|
116 { |
|
117 TEST_FRAMEWORK_LOG1(_L("CThreadExec::CActiveFnExec::Exec")); |
|
118 // signal the main thread |
|
119 TRequestStatus* reqStatus = &iStatus; |
|
120 iThread.RequestComplete(reqStatus, KErrNone); |
|
121 } |
|
122 |
|
123 void CThreadExec::CActiveFnExec::RunL() |
|
124 { |
|
125 TEST_FRAMEWORK_LOG1(_L("CThreadExec::CActiveFnExec::RunL")); |
|
126 iOwner.DoExec(); |
|
127 iStatus = KRequestPending; |
|
128 SetActive(); |
|
129 } |
|
130 |
|
131 void CThreadExec::CActiveFnExec::DoCancel() |
|
132 { |
|
133 TEST_FRAMEWORK_LOG1(_L("CThreadExec::CActiveFnExec::DoCancel")); |
|
134 TRequestStatus* reqStatus = &iStatus; |
|
135 User::RequestComplete(reqStatus, KErrCancel); |
|
136 } |
|
137 |