| 1 | |
| 2 | //============================================================================= |
| 3 | /** |
| 4 | * @file collection.cpp |
| 5 | * |
| 6 | * Shows how applications can use the ACE_Token_Collection |
| 7 | * utility. This example creates three collections and spawns a |
| 8 | * thread to operate on each. The threads use the collective |
| 9 | * acquire, renew, and release features of ACE_Token_Collection. |
| 10 | * |
| 11 | * @author Tim Harrison |
| 12 | */ |
| 13 | //============================================================================= |
| 14 | |
| 15 | |
| 16 | #include "ace/Get_Opt.h" |
| 17 | #include "ace/Local_Tokens.h" |
| 18 | #include "ace/Token_Collection.h" |
| 19 | #include "ace/Remote_Tokens.h" |
| 20 | #include "ace/Thread_Manager.h" |
| 21 | #include "ace/Service_Config.h" |
| 22 | |
| 23 | |
| 24 | #if defined (ACE_HAS_THREADS) && defined (ACE_HAS_THREADS_LIBRARY) |
| 25 | |
| 26 | static const char *server_host = ACE_DEFAULT_SERVER_HOST; |
| 27 | static int server_port = ACE_DEFAULT_SERVER_PORT; |
| 28 | // unused: static int threads = 2; |
| 29 | static int iterations = 50; |
| 30 | static int debug = 0; |
| 31 | static int remote = 0; |
| 32 | // unused: static int tokens = 5; |
| 33 | |
| 34 | static void * |
| 35 | run_thread (void *vp) |
| 36 | { |
| 37 | ACE_Token_Proxy *collection = (ACE_Token_Proxy *) vp; |
| 38 | |
| 39 | int count = iterations; |
| 40 | while (count--) |
| 41 | { |
| 42 | if (collection->acquire () == -1) |
| 43 | { |
| 44 | if (ACE_OS::last_error () == EDEADLK) |
| 45 | { |
| 46 | ACE_DEBUG ((LM_DEBUG, "deadlock detected in acquire")); |
| 47 | continue; |
| 48 | } |
| 49 | ACE_ERROR ((LM_ERROR, "(%t) %p acquire failed\n","run_thread")); |
| 50 | return (void *) -1; |
| 51 | } |
| 52 | |
| 53 | ACE_DEBUG ((LM_DEBUG, "(%t) %s acquired.\n", collection->name ())); |
| 54 | |
| 55 | if (collection->renew () == -1) |
| 56 | { |
| 57 | if (ACE_OS::last_error () == EDEADLK) |
| 58 | { |
| 59 | ACE_DEBUG ((LM_DEBUG, "deadlock detected")); |
| 60 | goto deadlock; |
| 61 | } |
| 62 | ACE_ERROR ((LM_ERROR, "(%t) %p renew failed\n","run_thread")); |
| 63 | return (void *) -1; |
| 64 | } |
| 65 | |
| 66 | ACE_DEBUG ((LM_DEBUG, "(%t) %s renewed.\n", collection->name ())); |
| 67 | |
| 68 | deadlock: |
| 69 | if (collection->release () == -1) |
| 70 | { |
| 71 | ACE_ERROR ((LM_ERROR, "(%t) %p release failed\n","run_thread")); |
| 72 | return (void *) -1; |
| 73 | } |
| 74 | |
| 75 | ACE_DEBUG ((LM_DEBUG, "(%t) %s released.\n", collection->name ())); |
| 76 | } |
| 77 | |
| 78 | ACE_DEBUG ((LM_DEBUG, "(%t) thread exiting.\n")); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int |
| 83 | parse_args (int argc, ACE_TCHAR *argv[]) |
| 84 | { |
| 85 | ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR); // | ACE_Log_Msg::VERBOSE); |
| 86 | |
| 87 | ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("un:dp:h:"), 1); |
| 88 | |
| 89 | for (int c; (c = get_opt ()) != -1; ) |
| 90 | { |
| 91 | switch (c) |
| 92 | { |
| 93 | case 'h': // specify the host machine on which the server is running |
| 94 | server_host = get_opt.opt_arg (); |
| 95 | remote = 1; |
| 96 | break; |
| 97 | case 'p': // specify the port on which the server is running |
| 98 | server_port = ACE_OS::atoi (get_opt.opt_arg ()); |
| 99 | remote = 1; |
| 100 | break; |
| 101 | case 'd': |
| 102 | debug = 1; |
| 103 | break; |
| 104 | case 'n': |
| 105 | iterations = ACE_OS::atoi (get_opt.opt_arg ()); |
| 106 | break; |
| 107 | case 'u': |
| 108 | // usage same as unknown. |
| 109 | ACE_FALLTHROUGH; |
| 110 | default: |
| 111 | ACE_ERROR_RETURN ((LM_ERROR, |
| 112 | "%n:\n" |
| 113 | "[-h <remote host>]\n" |
| 114 | "[-p <remote port>]\n" |
| 115 | "[-n <iterations>]\n" |
| 116 | "[-d debug]\n", 1), -1); |
| 117 | /* NOTREACHED */ |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int |
| 125 | ACE_TMAIN(int argc, ACE_TCHAR *argv[]) |
| 126 | { |
| 127 | if (parse_args (argc, argv) == -1) |
| 128 | return -1; |
| 129 | |
| 130 | ACE_Token_Proxy *A; // Mutex *A*. |
| 131 | ACE_Token_Proxy *B; // Mutex *B*. |
| 132 | ACE_Token_Proxy *R; // *R*eader Lock. |
| 133 | ACE_Token_Proxy *W; // *W*riter Lock. |
| 134 | |
| 135 | // Depending on the command line arguments, we will create local or |
| 136 | // remote tokens. The names of the tokens are not important as long |
| 137 | // as they are unique. |
| 138 | if (remote) |
| 139 | { |
| 140 | ACE_Remote_Mutex::set_server_address (ACE_INET_Addr (server_port, server_host)); |
| 141 | A = new ACE_Remote_Mutex ("R Mutex A", 0, debug); |
| 142 | B = new ACE_Remote_Mutex ("R Mutex B", 0, debug); |
| 143 | R = new ACE_Remote_RLock ("R Reader Lock", 0, debug); |
| 144 | W = new ACE_Remote_WLock ("R Writer Lock", 0, debug); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | A = new ACE_Local_Mutex ("L Mutex A", 0, debug); |
| 149 | B = new ACE_Local_Mutex ("L Mutex B", 0, debug); |
| 150 | R = new ACE_Local_RLock ("L Reader Lock", 0, debug); |
| 151 | W = new ACE_Local_WLock ("L Writer Lock", 0, debug); |
| 152 | } |
| 153 | |
| 154 | // These collections will be treated as Tokens by the threads. |
| 155 | ACE_Token_Collection collectionAR (debug, "A and Reader"); |
| 156 | ACE_Token_Collection collectionAW (debug, "A and Writer"); |
| 157 | ACE_Token_Collection collectionBR (debug, "B and Reader"); |
| 158 | |
| 159 | // AR and BR can run concurrently. Neither AR or BR can run when AW |
| 160 | // is running. |
| 161 | collectionAR.insert (*A); |
| 162 | collectionAR.insert (*R); |
| 163 | |
| 164 | collectionAW.insert (*A); |
| 165 | collectionAW.insert (*W); |
| 166 | |
| 167 | collectionBR.insert (*B); |
| 168 | collectionBR.insert (*R); |
| 169 | |
| 170 | // Spawn off three threads. |
| 171 | ACE_Thread_Manager *mgr = ACE_Thread_Manager::instance (); |
| 172 | |
| 173 | if (mgr->spawn (ACE_THR_FUNC (run_thread), |
| 174 | (void *) &collectionAR, THR_BOUND | THR_SUSPENDED) == -1) |
| 175 | ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn 1 failed"), -1); |
| 176 | |
| 177 | if (mgr->spawn (ACE_THR_FUNC (run_thread), |
| 178 | (void *) &collectionAW, THR_BOUND | THR_SUSPENDED) == -1) |
| 179 | ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn 2 failed"), -1); |
| 180 | |
| 181 | if (mgr->spawn (ACE_THR_FUNC (run_thread), |
| 182 | (void *) &collectionBR, THR_BOUND | THR_SUSPENDED) == -1) |
| 183 | ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "spawn 3 failed"), -1); |
| 184 | |
| 185 | #if ! defined (ACE_HAS_PTHREADS) |
| 186 | if (mgr->resume_all () == -1) |
| 187 | ACE_ERROR_RETURN ((LM_DEBUG, "%p\n", "resume failed"), -1); |
| 188 | #endif |
| 189 | |
| 190 | // Wait for all threads to exit. |
| 191 | mgr->wait (); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | #else |
| 197 | int |
| 198 | ACE_TMAIN(int, ACE_TCHAR *[]) |
| 199 | { |
| 200 | ACE_ERROR_RETURN ((LM_ERROR, |
| 201 | "threads not supported on this platform\n"), -1); |
| 202 | } |
| 203 | #endif /* ACE_HAS_THREADS && ACE_HAS_TOKENS_LIBRARY */ |