summaryrefslogtreecommitdiff
path: root/ACE/tests/Recursive_Mutex_Test.cpp
blob: 4ecd657ffa28666da6d820636f1ed73b1d295477 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

//=============================================================================
/**
 *  @file    Recursive_Mutex_Test.cpp
 *
 *    This test program verifies the functionality of the ACE_OS
 *    implementation of recursive mutexes on Win32 and Posix
 *    pthreads.
 *
 *  @author Prashant Jain <pjain@cs.wustl.edu> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
 */
//=============================================================================


#include "test_config.h"
#include "ace/Get_Opt.h"
#include "ace/Thread_Manager.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Recursive_Thread_Mutex.h"


#if defined (ACE_HAS_THREADS)

#include "ace/Guard_T.h"

// For all platforms except for Windows use the
// ACE_Recursive_Thread_Mutex.  Since Windows only supports timed
// recursive process mutexes and not timed recursive thread mutexes,
// use ACE_Process_Mutex.
#if defined (ACE_HAS_WTHREADS)
# include "ace/Process_Mutex.h"
  typedef ACE_Process_Mutex ACE_TEST_MUTEX;
#else
# include "ace/Thread_Mutex.h"
  using ACE_TEST_MUTEX = ACE_Recursive_Thread_Mutex;
#endif

#if !defined (ACE_HAS_MUTEX_TIMEOUTS)
static int reported_notsup = 0;
#endif /* ACE_HAS_MUTEX_TIMEOUTS */

// Total number of iterations.
static int const n_iterations = 100;
static size_t n_threads = ACE_MAX_THREADS;

// ACE_Recursive_Thread_Mutex::get_nesting_level() will return a
// meaningful value.
#if !defined (ACE_HAS_WTHREADS)
static bool nesting_level_supported = false;
#endif

static void
test_recursion_depth (int nesting_level,
                      ACE_TEST_MUTEX *rm)
{
  if (nesting_level < n_iterations)
    {
#if !defined (ACE_HAS_WTHREADS)
      // This will work for Windows, too, if ACE_TEST_MUTEX is
      // ACE_Recursive_Thread_Mutex instead of ACE_Process_Mutex.
      if (nesting_level_supported
          && nesting_level != 0
          && nesting_level != rm->get_nesting_level ())
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%P|%t) Pre-mutex acquire nesting ")
                      ACE_TEXT ("levels do not match.\n")));
        }
#endif  /* !ACE_HAS_WTHREADS */
      int result = rm->acquire ();
      ACE_TEST_ASSERT (result == 0);
#if !defined (ACE_HAS_WTHREADS)
      if (nesting_level_supported
          && (nesting_level + 1) != rm->get_nesting_level ())
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%P|%t) Post-mutex acquire nesting ")
                      ACE_TEXT ("levels do not match.\n")));
        }

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) = acquired, nesting = %d, thread id = %u\n"),
                  rm->get_nesting_level (),
                  rm->get_thread_id ()));
#endif /* !ACE_HAS_WTHREADS */

      test_recursion_depth (nesting_level + 1,
                            rm);

#if !defined (ACE_HAS_WTHREADS)
      if (nesting_level_supported
          && (nesting_level + 1) != rm->get_nesting_level ())
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%P|%t) Post recursion nesting ")
                      ACE_TEXT ("levels do not match.\n")));
        }
#endif  /* !ACE_HAS_WTHREADS */
      result = rm->release ();
      ACE_TEST_ASSERT (result == 0);
#if !defined (ACE_HAS_WTHREADS)
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) = released, nesting = %d, thread id = %u\n"),
                  rm->get_nesting_level (),
                  rm->get_thread_id ()));

      if (nesting_level_supported
          && nesting_level != 0
          && nesting_level != rm->get_nesting_level ())
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%P|%t) Post-mutex release nesting ")
                      ACE_TEXT ("levels do not match.\n")));
        }
#endif /* !ACE_HAS_WTHREADS */
    }
}

static void
test_timed_wait (int nesting_level,
                 ACE_TEST_MUTEX *rm)
{
  // Make sure that we're inside of a recursive level.
  if (nesting_level == 0)
    test_timed_wait (nesting_level + 1,
                     rm);
  else
    {
      u_int seed = (u_int) ACE_OS::time (0);

      for (size_t i = 0; i < ACE_MAX_ITERATIONS / 2; i++)
        {
          int result = 0;

          // First attempt to acquire the mutex with a timeout to verify
          // that mutex timeouts are working.

          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) = trying timed acquire on ")
                      ACE_TEXT ("iteration %d\n"),
                      i));

          ACE_Time_Value delta (1, 0);  // One second timeout
          ACE_Time_Value timeout = ACE_OS::gettimeofday ();
          timeout += delta;  // Must pass absolute time to acquire().

          if (rm->acquire (timeout) != 0)
            {
              if (errno == ETIME)
                ACE_DEBUG ((LM_DEBUG,
                            ACE_TEXT ("(%P|%t) = mutex acquisition ")
                            ACE_TEXT ("timed out\n")));
              else if (errno == ENOTSUP)
                {
#if !defined (ACE_HAS_MUTEX_TIMEOUTS)
                  if (!reported_notsup)
                    {
                      ACE_DEBUG ((LM_INFO,
                                  ACE_TEXT ("(%P|%t) %p, but ACE_HAS_MUTEX_TIMEOUTS is not defined - Ok\n"),
                                  ACE_TEXT ("mutex timed acquire")));
                      reported_notsup = 1;
                    }
#else
                  ACE_DEBUG ((LM_ERROR,
                              ACE_TEXT ("(%P|%t) %p - maybe ACE_HAS_MUTEX_TIMEOUTS should not be defined?\n"),
                              ACE_TEXT ("mutex timed acquire")));
#endif  /* ACE_HAS_MUTEX_TIMEOUTS */
                }
              else
                {
                  ACE_ERROR ((LM_ERROR,
                              ACE_TEXT ("(%P|%t) %p\n%a"),
                              ACE_TEXT ("mutex timeout failed\n")));
                  return;
                }
            }
          else
            {
              result = rm->release ();
              ACE_TEST_ASSERT (result == 0);
            }

          // Now try the standard mutex.

          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) = trying to acquire on iteration %d\n"),
                      i));
          result = rm->acquire ();
          ACE_TEST_ASSERT (result == 0);
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) = acquired on iteration %d\n"),
                      i));

          // Sleep for a random amount of time between 0 and 2 seconds.
          // Note that it's ok to use rand() here because we are running
          // within the critical section defined by the Thread_Mutex.
          ACE_OS::sleep (ACE_OS::rand_r (&seed) % 2);

          result = rm->release ();
          ACE_TEST_ASSERT (result == 0);
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) = released on iteration %d\n"),
                      i));

          // FUZZ: disable check_for_ACE_Guard
          // Basic ACE_Guard usage - automatically acquire the mutex on
          // guard construction and automatically release it on
          // destruction.
          {
            // Construct an ACE_Guard to implicitly acquire the mutex.
            ACE_Guard<ACE_TEST_MUTEX> guard (*rm);
            ACE_TEST_ASSERT (guard.locked () != 0);

            // Perform some operation which might exit the current scope
            // prematurely, e.g. by returning or throwing an exception.
            // ...

            // ACE_Guard object is destroyed when exiting scope and guard
            // destructor automatically releases mutex.
          }

          // Use an ACE_Guard to automatically acquire a mutex, but release
          // the mutex early.
          {
            // Construct an ACE_Guard to implicitly acquire the mutex.
            ACE_Guard<ACE_TEST_MUTEX> guard (*rm);
            ACE_TEST_ASSERT (guard.locked () != 0);

            // Perform some operation which might exit the current scope
            // prematurely, e.g. by returning or throwing an exception.
            // ...

            // Release the mutex since we no longer need it.
            guard.release ();
            ACE_TEST_ASSERT (guard.locked () == 0);

            // Do something else which does not require the mutex to be locked.
            // ...

            // ACE_Guard object's destructor will not release the mutex.
          }

          // Use an ACE_Guard to automatically acquire a mutex, but
          // relinquish ownership of the lock so that the mutex is not
          // automatically released on guard destruction. This is useful
          // when an operation might not release the mutex in some
          // conditions, in which case responsibility for releasing it is
          // passed to someone else.
          {
            // Construct an ACE_Guard to implicitly acquire the mutex.
            ACE_Guard<ACE_TEST_MUTEX> guard (*rm);
            ACE_TEST_ASSERT (guard.locked () != 0);

            // Perform some operation which might exit the current scope
            // prematurely, e.g. by returning or throwing an exception.
            // ...

            // Relinquish ownership of the mutex lock. Someone else must
            // now release it.
            guard.disown ();
            ACE_TEST_ASSERT (guard.locked () == 0);

            // ACE_Guard object's destructor will not release the mutex.
        }
          // We are now responsible for releasing the mutex.
          result = rm->release ();
          ACE_TEST_ASSERT (result == 0);

          // Construct an ACE_Guard without automatically acquiring the lock.
          {
            // Construct an ACE_Guard object without automatically
            // acquiring the mutex or taking ownership of an existing
            // lock. The third parameter tells the guard that the mutex
            // has not been locked.
            ACE_Guard<ACE_TEST_MUTEX> guard (*rm, 0, 0);
            ACE_TEST_ASSERT (guard.locked () == 0);

            // Conditionally acquire the mutex.
            if (i % 2 == 0)
              {
                guard.acquire ();
                ACE_TEST_ASSERT (guard.locked () != 0);
              }

            // Perform some operation that might exit the current scope
            // prematurely, e.g. by returning or throwing an exception.
            // ...

            // ACE_Guard object is destroyed when exiting scope and guard
            // destructor automatically releases if it was acquired above.
          }

          // Use an ACE_Guard to take ownership of a previously acquired
          // mutex.
          timeout = ACE_OS::gettimeofday ();
          timeout += delta;  // Must pass absolute time to acquire().
          if (rm->acquire (timeout) == 0)
            {
              // Construct an ACE_Guard object without automatically
              // acquiring the mutex, but instead take ownership of the
              // existing lock.  The third parameter tells the guard that
              // the mutex has already been locked.
              ACE_Guard<ACE_TEST_MUTEX> guard (*rm, 0, 1);
              ACE_TEST_ASSERT (guard.locked () != 0);

              // Perform some operation which might exit the current scope
              // prematurely, e.g. by returning or throwing an exception.
              // ...

              // ACE_Guard object is destroyed when exiting scope and guard
              // destructor automatically releases mutex.
            }
          // FUZZ: enable check_for_ACE_Guard
        }

      return;
    }
}

static void *
recursion_worker (void *arg)
{
  ACE_TEST_MUTEX *rm =
    reinterpret_cast<ACE_TEST_MUTEX *> (arg);

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%P|%t) Starting test of recursion depth\n")));
  test_recursion_depth (0, rm);

  return 0;
}

static void *
timed_worker (void *arg)
{
  ACE_TEST_MUTEX *rm =
    reinterpret_cast<ACE_TEST_MUTEX *> (arg);

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%P|%t) Starting test of timed wait\n")));
  test_timed_wait (0, rm);

  return 0;
}

#endif /* ACE_HAS_THREADS */

int
run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("Recursive_Mutex_Test"));

#if defined (ACE_HAS_THREADS)
  if (argc > 1)
    {
      n_threads = ACE_OS::atoi (argv[1]);
    }

  ACE_TEST_MUTEX rm;

#if !defined (ACE_HAS_WTHREADS)
  // This will work for Windows, too, if ACE_TEST_MUTEX is
  // ACE_Recursive_Thread_Mutex instead of ACE_Process_Mutex.
  nesting_level_supported =
    (rm.get_nesting_level () != -1 || errno != ENOTSUP);
#endif  /* !ACE_HAS_WTHREADS */

  ACE_Thread_Manager::instance ()->spawn_n (n_threads,
                                            ACE_THR_FUNC (recursion_worker),
                                            (void *) &rm);
  ACE_Thread_Manager::instance ()->wait ();

  ACE_Thread_Manager::instance ()->spawn_n (n_threads,
                                            ACE_THR_FUNC (timed_worker),
                                            (void *) &rm);
  ACE_Thread_Manager::instance ()->wait ();
#else
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);
  ACE_ERROR ((LM_ERROR,
              ACE_TEXT ("ACE doesn't support recursive process ")
              ACE_TEXT ("mutexes on this platform\n")));
#endif /* ACE_HAS_THREADS */
  ACE_END_TEST;
  return 0;
}