2 //=============================================================================
6 * This program tests various features of ACE_Thread and the
7 * thread-specific storage variant of <ACE_Singleton>.
9 * @author Prashant Jain and Doug Schmidt
11 //=============================================================================
14 #include "ace/OS_main.h"
15 #include "ace/Thread.h"
16 #include "ace/Log_Msg.h"
17 #include "ace/Atomic_Op.h"
23 #if defined (ACE_HAS_THREADS)
25 const int MAX_TASKS
= 4;
26 const int MAX_ITERATIONS
= 10;
28 ACE_Atomic_Op
<ACE_Token
, int> Test_Task::count_ (0);
29 ACE_Atomic_Op
<ACE_Token
, int> Test_Task::wait_count_ (0);
30 ACE_Atomic_Op
<ACE_Token
, int> Test_Task::max_count_ (0);
33 // ACE synchronization object.
34 static ACE_Token token
;
36 ACE_Atomic_Op
<ACE_Thread_Mutex
, int> TSS_Obj::count_
= 0;
41 ACE_DEBUG ((LM_DEBUG
, "(%t) TSS_Obj+: %d\n", TSS_Obj::count_
.value ()));
47 ACE_DEBUG ((LM_DEBUG
, "(%t) TSS_Obj-: %d\n", TSS_Obj::count_
.value ()));
50 Test_Task::Test_Task ()
54 "(%t) Test_Task+: %d\n", Test_Task::count_
.value ()));
57 Test_Task::~Test_Task ()
62 "(%t) Test_Task-: %d\n", Test_Task::count_
.value ()));
63 Test_Task::wait_count_
--;
67 Test_Task::svc (void *arg
)
69 // When the thread exits this thread-specific object will be deleted
71 ACE_TSS
<TSS_Obj
> tss (new TSS_Obj
);
73 TSS_DATA::instance ()->data (arg
);
75 Test_Task::wait_count_
++;
76 Test_Task::max_count_
++;
78 ACE_DEBUG ((LM_DEBUG
, "(%t) svc: waiting (data = %u)\n",
81 // Do a bunch of set operations on the TSS data just to make sure
82 // that it's truly in TSS (it it weren't, the assertion would fail).
84 while (Test_Task::max_count_
< num_tasks
)
86 TSS_DATA::instance ()->data (arg
);
90 ACE_DEBUG ((LM_DEBUG
, "(%t) svc: waiting finished (data = %u)\n",
94 ACE_ASSERT (TSS_DATA::instance ()->data () == arg
);
97 delete (Test_Task
*) arg
;
103 Test_Task::open (void *arg
)
105 if (ACE_Thread::spawn ((ACE_THR_FUNC
) Test_Task::svc
, arg
) == -1)
106 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "ACE_Thread::spawn"), 0);
112 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
114 num_tasks
= argc
> 1 ? ACE_OS::atoi (argv
[1]) : MAX_TASKS
;
116 Test_Task
**task_arr
= 0;
118 ACE_NEW_RETURN (task_arr
, Test_Task
*[num_tasks
], -1);
120 for (int i
= 0; i
< MAX_ITERATIONS
; i
++)
122 ACE_DEBUG ((LM_DEBUG
,
123 "(%t) ********* iteration %d **********\n"
124 "Test_Task::max_count_ %d\n",
126 Test_Task::max_count_
.value ()));
127 Test_Task::max_count_
= 0;
129 for (int j
= 0; j
< num_tasks
; j
++)
131 ACE_NEW_RETURN (task_arr
[j
], Test_Task
, -1);
132 task_arr
[j
]->open (task_arr
[j
]);
135 ACE_DEBUG ((LM_DEBUG
, "(%t) waiting for first thread started\n"));
139 ACE_Thread::yield ();
141 if (Test_Task::max_count_
!= 0 )
145 ACE_DEBUG ((LM_DEBUG
, "(%t) First thread started\n"
146 "Waiting for all threads finished\n"));
150 if (!(Test_Task::max_count_
== num_tasks
151 && Test_Task::wait_count_
== 0))
153 ACE_Thread::yield ();
156 ACE_DEBUG ((LM_DEBUG
,
157 "(%t) Test_Task::max_count_ = %d,"
158 " Test_Task::wait_count_ = %d",
159 Test_Task::max_count_
.value (),
160 Test_Task::wait_count_
.value ()));
164 ACE_DEBUG ((LM_DEBUG
, "(%t) all threads finished\n"));
170 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_TSS_Singleton
, TSS_Data
, ACE_SYNCH_MUTEX
);
176 ACE_TMAIN (int, ACE_TCHAR
*[])
178 ACE_ERROR ((LM_ERROR
, "threads not supported on this platform\n"));
181 #endif /* ACE_HAS_THREADS */