summaryrefslogtreecommitdiff
path: root/examples/Threads/test_tss1.cpp
blob: 7efdc9dc3efa80a3a9cb479e414fa1d1dc5f1fbf (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
// 
// = FILENAME
//    TSS_Test.cpp
//
// = DESCRIPTION
//     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. 
//
// = AUTHOR
//    Detlef Becker
// 
// ============================================================================

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

#if defined (ACE_HAS_THREADS)

static int iterations = 100;

class Errno
{
public:
  int error (void) { return this->errno_; }
  void error (int i) { this->errno_ = i; }
  
  int line (void) { return this->lineno_; }
  void line (int l) { this->lineno_ = l; }

  // Errno::flags_ is a static variable, so we've got to protect it
  // with a mutex since it isn't kept in thread-specific storage.
  int flags (void) { 
    ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_Mon, Errno::lock_, -1));

    return Errno::flags_;
  }
  int flags (int f)
  {
    ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, Errno::lock_, -1));

    Errno::flags_ = f;
    return 0;
  }

private:
  // = errno_ and lineno_ will be thread-specific data so they don't
  // need a lock.  
  int errno_;
  int lineno_;

  static int flags_;
#if defined (ACE_HAS_THREADS)
  // flags_ needs a lock. 
  static ACE_Thread_Mutex lock_;
#endif /* ACE_HAS_THREADS */
};

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

// This is our thread-specific error handler...
static ACE_TSS<Errno> TSS_Error;

#if defined (ACE_HAS_THREADS)
// Serializes output via cout.
static ACE_Thread_Mutex lock;

typedef ACE_TSS_Guard<ACE_Thread_Mutex> GUARD;
#else
// Serializes output via cout.
static ACE_Null_Mutex lock;

typedef ACE_Guard<ACE_Null_Mutex> GUARD;
#endif /* ACE_HAS_THREADS */

// Keeps track of whether Tester::close () has started.
static int close_started = 0;

template <ACE_SYNCH_1>
class Tester: public ACE_Task<ACE_SYNCH_2>
{
public:
  Tester (void) {}
  ~Tester (void) {}

  virtual int open (void *theArgs = 0);
  virtual int close (u_long theArg = 0);
  virtual int put (ACE_Message_Block *theMsgBlock,
		   ACE_Time_Value *theTimeVal = 0);
  virtual int svc (void);
};

template <ACE_SYNCH_1> int 
Tester<ACE_SYNCH_2>::open (void *)
{
  return this->activate ();
}

template <ACE_SYNCH_1>
int Tester<ACE_SYNCH_2>::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG, "close running\n!"));
  close_started = 1;
  ACE_OS::sleep (2);
  ACE_DEBUG ((LM_DEBUG, "close: trying to log error code 7!\n"));
  TSS_Error->error (7);
  ACE_DEBUG ((LM_DEBUG, "close: logging succeeded!\n"));
  return 0;
}

template <ACE_SYNCH_1> int 
Tester<ACE_SYNCH_2>::put (ACE_Message_Block *, ACE_Time_Value *)
{
  return 0;
}

template <ACE_SYNCH_1> int 
Tester<ACE_SYNCH_2>::svc (void)
{
  return 0;
}

int 
main (int, char *[])
{
  Tester<ACE_MT_SYNCH> tester;

  tester.open ();

  while (!close_started)
    continue;

  ACE_DEBUG ((LM_DEBUG, "main: trying to log error code 7!\n"));

  TSS_Error->error (3);

  ACE_DEBUG ((LM_DEBUG, "main: logging succeeded!\n"));
  return 0;
}

#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION)
template class ACE_TSS<Errno>;
#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */

#else
int 
main (void)
{
  ACE_ERROR_RETURN ((LM_ERROR, 
		     "ACE doesn't support support threads on this platform (yet)\n"),
		    -1);
}
#endif /* ACE_HAS_THREADS */