summaryrefslogtreecommitdiff
path: root/examples/Threads/task_five.cpp
blob: 8f96ee3420ee08d00571dad8530fb73e2f8d550d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// $Id$

// ============================================================================
//
// = LIBRARY
//    examples/Threads/
//
// = FILENAME
//    task_five.cpp
//
// = DESCRIPTION
//   Stress testing thread creation and thread cancellation using
//   ACE_Task.
//
// = AUTHOR
//    Author: Detlef Becker <Detlef.Becker@med.siemens.de>
//
// ============================================================================


#include "ace/OS_main.h"
#include "ace/Thread_Manager.h"
#include "ace/Task.h"
#include "ace/OS_NS_unistd.h"

ACE_RCSID(Threads, task_five, "$Id$")

static const int DEFAULT_TASKS = 100;
static const int DEFAULT_ITERATIONS = 10;

// Default stack size
static size_t default_stack_size =
#if defined (ACE_WIN32)
    0;
#else
    8192;
#endif /* ACE_WIN32 */
  u_int loop_count = 0;
  u_int error_count = 0;

class Test_Task : public ACE_Task<ACE_SYNCH>
{
public:
  Test_Task (ACE_Thread_Manager * = ACE_Thread_Manager::instance ());
  ~Test_Task (void) {};

  int open (void * = 0);
  int svc (void);
  int close (u_long);
  int shutdown (void);
  int synch (void);
};

Test_Task::Test_Task (ACE_Thread_Manager *thrmgr)
  : ACE_Task<ACE_SYNCH> (thrmgr)
{
}

int
Test_Task::open (void *)
{
  return this->activate (0,
                         1,
                         0,
                         ACE_DEFAULT_THREAD_PRIORITY,
                         -1,
                         0,
                         0,
                         0,
                         &default_stack_size);
}

int
Test_Task::svc (void)
{
  while (thr_mgr_->testcancel (ACE_OS::thr_self ()) == 0)
    // Sleep for 350 msecs.
    ACE_OS::sleep (ACE_Time_Value (0, 350000));

  return 0;
}

int
Test_Task::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) closing down\n"));
  return 0;
}

int
Test_Task::shutdown (void)
{
  return thr_mgr_->cancel_grp (grp_id_);
}

int
Test_Task::synch (void)
{
  return thr_mgr_->wait_grp (grp_id_);
}

static void
work (ACE_Thread_Manager *thr_mgr,
      int n_tasks,
      size_t stack_size)
{
  ACE_UNUSED_ARG (stack_size);

  int i;
  Test_Task *task_array;

  ACE_NEW (task_array,
           Test_Task[n_tasks]);

  ACE_DEBUG ((LM_DEBUG,
              "Opening Tasks, loop count = %d, error count = %d\n",
              loop_count,
              error_count));

  for (i = 0;
       i < n_tasks;
       i++)
    task_array[i].open ();

  ACE_OS::sleep (1);

  ACE_DEBUG ((LM_DEBUG,
              "Cancelling Tasks, loop count = %d, error count = %d\n",
              loop_count,
              error_count));

  for (i = 0; i < n_tasks; i++)
    task_array[i].shutdown ();

  ACE_DEBUG ((LM_DEBUG,
              "Synching Tasks, loop count = %d, error count = %d\n",
              loop_count,
              error_count));

  for (i = 0;

       i < n_tasks; i++)
    if (-1 == task_array[i].synch ())
      {
        ACE_ERROR ((LM_ERROR,
                    "Error in synch! loop count = %d, error count = %d\n",
                    loop_count,
                    error_count));
        error_count++;
      }

  ACE_DEBUG ((LM_DEBUG,
              "thr_mgr->wait ();! loop count = %d, error count = %d\n",
              loop_count,
              error_count));

  // Wait for all the threads to finish.
  thr_mgr->wait ();

  delete [] task_array;
  loop_count++;
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  size_t stack_size = argc > 1 ? ACE_OS::atoi (argv[1]) : default_stack_size;
  const int n_tasks = argc > 2 ? ACE_OS::atoi (argv[2]) : DEFAULT_TASKS;
  u_int iterations = argc > 3 ? ACE_OS::atoi (argv[3]) : DEFAULT_ITERATIONS;

  for (u_int i = 0; i < iterations; i++)
    work (ACE_Thread_Manager::instance (),
          n_tasks,
          stack_size);

  return 0;
}