~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to examples/Reactor/Misc/test_reactors.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// test_reactors.cpp,v 4.20 2003/12/30 23:18:59 shuston Exp
2
 
 
3
 
// Perform a torture test of multiple ACE_Reactors and ACE_Tasks in
4
 
// the same process...  Thanks to Detlef Becker for contributing this.
5
 
 
6
 
#include "ace/Reactor.h"
7
 
#include "ace/Service_Config.h"
8
 
#include "ace/Task.h"
9
 
#include "ace/Atomic_Op.h"
10
 
 
11
 
ACE_RCSID(Misc, test_reactors, "test_reactors.cpp,v 4.20 2003/12/30 23:18:59 shuston Exp")
12
 
 
13
 
#if defined (ACE_HAS_THREADS)
14
 
 
15
 
#include "ace/Recursive_Thread_Mutex.h"
16
 
 
17
 
static const int NUM_INVOCATIONS = 10;
18
 
static const int MAX_TASKS = 20;
19
 
 
20
 
class Test_Task : public ACE_Task<ACE_MT_SYNCH>
21
 
{
22
 
public:
23
 
  Test_Task (void);
24
 
  ~Test_Task (void);
25
 
 
26
 
  virtual int open (void *args = 0);
27
 
  virtual int close (u_long flags = 0);
28
 
  virtual int svc (void);
29
 
 
30
 
  virtual int handle_input (ACE_HANDLE handle);
31
 
  virtual int handle_close (ACE_HANDLE fd,
32
 
                            ACE_Reactor_Mask close_mask);
33
 
 
34
 
private:
35
 
  int handled_;
36
 
 
37
 
  static int task_count_;
38
 
};
39
 
 
40
 
int Test_Task::task_count_ = 0;
41
 
 
42
 
static ACE_Atomic_Op<ACE_Thread_Mutex, int> done_count = MAX_TASKS * 2;
43
 
 
44
 
static ACE_Recursive_Thread_Mutex reclock_;
45
 
 
46
 
Test_Task::Test_Task (void)
47
 
  : handled_ (0)
48
 
{
49
 
  ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon, reclock_);
50
 
 
51
 
  Test_Task::task_count_++;
52
 
  ACE_DEBUG ((LM_DEBUG,
53
 
              "(%t) TT+ Test_Task::task_count_ = %d\n",
54
 
              Test_Task::task_count_));
55
 
}
56
 
 
57
 
Test_Task::~Test_Task (void)
58
 
{
59
 
  ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon, reclock_);
60
 
 
61
 
  ACE_DEBUG ((LM_DEBUG,
62
 
              "(%t) TT- Test_Task::task_count_ = %d\n",
63
 
              Test_Task::task_count_));
64
 
}
65
 
 
66
 
int
67
 
Test_Task::open (void *args)
68
 
{
69
 
  this->reactor ((ACE_Reactor *) args);
70
 
  return this->activate (THR_NEW_LWP);
71
 
}
72
 
 
73
 
int
74
 
Test_Task::close (u_long)
75
 
{
76
 
  ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, reclock_, -1);
77
 
 
78
 
  Test_Task::task_count_--;
79
 
  ACE_DEBUG ((LM_DEBUG,
80
 
              "(%t) close Test_Task::task_count_ = %d\n",
81
 
              Test_Task::task_count_));
82
 
  return 0;
83
 
}
84
 
 
85
 
int
86
 
Test_Task::svc (void)
87
 
{
88
 
  for (int i = 0; i < NUM_INVOCATIONS; i++)
89
 
    {
90
 
      ACE_OS::thr_yield ();
91
 
 
92
 
      // ACE_DEBUG ((LM_DEBUG, "(%t) calling notify %d\n", i));
93
 
 
94
 
      if (this->reactor ()->notify (this, ACE_Event_Handler::READ_MASK) == -1)
95
 
        ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "notify"), -1);
96
 
 
97
 
      // ACE_DEBUG ((LM_DEBUG, "(%t) leaving notify %d\n", i));
98
 
    }
99
 
 
100
 
  return 0;
101
 
}
102
 
 
103
 
int
104
 
Test_Task::handle_close (ACE_HANDLE,
105
 
                         ACE_Reactor_Mask)
106
 
{
107
 
  ACE_DEBUG ((LM_DEBUG, "(%t) handle_close\n"));
108
 
  return 0;
109
 
}
110
 
 
111
 
int
112
 
Test_Task::handle_input (ACE_HANDLE)
113
 
{
114
 
  ACE_DEBUG ((LM_DEBUG, "(%t) handle_input\n"));
115
 
 
116
 
  this->handled_++;
117
 
 
118
 
  if (this->handled_ == NUM_INVOCATIONS)
119
 
    {
120
 
      done_count--;
121
 
      ACE_DEBUG ((LM_DEBUG,
122
 
                  "(%t) handle_input, handled_ = %d, done_count = %d\n",
123
 
                  this->handled_, done_count.value ()));
124
 
    }
125
 
 
126
 
  ACE_OS::thr_yield ();
127
 
  return -1;
128
 
}
129
 
 
130
 
static void *
131
 
worker (void *args)
132
 
{
133
 
  ACE_Reactor *reactor = (ACE_Reactor *) args;
134
 
 
135
 
  reactor->owner (ACE_Thread::self ());
136
 
 
137
 
  ACE_Time_Value timeout (4);
138
 
 
139
 
  for (;;)
140
 
    {
141
 
      //ACE_DEBUG ((LM_DEBUG, "(%t) calling handle_events\n"));
142
 
 
143
 
      switch (reactor->handle_events (timeout))
144
 
        {
145
 
        case -1:
146
 
          ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "reactor"), 0);
147
 
          /* NOTREACHED */
148
 
        case 0:
149
 
          ACE_ERROR_RETURN ((LM_ERROR, "(%t) timeout\n"), 0);
150
 
          /* NOTREACHED */
151
 
        }
152
 
 
153
 
      // ACE_DEBUG ((LM_DEBUG, "(%t) done with handle_events\n"));
154
 
 
155
 
    }
156
 
 
157
 
  ACE_NOTREACHED(return 0);
158
 
}
159
 
 
160
 
int
161
 
main (int, char *[])
162
 
{
163
 
  ACE_Reactor *react1 = ACE_Reactor::instance ();
164
 
  ACE_Reactor *react2 = new ACE_Reactor ();
165
 
  Test_Task tt1[MAX_TASKS];
166
 
  Test_Task tt2[MAX_TASKS];
167
 
 
168
 
  for (int i = 0; i < MAX_TASKS; i++)
169
 
    {
170
 
      tt1[i].open (react1);
171
 
      tt2[i].open (react2);
172
 
    }
173
 
 
174
 
  if (ACE_Thread_Manager::instance ()->spawn
175
 
      (ACE_THR_FUNC (worker), (void *) react1, THR_NEW_LWP) == -1)
176
 
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn"), -1);
177
 
 
178
 
  else if (ACE_Thread_Manager::instance ()->spawn
179
 
      (ACE_THR_FUNC (worker), (void *) react2, THR_NEW_LWP) == -1)
180
 
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn"), -1);
181
 
 
182
 
  ACE_Thread_Manager::instance ()->wait ();
183
 
  ACE_DEBUG ((LM_DEBUG, "(%t) done\n"));
184
 
 
185
 
  return 0;
186
 
}
187
 
 
188
 
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
189
 
template class ACE_Atomic_Op<ACE_Thread_Mutex, int>;
190
 
template class ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>;
191
 
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
192
 
#pragma instantiate ACE_Atomic_Op<ACE_Thread_Mutex, int>
193
 
#pragma instantiate ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>
194
 
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
195
 
 
196
 
#else
197
 
int
198
 
main (int, char *[])
199
 
{
200
 
  ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
201
 
  return 0;
202
 
}
203
 
#endif /* ACE_HAS_THREADS */