Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Threads / tss2.cpp
blobeba9b59e29d6051419c19376ad345aa8bbf796a5
2 //=============================================================================
3 /**
4 * @file tss2.cpp
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"
18 #include "TSS_Data.h"
19 #include "TSS_Obj.h"
20 #include "TSS_Task.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);
31 int num_tasks = 0;
33 // ACE synchronization object.
34 static ACE_Token token;
36 ACE_Atomic_Op<ACE_Thread_Mutex, int> TSS_Obj::count_ = 0;
38 TSS_Obj::TSS_Obj ()
40 TSS_Obj::count_++;
41 ACE_DEBUG ((LM_DEBUG, "(%t) TSS_Obj+: %d\n", TSS_Obj::count_.value ()));
44 TSS_Obj::~TSS_Obj ()
46 TSS_Obj::count_--;
47 ACE_DEBUG ((LM_DEBUG, "(%t) TSS_Obj-: %d\n", TSS_Obj::count_.value ()));
50 Test_Task::Test_Task ()
52 Test_Task::count_++;
53 ACE_DEBUG ((LM_DEBUG,
54 "(%t) Test_Task+: %d\n", Test_Task::count_.value ()));
57 Test_Task::~Test_Task ()
59 Test_Task::count_--;
61 ACE_DEBUG ((LM_DEBUG,
62 "(%t) Test_Task-: %d\n", Test_Task::count_.value ()));
63 Test_Task::wait_count_--;
66 void *
67 Test_Task::svc (void *arg)
69 // When the thread exits this thread-specific object will be deleted
70 // automatically.
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",
79 arg));
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);
87 ACE_Thread::yield ();
90 ACE_DEBUG ((LM_DEBUG, "(%t) svc: waiting finished (data = %u)\n",
91 arg));
93 #if 0
94 ACE_ASSERT (TSS_DATA::instance ()->data () == arg);
95 #endif
97 delete (Test_Task *) arg;
99 return 0;
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);
108 return 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"));
137 for (;;)
139 ACE_Thread::yield ();
141 if (Test_Task::max_count_ != 0 )
142 break;
145 ACE_DEBUG ((LM_DEBUG, "(%t) First thread started\n"
146 "Waiting for all threads finished\n"));
148 for (;;)
150 if (!(Test_Task::max_count_ == num_tasks
151 && Test_Task::wait_count_ == 0))
153 ACE_Thread::yield ();
154 continue;
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 ()));
161 break;
164 ACE_DEBUG ((LM_DEBUG, "(%t) all threads finished\n"));
167 return 0;
170 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_TSS_Singleton, TSS_Data, ACE_SYNCH_MUTEX);
173 #else
176 ACE_TMAIN (int, ACE_TCHAR *[])
178 ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
179 return 0;
181 #endif /* ACE_HAS_THREADS */