/[resiprocate]/main/sip/resiprocate/os/ThreadIf.cxx
ViewVC logotype

Contents of /main/sip/resiprocate/os/ThreadIf.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4601 - (show annotations) (download)
Thu May 12 01:10:49 2005 UTC (14 years, 6 months ago) by derek
File size: 5568 byte(s)
merged identity branch(4598) into main--removing identity branch after this
1
2 #if defined(WIN32)
3 #include <stdio.h>
4 #include <tchar.h>
5 #include <time.h>
6 //from Random.cxx
7 #include "resiprocate/os/Socket.hxx"
8 #endif
9
10 #include <cassert>
11 #include <iostream>
12 #include "resiprocate/os/ThreadIf.hxx"
13 #include "resiprocate/os/Mutex.hxx"
14 #include "resiprocate/os/Lock.hxx"
15 #include "resiprocate/os/Socket.hxx"
16 #include "resiprocate/os/Logger.hxx"
17
18 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
19
20 using namespace resip;
21
22 extern "C"
23 {
24 static void*
25 threadWrapper( void* threadParm )
26 {
27 assert( threadParm );
28 ThreadIf* t = static_cast < ThreadIf* > ( threadParm );
29
30 assert( t );
31 #if defined(WIN32)
32 srand(unsigned(time(0)) ^ unsigned(GetCurrentThreadId()) ^ unsigned(GetCurrentProcessId()));
33 #endif
34 t->thread();
35 #if defined(WIN32)
36 ExitThread( 0 );
37 #endif
38 return 0;
39 }
40 }
41
42 ThreadIf::ThreadIf() : mId(0), mShutdown(false), mShutdownMutex()
43 {
44 }
45
46
47 ThreadIf::~ThreadIf()
48 {
49 shutdown();
50 join();
51 }
52
53 void
54 ThreadIf::run()
55 {
56 assert(mId == 0);
57
58 #if defined(WIN32)
59 mThread = CreateThread(
60 NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
61 0, // DWORD dwStackSize, // initial thread stack size
62 (LPTHREAD_START_ROUTINE)threadWrapper, // LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
63 this, //LPVOID lpParameter, // argument for new thread
64 0, //DWORD dwCreationFlags, // creation flags
65 &mId// LPDWORD lpThreadId // pointer to receive thread ID
66 );
67 assert( mThread != 0 );
68 #else
69 // spawn the thread
70 if ( int retval = pthread_create( &mId, 0, threadWrapper, this) )
71 {
72 std::cerr << "Failed to spawn thread: " << retval << std::endl;
73 assert(0);
74 // TODO - ADD LOGING HERE
75 }
76 #endif
77 }
78
79 void
80 ThreadIf::join()
81 {
82 // !kh!
83 // perhaps assert instead of returning when join()ed already?
84 // programming error?
85 //assert(mId == 0);
86
87 if (mId == 0)
88 {
89 return;
90 }
91
92 #if defined(WIN32)
93 DWORD exitCode;
94 while (true)
95 {
96 if (GetExitCodeThread(mThread,&exitCode) != 0)
97 {
98 if (exitCode != STILL_ACTIVE)
99 {
100 break;
101 }
102 else
103 {
104 WaitForSingleObject(mThread,INFINITE);
105 }
106 }
107 else
108 {
109 // log something here
110 break;
111 }
112 }
113
114 // !kh!
115 CloseHandle(mThread);
116 #else
117 void* stat;
118 if (mId != pthread_self())
119 {
120 int r = pthread_join( mId , &stat );
121 if ( r != 0 )
122 {
123 InfoLog( << "pthread_join() returned " << r );
124 assert(0);
125 // TODO
126 }
127 }
128
129 #endif
130
131 mId = 0;
132 }
133 #if !defined(WIN32)
134 ThreadIf::Id
135 ThreadIf::selfId()
136 {
137 return pthread_self();
138 }
139 #endif
140
141 void
142 ThreadIf::shutdown()
143 {
144 Lock lock(mShutdownMutex);
145 if (!mShutdown)
146 {
147 mShutdown = true;
148 mShutdownCondition.signal();
149 }
150 }
151
152 bool
153 ThreadIf::waitForShutdown(int ms) const
154 {
155 Lock lock(mShutdownMutex);
156 mShutdownCondition.wait(mShutdownMutex, ms);
157 return mShutdown;
158 }
159
160 bool
161 ThreadIf::isShutdown() const
162 {
163 Lock lock(mShutdownMutex);
164 (void)lock;
165 return ( mShutdown );
166 }
167
168
169 // End of File
170
171 /* ====================================================================
172 * The Vovida Software License, Version 1.0
173 *
174 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
175 *
176 * Redistribution and use in source and binary forms, with or without
177 * modification, are permitted provided that the following conditions
178 * are met:
179 *
180 * 1. Redistributions of source code must retain the above copyright
181 * notice, this list of conditions and the following disclaimer.
182 *
183 * 2. Redistributions in binary form must reproduce the above copyright
184 * notice, this list of conditions and the following disclaimer in
185 * the documentation and/or other materials provided with the
186 * distribution.
187 *
188 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
189 * and "Vovida Open Communication Application Library (VOCAL)" must
190 * not be used to endorse or promote products derived from this
191 * software without prior written permission. For written
192 * permission, please contact vocal@vovida.org.
193 *
194 * 4. Products derived from this software may not be called "VOCAL", nor
195 * may "VOCAL" appear in their name, without prior written
196 * permission of Vovida Networks, Inc.
197 *
198 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
199 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
200 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
201 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
202 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
203 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
204 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
205 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
206 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
207 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
208 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
209 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
210 * DAMAGE.
211 *
212 * ====================================================================
213 *
214 * This software consists of voluntary contributions made by Vovida
215 * Networks, Inc. and many individuals on behalf of Vovida Networks,
216 * Inc. For more information on Vovida Networks, Inc., please see
217 * <http://www.vovida.org/>.
218 *
219 */

Properties

Name Value
svn:eol-style LF

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27