Also use Objects as part of an operation but as a result don't generate Any operation...
[ACE_TAO.git] / ACE / tests / Log_Thread_Inheritance_Test.cpp
blob78c4f687fba56ad58d20988ccb711c80a66bcc5c
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 ()
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
64 enum { THREAD_DEFAULTS = THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED };
66 explicit MyThread (bool openfile = false)
67 : openfile_ (openfile) {}
69 bool openfile_;
70 static MyThread childthread_;
72 int svc ()
74 if (openfile_)
76 ACE_LOG_MSG->msg_ostream (
77 new std::ofstream (
78 ACE_TEXT_ALWAYS_CHAR (
79 ACE_LOG_DIRECTORY
80 ACE_TEXT ("Log_Thread_Inheritance_Ostream")
81 ACE_LOG_FILE_EXT_NAME
83 ), true);
84 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
85 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER);
86 MyThread ends_first_thread;
87 ends_first_thread.activate (THREAD_DEFAULTS, 10);
88 ends_first_thread.wait ();
89 childthread_.activate (THREAD_DEFAULTS, 10);
91 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in svc() openfile_ is %C\n",
92 (openfile_ ? "true" : "false")));
93 if (!openfile_)
95 for (int i = 0; i < 100; ++i)
97 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - loop %d\n", i));
98 if (!(i % 10)) ACE_OS::thr_yield ();
101 return 0;
105 MyThread MyThread::childthread_;
107 int run_main (int, ACE_TCHAR *[])
109 ACE_START_TEST (ACE_TEXT ("Log_Thread_Inheritance_Test"));
110 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in run_main()\n"));
111 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - this test will crash ACE if it does not "
112 "have the fix for bug #3480.\n"));
113 ACE_OSTREAM_TYPE *initial_stream = ACE_LOG_MSG->msg_ostream ();
114 MyThread mt (true);
115 mt.activate ();
116 mt.wait ();
117 MyThread::childthread_.wait ();
118 ACE_LOG_MSG->msg_ostream (initial_stream, 0);
119 #ifdef ACE_HAS_PTHREADS
120 if (!test_inherited_attributes ()) return -1;
121 #endif /* ACE_HAS_PTHREADS */
122 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - Test passed.\n"));
123 ACE_END_TEST;
124 return 0;
127 #endif