summaryrefslogtreecommitdiff
path: root/ACE/tests/Log_Thread_Inheritance_Test.cpp
blob: 78c4f687fba56ad58d20988ccb711c80a66bcc5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "test_config.h"
#include "ace/Log_Msg.h"
#include "ace/Task.h"
#include <fstream>

#ifdef ACE_HAS_PTHREADS
# include <pthread.h>
#endif /* ACE_HAS_PTHREADS */

#if !defined (ACE_HAS_THREADS) || defined (ACE_LACKS_IOSTREAM_TOTALLY)
int run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Log_Thread_Inheritance_Test"));
  ACE_END_TEST;
  return 0;
}
#else

#ifdef ACE_HAS_PTHREADS
struct Inheritor : ACE_Task_Base
{
  int svc ()
  {
    ACE_DEBUG ((LM_DEBUG, "(%P|%t) - this test might crash ACE if it does not "
                          "have the fix for the second bug in #3480.\n"));
    return 0;
  }
};

extern "C"
void* spawn_ace_task (void*)
{
  Inheritor inheritor;

  inheritor.activate ();
  inheritor.wait ();

  return 0;
}

bool test_inherited_attributes ()
{
  // This test verifies ACE_OS_Log_Msg_Attributes correctly initializes
  // when an ACE thread is created from a non-ACE thread (i.e. pthreads)
  // and is then used for logging.
  //
  // This test will cause occasional SEGVs on failure.
  // stallions 2009/02/05
  pthread_t parent;

  if (pthread_create (&parent, 0, spawn_ace_task, 0) != 0)
    {
      return false;
    }
  pthread_join (parent, 0);

  return true;
}
#endif /* ACE_HAS_PTHREADS */

struct MyThread : ACE_Task_Base
{

  enum { THREAD_DEFAULTS = THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED };

  explicit MyThread (bool openfile = false)
    : openfile_ (openfile) {}

  bool openfile_;
  static MyThread childthread_;

  int svc ()
  {
    if (openfile_)
      {
        ACE_LOG_MSG->msg_ostream (
          new std::ofstream (
            ACE_TEXT_ALWAYS_CHAR (
              ACE_LOG_DIRECTORY
              ACE_TEXT ("Log_Thread_Inheritance_Ostream")
              ACE_LOG_FILE_EXT_NAME
            )
          ), true);
        ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
        ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER);
        MyThread ends_first_thread;
        ends_first_thread.activate (THREAD_DEFAULTS, 10);
        ends_first_thread.wait ();
        childthread_.activate (THREAD_DEFAULTS, 10);
      }
    ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in svc() openfile_ is %C\n",
                (openfile_ ? "true" : "false")));
    if (!openfile_)
      {
        for (int i = 0; i < 100; ++i)
          {
            ACE_DEBUG ((LM_DEBUG, "(%P|%t) - loop %d\n", i));
            if (!(i % 10)) ACE_OS::thr_yield ();
          }
      }
    return 0;
  }
};

MyThread MyThread::childthread_;

int run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Log_Thread_Inheritance_Test"));
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) - in run_main()\n"));
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) - this test will crash ACE if it does not "
              "have the fix for bug #3480.\n"));
  ACE_OSTREAM_TYPE *initial_stream = ACE_LOG_MSG->msg_ostream ();
  MyThread mt (true);
  mt.activate ();
  mt.wait ();
  MyThread::childthread_.wait ();
  ACE_LOG_MSG->msg_ostream (initial_stream, 0);
#ifdef ACE_HAS_PTHREADS
  if (!test_inherited_attributes ()) return -1;
#endif /* ACE_HAS_PTHREADS */
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) - Test passed.\n"));
  ACE_END_TEST;
  return 0;
}

#endif