| 1 | |
| 2 | //============================================================================= |
| 3 | /** |
| 4 | * @file test_mutex.cpp |
| 5 | * |
| 6 | * @author Tim Harrison |
| 7 | */ |
| 8 | //============================================================================= |
| 9 | |
| 10 | #include "ace/Get_Opt.h" |
| 11 | #include "ace/Local_Tokens.h" |
| 12 | #include "ace/Remote_Tokens.h" |
| 13 | #include "ace/Thread.h" |
| 14 | #include "ace/Thread_Manager.h" |
| 15 | |
| 16 | #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY) |
| 17 | |
| 18 | static ACE_Token_Proxy *mutex; |
| 19 | static int remote_mutexes = 0; |
| 20 | static const char *server_host = ACE_DEFAULT_SERVER_HOST; |
| 21 | static int server_port = ACE_DEFAULT_SERVER_PORT; |
| 22 | static int iterations = 100; |
| 23 | static int spawn_count = 2; |
| 24 | |
| 25 | static void * |
| 26 | run_test (void *) |
| 27 | { |
| 28 | int count = iterations; |
| 29 | // test recursive acquisition of a global proxy |
| 30 | while (count--) |
| 31 | { |
| 32 | if (mutex->acquire () == -1) |
| 33 | { |
| 34 | ACE_ERROR ((LM_ERROR, "(%t) %p acquire failed\n","test_mutex")); |
| 35 | return (void *) -1; |
| 36 | } |
| 37 | |
| 38 | // mutex->acquire (); |
| 39 | if (mutex->renew () == -1) |
| 40 | { |
| 41 | ACE_ERROR ((LM_ERROR, "(%t) %p renew failed\n","test_mutex")); |
| 42 | return (void *) -1; |
| 43 | } |
| 44 | |
| 45 | if (mutex->release () == -1) |
| 46 | { |
| 47 | ACE_ERROR ((LM_ERROR, "(%t) %p release failed\n","test_mutex")); |
| 48 | return (void *) -1; |
| 49 | } |
| 50 | |
| 51 | // mutex->release (); |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int |
| 58 | parse_args (int argc, ACE_TCHAR *argv[]) |
| 59 | { |
| 60 | ACE_LOG_MSG->open (argv[0]); |
| 61 | |
| 62 | ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("t:uh:p:n:"), 1); |
| 63 | |
| 64 | for (int c; (c = get_opt ()) != -1; ) |
| 65 | { |
| 66 | switch (c) |
| 67 | { |
| 68 | case 't': |
| 69 | spawn_count = ACE_OS::atoi (get_opt.opt_arg ()); |
| 70 | break; |
| 71 | case 'h': // specify the host machine on which the server is running |
| 72 | server_host = get_opt.opt_arg (); |
| 73 | remote_mutexes = 1; |
| 74 | break; |
| 75 | case 'p': // specify the port on which the server is running |
| 76 | server_port = ACE_OS::atoi (get_opt.opt_arg ()); |
| 77 | remote_mutexes = 1; |
| 78 | break; |
| 79 | case 'n': // specify the port on which the server is running |
| 80 | iterations = ACE_OS::atoi (get_opt.opt_arg ()); |
| 81 | break; |
| 82 | case 'u': |
| 83 | default: |
| 84 | ACE_ERROR_RETURN ((LM_ERROR, |
| 85 | "%n:\n" |
| 86 | "[-h <remote host>]\n" |
| 87 | "[-p <remote port>]\n" |
| 88 | "[-n <iterations>]\n" |
| 89 | "[-t <threads>]\n" |
| 90 | "[-h <remote host>]\n" |
| 91 | "[-p <remote port>]\n", 1), -1); |
| 92 | /* NOTREACHED */ |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | ACE_TMAIN (int argc, ACE_TCHAR *argv[]) |
| 101 | { |
| 102 | ACE_Thread_Manager thread_mgr; |
| 103 | |
| 104 | if (parse_args (argc, argv) == -1) |
| 105 | return -1; |
| 106 | |
| 107 | if (remote_mutexes) |
| 108 | { |
| 109 | ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port, server_host)); |
| 110 | mutex = new ACE_Remote_Mutex ("Remote TOKEN", 0, 1); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | mutex = new ACE_Local_Mutex ("Local TOKEN", 0, 1); |
| 115 | } |
| 116 | |
| 117 | if (thread_mgr.spawn_n (spawn_count, |
| 118 | ACE_THR_FUNC (run_test), |
| 119 | 0, |
| 120 | THR_BOUND) == -1) |
| 121 | ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn"), -1); |
| 122 | |
| 123 | thread_mgr.wait (); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | #else |
| 128 | int ACE_TMAIN (int, ACE_TCHAR *[]) |
| 129 | { |
| 130 | ACE_ERROR_RETURN ((LM_ERROR, "you must have threads to run this test program\n"), -1); |
| 131 | } |
| 132 | #endif /* ACE_HAS_THREADS */ |