Commit 7b56d86e65029a85888d017150a62e2a3b025f32
1 parent
8f07f59b
Exists in
master
and in
5 other branches
Como as filas da sessão passaram a ser indepententes agora é possível usar o lock por sessão.
Showing
5 changed files
with
36 additions
and
118 deletions
Show diff stats
src/classlib/local.cc
| ... | ... | @@ -46,7 +46,8 @@ |
| 46 | 46 | |
| 47 | 47 | #endif |
| 48 | 48 | |
| 49 | -#include <pw3270/class.h> | |
| 49 | +#include "private.h" | |
| 50 | + | |
| 50 | 51 | #include <lib3270/log.h> |
| 51 | 52 | #include <lib3270/popup.h> |
| 52 | 53 | #include <string.h> |
| ... | ... | @@ -114,7 +115,7 @@ |
| 114 | 115 | namespace PW3270_NAMESPACE |
| 115 | 116 | { |
| 116 | 117 | |
| 117 | - class local : public session, private module | |
| 118 | + class local : public session, private module, private recursive_mutex | |
| 118 | 119 | { |
| 119 | 120 | private: |
| 120 | 121 | |
| ... | ... | @@ -243,8 +244,6 @@ |
| 243 | 244 | throw exception("Can't find symbol %s",call[f].name); |
| 244 | 245 | } |
| 245 | 246 | |
| 246 | - session::lock(); | |
| 247 | - | |
| 248 | 247 | // Get Session handle, setup base callbacks |
| 249 | 248 | set_log_handler(loghandler); |
| 250 | 249 | set_trace_handler(tracehandler); |
| ... | ... | @@ -252,13 +251,11 @@ |
| 252 | 251 | |
| 253 | 252 | set_display_charset(); |
| 254 | 253 | |
| 255 | - session::unlock(); | |
| 256 | - | |
| 257 | 254 | } |
| 258 | 255 | |
| 259 | 256 | virtual ~local() |
| 260 | 257 | { |
| 261 | - session::lock(); | |
| 258 | + this->lock(); | |
| 262 | 259 | |
| 263 | 260 | if(is_connected()) { |
| 264 | 261 | disconnect(); |
| ... | ... | @@ -276,7 +273,7 @@ |
| 276 | 273 | } |
| 277 | 274 | catch(exception e) { } |
| 278 | 275 | |
| 279 | - session::unlock(); | |
| 276 | + this->unlock(); | |
| 280 | 277 | |
| 281 | 278 | } |
| 282 | 279 | |
| ... | ... | @@ -300,9 +297,9 @@ |
| 300 | 297 | |
| 301 | 298 | int connect(void) |
| 302 | 299 | { |
| 303 | - session::lock(); | |
| 300 | + this->lock(); | |
| 304 | 301 | int rc = _connect(hSession,0); |
| 305 | - session::unlock(); | |
| 302 | + this->unlock(); | |
| 306 | 303 | |
| 307 | 304 | return rc; |
| 308 | 305 | } |
| ... | ... | @@ -314,9 +311,9 @@ |
| 314 | 311 | |
| 315 | 312 | int disconnect(void) |
| 316 | 313 | { |
| 317 | - session::lock(); | |
| 314 | + this->lock(); | |
| 318 | 315 | int rc = _disconnect(hSession); |
| 319 | - session::unlock(); | |
| 316 | + this->unlock(); | |
| 320 | 317 | |
| 321 | 318 | return rc; |
| 322 | 319 | } |
| ... | ... | @@ -338,9 +335,9 @@ |
| 338 | 335 | |
| 339 | 336 | int iterate(bool wait) |
| 340 | 337 | { |
| 341 | - session::lock(); | |
| 338 | + this->lock(); | |
| 342 | 339 | _main_iterate(hSession,wait); |
| 343 | - session::unlock(); | |
| 340 | + this->unlock(); | |
| 344 | 341 | return 0; |
| 345 | 342 | } |
| 346 | 343 | ... | ... |
src/classlib/session.cc
| ... | ... | @@ -32,7 +32,7 @@ |
| 32 | 32 | #include <string.h> |
| 33 | 33 | #include <malloc.h> |
| 34 | 34 | |
| 35 | - #include <pw3270/class.h> | |
| 35 | + #include "private.h" | |
| 36 | 36 | |
| 37 | 37 | #ifndef WIN32 |
| 38 | 38 | #include <unistd.h> |
| ... | ... | @@ -42,85 +42,6 @@ |
| 42 | 42 | #include <syslog.h> |
| 43 | 43 | #endif // HAVE_SYSLOG |
| 44 | 44 | |
| 45 | -#if __cplusplus < 201103L | |
| 46 | - | |
| 47 | - #define nullptr NULL | |
| 48 | - | |
| 49 | - #ifdef _WIN32 | |
| 50 | - | |
| 51 | - class recursive_mutex { | |
| 52 | - private: | |
| 53 | - HANDLE hMutex; | |
| 54 | - | |
| 55 | - public: | |
| 56 | - recursive_mutex() { | |
| 57 | - hMutex = CreateMutex(NULL,FALSE,NULL); | |
| 58 | - }; | |
| 59 | - | |
| 60 | - ~recursive_mutex() { | |
| 61 | - CloseHandle(hMutex); | |
| 62 | - }; | |
| 63 | - | |
| 64 | - void lock(void) { | |
| 65 | - WaitForSingleObject(hMutex,INFINITE); | |
| 66 | - }; | |
| 67 | - | |
| 68 | - void unlock(void) { | |
| 69 | - ReleaseMutex(hMutex); | |
| 70 | - }; | |
| 71 | - | |
| 72 | - bool try_lock(void) { | |
| 73 | - if(WaitForSingleObject(hMutex,1) == WAIT_OBJECT_0) | |
| 74 | - return true; | |
| 75 | - return false; | |
| 76 | - }; | |
| 77 | - }; | |
| 78 | - | |
| 79 | - #else | |
| 80 | - | |
| 81 | - class recursive_mutex { | |
| 82 | - private: | |
| 83 | - pthread_mutex_t mtx; | |
| 84 | - pthread_mutexattr_t mtxAttr; | |
| 85 | - | |
| 86 | - public: | |
| 87 | - recursive_mutex() { | |
| 88 | - | |
| 89 | - memset(&mtx,0,sizeof(mtx)); | |
| 90 | - memset(&mtxAttr,0,sizeof(mtxAttr)); | |
| 91 | - | |
| 92 | - pthread_mutexattr_init(&mtxAttr); | |
| 93 | - pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_RECURSIVE); | |
| 94 | - pthread_mutex_init(&mtx, &mtxAttr); | |
| 95 | - }; | |
| 96 | - | |
| 97 | - ~recursive_mutex() { | |
| 98 | - pthread_mutex_destroy(&mtx); | |
| 99 | - }; | |
| 100 | - | |
| 101 | - void lock(void) { | |
| 102 | - pthread_mutex_lock(&mtx); | |
| 103 | - }; | |
| 104 | - | |
| 105 | - void unlock(void) { | |
| 106 | - pthread_mutex_unlock(&mtx); | |
| 107 | - }; | |
| 108 | - | |
| 109 | - bool try_lock(void) { | |
| 110 | - return pthread_mutex_trylock(&mtx) == 0; | |
| 111 | - }; | |
| 112 | - }; | |
| 113 | - | |
| 114 | - | |
| 115 | - | |
| 116 | - #endif // _WIN32 | |
| 117 | - | |
| 118 | -#else | |
| 119 | - | |
| 120 | - #include <recursive_mutex> | |
| 121 | - | |
| 122 | -#endif // !c11 | |
| 123 | - | |
| 124 | 45 | |
| 125 | 46 | /*--[ Implement ]--------------------------------------------------------------------------------------------------*/ |
| 126 | 47 | |
| ... | ... | @@ -171,13 +92,12 @@ |
| 171 | 92 | |
| 172 | 93 | static recursive_mutex mtx; |
| 173 | 94 | |
| 174 | - | |
| 175 | - void session::lock() | |
| 95 | + inline void lock() | |
| 176 | 96 | { |
| 177 | 97 | mtx.lock(); |
| 178 | 98 | } |
| 179 | 99 | |
| 180 | - void session::unlock() | |
| 100 | + inline void unlock() | |
| 181 | 101 | { |
| 182 | 102 | mtx.unlock(); |
| 183 | 103 | } |
| ... | ... | @@ -246,15 +166,31 @@ |
| 246 | 166 | // Factory methods and settings |
| 247 | 167 | session * session::create(const char *name) throw (std::exception) |
| 248 | 168 | { |
| 169 | + session *rc = nullptr; | |
| 170 | + | |
| 249 | 171 | trace("%s(%s)",__FUNCTION__,name); |
| 250 | 172 | |
| 251 | - if(factory) | |
| 252 | - return factory(name); | |
| 173 | + lock(); | |
| 174 | + | |
| 175 | + try | |
| 176 | + { | |
| 177 | + if(factory) | |
| 178 | + rc = factory(name); | |
| 179 | + else if(name && *name) | |
| 180 | + rc = create_remote(name); | |
| 181 | + else | |
| 182 | + rc = create_local(); | |
| 183 | + | |
| 184 | + } | |
| 185 | + catch(std::exception &e) | |
| 186 | + { | |
| 187 | + unlock(); | |
| 188 | + throw e; | |
| 189 | + } | |
| 253 | 190 | |
| 254 | - if(name && *name) | |
| 255 | - return create_remote(name); | |
| 191 | + unlock(); | |
| 256 | 192 | |
| 257 | - return create_local(); | |
| 193 | + return rc; | |
| 258 | 194 | } |
| 259 | 195 | |
| 260 | 196 | session * session::start(const char *name) | ... | ... |
src/include/pw3270/class.h
| ... | ... | @@ -132,9 +132,6 @@ |
| 132 | 132 | static void init(); |
| 133 | 133 | static void deinit(); |
| 134 | 134 | |
| 135 | - static void lock(); | |
| 136 | - static void unlock(); | |
| 137 | - | |
| 138 | 135 | // Factory methods and settings |
| 139 | 136 | static session * start(const char *name = 0); |
| 140 | 137 | static session * create(const char *name = 0) throw (std::exception); | ... | ... |
src/java/jni3270.cbp
| ... | ... | @@ -42,6 +42,7 @@ |
| 42 | 42 | <Unit filename="../classlib/exception.cc" /> |
| 43 | 43 | <Unit filename="../classlib/local.cc" /> |
| 44 | 44 | <Unit filename="../classlib/module.cc" /> |
| 45 | + <Unit filename="../classlib/private.h" /> | |
| 45 | 46 | <Unit filename="../classlib/remote.cc" /> |
| 46 | 47 | <Unit filename="../classlib/session.cc" /> |
| 47 | 48 | <Unit filename="../classlib/testprogram.cc" /> | ... | ... |
src/java/main.cc
| ... | ... | @@ -76,14 +76,10 @@ JNIEXPORT jint JNICALL Java_pw3270_terminal_init__Ljava_lang_String_2(JNIEnv *en |
| 76 | 76 | |
| 77 | 77 | try { |
| 78 | 78 | |
| 79 | - session::lock(); | |
| 80 | - | |
| 81 | 79 | jlong handle = reinterpret_cast<jlong>(session::create(id)); |
| 82 | 80 | env->SetLongField(obj, getHandleField(env, obj), handle); |
| 83 | 81 | env->ReleaseStringUTFChars( j_id, id); |
| 84 | 82 | |
| 85 | - session::unlock(); | |
| 86 | - | |
| 87 | 83 | } catch(std::exception &e) { |
| 88 | 84 | |
| 89 | 85 | env->ReleaseStringUTFChars( j_id, id); |
| ... | ... | @@ -99,23 +95,14 @@ JNIEXPORT jint JNICALL Java_pw3270_terminal_deinit(JNIEnv *env, jobject obj) { |
| 99 | 95 | |
| 100 | 96 | try { |
| 101 | 97 | |
| 102 | - session::lock(); | |
| 103 | - | |
| 104 | 98 | session *s = getHandle(env,obj); |
| 105 | 99 | |
| 106 | - trace("********************* Destruindo objeto %p",s); | |
| 107 | - | |
| 108 | 100 | if(s) { |
| 109 | - trace("********************* Destruindo objeto %p",s); | |
| 110 | 101 | delete s; |
| 111 | - trace("********************* Destruindo objeto %p",s); | |
| 112 | 102 | } |
| 113 | 103 | |
| 114 | - trace("********************* Destruindo objeto %p",s); | |
| 115 | 104 | env->SetLongField(obj, getHandleField(env, obj), 0); |
| 116 | - trace("********************* Destruindo objeto %p",s); | |
| 117 | 105 | |
| 118 | - session::unlock(); | |
| 119 | 106 | |
| 120 | 107 | } catch(std::exception &e) { |
| 121 | 108 | ... | ... |