summaryrefslogtreecommitdiff
path: root/examples/Threads/test_tss2.cpp
blob: 24a8d958e91d8371d363b9f7a0cd9206b7cff405 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
// 
// = FILENAME
//    TSS_Test.cpp
//
// = DESCRIPTION
//     This program tests thread specific storage of data. The ACE_TSS
//     wrapper transparently ensures that the objects of this class
//     will be placed in thread-specific storage. All calls on
//     ACE_TSS::operator->() are delegated to the appropriate method
//     in the Errno class. 
//
// = AUTHOR
//    Prashant Jain and Doug Schmidt
// 
// ============================================================================

#include "ace/Task.h"
#include "ace/Token.h"

#if defined (ACE_HAS_THREADS)

class TSS_Obj
{
public:

  TSS_Obj (void);
  ~TSS_Obj (void);

private:
  static int count_;
  static ACE_Thread_Mutex  lock_;
};

int TSS_Obj::count_ = 0;
ACE_Thread_Mutex TSS_Obj::lock_;

TSS_Obj::TSS_Obj (void)
{
  ACE_GUARD (ACE_Thread_Mutex, ace_mon, lock_);

  count_++;
  cout << "TO+ : " << count_ << endl;
}

TSS_Obj::~TSS_Obj (void)
{
  ACE_GUARD (ACE_Thread_Mutex, ace_mon, lock_);

  count_--;
  cout << "TO- : " << count_ << endl;
}

class Test_Task
{
public:

  Test_Task (void);
  ~Test_Task (void);

  int open (void *arg);

  static void *svc (void *arg);
  static int wait_count_;
  static int max_count_;

private:
  static int count_;
};

int Test_Task::count_ = 0;
int Test_Task::wait_count_ = 0;
int Test_Task::max_count_ = 0;
int num_threads_ = 0;

ACE_Token token;

Test_Task::Test_Task (void)
{
  ACE_GUARD (ACE_Token, ace_mon, token);

  count_++;
  cout << "Test_Task+ : " 
       << count_ << " (" 
       << ACE_OS::thr_self () 
       << ")" << endl;
}

Test_Task::~Test_Task (void)
{
  ACE_GUARD (ACE_Token, ace_mon, token);

  count_--;
  cout << "Test_Task- : " 
       << count_ << " (" 
       << ACE_OS::thr_self () 
       << ")" << endl;

  wait_count_--;
}

void *
Test_Task::svc (void *arg)
{
  ACE_TSS<TSS_Obj> tss (new TSS_Obj);

  {
    ACE_GUARD_RETURN (ACE_Token, ace_mon, token, 0);

    wait_count_++;
    max_count_++;
    cout << "svc: waiting (" << ACE_OS::thr_self () << ")" << endl;
  }

  while (1)
    {   
      {
	ACE_GUARD_RETURN (ACE_Token, ace_mon, token, 0);

	if (max_count_ >= num_threads_)
	  break;
	else
	  {
	    ace_mon.release ();
	    ACE_Thread::yield ();
	    ace_mon.acquire ();
	  }
      }

      {
	ACE_GUARD_RETURN (ACE_Token, ace_mon, token, 0);

	cout << "svc: waiting (" << ACE_OS::thr_self () << ") finished" << endl;
      }
    }

  delete (Test_Task *) arg;

  return 0;
}

int 
Test_Task::open (void *arg)
{
  if (ACE_Thread::spawn (Test_Task::svc, arg) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Thread::spawn"), 0);

  return 0;
}

int 
main (int argc, char **argv)
{
  if (argc != 2)
    {
      cout << "Missing parameters!" << endl;
      return 1;
    }

  int num_Tasks = atoi (argv[1]);

  num_threads_ = num_Tasks;

  Test_Task **task_arr = (Test_Task**) new char[sizeof (Test_Task*) * num_Tasks];

  while (1)
    {
      {
	ACE_GUARD_RETURN (ACE_Token, ace_mon, token, -1);

	cout << "ReseTest_Tasking Test_Task::max_count_ from: " 
	     << Test_Task::max_count_ << endl;

	Test_Task::max_count_ = 0;
      }

      for (int i = 0; i < num_Tasks; i++)
	{
          task_arr[i] = new Test_Task;
	  task_arr[i]->open (task_arr[i]);
	}

      cout << "Waiting for first thread started..." << endl;

      for (;;)
	{
	  ACE_GUARD_RETURN (ACE_Token, ace_mon, token, -1);

	  if (Test_Task::max_count_ != 0 )
	    {
	      ace_mon.release ();
	      ACE_Thread::yield ();
	      ace_mon.acquire ();
	      break;
	    }
	  ace_mon.release ();
	  ACE_Thread::yield ();
	  ace_mon.acquire ();
	}

      {
	ACE_GUARD_RETURN (ACE_Token, ace_mon, token, -1);

	cout << "First thread started!" << endl 
	     << "Waiting for all threads finished..." << endl;
      }

      for (;;)
	{
	  ACE_GUARD_RETURN (ACE_Token, ace_mon, token, -1);

	  if (!(Test_Task::max_count_ == num_threads_ 
		&& Test_Task::wait_count_ == 0))
	    {
	      ace_mon.release ();
	      ACE_Thread::yield ();
	      ace_mon.acquire ();
	      continue;
	    }

	  cout << "Test_Task::max_count_ = " 
	       << Test_Task::max_count_ 
	       << " Test_Task::wait_count_ = " 
	       << Test_Task::wait_count_ 
	       << endl;
	  break;
	}

      {
	ACE_GUARD_RETURN (ACE_Token, ace_mon, token, -1);
	cout << "All threads finished..." << endl;
      }

      ACE_OS::sleep (2);
    }

  return 0;
}

#else
int 
main (int, char *[])
{
  ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
  return 0;
}
#endif /* ACE_HAS_THREADS */