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 |
#include <pthread.h>
|
|
25 |
#include <unistd.h>
|
|
26 |
#include <errno.h>
|
|
27 |
|
|
28 |
/*******************************************************************************
|
|
29 |
*
|
|
30 |
* Local Includes
|
|
31 |
*
|
|
32 |
******************************************************************************/
|
|
33 |
#include "CAThread.h"
|
|
34 |
|
|
35 |
/*******************************************************************************
|
|
36 |
*
|
|
37 |
* Implementation
|
|
38 |
*
|
|
39 |
******************************************************************************/
|
|
40 |
CAThread::CAThread()
|
|
41 |
{
|
|
42 |
iThreadHandle = 0;
|
|
43 |
iThreadState = TS_INIT;
|
|
44 |
iProc = NULL;
|
|
45 |
}
|
|
46 |
|
|
47 |
|
|
48 |
CAThread::CAThread( string aThreadName )
|
|
49 |
{
|
|
50 |
iThreadHandle = 0;
|
|
51 |
iThreadState = TS_INIT;
|
|
52 |
iProc = NULL;
|
|
53 |
iThreadName = aThreadName;
|
|
54 |
}
|
|
55 |
|
|
56 |
|
|
57 |
CAThread::~CAThread()
|
|
58 |
{
|
|
59 |
assert( iThreadHandle == 0 );
|
|
60 |
assert( iThreadState != TS_ACTIVE );
|
|
61 |
}
|
|
62 |
|
|
63 |
|
|
64 |
/*******************************************************************************
|
|
65 |
*
|
|
66 |
* PUBLIC METHOD: StartThread
|
|
67 |
*
|
|
68 |
******************************************************************************/
|
|
69 |
TThreadError CAThread::StartThread( void *aStartProc, void *aArg, int *aSystemSpecificError )
|
|
70 |
{
|
|
71 |
int err;
|
|
72 |
|
|
73 |
// check params
|
|
74 |
assert( aSystemSpecificError != NULL );
|
|
75 |
*aSystemSpecificError = 0;
|
|
76 |
|
|
77 |
// check state
|
|
78 |
if( iThreadState != TS_INIT ) {
|
|
79 |
return TE_INVALIDSTATE;
|
|
80 |
}
|
|
81 |
|
|
82 |
// NOTE: we use the handle as a replacement for pthread_t. This is OK since they are both
|
|
83 |
// 4-bytes integers -- however -- this obviously breaks the abstraction of the pthread_t
|
|
84 |
// type so I validate this assumption here.
|
|
85 |
assert( sizeof(pthread_t) == sizeof(HANDLE) );
|
|
86 |
|
|
87 |
// create the thread
|
|
88 |
err = pthread_create( (pthread_t*)&iThreadHandle, NULL, (void*(*)(void*))aStartProc, aArg );
|
|
89 |
iProc = aStartProc;
|
|
90 |
|
|
91 |
// handle errors
|
|
92 |
if( err != 0 ) {
|
|
93 |
*aSystemSpecificError = errno;
|
|
94 |
return TE_ERROR;
|
|
95 |
}
|
|
96 |
|
|
97 |
// done
|
|
98 |
iThreadState = TS_ACTIVE;
|
|
99 |
return TE_NONE;
|
|
100 |
}
|
|
101 |
|
|
102 |
|
|
103 |
/*******************************************************************************
|
|
104 |
*
|
|
105 |
* PUBLIC METHOD: WaitForThread - To implement this method using pthreads we
|
|
106 |
* use two functions. pthread_getschedparam is a non-blocking call that returns
|
|
107 |
* whether the thread is still active - pthread_join waits infinitely for the
|
|
108 |
* thread to complete. At the moment this implementation doesn't support wait
|
|
109 |
* periods other than zero and infinite.
|
|
110 |
*
|
|
111 |
******************************************************************************/
|
|
112 |
TThreadError CAThread::WaitForThread( int aTimeout )
|
|
113 |
{
|
|
114 |
int err, thread_return_value;
|
|
115 |
int policy;
|
|
116 |
struct sched_param param;
|
|
117 |
|
|
118 |
// check the state
|
|
119 |
if( iThreadState != TS_ACTIVE ) {
|
|
120 |
return TE_INVALIDSTATE;
|
|
121 |
}
|
|
122 |
|
|
123 |
// check the param
|
|
124 |
assert( (aTimeout == 0) || (aTimeout == -1) );
|
|
125 |
|
|
126 |
// in the non-blocking case we check whether the thread is active, if it is then we just return, otherwise, we then join with it.
|
|
127 |
if( aTimeout == 0 ) {
|
|
128 |
err = pthread_getschedparam( iThreadHandle, &policy, ¶m );
|
|
129 |
if( err == 0 ) {
|
|
130 |
return TE_TIMEOUT;
|
|
131 |
} else if( err != ESRCH ) {
|
|
132 |
return TE_ERROR;
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
// we are either waiting indefinitely, or, we are waiting non-blocking but have just worked out that the thread is not running anymore
|
|
137 |
err = pthread_join( iThreadHandle, (void**)&thread_return_value );
|
|
138 |
assert( err == 0 );
|
|
139 |
|
|
140 |
// cleanup our state
|
|
141 |
iThreadHandle = 0;
|
|
142 |
iThreadState = TS_DONE;
|
|
143 |
return TE_NONE;
|
|
144 |
}
|
|
145 |
|
|
146 |
|
|
147 |
/*******************************************************************************
|
|
148 |
*
|
|
149 |
* PUBLIC METHOD: GetThreadState
|
|
150 |
*
|
|
151 |
******************************************************************************/
|
|
152 |
TThreadState CAThread::GetThreadState()
|
|
153 |
{
|
|
154 |
return iThreadState;
|
|
155 |
}
|