1 /********************************************************************
3 * Copyright (c) 1999-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 # ifndef _INCLUDE_POSIX_SOURCE
9 # define _INCLUDE_POSIX_SOURCE
13 /* Define __EXTENSIONS__ for Solaris and old friends in strict mode. */
14 #ifndef __EXTENSIONS__
15 #define __EXTENSIONS__
18 // Defines _XOPEN_SOURCE for access to POSIX functions.
19 // Must be before any other #includes.
20 #include "uposixdefs.h"
22 #include "simplethread.h"
24 #include "unicode/utypes.h"
25 #include "unicode/ustring.h"
30 #include "unicode/resbund.h"
31 #include "unicode/udata.h"
32 #include "unicode/uloc.h"
33 #include "unicode/locid.h"
39 #include <ctype.h> // tolower, toupper
41 #if U_PLATFORM_USES_ONLY_WIN32_API
42 /* Prefer native Windows APIs even if POSIX is implemented (i.e., on Cygwin). */
44 #elif U_PLATFORM_IMPLEMENTS_POSIX
50 /* Needed by z/OS to get usleep */
51 #if U_PLATFORM == U_PF_OS390
63 #if (ICU_USE_THREADS == 1)
67 #if defined(__hpux) && defined(HPUX_CMA)
68 # if defined(read) // read being defined as cma_read causes trouble with iostream::read
73 #if U_PLATFORM == U_PF_OS390
74 #include <sys/types.h>
77 #if U_PLATFORM != U_PF_OS390
81 /* Define _XPG4_2 for Solaris and friends. */
86 /* Define __USE_XOPEN_EXTENDED for Linux and glibc. */
87 #ifndef __USE_XOPEN_EXTENDED
88 #define __USE_XOPEN_EXTENDED
91 /* Define _INCLUDE_XOPEN_SOURCE_EXTENDED for HP/UX (11?). */
92 #ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED
93 #define _INCLUDE_XOPEN_SOURCE_EXTENDED
105 #if (ICU_USE_THREADS==0)
106 SimpleThread::SimpleThread()
109 SimpleThread::~SimpleThread()
113 SimpleThread::start()
121 SimpleThread::sleep(int32_t millis
)
125 SimpleThread::isRunning() {
130 #include "unicode/putil.h"
133 #include "unicode/numfmt.h"
134 #include "unicode/choicfmt.h"
135 #include "unicode/msgfmt.h"
136 #include "unicode/locid.h"
137 #include "unicode/ucol.h"
138 #include "unicode/calendar.h"
141 #if U_PLATFORM_USES_ONLY_WIN32_API
144 # define VC_EXTRALEAN
145 # define WIN32_LEAN_AND_MEAN
153 //-----------------------------------------------------------------------------------
155 // class SimpleThread Windows Implementation
157 //-----------------------------------------------------------------------------------
158 struct Win32ThreadImplementation
161 unsigned int fThreadID
;
165 extern "C" unsigned int __stdcall
SimpleThreadProc(void *arg
)
167 ((SimpleThread
*)arg
)->run();
171 SimpleThread::SimpleThread()
174 Win32ThreadImplementation
*imp
= new Win32ThreadImplementation
;
176 fImplementation
= imp
;
179 SimpleThread::~SimpleThread()
181 // Destructor. Because we start the thread running with _beginthreadex(),
182 // we own the Windows HANDLE for the thread and must
184 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
186 if (imp
->fHandle
!= 0) {
187 CloseHandle(imp
->fHandle
);
191 delete (Win32ThreadImplementation
*)fImplementation
;
194 int32_t SimpleThread::start()
196 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
197 if(imp
->fHandle
!= NULL
) {
198 // The thread appears to have already been started.
199 // This is probably an error on the part of our caller.
203 imp
->fHandle
= (HANDLE
) _beginthreadex(
205 0x20000, // Stack Size
206 SimpleThreadProc
, // Function to Run
207 (void *)this, // Arg List
208 0, // initflag. Start running, not suspended
209 &imp
->fThreadID
// thraddr
212 if (imp
->fHandle
== 0) {
224 UBool
SimpleThread::isRunning() {
226 // Test whether the thread associated with the SimpleThread object is
227 // still actually running.
229 // NOTE: on Win64 on Itanium processors, a crashes
230 // occur if the main thread of a process exits concurrently with some
231 // other thread(s) exiting. To avoid the possibility, we wait until the
232 // OS indicates that all threads have terminated, rather than waiting
233 // only until the end of the user's Run function has been reached.
235 // I don't know whether the crashes represent a Windows bug, or whether
236 // main() programs are supposed to have to wait for their threads.
238 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
241 DWORD threadExitCode
;
243 if (imp
->fHandle
== 0) {
244 // No handle, thread must not be running.
247 success
= GetExitCodeThread(imp
->fHandle
, &threadExitCode
) != 0;
249 // Can't get status, thread must not be running.
252 return (threadExitCode
== STILL_ACTIVE
);
256 void SimpleThread::sleep(int32_t millis
)
261 //-----------------------------------------------------------------------------------
263 // class SimpleThread NULL Implementation
265 //-----------------------------------------------------------------------------------
266 #elif U_PLATFORM == U_PF_CLASSIC_MACOS
268 // since the Mac has no preemptive threading (at least on MacOS 8), only
269 // cooperative threading, threads are a no-op. We have no yield() calls
270 // anywhere in the ICU, so we are guaranteed to be thread-safe.
274 SimpleThread::SimpleThread()
277 SimpleThread::~SimpleThread()
281 SimpleThread::start()
289 SimpleThread::sleep(int32_t millis
)
293 SimpleThread::isRunning() {
300 //-----------------------------------------------------------------------------------
302 // class SimpleThread POSIX implementation
304 // A note on the POSIX vs the Windows implementations of this class..
305 // On Windows, the main thread must verify that other threads have finished
306 // before exiting, or crashes occasionally occur. (Seen on Itanium Win64 only)
307 // The function SimpleThread::isRunning() is used for this purpose.
309 // On POSIX, there is NO reliable non-blocking mechanism to determine
310 // whether a thread has exited. pthread_kill(thread, 0) almost works,
311 // but the system can recycle thread ids immediately, so seeing that a
312 // thread exists with this call could mean that the original thread has
313 // finished and a new one started with the same ID. Useless.
315 // So we need to do the check with user code, by setting a flag just before
316 // the thread function returns. A technique that is guaranteed to fail
317 // on Windows, because it indicates that the thread is done before all
318 // system level cleanup has happened.
320 //-----------------------------------------------------------------------------------
324 struct PosixThreadImplementation
328 UBool fRan
; // True if the thread was successfully started
331 extern "C" void* SimpleThreadProc(void *arg
)
333 // This is the code that is run in the new separate thread.
334 SimpleThread
*This
= (SimpleThread
*)arg
;
335 This
->run(); // Run the user code.
337 // The user function has returned. Set the flag indicating that this thread
338 // is done. Need a mutex for memory barrier purposes only, so that other thread
339 // will reliably see that the flag has changed.
340 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)This
->fImplementation
;
342 imp
->fRunning
= FALSE
;
347 SimpleThread::SimpleThread()
349 PosixThreadImplementation
*imp
= new PosixThreadImplementation
;
350 imp
->fRunning
= FALSE
;
352 fImplementation
= imp
;
355 SimpleThread::~SimpleThread()
357 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
359 pthread_join(imp
->fThread
, NULL
);
362 fImplementation
= (void *)0xdeadbeef;
365 int32_t SimpleThread::start()
368 static pthread_attr_t attr
;
369 static UBool attrIsInitialized
= FALSE
;
371 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
372 imp
->fRunning
= TRUE
;
376 if (attrIsInitialized
== FALSE
) {
377 rc
= pthread_attr_create(&attr
);
378 attrIsInitialized
= TRUE
;
380 rc
= pthread_create(&(imp
->fThread
),attr
,&SimpleThreadProc
,(void*)this);
382 if (attrIsInitialized
== FALSE
) {
383 rc
= pthread_attr_init(&attr
);
384 #if U_PLATFORM == U_PF_OS390
386 int detachstate
= 0; // jdc30: detach state of zero causes
387 //threads created with this attr to be in
388 //an undetached state. An undetached
389 //thread will keep its resources after
391 pthread_attr_setdetachstate(&attr
, &detachstate
);
394 // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
395 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_JOINABLE
);
397 attrIsInitialized
= TRUE
;
399 rc
= pthread_create(&(imp
->fThread
),&attr
,&SimpleThreadProc
,(void*)this);
403 // some kind of error occured, the thread did not start.
405 imp
->fRunning
= FALSE
;
413 SimpleThread::isRunning() {
414 // Note: Mutex functions are used here not for synchronization,
415 // but to force memory barriors to exist, to ensure that one thread
416 // can see changes made by another when running on processors
417 // with memory models having weak coherency.
418 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
420 UBool retVal
= imp
->fRunning
;
426 void SimpleThread::sleep(int32_t millis
)
428 #if U_PLATFORM == U_PF_SOLARIS
433 cma_sleep(millis
/100);
434 #elif U_PLATFORM == U_PF_HPUX || U_PLATFORM == U_PF_OS390
436 while(millis
>= 1000000) {
444 usleep(millis
* 1000);
453 #error No implementation for threads! Cannot test.
457 //-------------------------------------------------------------------------------------------
459 // class ThreadWithStatus - a thread that we can check the status and error condition of
461 //-------------------------------------------------------------------------------------------
462 class ThreadWithStatus
: public SimpleThread
465 UBool
getError() { return (fErrors
> 0); }
466 UBool
getError(UnicodeString
& fillinError
) { fillinError
= fErrorString
; return (fErrors
> 0); }
467 virtual ~ThreadWithStatus(){}
469 ThreadWithStatus() : fErrors(0) {}
470 void error(const UnicodeString
&error
) {
471 fErrors
++; fErrorString
= error
;
472 SimpleThread::errorFunc();
474 void error() { error("An error occured."); }
477 UnicodeString fErrorString
;
480 #endif // ICU_USE_THREADS