Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / Threads / tss1.cpp
blob201aee8885e111cfb0d259c7c9a93a38a80c7149
2 //=============================================================================
3 /**
4 * @file tss1.cpp
6 * This program tests thread specific storage of data. The ACE_TSS
7 * wrapper transparently ensures that the objects of this class
8 * will be placed in thread-specific storage. All calls on
9 * ACE_TSS::operator->() are delegated to the appropriate method
10 * in the Errno class. Note that each thread of control has its
11 * own unique TSS object.
13 * @author Detlef Becker <Detlef.Becker@med.siemens.de>
15 //=============================================================================
18 #include "ace/OS_main.h"
19 #include "ace/Service_Config.h"
20 #include "ace/Task.h"
23 #if defined (ACE_HAS_THREADS)
25 #include "thread_specific.h"
27 static const int iterations = 100;
29 // Static variables.
30 ACE_MT (ACE_Thread_Mutex Errno::lock_);
31 int Errno::flags_;
33 // This is our thread-specific error handler...
34 ACE_TSS<Errno> TSS_Error;
36 // Keeps track of whether Tester::close () has started.
37 // (Sun C++ 4.2 with -O3 won't link if the following is static.)
38 int close_started = 0;
40 template <ACE_SYNCH_DECL>
41 class Tester: public ACE_Task<ACE_SYNCH_USE>
43 public:
44 Tester () {}
45 ~Tester () {}
47 virtual int svc ();
49 //FUZZ: disable check_for_lack_ACE_OS
50 /// Activate the thread.
51 virtual int open (void *args = 0);
53 ///FUZZ: enable check_for_lack_ACE_OS
54 virtual int close (u_long args = 0);
57 template <ACE_SYNCH_DECL> int
58 Tester<ACE_SYNCH_USE>::svc ()
60 ACE_DEBUG ((LM_DEBUG,
61 "(%t) svc: setting error code to 1\n"));
62 TSS_Error->error (1);
64 for (int i = 0; i < iterations; i++)
65 // Print out every tenth iteration.
66 if ((i % 10) == 1)
67 ACE_DEBUG ((LM_DEBUG,
68 "(%t) error = %d\n",
69 TSS_Error->error ()));
70 this->close ();
72 return 0;
75 template <ACE_SYNCH_DECL> int
76 Tester<ACE_SYNCH_USE>::open (void *)
78 // Make this an Active Object.
79 return this->activate ();
82 template <ACE_SYNCH_DECL>
83 int Tester<ACE_SYNCH_USE>::close (u_long)
85 ACE_DEBUG ((LM_DEBUG,
86 "(%t) close running\n"));
87 close_started = 1;
88 ACE_DEBUG ((LM_DEBUG,
89 "(%t) close: setting error code to 7\n"));
90 TSS_Error->error (7);
91 ACE_DEBUG ((LM_DEBUG,
92 "(%t) close: error = %d\n",
93 TSS_Error->error ()));
94 //close_started = 0;
95 return 0;
98 int
99 ACE_TMAIN (int, ACE_TCHAR *[])
101 Tester<ACE_MT_SYNCH> tester;
103 ACE_DEBUG ((LM_DEBUG,
104 "(%t) main: setting error code to 3\n"));
105 TSS_Error->error (3);
106 ACE_DEBUG ((LM_DEBUG,
107 "(%t) main: error = %d\n",
108 TSS_Error->error ()));
110 // Spawn off a thread and make test an Active Object.
111 tester.open ();
113 // Keep looping until <Tester::close> is called.
114 for (int i = 0; !close_started; i++) {
115 // while (!close_started)
116 if ((i % 100) == 0) {
117 ACE_DEBUG ((LM_DEBUG,
118 "(%t) error = %d\n",
119 TSS_Error->error ()));
122 ACE_DEBUG ((LM_DEBUG,
123 "(%t) main: setting error code to 4\n"));
124 TSS_Error->error (4);
125 ACE_DEBUG ((LM_DEBUG,
126 "(%t) main: error = %d\n",
127 TSS_Error->error ()));
129 // Keep looping until <Tester::close> finishes.
130 while (close_started != 0)
131 ACE_DEBUG ((LM_DEBUG,
132 "(%t) error = %d\n",
133 TSS_Error->error ()));
134 return 0;
136 #else
138 ACE_TMAIN (int, ACE_TCHAR *[])
140 ACE_ERROR_RETURN ((LM_ERROR,
141 "ACE doesn't support support threads on this platform (yet)\n"),
142 -1);
144 #endif /* ACE_HAS_THREADS */