0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 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 |
* System Includes
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
#include <stdio.h>
|
|
22 |
#include <stdlib.h>
|
|
23 |
#include <assert.h>
|
|
24 |
|
|
25 |
/*******************************************************************************
|
|
26 |
*
|
|
27 |
* Local Includes
|
|
28 |
*
|
|
29 |
******************************************************************************/
|
|
30 |
#include "CAThread.h"
|
|
31 |
|
|
32 |
/*******************************************************************************
|
|
33 |
*
|
|
34 |
* Implementation
|
|
35 |
*
|
|
36 |
******************************************************************************/
|
|
37 |
CAThread::CAThread()
|
|
38 |
{
|
|
39 |
iThreadHandle = NULL;
|
|
40 |
iThreadState = TS_INIT;
|
|
41 |
iProc = NULL;
|
|
42 |
}
|
|
43 |
|
|
44 |
|
|
45 |
CAThread::CAThread( string aThreadName )
|
|
46 |
{
|
|
47 |
iThreadHandle = NULL;
|
|
48 |
iThreadState = TS_INIT;
|
|
49 |
iProc = NULL;
|
|
50 |
iThreadName = aThreadName;
|
|
51 |
}
|
|
52 |
|
|
53 |
|
|
54 |
CAThread::~CAThread()
|
|
55 |
{
|
|
56 |
assert( iThreadHandle == NULL );
|
|
57 |
assert( iThreadState != TS_ACTIVE );
|
|
58 |
}
|
|
59 |
|
|
60 |
|
|
61 |
TThreadError CAThread::StartThread( void *aStartProc, void *aArg, int *aSystemSpecificError )
|
|
62 |
{
|
|
63 |
// check params
|
|
64 |
assert( aSystemSpecificError != NULL );
|
|
65 |
*aSystemSpecificError = 0;
|
|
66 |
|
|
67 |
// create the thread
|
|
68 |
iThreadHandle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)aStartProc, aArg, 0, NULL );
|
|
69 |
iProc = aStartProc;
|
|
70 |
|
|
71 |
// handle errors
|
|
72 |
if( iThreadHandle == NULL ) {
|
|
73 |
*aSystemSpecificError = GetLastError();
|
|
74 |
return TE_ERROR;
|
|
75 |
}
|
|
76 |
|
|
77 |
// done
|
|
78 |
iThreadState = TS_ACTIVE;
|
|
79 |
return TE_NONE;
|
|
80 |
}
|
|
81 |
|
|
82 |
|
|
83 |
TThreadError CAThread::WaitForThread( int aTimeout )
|
|
84 |
{
|
|
85 |
int err;
|
|
86 |
|
|
87 |
// wait for the object
|
|
88 |
err = WaitForSingleObject( iThreadHandle, aTimeout );
|
|
89 |
|
|
90 |
// handle the case that we got it
|
|
91 |
if( err == WAIT_OBJECT_0 ) {
|
|
92 |
iThreadState = TS_DONE;
|
|
93 |
iThreadHandle = NULL;
|
|
94 |
return TE_NONE;
|
|
95 |
}
|
|
96 |
|
|
97 |
// handle a timeout
|
|
98 |
if( err == WAIT_TIMEOUT ) {
|
|
99 |
return TE_TIMEOUT;
|
|
100 |
}
|
|
101 |
|
|
102 |
// handle all other responses
|
|
103 |
assert( !"INVALID CODE PATH" );
|
|
104 |
return TE_NONE;
|
|
105 |
}
|
|
106 |
|
|
107 |
|
|
108 |
TThreadState CAThread::GetThreadState()
|
|
109 |
{
|
|
110 |
return iThreadState;
|
|
111 |
}
|