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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// TSS_Test.cpp
//
// = DESCRIPTION
// This program tests various features of ACE_Thread and the
// thread-specific storage variant of <ACE_Singleton>.
//
// = AUTHOR
// Prashant Jain and Doug Schmidt
//
// ============================================================================
#include "ace/Thread.h"
#include "ace/Log_Msg.h"
#include "ace/Atomic_Op.h"
#include "TSS_Data.h"
#include "TSS_Obj.h"
#include "TSS_Task.h"
ACE_RCSID(Threads, tss2, "$Id$")
#if defined (ACE_HAS_THREADS)
const int MAX_TASKS = 4;
const int MAX_ITERATIONS = 10;
ACE_Atomic_Op<ACE_Token, int> Test_Task::count_ (0);
ACE_Atomic_Op<ACE_Token, int> Test_Task::wait_count_ (0);
ACE_Atomic_Op<ACE_Token, int> Test_Task::max_count_ (0);
int num_tasks = 0;
// ACE synchronization object.
static ACE_Token token;
ACE_Atomic_Op<ACE_Thread_Mutex, int> TSS_Obj::count_ = 0;
TSS_Obj::TSS_Obj (void)
{
TSS_Obj::count_++;
ACE_DEBUG ((LM_DEBUG, "(%t) TSS_Obj+: %d\n", TSS_Obj::count_.value ()));
}
TSS_Obj::~TSS_Obj (void)
{
TSS_Obj::count_--;
ACE_DEBUG ((LM_DEBUG, "(%t) TSS_Obj-: %d\n", TSS_Obj::count_.value ()));
}
Test_Task::Test_Task (void)
{
Test_Task::count_++;
ACE_DEBUG ((LM_DEBUG,
"(%t) Test_Task+: %d\n", Test_Task::count_.value ()));
}
Test_Task::~Test_Task (void)
{
Test_Task::count_--;
ACE_DEBUG ((LM_DEBUG,
"(%t) Test_Task-: %d\n", Test_Task::count_.value ()));
Test_Task::wait_count_--;
}
void *
Test_Task::svc (void *arg)
{
// When the thread exits this thread-specific object will be deleted
// automatically.
ACE_TSS<TSS_Obj> tss (new TSS_Obj);
TSS_DATA::instance ()->data (arg);
Test_Task::wait_count_++;
Test_Task::max_count_++;
ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting (data = %u)\n",
arg));
// Do a bunch of set operations on the TSS data just to make sure
// that it's truly in TSS (it it weren't, the assertion would fail).
while (Test_Task::max_count_ < num_tasks)
{
TSS_DATA::instance ()->data (arg);
ACE_Thread::yield ();
}
ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting finished (data = %u)\n",
arg));
#if 0
ACE_ASSERT (TSS_DATA::instance ()->data () == arg);
#endif
delete (Test_Task *) arg;
return 0;
}
int
Test_Task::open (void *arg)
{
if (ACE_Thread::spawn ((ACE_THR_FUNC) Test_Task::svc, arg) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_Thread::spawn"), 0);
return 0;
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
num_tasks = argc > 1 ? ACE_OS::atoi (argv[1]) : MAX_TASKS;
Test_Task **task_arr;
ACE_NEW_RETURN (task_arr, Test_Task *[num_tasks], -1);
for (int i = 0; i < MAX_ITERATIONS; i++)
{
ACE_DEBUG ((LM_DEBUG,
"(%t) ********* iteration %d **********\n"
"Test_Task::max_count_ %d\n",
i,
Test_Task::max_count_.value ()));
Test_Task::max_count_ = 0;
for (int j = 0; j < num_tasks; j++)
{
ACE_NEW_RETURN (task_arr[j], Test_Task, -1);
task_arr[j]->open (task_arr[j]);
}
ACE_DEBUG ((LM_DEBUG, "(%t) waiting for first thread started\n"));
for (;;)
{
ACE_Thread::yield ();
if (Test_Task::max_count_ != 0 )
break;
}
ACE_DEBUG ((LM_DEBUG, "(%t) First thread started\n"
"Waiting for all threads finished\n"));
for (;;)
{
if (!(Test_Task::max_count_ == num_tasks
&& Test_Task::wait_count_ == 0))
{
ACE_Thread::yield ();
continue;
}
ACE_DEBUG ((LM_DEBUG,
"(%t) Test_Task::max_count_ = %d,"
" Test_Task::wait_count_ = %d",
Test_Task::max_count_.value (),
Test_Task::wait_count_.value ()));
break;
}
ACE_DEBUG ((LM_DEBUG, "(%t) all threads finished\n"));
}
return 0;
}
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>;
template class ACE_Atomic_Op<ACE_Thread_Mutex, int>;
template class ACE_Atomic_Op_Ex<ACE_Token, int>;
template class ACE_Atomic_Op<ACE_Token, int>;
template class ACE_Guard<ACE_Token>;
template class ACE_TSS<TSS_Data>;
template class ACE_TSS<TSS_Obj>;
template class ACE_TSS_Singleton<TSS_Data, ACE_SYNCH_MUTEX>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>
#pragma instantiate ACE_Atomic_Op<ACE_Thread_Mutex, int>
#pragma instantiate ACE_Atomic_Op_Ex<ACE_Token, int>
#pragma instantiate ACE_Atomic_Op_Ex<ACE_Thread_Mutex, int>
#pragma instantiate ACE_Atomic_Op<ACE_Token, int>
#pragma instantiate ACE_Atomic_Op_Ex<ACE_Token, int>
#pragma instantiate ACE_Guard<ACE_Token>
#pragma instantiate ACE_TSS<TSS_Data>
#pragma instantiate ACE_TSS<TSS_Obj>
#pragma instantiate ACE_TSS_Singleton<TSS_Data, ACE_SYNCH_MUTEX>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
return 0;
}
#endif /* ACE_HAS_THREADS */
|