Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / Log_Thread_Inheritance_Test.cpp
blobe27fa012b65192773cb12f3835af03be6850be74
1 #include "test_config.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Task.h"
4 #include <fstream>
6 #ifdef ACE_HAS_PTHREADS
7 # include <pthread.h>
8 #endif /* ACE_HAS_PTHREADS */
10 #if !defined (ACE_HAS_THREADS) || defined (ACE_LACKS_IOSTREAM_TOTALLY)
11 int run_main (int, ACE_TCHAR *[])
13 ACE_START_TEST (ACE_TEXT ("Log_Thread_Inheritance_Test"));
14 ACE_END_TEST;
15 return 0;
17 #else
19 #ifdef ACE_HAS_PTHREADS
20 struct Inheritor : ACE_Task_Base
22 int svc () override
24 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - this test might crash ACE if it does not "
25 "have the fix for the second bug in #3480.\n"));
26 return 0;
30 extern "C"
31 void* spawn_ace_task (void*)
33 Inheritor inheritor;
35 inheritor.activate ();
36 inheritor.wait ();
38 return 0;
41 bool test_inherited_attributes ()
43 // This test verifies ACE_OS_Log_Msg_Attributes correctly initializes
44 // when an ACE thread is created from a non-ACE thread (i.e. pthreads)
45 // and is then used for logging.
47 // This test will cause occasional SEGVs on failure.
48 // stallions 2009/02/05
49 pthread_t parent;
51 if (pthread_create (&parent, 0, spawn_ace_task, 0) != 0)
53 return false;
55 pthread_join (parent, 0);
57 return true;
59 #endif /* ACE_HAS_PTHREADS */
61 struct MyThread : ACE_Task_Base
63 enum { THREAD_DEFAULTS = THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED };
65 explicit MyThread (bool openfile = false)
66 : openfile_ (openfile) {}
68 bool openfile_;
69 static MyThread childthread_;
71 int svc () override
73 if (openfile_)
75 ACE_LOG_MSG->msg_ostream (
76 new std::ofstream (
77 ACE_TEXT_ALWAYS_CHAR (
78 ACE_LOG_DIRECTORY
79 ACE_TEXT ("Log_Thread_Inheritance_Ostream")
80 ACE_LOG_FILE_EXT_NAME
82 ), true);
83 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
84 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER);
85 MyThread ends_first_thread;
86 ends_first_thread.activate (THREAD_DEFAULTS, 10);
87 ends_first_thread.wait ();
88 childthread_.activate (THREAD_DEFAULTS, 10);
90 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in svc() openfile_ is %C\n",
91 (openfile_ ? "true" : "false")));
92 if (!openfile_)
94 for (int i = 0; i < 100; ++i)
96 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - loop %d\n", i));
97 if (!(i % 10)) ACE_OS::thr_yield ();
100 return 0;
104 MyThread MyThread::childthread_;
106 int run_main (int, ACE_TCHAR *[])
108 ACE_START_TEST (ACE_TEXT ("Log_Thread_Inheritance_Test"));
109 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in run_main()\n"));
110 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - this test will crash ACE if it does not "
111 "have the fix for bug #3480.\n"));
112 ACE_OSTREAM_TYPE *initial_stream = ACE_LOG_MSG->msg_ostream ();
113 MyThread mt (true);
114 mt.activate ();
115 mt.wait ();
116 MyThread::childthread_.wait ();
117 ACE_LOG_MSG->msg_ostream (initial_stream, 0);
118 #ifdef ACE_HAS_PTHREADS
119 if (!test_inherited_attributes ()) return -1;
120 #endif /* ACE_HAS_PTHREADS */
121 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - Test passed.\n"));
122 ACE_END_TEST;
123 return 0;
126 #endif