00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Remote_Tokens.h 00006 * 00007 * Remote_Tokens.h,v 4.19 2005/10/28 16:14:55 ossama Exp 00008 * 00009 * @author Douglas C. Schmidt (schmidt@cs.wustl.edu) 00010 * @author Tim Harrison (harrison@cs.wustl.edu) 00011 */ 00012 //============================================================================= 00013 00014 #ifndef ACE_REMOTE_MUTEX_H 00015 #define ACE_REMOTE_MUTEX_H 00016 00017 #include /**/ "ace/pre.h" 00018 00019 #include "ace/INET_Addr.h" 00020 00021 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00022 # pragma once 00023 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00024 00025 #include "ace/SOCK_Connector.h" 00026 #include "ace/SOCK_Stream.h" 00027 #include "ace/Synch_Options.h" 00028 #include "ace/Local_Tokens.h" 00029 #include "ace/Token_Request_Reply.h" 00030 00031 #if defined (ACE_HAS_TOKENS_LIBRARY) 00032 00033 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00034 00035 /** 00036 * @class ACE_Remote_Token_Proxy 00037 * 00038 * @brief Proxy for acquiring, renewing, and releasing a distributed 00039 * synchronization token. 00040 * 00041 * The Remote_Token_Proxy class implements the mechanisms for 00042 * distributed token operations. It is similar to the 00043 * ACE_Token_Proxy. 00044 * = BUGS 00045 * Distributed sleep_hooks have not been implemented. <owner_id> 00046 * is not implemented. 00047 */ 00048 class ACE_Export ACE_Remote_Token_Proxy : public ACE_Token_Proxy 00049 { 00050 public: 00051 /// Null construction. 00052 ACE_Remote_Token_Proxy (void); 00053 00054 /// Death. 00055 virtual ~ACE_Remote_Token_Proxy (void); 00056 00057 /** 00058 * Same as Token_Proxy. <name> is the string uniquely identifying 00059 * the token. <ignore_deadlock> can be 1 to disable deadlock 00060 * notifications. <debug> prints debug messages. 00061 */ 00062 int open (const ACE_TCHAR *name, 00063 int ignore_deadlock = 0, 00064 int debug = 0); 00065 00066 00067 /** 00068 * Open a connection with the token server. This only need be used 00069 * when the user wishes to explicitly open a connection to check if 00070 * the server exists. Connections are stored in the 00071 * ACE_Token_Connections singleton as thread-specific data. That 00072 * is, every thread has only one connection that is used for all 00073 * remote tokens. 00074 */ 00075 int initiate_connection (void); 00076 00077 /** 00078 * Acquire the distributed token. If notify is specified and the 00079 * token is already held, the owner is notified. options contains 00080 * the timeout value for the acquire call. The timer is kept at the 00081 * token server. Asynchronous operations are not supported. 00082 * Returns 0 on success, -1 on failure with <errno> == problem. 00083 */ 00084 virtual int acquire (int notify = 0, 00085 void (*sleep_hook)(void *) = 0, 00086 ACE_Synch_Options &options = 00087 ACE_Synch_Options::synch); 00088 00089 /** 00090 * Try to acquire the distributed token. If the token is already 00091 * held, the call returns without queueing the caller as a waiter. 00092 * Returns 0 on success (the token was acquired), and -1 with 00093 * EWOULDBLOCK if the token was already held. 00094 */ 00095 virtual int tryacquire (void (*sleep_hook)(void *) = 0); 00096 00097 /** 00098 * Renew the token by offering to release it if there are any other 00099 * waiters, otherwise get the token back immediately. This renew 00100 * has the same semantics as ACE_Local_Mutex release. It is 00101 * semantically equivalent to <release> followed by <acquire>, but 00102 * it is faster. options contains the timeout value used if renew 00103 * blocks. As with acquire, the timer is maintained at the token 00104 * server. If there are waiters and requeue_position == -1, the 00105 * caller is queued at the rear of the waiter list. Otherwise, 00106 * requeue_position specifies the number of waiters to "let by" 00107 * before reacquiring the token (effectively, the position in the 00108 * waiter list.) 00109 */ 00110 virtual int renew (int requeue_position = 0, 00111 ACE_Synch_Options &options = 00112 ACE_Synch_Options::synch); 00113 00114 /** 00115 * Release the distributed token. Similar to ACE_Local_Mutex, if the 00116 * caller is not the owner, it is removed from the waiter list (if 00117 * applicable.) Returns 0 on success, -1 on failure with <errno> == 00118 * problem. 00119 */ 00120 virtual int release (ACE_Synch_Options &options = 00121 ACE_Synch_Options::synch); 00122 00123 /// Become interface compliant for ACE_Guard<>. This has no 00124 /// functionality. 00125 virtual int remove (ACE_Synch_Options &options = 00126 ACE_Synch_Options::synch); 00127 00128 /// Override the default to do nothing. 00129 virtual void token_acquired (ACE_TPQ_Entry *); 00130 00131 /// The client id of the current token holder 00132 virtual const ACE_TCHAR* owner_id (void); 00133 00134 /** 00135 * Sets the server address for all instances of ACE_Remote_Token_Proxy 00136 * If this isn't called, the environment variable TOKEN_SERVER is 00137 * checked for the server address. If that is not specified, all 00138 * ACE_Remote_** operations will fail. 00139 */ 00140 static void set_server_address (const ACE_INET_Addr &server_address); 00141 00142 /// Dump the state of the class. 00143 void dump (void) const; 00144 00145 protected: 00146 00147 /// If shadows report deadlock, go remote anyway 00148 int ignore_shadow_deadlock_; 00149 00150 /// Perform the request and wait for the reply. 00151 int request_reply (ACE_Token_Request &request, 00152 ACE_Synch_Options &options); 00153 }; 00154 00155 /** 00156 * @class ACE_Remote_Mutex 00157 * 00158 * @brief Proxy for acquiring, renewing, and releasing a distributed 00159 * mutex. 00160 * 00161 * This is the remote equivalent to ACE_Local_Mutex. The 00162 * Remote_Mutex class offers methods for acquiring, renewing, and 00163 * releasing a distributed synchronization mutex. Similar to 00164 * ACE_Local_Mutex, ACE_Remote_Token_Proxy offers recursive 00165 * acquisition, FIFO waiter ordering, and deadlock detection. It 00166 * depends on the Token Server for its distributed synchronization 00167 * semantics. 00168 */ 00169 class ACE_Export ACE_Remote_Mutex : public ACE_Remote_Token_Proxy 00170 { 00171 public: 00172 /// Null creation. Remote_Token_Proxy::open must be called. 00173 ACE_Remote_Mutex (void); 00174 00175 /// Calls Remote_Token_Proxy::open for you. 00176 ACE_Remote_Mutex (const ACE_TCHAR *token_name, 00177 int ignore_deadlock = 0, 00178 int debug = 0); 00179 00180 /// Dump the state of the class. 00181 void dump (void) const; 00182 00183 /// Return deep copy. 00184 virtual ACE_Token_Proxy *clone (void) const; 00185 00186 protected: 00187 /// Make the correct type of ACE_Tokens. 00188 /// This is called by the ACE_Token_Manager. 00189 virtual ACE_Tokens *create_token (const ACE_TCHAR *name); 00190 }; 00191 00192 /** 00193 * @class ACE_Remote_RLock 00194 * 00195 * @brief Proxy for acquiring, renewing, and releasing a distributed 00196 * readers lock. 00197 * 00198 * This is the remote equivalent to ACE_Local_RLock. Multiple 00199 * readers can hold the lock simultaneously when no writers have 00200 * the lock. Alternatively, when a writer holds the lock, no other 00201 * participants (readers or writers) may hold the lock. 00202 * ACE_Remote_RLock depends on the ACE Token Server for its 00203 * distributed synchronization semantics. 00204 */ 00205 class ACE_Export ACE_Remote_RLock : public ACE_Remote_Token_Proxy 00206 { 00207 public: 00208 ACE_Remote_RLock (void); 00209 00210 ACE_Remote_RLock (const ACE_TCHAR *token_name, 00211 int ignore_deadlock = 0, 00212 int debug = 0); 00213 00214 ACE_Remote_RLock (const ACE_Remote_RLock &mutex); 00215 00216 /// Dump the state of the class. 00217 void dump (void) const; 00218 00219 /// Returns ACE_RW_Token::RLOCK; 00220 virtual int type (void) const; 00221 00222 /// Return deep copy. 00223 virtual ACE_Token_Proxy *clone (void) const; 00224 00225 protected: 00226 /// Make the correct type of ACE_Tokens. This is called by the Token 00227 /// Manager. 00228 virtual ACE_Tokens *create_token (const ACE_TCHAR *name); 00229 }; 00230 00231 /** 00232 * @class ACE_Remote_WLock 00233 * 00234 * @brief Proxy for acquiring, renewing, and releasing a distributed 00235 * writers lock. 00236 * 00237 * Shields applications from details of interacting with the 00238 * ACE_Token_Server. The token_name_ is just the string that the 00239 * Token Server uses to identify the token. The client_id_ (also 00240 * used by the Token Server,) identifies the owner of the token and 00241 * is used for deadlock detection. 00242 */ 00243 class ACE_Export ACE_Remote_WLock : public ACE_Remote_Token_Proxy 00244 { 00245 public: 00246 ACE_Remote_WLock (void); 00247 00248 ACE_Remote_WLock (const ACE_TCHAR *token_name, 00249 int ignore_deadlock = 0, 00250 int debug = 0); 00251 00252 ACE_Remote_WLock (const ACE_Remote_WLock &mutex); 00253 00254 /// Dump the state of the class. 00255 void dump (void) const; 00256 00257 /// Returns ACE_RW_Token::WLOCK; 00258 virtual int type (void) const; 00259 00260 /// Return deep copy. 00261 virtual ACE_Token_Proxy *clone (void) const; 00262 00263 protected: 00264 /// Make the correct type of ACE_Tokens. This is called by the Token 00265 /// Manager. 00266 virtual ACE_Tokens *create_token (const ACE_TCHAR *name); 00267 }; 00268 00269 /** 00270 * @class ACE_TSS_Connection 00271 * 00272 * @brief Class for providing a connection per thread. 00273 * 00274 * ACE_TSS_Connection provides a single access point for all 00275 * threads to access thread-specific connections. This prevents 00276 * resource-sharing problems such as thread serialization. 00277 */ 00278 class ACE_Export ACE_TSS_Connection : public ACE_TSS<ACE_SOCK_Stream> 00279 { 00280 public: 00281 // Necessary to make some compilers work... 00282 ACE_TSS_Connection (void); 00283 ~ACE_TSS_Connection (void); 00284 00285 /// Retrieve the thread's connection 00286 ACE_SOCK_Stream *get_connection (void); 00287 00288 /// Factory Method that creates a new SOCK Stream. 00289 virtual ACE_SOCK_Stream *make_TSS_TYPE (void) const; 00290 00291 /// Inheritence and operator overloading don't mix. Redefine this 00292 /// from ACE_TSS so that we can use it. 00293 operator ACE_SOCK_Stream *(void); 00294 00295 /// Set the server address. 00296 static void set_server_address (const ACE_INET_Addr &server_address); 00297 00298 /// Dump the state of the class. 00299 void dump (void) const; 00300 00301 protected: 00302 /// The address of the Token Server used by all instances of 00303 /// Token_Proxy. 00304 static ACE_INET_Addr server_address_; 00305 00306 private: 00307 /// Private: should not be used 00308 ACE_TSS_Connection (const ACE_TSS_Connection &); 00309 void operator= (const ACE_TSS_Connection &); 00310 }; 00311 00312 ACE_END_VERSIONED_NAMESPACE_DECL 00313 00314 #endif /* ACE_HAS_TOKENS_LIBRARY */ 00315 00316 #if defined (__ACE_INLINE__) 00317 #include "ace/Remote_Tokens.inl" 00318 #endif /* __ACE_INLINE__ */ 00319 00320 #include /**/ "ace/post.h" 00321 00322 #endif /* ACE_REMOTE_TOKEN_H */