Compare commits

...

Author SHA1 Message Date
  Ofer Sadgat 61fb484fed fixed return value changes 8 years ago
  Ofer Sadgat efb43f433c fixed return value changes 8 years ago
  Ofer Sadgat e70552f2b8 fixed spacing and changed return value 8 years ago
  Ofer Sadgat ddf56fc7d5 removed redeclaration 8 years ago
  Ofer Sadgat a3923612d1 dos2unix + compile fixes 8 years ago
  Ofer Sadgat 2bdf9748f0 added native windows threading support 8 years ago
10 changed files with 454 additions and 125 deletions
Split View
  1. +7
    -1
      configure.ac
  2. +52
    -3
      src/data-types/mailsasl.c
  3. +121
    -62
      src/data-types/mailsem.c
  4. +4
    -0
      src/data-types/mailstream_compress.c
  5. +41
    -20
      src/data-types/mailstream_ssl.c
  6. +1
    -1
      src/data-types/mailstream_ssl_private.h
  7. +119
    -27
      src/engine/mailengine.c
  8. +51
    -5
      src/engine/mailprivacy_gnupg.c
  9. +54
    -6
      src/engine/mailprivacy_smime.c
  10. +4
    -0
      src/windows/win_etpan.h

+ 7
- 1
configure.ac View File

@ -440,8 +440,14 @@ AC_SUBST(DBLIB)
dnl Threading support, if enabled
AC_ARG_ENABLE(threads, [ --disable-threads do not include multithreading support using pthread],,[enable_threads=yes])
AC_ARG_ENABLE(windows-threads, [ --enable-windows-threads include multithreading support using windows threads],[enable_windows_threads=yes],)
if test "x$enable_threads" = "xyes"; then
if test "x$enable_windows_threads" = "xyes"; then
AC_CHECK_HEADERS(pthread.h, [], [])
AC_DEFINE([LIBETPAN_REENTRANT], 1, [Define to include multithreading support])
LIBSUFFIX=
REENTRANT=1
elif test "x$enable_threads" = "xyes"; then
dnl Try to find a good CFLAGS/LDFLAGS for pthreads
AC_CHECK_HEADERS(pthread.h, [], [AC_MSG_ERROR([pthread support required])])
CPPFLAGS="$CPPFLAGS -D_REENTRANT"


+ 52
- 3
src/data-types/mailsasl.c View File

@ -6,45 +6,90 @@
#ifdef USE_SASL
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#include <pthread.h>
#elif (defined WIN32)
#include <windows.h>
#endif
#endif
#include <sasl/sasl.h>
#include <stdlib.h>
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
static pthread_mutex_t sasl_lock = PTHREAD_MUTEX_INITIALIZER;
#elif (defined WIN32)
static CRITICAL_SECTION sasl_lock = { 0 };
static int sasl_lock_init_done = 0;
#endif
#endif
static int sasl_use_count = 0;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#elif (defined WIN32)
void mailsasl_init_lock(){
static int mainsasl_init_lock_done = 0;
if (InterlockedExchange(&mainsasl_init_lock_done, 1) == 0){
InitializeCriticalSection(&sasl_lock);
}
}
#endif
#endif
void mailsasl_external_ref(void)
{
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&sasl_lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&sasl_lock);
#elif (defined WIN32)
EnterCriticalSection(&sasl_lock);
#endif
#endif
sasl_use_count ++;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&sasl_lock);
#elif (defined WIN32)
LeaveCriticalSection(&sasl_lock);
#endif
#endif
}
void mailsasl_ref(void)
{
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&sasl_lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&sasl_lock);
#elif (defined WIN32)
EnterCriticalSection(&sasl_lock);
#endif
#endif
sasl_use_count ++;
if (sasl_use_count == 1)
sasl_client_init(NULL);
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&sasl_lock);
#elif (defined WIN32)
LeaveCriticalSection(&sasl_lock);
#endif
#endif
}
void mailsasl_unref(void)
{
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&sasl_lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&sasl_lock);
#elif (defined WIN32)
EnterCriticalSection(&sasl_lock);
#endif
#endif
sasl_use_count --;
if (sasl_use_count == 0) {
@ -53,7 +98,11 @@ void mailsasl_unref(void)
#endif
}
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&sasl_lock);
#elif (defined WIN32)
LeaveCriticalSection(&sasl_lock);
#endif
#endif
}


+ 121
- 62
src/data-types/mailsem.c View File

@ -46,8 +46,12 @@
#include <unistd.h>
#ifdef LIBETPAN_REENTRANT
#include <pthread.h>
#include <semaphore.h>
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#include <pthread.h>
#include <semaphore.h>
#elif (defined WIN32)
#include <windows.h>
#endif
#endif
struct mailsem_internal {
@ -58,33 +62,45 @@ struct mailsem_internal {
unsigned long waiters_count;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
/* Serialize access to <count> and <waiters_count>. */
pthread_mutex_t lock;
/* Condition variable that blocks the <count> 0. */
pthread_cond_t count_nonzero;
#elif (defined WIN32)
HANDLE semaphore;
#endif
#endif
};
enum {
SEMKIND_SEMOPEN,
SEMKIND_SEMINIT,
SEMKIND_INTERNAL
};
#if (defined(LIBETPAN_REENTRANT) && defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)) || !defined(LIBETPAN_REENTRANT)
static int mailsem_internal_init(struct mailsem_internal * s,
unsigned int initial_count)
unsigned int initial_count)
{
#ifdef LIBETPAN_REENTRANT
int r;
r = pthread_mutex_init(&s->lock, NULL);
if (r != 0)
goto err;
goto err;
r = pthread_cond_init(&s->count_nonzero, NULL);
if (r != 0)
goto destroy_mutex;
goto destroy_mutex;
s->count = initial_count;
s->waiters_count = 0;
return 0;
destroy_mutex:
pthread_mutex_destroy(&s->lock);
err:
@ -106,36 +122,36 @@ int mailsem_internal_wait(struct mailsem_internal * s)
{
#ifdef LIBETPAN_REENTRANT
int r;
/* Acquire mutex to enter critical section. */
r = pthread_mutex_lock(&s->lock);
if (r != 0)
goto err;
goto err;
/* Keep track of the number of waiters so that <sema_post> works correctly. */
s->waiters_count ++;
/* Wait until the semaphore count is > 0, then atomically release */
/* <lock> and wait for <count_nonzero> to be signaled. */
while (s->count == 0) {
r = pthread_cond_wait(&s->count_nonzero, &s->lock);
if (r != 0)
goto unlock;
r = pthread_cond_wait(&s->count_nonzero, &s->lock);
if (r != 0)
goto unlock;
}
/* <s->lock> is now held. */
/* Decrement the waiters count. */
s->waiters_count --;
/* Decrement the semaphore's count. */
s->count --;
/* Release mutex to leave critical section. */
pthread_mutex_unlock(&s->lock);
return 0;
unlock:
pthread_mutex_unlock(&s->lock);
err:
@ -149,25 +165,25 @@ static int mailsem_internal_post(struct mailsem_internal * s)
{
#ifdef LIBETPAN_REENTRANT
int r;
r = pthread_mutex_lock(&s->lock);
if (r != 0)
goto err;
goto err;
/* Always allow one thread to continue if it is waiting. */
if (s->waiters_count > 0) {
r = pthread_cond_signal(&s->count_nonzero);
if (r != 0)
goto unlock;
r = pthread_cond_signal(&s->count_nonzero);
if (r != 0)
goto unlock;
}
/* Increment the semaphore's count. */
s->count ++;
pthread_mutex_unlock(&s->lock);
return 0;
unlock:
pthread_mutex_unlock(&s->lock);
err:
@ -177,11 +193,53 @@ static int mailsem_internal_post(struct mailsem_internal * s)
#endif
}
enum {
SEMKIND_SEMOPEN,
SEMKIND_SEMINIT,
SEMKIND_INTERNAL
};
#elif (defined WIN32)
static int mailsem_internal_init(struct mailsem_internal * s,
unsigned int initial_count)
{
s->semaphore = CreateSemaphore(
NULL, // default security attributes
initial_count, // initial count
0x7FFFFFFF, // maximum count
NULL); // unnamed semaphore
return s->semaphore == NULL ? -1 : 0;
}
static void mailsem_internal_destroy(struct mailsem_internal * s)
{
if (s->semaphore != NULL){
CloseHandle(s->semaphore);
}
}
int mailsem_internal_wait(struct mailsem_internal * s)
{
DWORD dwWaitResult = WAIT_TIMEOUT;
while (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_FAILED){
dwWaitResult = WaitForSingleObject(
s->semaphore, // handle to semaphore
INFINITE); // zero-second time-out interval
}
return dwWaitResult == WAIT_FAILED ? -1 : 0;
}
static int mailsem_internal_post(struct mailsem_internal * s)
{
if (!ReleaseSemaphore(
s->semaphore, // handle to semaphore
1, // increase count by one
NULL) ) // not interested in previous count
{
return -1;
}
return 0;
}
#endif
#if 0
#define SEMNAME_LEN 64
@ -191,43 +249,43 @@ struct mailsem * mailsem_new(void)
#ifdef LIBETPAN_REENTRANT
struct mailsem * sem;
int r;
sem = malloc(sizeof(* sem));
if (sem == NULL)
goto err;
goto err;
sem->sem_sem = malloc(sizeof(sem_t));
if (sem->sem_sem == NULL)
goto free_sem;
goto free_sem;
r = sem_init(sem->sem_sem, 0, 0);
if (r < 0) {
char name[SEMNAME_LEN];
pid_t pid;
free(sem->sem_sem);
pid = getpid();
snprintf(name, sizeof(name), "sem-%p-%i", sem, pid);
#ifndef __CYGWIN__
sem->sem_sem = sem_open(name, O_CREAT | O_EXCL, 0700, 0);
if (sem->sem_sem == (sem_t *) SEM_FAILED)
goto free_sem;
goto free_sem;
sem->sem_kind = SEMKIND_SEMOPEN;
#else
goto free_sem;
#endif
}
else {
sem->sem_kind = SEMKIND_SEMINIT;
sem->sem_kind = SEMKIND_SEMINIT;
}
return sem;
free_sem:
free(sem);
free(sem);
err:
return NULL;
#else
@ -239,20 +297,20 @@ void mailsem_free(struct mailsem * sem)
{
#ifdef LIBETPAN_REENTRANT
if (sem->sem_kind == SEMKIND_SEMOPEN) {
char name[SEMNAME_LEN];
pid_t pid;
pid = getpid();
char name[SEMNAME_LEN];
pid_t pid;
pid = getpid();
#ifndef __CYGWIN__
sem_close((sem_t *) sem->sem_sem);
snprintf(name, sizeof(name), "sem-%p-%i", sem, pid);
sem_unlink(name);
sem_close((sem_t *) sem->sem_sem);
snprintf(name, sizeof(name), "sem-%p-%i", sem, pid);
sem_unlink(name);
#endif
}
else {
sem_destroy((sem_t *) sem->sem_sem);
free(sem->sem_sem);
sem_destroy((sem_t *) sem->sem_sem);
free(sem->sem_sem);
}
free(sem);
#endif
@ -277,6 +335,7 @@ int mailsem_down(struct mailsem * sem)
}
#endif
struct mailsem * mailsem_new(void)
{
struct mailsem * sem;


+ 4
- 0
src/data-types/mailstream_compress.c View File

@ -59,6 +59,10 @@ mailstream_low * mailstream_low_compress_open(mailstream_low * ms)
#define CHUNK_SIZE 1024
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
static ssize_t mailstream_low_compress_read(mailstream_low * s, void * buf, size_t count);
static ssize_t mailstream_low_compress_write(mailstream_low * s, const void * buf, size_t count);
static int mailstream_low_compress_close(mailstream_low * s);


+ 41
- 20
src/data-types/mailstream_ssl.c View File

@ -89,6 +89,9 @@
# ifdef LIBETPAN_REENTRANT
# if HAVE_PTHREAD_H
# include <pthread.h>
# elif defined(WIN32)
void mailprivacy_gnupg_init_lock();
void mailprivacy_smime_init_lock();
# endif
# endif
#endif
@ -133,24 +136,23 @@ struct mailstream_ssl_data {
#endif
#ifdef USE_SSL
#ifdef LIBETPAN_REENTRANT
# if HAVE_PTHREAD_H
# define MUTEX_LOCK(x) pthread_mutex_lock(x)
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)
static pthread_mutex_t ssl_lock = PTHREAD_MUTEX_INITIALIZER;
# elif (defined WIN32)
# define MUTEX_LOCK(x) EnterCriticalSection(x);
# define MUTEX_UNLOCK(x) LeaveCriticalSection(x);
static CRITICAL_SECTION ssl_lock;
# else
# error "What are your threads?"
# endif
#else
# define MUTEX_LOCK(x)
# define MUTEX_UNLOCK(x)
#endif
static int gnutls_init_not_required = 0;
static int openssl_init_done = 0;
# ifdef LIBETPAN_REENTRANT
# if HAVE_PTHREAD_H
# define MUTEX_LOCK(x) pthread_mutex_lock(x)
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)
static pthread_mutex_t ssl_lock = PTHREAD_MUTEX_INITIALIZER;
# elif defined (WIN32)
# define MUTEX_LOCK(x) EnterCriticalSection(x);
# define MUTEX_UNLOCK(x) LeaveCriticalSection(x);
static CRITICAL_SECTION ssl_lock;
void mailsasl_init_lock();
# endif
# else
# define MUTEX_LOCK(x)
# define MUTEX_UNLOCK(x)
# endif
static int gnutls_init_not_required = 0;
static int openssl_init_done = 0;
#endif
// Used to make OpenSSL thread safe
@ -227,10 +229,17 @@ static int openssl_init_done = 0;
#endif
#endif
void mailstream_ssl_init_lock(void)
int mailstream_ssl_init_lock(void)
{
#if !defined (HAVE_PTHREAD_H) && defined (WIN32) && defined (USE_SSL)
InitializeCriticalSection(&ssl_lock);
static long volatile mailstream_ssl_init_lock_done = 0;
int result = 0;
if ((result = InterlockedExchange(&mailstream_ssl_init_lock_done, 1)) == 0){
InitializeCriticalSection(&ssl_lock);
}
return result != 0 ? -1 : 0;
#else
return 0;
#endif
}
@ -260,9 +269,21 @@ void mailstream_ssl_init_not_required(void)
static inline void mailstream_ssl_init(void)
{
if (mailstream_ssl_init_lock() == -1){
return;
}
#ifdef USE_SSL
MUTEX_LOCK(&ssl_lock);
#ifndef USE_GNUTLS
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#elif (defined WIN32)
mailprivacy_gnupg_init_lock();
mailprivacy_smime_init_lock();
mailsasl_init_lock();
mmapstring_init_lock();
#endif
if (!openssl_init_done) {
#if defined (HAVE_PTHREAD_H) && !defined (WIN32) && defined (USE_SSL) && defined (LIBETPAN_REENTRANT)
mailstream_openssl_reentrant_setup();


+ 1
- 1
src/data-types/mailstream_ssl_private.h View File

@ -2,6 +2,6 @@
#define MAILSTREAM_SSL_PRIVATE_H
extern void mailstream_ssl_init_lock(void);
extern int mailstream_ssl_init_lock(void);
#endif

+ 119
- 27
src/engine/mailengine.c View File

@ -47,7 +47,11 @@
#include <stdlib.h>
#include "mailprivacy.h"
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#include <pthread.h>
#elif (defined WIN32)
#include <windows.h>
#endif
#endif
#include <string.h>
@ -62,7 +66,11 @@ struct message_ref_elt {
struct mailfolder * folder;
int lost;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_t lock;
#elif (defined WIN32)
CRITICAL_SECTION lock;
#endif
#endif
};
@ -77,9 +85,13 @@ message_ref_elt_new(struct mailfolder * folder, mailmessage * msg)
goto err;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
r = pthread_mutex_init(&ref->lock, NULL);
if (r != 0)
goto free;
#elif (defined WIN32)
InitializeCriticalSection(&ref->lock);
#endif
#endif
ref->msg = msg;
@ -99,7 +111,11 @@ message_ref_elt_new(struct mailfolder * folder, mailmessage * msg)
static void message_ref_elt_free(struct message_ref_elt * ref_elt)
{
#ifdef LIBETPAN_REENTRANT
pthread_mutex_destroy(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_destroy(&ref_elt->lock);
#elif (defined WIN32)
DeleteCriticalSection(&ref_elt->lock);
#endif
#endif
free(ref_elt);
}
@ -109,14 +125,22 @@ static inline int message_ref(struct message_ref_elt * ref_elt)
int count;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&ref_elt->lock);
#elif (defined WIN32)
EnterCriticalSection(&ref_elt->lock);
#endif
#endif
ref_elt->ref_count ++;
count = ref_elt->ref_count;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&ref_elt->lock);
#elif (defined WIN32)
LeaveCriticalSection(&ref_elt->lock);
#endif
#endif
return count;
@ -127,14 +151,22 @@ static inline int message_unref(struct message_ref_elt * ref_elt)
int count;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&ref_elt->lock);
#elif (defined WIN32)
EnterCriticalSection(&ref_elt->lock);
#endif
#endif
ref_elt->ref_count --;
count = ref_elt->ref_count;
ref_elt->ref_count --;
count = ref_elt->ref_count;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&ref_elt->lock);
#elif (defined WIN32)
LeaveCriticalSection(&ref_elt->lock);
#endif
#endif
return count;
@ -157,15 +189,26 @@ static inline int message_mime_ref(struct mailprivacy * privacy,
message_ref(ref_elt);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&ref_elt->lock);
#elif (defined WIN32)
EnterCriticalSection(&ref_elt->lock);
#endif
#endif
ref_elt->mime_ref_count ++;
count = ref_elt->mime_ref_count;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&ref_elt->lock);
#elif (defined WIN32)
LeaveCriticalSection(&ref_elt->lock);
#endif
#endif
return count;
}
@ -177,7 +220,11 @@ static inline int message_mime_unref(struct mailprivacy * privacy,
message_unref(ref_elt);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&ref_elt->lock);
#elif (defined WIN32)
EnterCriticalSection(&ref_elt->lock);
#endif
#endif
ref_elt->mime_ref_count --;
@ -187,7 +234,11 @@ static inline int message_mime_unref(struct mailprivacy * privacy,
count = ref_elt->mime_ref_count;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&ref_elt->lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&ref_elt->lock);
#elif (defined WIN32)
LeaveCriticalSection(&ref_elt->lock);
#endif
#endif
return count;
@ -1015,7 +1066,11 @@ struct mailengine {
struct mailprivacy * privacy;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_t storage_hash_lock;
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_t storage_hash_lock;
#elif (defined WIN32)
CRITICAL_SECTION storage_hash_lock;
#endif
#endif
/* storage => storage_ref_info */
chash * storage_hash;
@ -1033,12 +1088,20 @@ get_storage_ref_info(struct mailengine * engine,
key.data = &storage;
key.len = sizeof(storage);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&engine->storage_hash_lock);
#endif
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&engine->storage_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&engine->storage_hash_lock);
#endif
#endif
r = chash_get(engine->storage_hash, &key, &data);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&engine->storage_hash_lock);
#endif
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&engine->storage_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&engine->storage_hash_lock);
#endif
#endif
if (r < 0)
return NULL;
@ -1067,12 +1130,20 @@ add_storage_ref_info(struct mailengine * engine,
data.len = 0;
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&engine->storage_hash_lock);
#endif
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&engine->storage_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&engine->storage_hash_lock);
#endif
#endif
r = chash_set(engine->storage_hash, &key, &data, NULL);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&engine->storage_hash_lock);
#endif
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&engine->storage_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&engine->storage_hash_lock);
#endif
#endif
if (r < 0)
goto free;
@ -1098,7 +1169,11 @@ remove_storage_ref_info(struct mailengine * engine,
key.len = sizeof(storage);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_lock(&engine->storage_hash_lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&engine->storage_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&engine->storage_hash_lock);
#endif
#endif
chash_get(engine->storage_hash, &key, &data);
@ -1111,8 +1186,12 @@ remove_storage_ref_info(struct mailengine * engine,
}
#ifdef LIBETPAN_REENTRANT
pthread_mutex_unlock(&engine->storage_hash_lock);
#endif
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&engine->storage_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&engine->storage_hash_lock);
#endif
#endif
}
struct mailengine *
@ -1128,11 +1207,16 @@ libetpan_engine_new(struct mailprivacy * privacy)
engine->privacy = privacy;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
r = pthread_mutex_init(&engine->storage_hash_lock, NULL);
if (r != 0)
goto free;
#elif (defined WIN32)
InitializeCriticalSection(&engine->storage_hash_lock);
#endif
#endif
engine->storage_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
if (engine->storage_hash == NULL)
goto destroy_mutex;
@ -1141,7 +1225,11 @@ libetpan_engine_new(struct mailprivacy * privacy)
destroy_mutex:
#ifdef LIBETPAN_REENTRANT
pthread_mutex_destroy(&engine->storage_hash_lock);
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_destroy(&engine->storage_hash_lock);
#elif (defined WIN32)
DeleteCriticalSection(&engine->storage_hash_lock);
#endif
#endif
free:
free(engine);
@ -1153,8 +1241,12 @@ void libetpan_engine_free(struct mailengine * engine)
{
chash_free(engine->storage_hash);
#ifdef LIBETPAN_REENTRANT
pthread_mutex_destroy(&engine->storage_hash_lock);
#endif
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_destroy(&engine->storage_hash_lock);
#elif (defined WIN32)
DeleteCriticalSection(&engine->storage_hash_lock);
#endif
#endif
free(engine);
}


+ 51
- 5
src/engine/mailprivacy_gnupg.c View File

@ -63,7 +63,11 @@
#include <libetpan/mailmime.h>
#include <libetpan/libetpan-config.h>
#ifdef LIBETPAN_REENTRANT
#include <pthread.h>
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#include <pthread.h>
#elif (defined WIN32)
#include <windows.h>
#endif
#endif
#include <ctype.h>
@ -2815,8 +2819,30 @@ static struct mailprivacy_protocol pgp_protocol = {
/* encryption_tab */ pgp_encryption_tab
};
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
static pthread_mutex_t encryption_id_hash_lock = PTHREAD_MUTEX_INITIALIZER;
#elif (defined WIN32)
static CRITICAL_SECTION encryption_id_hash_lock = {0};
static int mailprivacy_gnupg_init_lock_done = 0;
#endif
#endif
void mailprivacy_gnupg_init_lock()
{
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#elif (defined WIN32)
if (InterlockedExchange(&mailprivacy_gnupg_init_lock_done, 1) == 0){
InitializeCriticalSection(&encryption_id_hash_lock);
}
#endif
#endif
}
int mailprivacy_gnupg_init(struct mailprivacy * privacy)
{
mailprivacy_gnupg_init_lock();
return mailprivacy_register(privacy, &pgp_protocol);
}
@ -2825,10 +2851,6 @@ void mailprivacy_gnupg_done(struct mailprivacy * privacy)
mailprivacy_unregister(privacy, &pgp_protocol);
}
#ifdef LIBETPAN_REENTRANT
static pthread_mutex_t encryption_id_hash_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
static chash * encryption_id_hash = NULL;
static clist * get_list(struct mailprivacy * privacy, mailmessage * msg)
@ -2859,7 +2881,11 @@ void mailprivacy_gnupg_encryption_id_list_clear(struct mailprivacy * privacy,
clistiter * iter;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&encryption_id_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&encryption_id_hash_lock);
#endif
#endif
encryption_id_list = get_list(privacy, msg);
if (encryption_id_list != NULL) {
@ -2884,7 +2910,11 @@ void mailprivacy_gnupg_encryption_id_list_clear(struct mailprivacy * privacy,
}
}
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&encryption_id_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&encryption_id_hash_lock);
#endif
#endif
}
@ -2894,11 +2924,19 @@ clist * mailprivacy_gnupg_encryption_id_list(struct mailprivacy * privacy,
clist * encryption_id_list;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&encryption_id_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&encryption_id_hash_lock);
#endif
#endif
encryption_id_list = get_list(privacy, msg);
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&encryption_id_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&encryption_id_hash_lock);
#endif
#endif
return encryption_id_list;
@ -2912,7 +2950,11 @@ static int mailprivacy_gnupg_add_encryption_id(struct mailprivacy * privacy,
int res;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&encryption_id_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&encryption_id_hash_lock);
#endif
#endif
res = -1;
@ -2956,7 +2998,11 @@ static int mailprivacy_gnupg_add_encryption_id(struct mailprivacy * privacy,
}
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&encryption_id_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&encryption_id_hash_lock);
#endif
#endif
return res;


+ 54
- 6
src/engine/mailprivacy_smime.c View File

@ -47,7 +47,13 @@
# include <sys/wait.h>
# include <dirent.h>
#endif
#include <pthread.h>
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#include <pthread.h>
#elif (defined WIN32)
#include <windows.h>
#endif
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -1194,8 +1200,29 @@ static struct mailprivacy_protocol smime_protocol = {
/* encryption_tab */ smime_encryption_tab
};
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
static pthread_mutex_t encryption_id_hash_lock = PTHREAD_MUTEX_INITIALIZER;
#elif (defined WIN32)
static CRITICAL_SECTION encryption_id_hash_lock = {0};
#endif
#endif
void mailprivacy_smime_init_lock(){
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
#elif (defined WIN32)
static int mailprivacy_smime_init_lock_done = 0;
if (InterlockedExchange(&mailprivacy_smime_init_lock_done, 1) == 0){
InitializeCriticalSection(&encryption_id_hash_lock);
}
#endif
#endif
}
int mailprivacy_smime_init(struct mailprivacy * privacy)
{
mailprivacy_smime_init_lock();
certificates = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL);
if (certificates == NULL)
goto err;
@ -1692,11 +1719,6 @@ static int smime_command_passphrase(struct mailprivacy * privacy,
return NO_ERROR_SMIME;
}
#ifdef LIBETPAN_REENTRANT
static pthread_mutex_t encryption_id_hash_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
static chash * encryption_id_hash = NULL;
static clist * get_list(struct mailprivacy * privacy, mailmessage * msg)
@ -1727,8 +1749,13 @@ void mailprivacy_smime_encryption_id_list_clear(struct mailprivacy * privacy,
clistiter * iter;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&encryption_id_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&encryption_id_hash_lock);
#endif
#endif
encryption_id_list = get_list(privacy, msg);
if (encryption_id_list != NULL) {
chashdatum key;
@ -1751,8 +1778,13 @@ void mailprivacy_smime_encryption_id_list_clear(struct mailprivacy * privacy,
encryption_id_hash = NULL;
}
}
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&encryption_id_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&encryption_id_hash_lock);
#endif
#endif
}
@ -1762,11 +1794,19 @@ clist * mailprivacy_smime_encryption_id_list(struct mailprivacy * privacy,
clist * encryption_id_list;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&encryption_id_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&encryption_id_hash_lock);
#endif
#endif
encryption_id_list = get_list(privacy, msg);
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&encryption_id_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&encryption_id_hash_lock);
#endif
#endif
return encryption_id_list;
@ -1780,7 +1820,11 @@ static int mailprivacy_smime_add_encryption_id(struct mailprivacy * privacy,
int res;
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_lock(&encryption_id_hash_lock);
#elif (defined WIN32)
EnterCriticalSection(&encryption_id_hash_lock);
#endif
#endif
res = -1;
@ -1824,7 +1868,11 @@ static int mailprivacy_smime_add_encryption_id(struct mailprivacy * privacy,
}
#ifdef LIBETPAN_REENTRANT
#if defined(HAVE_PTHREAD_H) && !defined(IGNORE_PTHREAD_H)
pthread_mutex_unlock(&encryption_id_hash_lock);
#elif (defined WIN32)
LeaveCriticalSection(&encryption_id_hash_lock);
#endif
#endif
return res;


+ 4
- 0
src/windows/win_etpan.h View File

@ -17,10 +17,14 @@
#endif
#ifdef WIN32
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
#define EINPROGRESS WSAEINPROGRESS
#include <windows.h>
#endif
#ifdef __cplusplus


Loading…
Cancel
Save