summaryrefslogtreecommitdiff
path: root/tests/Auto_IncDec_Test.cpp
blob: 015a9da2a259bedb0af77b9478f204b1d1f3cb9c (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// $Id$

//============================================================================
//
// = LIBRARY
//     tests
//
// = FILENAME
//     Auto_IncDec_Test.cpp
//
// = DESCRIPTION
//     This is a simple test of the Auto Increment/Decrement Class in
//     ACE.
//
// = AUTHOR
//     Edan Ayal <EdanA@cti2.com>
//
//============================================================================

#include "ace/Auto_IncDec_T.h"
#include "ace/Thread_Manager.h"
#include "ace/Atomic_Op.h"
#include "tests/test_config.h"


ACE_RCSID(tests, Auto_IncDec_Test, "Auto_IncDec_Test.cpp, by Edan Ayal")

#if defined (ACE_HAS_THREADS)

// Default number of threads.
static size_t n_threads = 15;

typedef ACE_Atomic_Op<ACE_Thread_Mutex, int> INTERLOCKED_INT;
static INTERLOCKED_INT current_threads_in_first_section;
static INTERLOCKED_INT current_threads_in_second_section;

static void *
worker (void *)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT (" (%t) worker starting\n")));

  { // First section.
    ACE_Auto_IncDec<INTERLOCKED_INT> threads_in_section_auto_inc_dec
      (current_threads_in_first_section);

    // Wait according to the number of threads...
    ACE_Time_Value pause (current_threads_in_first_section.value (),
                          0);
    ACE_OS::sleep (pause);
  }

  { // Second section.
    ACE_Auto_IncDec<INTERLOCKED_INT> threads_in_section_auto_inc_dec
      (current_threads_in_second_section);

    // Wait according to the number of threads inside the previous
    // section...
      ACE_Time_Value pause (current_threads_in_first_section.value (),
                            0);
    ACE_OS::sleep (pause);
  }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT (" (%t) worker exiting\n")));
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Atomic_Op<ACE_Thread_Mutex, int>;
template class ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>;
template class ACE_Auto_IncDec<ACE_Atomic_Op<ACE_Thread_Mutex, int> >;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Atomic_Op<ACE_Thread_Mutex, int>
#pragma instantiate ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>
#pragma instantiate ACE_Auto_IncDec<ACE_Atomic_Op<ACE_Thread_Mutex, int> >
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

#else
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Auto_IncDec<int>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Auto_IncDec<int>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#endif /* ACE_HAS_THREADS */

// Spawn off threads.

int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Auto_IncDec_Test"));

#if defined (ACE_HAS_THREADS)
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT (" (%t) main thread starting\n")));

  current_threads_in_first_section = 0;
  current_threads_in_second_section = 0;

  if (ACE_Thread_Manager::instance ()->spawn_n
      (n_threads,
       ACE_THR_FUNC (worker),
       0,
       THR_NEW_LWP) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("spawn_n")),
                      -1);
  // Make sure at least one thread is started...
  ACE_Thread::yield ();

  while (ACE_Thread_Manager::instance ()->count_threads ())
    {
      // wait according to the number of threads...
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT (" %d in first section, %d in second section\n"),
                  current_threads_in_first_section.value (),
                  current_threads_in_second_section.value ()));

      ACE_Time_Value pause (1, 0);
      ACE_OS::sleep (pause);
    }

  ACE_Thread_Manager::instance ()->wait ();

  ACE_ASSERT (current_threads_in_first_section.value () == 0
              && current_threads_in_second_section.value () == 0);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT (" (%t) exiting main thread\n")));
#else
  int counter = 0;
  {
    ACE_Auto_IncDec<int> Auto_IncDec1 (counter);
    ACE_ASSERT (counter == 1);

    ACE_Auto_IncDec<int> Auto_IncDec2 (counter);
    ACE_ASSERT (counter == 2);

    {
      ACE_ASSERT (counter == 2);
      ACE_Auto_IncDec<int> Auto_IncDec3 (counter);
      ACE_ASSERT (counter == 3);
    }

    ACE_ASSERT (counter == 2);
  }

  ACE_ASSERT (counter == 0);

#endif /* ACE_HAS_THREADS */

  ACE_END_TEST;
  return 0;
}