1 /********************************************************************
3 * Copyright (c) 1999-2013, 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
65 #if (ICU_USE_THREADS == 1)
69 #if defined(__hpux) && defined(HPUX_CMA)
70 # if defined(read) // read being defined as cma_read causes trouble with iostream::read
75 #if U_PLATFORM == U_PF_OS390
76 #include <sys/types.h>
79 #if U_PLATFORM != U_PF_OS390
83 /* Define _XPG4_2 for Solaris and friends. */
88 /* Define __USE_XOPEN_EXTENDED for Linux and glibc. */
89 #ifndef __USE_XOPEN_EXTENDED
90 #define __USE_XOPEN_EXTENDED
93 /* Define _INCLUDE_XOPEN_SOURCE_EXTENDED for HP/UX (11?). */
94 #ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED
95 #define _INCLUDE_XOPEN_SOURCE_EXTENDED
107 #if (ICU_USE_THREADS==0)
108 SimpleThread::SimpleThread()
111 SimpleThread::~SimpleThread()
115 SimpleThread::start()
123 SimpleThread::sleep(int32_t millis
)
127 SimpleThread::isRunning() {
132 #include "unicode/putil.h"
135 #include "unicode/numfmt.h"
136 #include "unicode/choicfmt.h"
137 #include "unicode/msgfmt.h"
138 #include "unicode/locid.h"
139 #include "unicode/ucol.h"
140 #include "unicode/calendar.h"
143 #if U_PLATFORM_USES_ONLY_WIN32_API
146 # define VC_EXTRALEAN
147 # define WIN32_LEAN_AND_MEAN
155 //-----------------------------------------------------------------------------------
157 // class SimpleThread Windows Implementation
159 //-----------------------------------------------------------------------------------
160 struct Win32ThreadImplementation
163 unsigned int fThreadID
;
167 extern "C" unsigned int __stdcall
SimpleThreadProc(void *arg
)
169 ((SimpleThread
*)arg
)->run();
173 SimpleThread::SimpleThread()
176 Win32ThreadImplementation
*imp
= new Win32ThreadImplementation
;
178 fImplementation
= imp
;
181 SimpleThread::~SimpleThread()
183 // Destructor. Because we start the thread running with _beginthreadex(),
184 // we own the Windows HANDLE for the thread and must
186 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
188 if (imp
->fHandle
!= 0) {
189 CloseHandle(imp
->fHandle
);
193 delete (Win32ThreadImplementation
*)fImplementation
;
196 int32_t SimpleThread::start()
198 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
199 if(imp
->fHandle
!= NULL
) {
200 // The thread appears to have already been started.
201 // This is probably an error on the part of our caller.
205 imp
->fHandle
= (HANDLE
) _beginthreadex(
207 0x20000, // Stack Size
208 SimpleThreadProc
, // Function to Run
209 (void *)this, // Arg List
210 0, // initflag. Start running, not suspended
211 &imp
->fThreadID
// thraddr
214 if (imp
->fHandle
== 0) {
226 UBool
SimpleThread::isRunning() {
228 // Test whether the thread associated with the SimpleThread object is
229 // still actually running.
231 // NOTE: on Win64 on Itanium processors, a crashes
232 // occur if the main thread of a process exits concurrently with some
233 // other thread(s) exiting. To avoid the possibility, we wait until the
234 // OS indicates that all threads have terminated, rather than waiting
235 // only until the end of the user's Run function has been reached.
237 // I don't know whether the crashes represent a Windows bug, or whether
238 // main() programs are supposed to have to wait for their threads.
240 Win32ThreadImplementation
*imp
= (Win32ThreadImplementation
*)fImplementation
;
243 DWORD threadExitCode
;
245 if (imp
->fHandle
== 0) {
246 // No handle, thread must not be running.
249 success
= GetExitCodeThread(imp
->fHandle
, &threadExitCode
) != 0;
251 // Can't get status, thread must not be running.
254 return (threadExitCode
== STILL_ACTIVE
);
258 void SimpleThread::sleep(int32_t millis
)
263 //-----------------------------------------------------------------------------------
265 // class SimpleThread NULL Implementation
267 //-----------------------------------------------------------------------------------
268 #elif U_PLATFORM == U_PF_CLASSIC_MACOS
270 // since the Mac has no preemptive threading (at least on MacOS 8), only
271 // cooperative threading, threads are a no-op. We have no yield() calls
272 // anywhere in the ICU, so we are guaranteed to be thread-safe.
276 SimpleThread::SimpleThread()
279 SimpleThread::~SimpleThread()
283 SimpleThread::start()
291 SimpleThread::sleep(int32_t millis
)
295 SimpleThread::isRunning() {
302 //-----------------------------------------------------------------------------------
304 // class SimpleThread POSIX implementation
306 // A note on the POSIX vs the Windows implementations of this class..
307 // On Windows, the main thread must verify that other threads have finished
308 // before exiting, or crashes occasionally occur. (Seen on Itanium Win64 only)
309 // The function SimpleThread::isRunning() is used for this purpose.
311 // On POSIX, there is NO reliable non-blocking mechanism to determine
312 // whether a thread has exited. pthread_kill(thread, 0) almost works,
313 // but the system can recycle thread ids immediately, so seeing that a
314 // thread exists with this call could mean that the original thread has
315 // finished and a new one started with the same ID. Useless.
317 // So we need to do the check with user code, by setting a flag just before
318 // the thread function returns. A technique that is guaranteed to fail
319 // on Windows, because it indicates that the thread is done before all
320 // system level cleanup has happened.
322 //-----------------------------------------------------------------------------------
326 struct PosixThreadImplementation
330 UBool fRan
; // True if the thread was successfully started
333 extern "C" void* SimpleThreadProc(void *arg
)
335 // This is the code that is run in the new separate thread.
336 SimpleThread
*This
= (SimpleThread
*)arg
;
337 This
->run(); // Run the user code.
339 // The user function has returned. Set the flag indicating that this thread
340 // is done. Need a mutex for memory barrier purposes only, so that other thread
341 // will reliably see that the flag has changed.
342 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)This
->fImplementation
;
344 imp
->fRunning
= FALSE
;
349 SimpleThread::SimpleThread()
351 PosixThreadImplementation
*imp
= new PosixThreadImplementation
;
352 imp
->fRunning
= FALSE
;
354 fImplementation
= imp
;
357 SimpleThread::~SimpleThread()
359 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
361 pthread_join(imp
->fThread
, NULL
);
364 fImplementation
= (void *)0xdeadbeef;
367 int32_t SimpleThread::start()
370 static pthread_attr_t attr
;
371 static UBool attrIsInitialized
= FALSE
;
373 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
374 imp
->fRunning
= TRUE
;
378 if (attrIsInitialized
== FALSE
) {
379 rc
= pthread_attr_create(&attr
);
380 attrIsInitialized
= TRUE
;
382 rc
= pthread_create(&(imp
->fThread
),attr
,&SimpleThreadProc
,(void*)this);
384 if (attrIsInitialized
== FALSE
) {
385 rc
= pthread_attr_init(&attr
);
386 #if U_PLATFORM == U_PF_OS390
388 int detachstate
= 0; // jdc30: detach state of zero causes
389 //threads created with this attr to be in
390 //an undetached state. An undetached
391 //thread will keep its resources after
393 pthread_attr_setdetachstate(&attr
, &detachstate
);
396 // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
397 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_JOINABLE
);
399 attrIsInitialized
= TRUE
;
401 rc
= pthread_create(&(imp
->fThread
),&attr
,&SimpleThreadProc
,(void*)this);
405 // some kind of error occured, the thread did not start.
407 imp
->fRunning
= FALSE
;
415 SimpleThread::isRunning() {
416 // Note: Mutex functions are used here not for synchronization,
417 // but to force memory barriors to exist, to ensure that one thread
418 // can see changes made by another when running on processors
419 // with memory models having weak coherency.
420 PosixThreadImplementation
*imp
= (PosixThreadImplementation
*)fImplementation
;
422 UBool retVal
= imp
->fRunning
;
428 void SimpleThread::sleep(int32_t millis
)
430 #if U_PLATFORM == U_PF_SOLARIS
435 cma_sleep(millis
/100);
436 #elif U_PLATFORM == U_PF_HPUX || U_PLATFORM == U_PF_OS390
438 while(millis
>= 1000000) {
446 usleep(millis
* 1000);
455 #error No implementation for threads! Cannot test.
459 //-------------------------------------------------------------------------------------------
461 // class ThreadWithStatus - a thread that we can check the status and error condition of
463 //-------------------------------------------------------------------------------------------
464 class ThreadWithStatus
: public SimpleThread
467 UBool
getError() { return (fErrors
> 0); }
468 UBool
getError(UnicodeString
& fillinError
) { fillinError
= fErrorString
; return (fErrors
> 0); }
469 virtual ~ThreadWithStatus(){}
471 ThreadWithStatus() : fErrors(0) {}
472 void error(const UnicodeString
&error
) {
473 fErrors
++; fErrorString
= error
;
474 SimpleThread::errorFunc();
476 void error() { error("An error occured."); }
479 UnicodeString fErrorString
;
482 #endif // ICU_USE_THREADS