1 /********************************************************************
3 * Copyright (c) 1999-2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 # ifndef _INCLUDE_POSIX_SOURCE
9 # define _INCLUDE_POSIX_SOURCE
13 #include "simplethread.h"
15 #include "unicode/utypes.h"
16 #include "unicode/ustring.h"
21 #include "unicode/resbund.h"
22 #include "unicode/udata.h"
23 #include "unicode/uloc.h"
24 #include "unicode/locid.h"
29 #include <ctype.h> // tolower, toupper
31 #if !defined(U_WINDOWS) && !defined(XP_MAC) && !defined(U_RHAPSODY)
35 /* Needed by z/OS to get usleep */
39 #define _XOPEN_SOURCE_EXTENDED 1
44 /*#include "platform_xopen_source_extended.h"*/
47 #if defined(POSIX) || defined(U_SOLARIS) || defined(U_AIX) || defined(U_HPUX)
50 #if (ICU_USE_THREADS == 1)
54 #if defined(__hpux) && defined(HPUX_CMA)
55 # if defined(read) // read being defined as cma_read causes trouble with iostream::read
60 /* Define __EXTENSIONS__ for Solaris and old friends in strict mode. */
61 #ifndef __EXTENSIONS__
62 #define __EXTENSIONS__
66 #include <sys/types.h>
73 /* Define _XPG4_2 for Solaris and friends. */
78 /* Define __USE_XOPEN_EXTENDED for Linux and glibc. */
79 #ifndef __USE_XOPEN_EXTENDED
80 #define __USE_XOPEN_EXTENDED
83 /* Define _INCLUDE_XOPEN_SOURCE_EXTENDED for HP/UX (11?). */
84 #ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED
85 #define _INCLUDE_XOPEN_SOURCE_EXTENDED
97 #if (ICU_USE_THREADS==0)
98 SimpleThread::SimpleThread()
101 SimpleThread::~SimpleThread()
105 SimpleThread::start()
113 SimpleThread::sleep(int32_t millis
)
117 SimpleThread::isRunning() {
122 #include "unicode/putil.h"
125 #include "unicode/numfmt.h"
126 #include "unicode/choicfmt.h"
127 #include "unicode/msgfmt.h"
128 #include "unicode/locid.h"
129 #include "unicode/ucol.h"
130 #include "unicode/calendar.h"
136 # define VC_EXTRALEAN
137 # define WIN32_LEAN_AND_MEAN
145 //-----------------------------------------------------------------------------------
147 // class SimpleThread Windows Implementation
149 //-----------------------------------------------------------------------------------
150 struct Win32ThreadImplementation
153 unsigned int fThreadID
;
157 extern "C" unsigned int __stdcall
SimpleThreadProc(void *arg
)
159 ((SimpleThread
*)arg
)->run();
163 SimpleThread::SimpleThread()
166 Win32ThreadImplementation
*imp
= new Win32ThreadImplementation
;
168 fImplementation
= imp
;
171 SimpleThread::~SimpleThread()
173 // Destructor. Because we start the thread running with _beginthreadex(),
174 // we own the Windows HANDLE for the thread and must
176 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
178 if (imp
->fHandle
!= 0) {
179 CloseHandle(imp
->fHandle
);
183 delete (Win32ThreadImplementation
*)fImplementation
;
186 int32_t SimpleThread::start()
188 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
189 if(imp
->fHandle
!= NULL
) {
190 // The thread appears to have already been started.
191 // This is probably an error on the part of our caller.
195 imp
->fHandle
= (HANDLE
) _beginthreadex(
197 0x20000, // Stack Size
198 SimpleThreadProc
, // Function to Run
199 (void *)this, // Arg List
200 0, // initflag. Start running, not suspended
201 &imp
->fThreadID
// thraddr
204 if (imp
->fHandle
== 0) {
216 UBool
SimpleThread::isRunning() {
218 // Test whether the thread associated with the SimpleThread object is
219 // still actually running.
221 // NOTE: on Win64 on Itanium processors, a crashes
222 // occur if the main thread of a process exits concurrently with some
223 // other thread(s) exiting. To avoid the possibility, we wait until the
224 // OS indicates that all threads have terminated, rather than waiting
225 // only until the end of the user's Run function has been reached.
227 // I don't know whether the crashes represent a Windows bug, or whether
228 // main() programs are supposed to have to wait for their threads.
230 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
233 DWORD threadExitCode
;
235 if (imp
->fHandle
== 0) {
236 // No handle, thread must not be running.
239 success
= GetExitCodeThread(imp
->fHandle
, &threadExitCode
) != 0;
241 // Can't get status, thread must not be running.
244 return (threadExitCode
== STILL_ACTIVE
);
248 void SimpleThread::sleep(int32_t millis
)
253 //-----------------------------------------------------------------------------------
255 // class SimpleThread NULL Implementation
257 //-----------------------------------------------------------------------------------
260 // since the Mac has no preemptive threading (at least on MacOS 8), only
261 // cooperative threading, threads are a no-op. We have no yield() calls
262 // anywhere in the ICU, so we are guaranteed to be thread-safe.
266 SimpleThread::SimpleThread()
269 SimpleThread::~SimpleThread()
273 SimpleThread::start()
281 SimpleThread::sleep(int32_t millis
)
285 SimpleThread::isRunning() {
292 //-----------------------------------------------------------------------------------
294 // class SimpleThread POSIX implementation
296 // A note on the POSIX vs the Windows implementations of this class..
297 // On Windows, the main thread must verify that other threads have finished
298 // before exiting, or crashes occasionally occur. (Seen on Itanium Win64 only)
299 // The function SimpleThread::isRunning() is used for this purpose.
301 // On POSIX, there is NO reliable non-blocking mechanism to determine
302 // whether a thread has exited. pthread_kill(thread, 0) almost works,
303 // but the system can recycle thread ids immediately, so seeing that a
304 // thread exists with this call could mean that the original thread has
305 // finished and a new one started with the same ID. Useless.
307 // So we need to do the check with user code, by setting a flag just before
308 // the thread function returns. A technique that is guaranteed to fail
309 // on Windows, because it indicates that the thread is done before all
310 // system level cleanup has happened.
312 //-----------------------------------------------------------------------------------
313 #if defined(POSIX)||defined(U_SOLARIS)||defined(U_AIX)||defined(U_HPUX)
316 struct PosixThreadImplementation
320 UBool fRan
; // True if the thread was successfully started
323 extern "C" void* SimpleThreadProc(void *arg
)
325 // This is the code that is run in the new separate thread.
326 SimpleThread
*This
= (SimpleThread
*)arg
;
327 This
->run(); // Run the user code.
329 // The user function has returned. Set the flag indicating that this thread
330 // is done. Need a mutex for memory barrier purposes only, so that other thread
331 // will reliably see that the flag has changed.
332 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)This
->fImplementation
;
334 imp
->fRunning
= FALSE
;
339 SimpleThread::SimpleThread()
341 PosixThreadImplementation
*imp
= new PosixThreadImplementation
;
342 imp
->fRunning
= FALSE
;
344 fImplementation
= imp
;
347 SimpleThread::~SimpleThread()
349 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
351 pthread_join(imp
->fThread
, NULL
);
354 fImplementation
= (void *)0xdeadbeef;
357 int32_t SimpleThread::start()
360 static pthread_attr_t attr
;
361 static UBool attrIsInitialized
= FALSE
;
363 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
364 imp
->fRunning
= TRUE
;
368 if (attrIsInitialized
== FALSE
) {
369 rc
= pthread_attr_create(&attr
);
370 attrIsInitialized
= TRUE
;
372 rc
= pthread_create(&(imp
->fThread
),attr
,&SimpleThreadProc
,(void*)this);
374 if (attrIsInitialized
== FALSE
) {
375 rc
= pthread_attr_init(&attr
);
378 int detachstate
= 0; // jdc30: detach state of zero causes
379 //threads created with this attr to be in
380 //an undetached state. An undetached
381 //thread will keep its resources after
383 pthread_attr_setdetachstate(&attr
, &detachstate
);
386 // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
387 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_JOINABLE
);
389 attrIsInitialized
= TRUE
;
391 rc
= pthread_create(&(imp
->fThread
),&attr
,&SimpleThreadProc
,(void*)this);
395 // some kind of error occured, the thread did not start.
397 imp
->fRunning
= FALSE
;
405 SimpleThread::isRunning() {
406 // Note: Mutex functions are used here not for synchronization,
407 // but to force memory barriors to exist, to ensure that one thread
408 // can see changes made by another when running on processors
409 // with memory models having weak coherency.
410 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
412 UBool retVal
= imp
->fRunning
;
418 void SimpleThread::sleep(int32_t millis
)
425 cma_sleep(millis
/100);
426 #elif defined(U_HPUX) || defined(OS390)
428 while(millis
>= 1000000) {
436 usleep(millis
* 1000);
445 #error No implementation for threads! Cannot test.
449 //-------------------------------------------------------------------------------------------
451 // class ThreadWithStatus - a thread that we can check the status and error condition of
453 //-------------------------------------------------------------------------------------------
454 class ThreadWithStatus
: public SimpleThread
457 UBool
getError() { return (fErrors
> 0); }
458 UBool
getError(UnicodeString
& fillinError
) { fillinError
= fErrorString
; return (fErrors
> 0); }
459 virtual ~ThreadWithStatus(){}
461 ThreadWithStatus() : fErrors(0) {}
462 void error(const UnicodeString
&error
) {
463 fErrors
++; fErrorString
= error
;
464 SimpleThread::errorFunc();
466 void error() { error("An error occured."); }
469 UnicodeString fErrorString
;
472 #endif // ICU_USE_THREADS