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