| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -297,6 +297,7 @@ The Dragon De Monsyne | |||
| 297 | 297 | Skip Montanaro | |
| 298 | 298 | Sape Mullender | |
| 299 | 299 | Sjoerd Mullender | |
| 300 | + Michael Muller | ||
| 300 | 301 | Takahiro Nakayama | |
| 301 | 302 | Travers Naran | |
| 302 | 303 | Fredrik Nehr | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -101,82 +101,113 @@ PyThread__exit_prog(int status) | |||
| 101 | 101 | #endif /* NO_EXIT_PROG */ | |
| 102 | 102 | ||
| 103 | 103 | /* | |
| 104 | - * Lock support. It has too be implemented as semaphores. | ||
| 105 | - * I [Dag] tried to implement it with mutex but I could find a way to | ||
| 106 | - * tell whether a thread already own the lock or not. | ||
| 104 | + * Lock support. This is implemented with an event semaphore and critical | ||
| 105 | + * sections to make it behave more like a posix mutex than its OS/2 | ||
| 106 | + # counterparts. | ||
| 107 | 107 | */ | |
| 108 | + | ||
| 109 | + typedef struct os2_lock_t { | ||
| 110 | + int is_set; | ||
| 111 | + HEV changed; | ||
| 112 | + } *type_os2_lock; | ||
| 113 | + | ||
| 108 | 114 | PyThread_type_lock | |
| 109 | 115 | PyThread_allocate_lock(void) | |
| 110 | 116 | { | |
| 111 | - HMTX aLock; | ||
| 112 | 117 | APIRET rc; | |
| 118 | + type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t)); | ||
| 113 | 119 | ||
| 114 | 120 | dprintf(("PyThread_allocate_lock called\n")); | |
| 115 | 121 | if (!initialized) | |
| 116 | 122 | PyThread_init_thread(); | |
| 117 | 123 | ||
| 118 | - DosCreateMutexSem(NULL, /* Sem name */ | ||
| 119 | - &aLock, /* the semaphore */ | ||
| 120 | - 0, /* shared ? */ | ||
| 121 | - 0); /* initial state */ | ||
| 124 | + lock->is_set = 0; | ||
| 122 | 125 | ||
| 123 | - dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); | ||
| 126 | + DosCreateEventSem(NULL, &lock->changed, 0, 0); | ||
| 124 | 127 | ||
| 125 | - return (PyThread_type_lock) aLock; | ||
| 128 | + dprintf(("%ld: PyThread_allocate_lock() -> %p\n", | ||
| 129 | + PyThread_get_thread_ident(), | ||
| 130 | + lock->changed)); | ||
| 131 | + | ||
| 132 | + return (PyThread_type_lock) lock; | ||
| 126 | 133 | } | |
| 127 | 134 | ||
| 128 | 135 | void | |
| 129 | 136 | PyThread_free_lock(PyThread_type_lock aLock) | |
| 130 | 137 | { | |
| 138 | + type_os2_lock lock = (type_os2_lock)aLock; | ||
| 131 | 139 | dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); | |
| 132 | 140 | ||
| 133 | - DosCloseMutexSem((HMTX)aLock); | ||
| 141 | + DosCloseEventSem(lock->changed); | ||
| 142 | + free(aLock); | ||
| 134 | 143 | } | |
| 135 | 144 | ||
| 136 | 145 | /* | |
| 137 | 146 | * Return 1 on success if the lock was acquired | |
| 138 | 147 | * | |
| 139 | - * and 0 if the lock was not acquired. This means a 0 is returned | ||
| 140 | - * if the lock has already been acquired by this thread! | ||
| 148 | + * and 0 if the lock was not acquired. | ||
| 141 | 149 | */ | |
| 142 | 150 | int | |
| 143 | 151 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) | |
| 144 | 152 | { | |
| 145 | - int success = 1; | ||
| 146 | - ULONG rc, count; | ||
| 153 | + int done = 0; | ||
| 154 | + ULONG count; | ||
| 147 | 155 | PID pid = 0; | |
| 148 | 156 | TID tid = 0; | |
| 157 | + type_os2_lock lock = (type_os2_lock)aLock; | ||
| 149 | 158 | ||
| 150 | 159 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(), | |
| 151 | 160 | aLock, waitflag)); | |
| 152 | 161 | ||
| 153 | - DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count); | ||
| 154 | - if( tid == PyThread_get_thread_ident() ) { /* if we own this lock */ | ||
| 155 | - success = 0; | ||
| 156 | - } else { | ||
| 157 | - rc = DosRequestMutexSem((HMTX) aLock, | ||
| 158 | - (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0)); | ||
| 162 | + while (!done) { | ||
| 163 | + /* if the lock is currently set, we have to wait for the state to change */ | ||
| 164 | + if (lock->is_set) { | ||
| 165 | + if (!waitflag) | ||
| 166 | + return 0; | ||
| 167 | + DosWaitEventSem(lock->changed, SEM_INDEFINITE_WAIT); | ||
| 168 | + } | ||
| 159 | 169 | ||
| 160 | - if( rc != 0) { | ||
| 161 | - success = 0; /* We failed */ | ||
| 170 | + /* | ||
| 171 | + * enter a critical section and try to get the semaphore. If | ||
| 172 | + * it is still locked, we will try again. | ||
| 173 | + */ | ||
| 174 | + if (DosEnterCritSec()) | ||
| 175 | + return 0; | ||
| 176 | + | ||
| 177 | + if (!lock->is_set) { | ||
| 178 | + lock->is_set = 1; | ||
| 179 | + DosResetEventSem(lock->changed, &count); | ||
| 180 | + done = 1; | ||
| 162 | 181 | } | |
| 163 | - } | ||
| 164 | 182 | ||
| 165 | - dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", | ||
| 166 | - PyThread_get_thread_ident(),aLock, waitflag, success)); | ||
| 183 | + DosExitCritSec(); | ||
| 184 | + } | ||
| 167 | 185 | ||
| 168 | - return success; | ||
| 186 | + return 1; | ||
| 169 | 187 | } | |
| 170 | 188 | ||
| 171 | - void | ||
| 172 | - PyThread_release_lock(PyThread_type_lock aLock) | ||
| 189 | + void PyThread_release_lock(PyThread_type_lock aLock) | ||
| 173 | 190 | { | |
| 191 | + type_os2_lock lock = (type_os2_lock)aLock; | ||
| 174 | 192 | dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); | |
| 175 | 193 | ||
| 176 | - if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) { | ||
| 194 | + if (!lock->is_set) { | ||
| 195 | + dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", | ||
| 196 | + PyThread_get_thread_ident(), aLock, GetLastError())); | ||
| 197 | + return; | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + | ||
| 201 | + if (DosEnterCritSec()) { | ||
| 177 | 202 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", | |
| 178 | 203 | PyThread_get_thread_ident(), aLock, GetLastError())); | |
| 204 | + return; | ||
| 179 | 205 | } | |
| 206 | + | ||
| 207 | + lock->is_set = 0; | ||
| 208 | + DosPostEventSem(lock->changed); | ||
| 209 | + | ||
| 210 | + DosExitCritSec(); | ||
| 180 | 211 | } | |
| 181 | 212 | ||
| 182 | 213 | /* | |
| Back | FazBrowse Home | New Git URL |
0 commit comments