1 // This test program verifies the functionality of the ACE_OS
2 // implementation of recursive mutexes on Win32 and Posix pthreads.
4 #include "ace/OS_main.h"
5 #include "ace/Service_Config.h"
6 #include "ace/Thread_Manager.h"
7 #include "ace/Get_Opt.h"
10 #if defined (ACE_HAS_THREADS)
12 #include "ace/Guard_T.h"
13 #include "ace/Recursive_Thread_Mutex.h"
15 // Total number of iterations.
16 static size_t n_iterations
= 1000;
17 static size_t n_threads
= 4;
19 // Explain usage and exit.
21 print_usage_and_die ()
24 "usage: %n [-t n_threads] [-n iteration_count]\n"));
28 // Parse the command-line arguments and set options.
31 parse_args (int argc
, ACE_TCHAR
*argv
[])
33 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("n:t:"));
37 while ((c
= get_opt ()) != -1)
41 n_iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
44 n_threads
= ACE_OS::atoi (get_opt
.opt_arg ());
47 print_usage_and_die ();
53 recursive_worker (size_t nesting_level
,
54 ACE_Recursive_Thread_Mutex
*rm
)
56 if (nesting_level
< n_iterations
)
59 "(%P|%t) = trying to acquire, nesting = %d, thread id = %u\n",
60 rm
->get_nesting_level (), rm
->get_thread_id ()));
62 // This illustrates the use of the ACE_GUARD with an
63 // ACE_Recursive_Thread_Mutex.
64 ACE_GUARD (ACE_Recursive_Thread_Mutex
, ace_mon
, *rm
);
66 "(%P|%t) = acquired, nesting = %d, thread id = %u\n",
67 rm
->get_nesting_level (), rm
->get_thread_id ()));
69 recursive_worker (nesting_level
+ 1, rm
);
72 "(%P|%t) = released, nesting = %d, thread id = %u\n",
73 rm
->get_nesting_level (), rm
->get_thread_id ()));
80 ACE_Recursive_Thread_Mutex
*rm
81 = (ACE_Recursive_Thread_Mutex
*) arg
;
83 recursive_worker (0, rm
);
88 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
90 ACE_Service_Config
daemon (argv
[0]);
92 parse_args (argc
, argv
);
93 ACE_Recursive_Thread_Mutex rm
;
95 ACE_Thread_Manager::instance ()->spawn_n (n_threads
,
96 ACE_THR_FUNC (worker
),
99 ACE_Thread_Manager::instance ()->wait ();
104 ACE_TMAIN (int, ACE_TCHAR
*[])
106 ACE_ERROR_RETURN ((LM_ERROR
,
107 "ACE doesn't support support process mutexes on this platform (yet)\n"),
110 #endif /* ACE_WIN32 */