summaryrefslogtreecommitdiff
path: root/ACE/examples/Threads/tss1.cpp
blob: 2ef87c3819d5ce34be5b90356b3f6611a99e8ff6 (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

//=============================================================================
/**
 *  @file    tss1.cpp
 *
 *   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.  Note that each thread of control has its
 *   own unique TSS object.
 *
 *  @author Detlef Becker <Detlef.Becker@med.siemens.de>
 */
//=============================================================================


#include "ace/OS_main.h"
#include "ace/Service_Config.h"
#include "ace/Task.h"



#if defined (ACE_HAS_THREADS)

#include "thread_specific.h"

// (Sun C++ 4.2 with -O3 won't link if the following is not const.)
static const int iterations = 100;

// Static variables.
ACE_MT (ACE_Thread_Mutex Errno::lock_);
int Errno::flags_;

// This is our thread-specific error handler...
// (Sun C++ 4.2 with -O3 won't link if the following is static.)
ACE_TSS<Errno> TSS_Error;

// Keeps track of whether Tester::close () has started.
// (Sun C++ 4.2 with -O3 won't link if the following is static.)
int close_started = 0;

template <ACE_SYNCH_DECL>
class Tester: public ACE_Task<ACE_SYNCH_USE>
{
public:
  Tester (void) {}
  ~Tester (void) {}

  virtual int svc (void);

  //FUZZ: disable check_for_lack_ACE_OS
  /// Activate the thread.
  virtual int open (void *args = 0);

  ///FUZZ: enable check_for_lack_ACE_OS
  virtual int close (u_long args = 0);
};

template <ACE_SYNCH_DECL> int
Tester<ACE_SYNCH_USE>::svc (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%t) svc: setting error code to 1\n"));
  TSS_Error->error (1);

  for (int i = 0; i < iterations; i++)
    // Print out every tenth iteration.
    if ((i % 10) == 1)
      ACE_DEBUG ((LM_DEBUG,
                  "(%t) error = %d\n",
                  TSS_Error->error ()));
  this->close ();

  return 0;
}

template <ACE_SYNCH_DECL> int
Tester<ACE_SYNCH_USE>::open (void *)
{
  // Make this an Active Object.
  return this->activate ();
}

template <ACE_SYNCH_DECL>
int Tester<ACE_SYNCH_USE>::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%t) close running\n"));
  close_started = 1;
  ACE_DEBUG ((LM_DEBUG,
              "(%t) close: setting error code to 7\n"));
  TSS_Error->error (7);
  ACE_DEBUG ((LM_DEBUG,
              "(%t) close: error = %d\n",
              TSS_Error->error ()));
  //close_started = 0;
  return 0;
}

int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  Tester<ACE_MT_SYNCH> tester;

  ACE_DEBUG ((LM_DEBUG,
              "(%t) main: setting error code to 3\n"));
  TSS_Error->error (3);
  ACE_DEBUG ((LM_DEBUG,
              "(%t) main: error = %d\n",
              TSS_Error->error ()));

  // Spawn off a thread and make test an Active Object.
  tester.open ();

  // Keep looping until <Tester::close> is called.
  for (int i = 0; !close_started; i++) {
  // while (!close_started)
    if ((i % 100) == 0) {
      ACE_DEBUG ((LM_DEBUG,
                  "(%t) error = %d\n",
                  TSS_Error->error ()));
    }
  }
  ACE_DEBUG ((LM_DEBUG,
              "(%t) main: setting error code to 4\n"));
  TSS_Error->error (4);
  ACE_DEBUG ((LM_DEBUG,
              "(%t) main: error = %d\n",
              TSS_Error->error ()));

  // Keep looping until <Tester::close> finishes.
  while (close_started != 0)
    ACE_DEBUG ((LM_DEBUG,
                "(%t) error = %d\n",
                TSS_Error->error ()));
  return 0;
}
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
  ACE_ERROR_RETURN ((LM_ERROR,
                     "ACE doesn't support support threads on this platform (yet)\n"),
                    -1);
}
#endif /* ACE_HAS_THREADS */