diff options
| author | Brent Cook <busterb@gmail.com> | 2019-01-01 02:40:59 -0600 |
|---|---|---|
| committer | Brent Cook <busterb@gmail.com> | 2019-01-01 15:44:37 -0600 |
| commit | e50817195679a6156ca331c15ecef355b43e81dc (patch) | |
| tree | d5b50c13c9c3562c7817fb609adedc83ffd8f97c | |
| parent | 0f0bec5a76cffe27293c8f88449e86d674f7d9ef (diff) | |
| download | portable-e50817195679a6156ca331c15ecef355b43e81dc.tar.gz portable-e50817195679a6156ca331c15ecef355b43e81dc.tar.bz2 portable-e50817195679a6156ca331c15ecef355b43e81dc.zip | |
make locks self-initialize, switch to critical sections
| -rw-r--r-- | crypto/compat/crypto_lock_win.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/crypto/compat/crypto_lock_win.c b/crypto/compat/crypto_lock_win.c index ef21eee..43a7f05 100644 --- a/crypto/compat/crypto_lock_win.c +++ b/crypto/compat/crypto_lock_win.c | |||
| @@ -19,16 +19,7 @@ | |||
| 19 | 19 | ||
| 20 | #include <openssl/crypto.h> | 20 | #include <openssl/crypto.h> |
| 21 | 21 | ||
| 22 | static HANDLE locks[CRYPTO_NUM_LOCKS]; | 22 | static volatile LPCRITICAL_SECTION locks[CRYPTO_NUM_LOCKS] = { NULL }; |
| 23 | |||
| 24 | void | ||
| 25 | crypto_init_locks(void) | ||
| 26 | { | ||
| 27 | int i; | ||
| 28 | |||
| 29 | for (i = 0; i < CRYPTO_NUM_LOCKS; i++) | ||
| 30 | locks[i] = CreateMutex(NULL, FALSE, NULL); | ||
| 31 | } | ||
| 32 | 23 | ||
| 33 | void | 24 | void |
| 34 | CRYPTO_lock(int mode, int type, const char *file, int line) | 25 | CRYPTO_lock(int mode, int type, const char *file, int line) |
| @@ -36,10 +27,20 @@ CRYPTO_lock(int mode, int type, const char *file, int line) | |||
| 36 | if (type < 0 || type >= CRYPTO_NUM_LOCKS) | 27 | if (type < 0 || type >= CRYPTO_NUM_LOCKS) |
| 37 | return; | 28 | return; |
| 38 | 29 | ||
| 30 | if (locks[type] == NULL) { | ||
| 31 | LPCRITICAL_SECTION lcs = malloc(sizeof(CRITICAL_SECTION)); | ||
| 32 | InitializeCriticalSection(lcs); | ||
| 33 | if (InterlockedCompareExchangePointer((void **)&locks[type], (void *)lcs, NULL) != NULL) | ||
| 34 | { | ||
| 35 | DeleteCriticalSection(lcs); | ||
| 36 | free(lcs); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 39 | if (mode & CRYPTO_LOCK) | 40 | if (mode & CRYPTO_LOCK) |
| 40 | WaitForSingleObject(locks[type], INFINITE); | 41 | EnterCriticalSection(locks[type]); |
| 41 | else | 42 | else |
| 42 | ReleaseMutex(locks[type]); | 43 | LeaveCriticalSection(locks[type]); |
| 43 | } | 44 | } |
| 44 | 45 | ||
| 45 | int | 46 | int |
