Skip to content

Commit

Permalink
Remove the sem_init implementation
Browse files Browse the repository at this point in the history
pthread_mutex works on Linux anyway and isn't obviously any better at
coping with fork() issues.
  • Loading branch information
public committed Dec 26, 2013
1 parent b1f38cf commit a193164
Showing 1 changed file with 2 additions and 93 deletions.
95 changes: 2 additions & 93 deletions cryptography/hazmat/backends/openssl/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@
/* XXX complain? */;
}
#elif defined(__APPLE__)
#else
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
Expand Down Expand Up @@ -178,97 +178,6 @@
CHECK_STATUS("pthread_mutex_unlock[3]");
}
#else
#include <unistd.h>
#include <semaphore.h>
#define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
struct CryptographyOpaque_ThreadLock {
sem_t sem;
int initialized;
};
typedef struct CryptographyOpaque_ThreadLock CryptographyOpaque_ThreadLock;
int CryptographyThreadLockInit
(struct CryptographyOpaque_ThreadLock *lock)
{
int status, error = 0;
lock->initialized = 0;
status = sem_init(&lock->sem, 0, 1);
CHECK_STATUS("sem_init");
if (error)
return 0;
lock->initialized = 1;
return 1;
}
void CryptographyOpaqueDealloc_ThreadLock
(struct CryptographyOpaque_ThreadLock *lock)
{
int status, error = 0;
if (lock->initialized) {
status = sem_destroy(&lock->sem);
CHECK_STATUS("sem_destroy");
/* 'error' is ignored;
CHECK_STATUS already printed an error message */
}
}
/*
* As of February 2002, Cygwin thread implementations mistakenly report error
* codes in the return value of the sem_ calls (like the pthread_ functions).
* Correct implementations return -1 and put the code in errno. This supports
* either.
*/
static int
rpythread_fix_status(int status)
{
return (status == -1) ? errno : status;
}
CryptographyLockStatus CryptographyThreadAcquireLock
(struct CryptographyOpaque_ThreadLock *lock, int intr_flag)
{
CryptographyLockStatus success;
sem_t *thelock = &lock->sem;
int status, error = 0;
struct timespec ts;
do {
status = rpythread_fix_status(sem_wait(thelock));
/* Retry if interrupted by a signal, unless the caller wants to be
notified. */
} while (!intr_flag && status == EINTR);
/* Don't check the status if we're stopping because of an interrupt. */
if (!(intr_flag && status == EINTR)) {
CHECK_STATUS("sem_wait");
}
if (status == 0) {
success = CRYPTOGRAPHY_LOCK_ACQUIRED;
} else if (intr_flag && status == EINTR) {
success = CRYPTOGRAPHY_LOCK_INTR;
} else {
success = CRYPTOGRAPHY_LOCK_FAILURE;
}
return success;
}
void CryptographyThreadReleaseLock
(struct CryptographyOpaque_ThreadLock *lock)
{
sem_t *thelock = &lock->sem;
int status, error = 0;
status = sem_post(thelock);
CHECK_STATUS("sem_post");
}
#endif
static int Cryptography_lock_count = -1;
Expand Down

0 comments on commit a193164

Please sign in to comment.