Commit 87eccb42 87eccb4272e08ae729a57ca81ca25415e3c5d40c by Alain Magloire

Use the monitor for locking.

1 parent 851ed504
......@@ -24,10 +24,7 @@
#include <sys/types.h>
#include <mailutils/mailer.h>
#ifdef HAVE_PTHREAD_H
# define __USE_UNIX98 /* ?? */
# include <pthread.h>
#endif
#include <mailutils/monitor.h>
#ifdef _cplusplus
extern "C" {
#endif
......@@ -62,9 +59,7 @@ struct _mailer
debug_t debug;
url_t url;
int flags;
#ifdef WITH_PTHREAD
pthread_rwlock_t rwlock;
#endif
monitor_t monitor;
/* Pointer to the specific mailer data. */
void *data;
......@@ -77,11 +72,6 @@ struct _mailer
int (*_send_message) __P ((mailer_t, message_t));
};
/* Mail locks. */
extern int mailer_rdlock __P ((mailer_t));
extern int mailer_wrlock __P ((mailer_t));
extern int mailer_unlock __P ((mailer_t));
#define MAILER_NOTIFY(mailer, type) \
if (mailer->observer) observer_notify (mailer->observer, type)
......
......@@ -127,6 +127,7 @@ int
list_remove (list_t list, void *item)
{
struct list_data *current, *previous;
int status = ENOENT;
if (list == NULL)
return EINVAL;
monitor_wrlock (list->monitor);
......@@ -139,8 +140,8 @@ list_remove (list_t list, void *item)
current->next->prev = previous;
free (current);
list->count--;
monitor_unlock (list->monitor);
return 0;
status = 0;
break;
}
}
monitor_unlock (list->monitor);
......@@ -152,6 +153,7 @@ list_get (list_t list, size_t index, void **pitem)
{
struct list_data *current;
size_t count;
int status = ENOENT;
if (list == NULL || pitem == NULL)
return EINVAL;
monitor_rdlock (list->monitor);
......@@ -161,10 +163,10 @@ list_get (list_t list, size_t index, void **pitem)
if (count == index)
{
*pitem = current->item;
monitor_unlock (list->monitor);
return 0;
status = 0;
break;
}
}
monitor_unlock (list->monitor);
return ENOENT;
return status;
}
......
......@@ -27,7 +27,6 @@
#include <mailutils/registrar.h>
#include <mailutils/iterator.h>
#include <misc.h>
#include <mailer0.h>
/*
......@@ -74,7 +73,7 @@ mailer_create (mailer_t *pmailer, const char *name, int id)
if (mailer == NULL)
return ENOMEM;
status = RWLOCK_INIT (&(mailer->rwlock), NULL);
status = monitor_create (&(mailer->monitor), mailer);
if (status != 0)
{
mailer_destroy (&mailer);
......@@ -109,9 +108,8 @@ mailer_destroy (mailer_t *pmailer)
if (pmailer && *pmailer)
{
mailer_t mailer = *pmailer;
#ifdef WITH_PTHREAD
pthread_rwlock_t rwlock = mailer->rwlock;
#endif
monitor_t monitor = mailer->monitor;
if (mailer->observable)
{
observable_notify (mailer->observable, MU_EVT_MAILER_DESTROY);
......@@ -121,7 +119,7 @@ mailer_destroy (mailer_t *pmailer)
if (mailer->_destroy)
mailer->_destroy (mailer);
RWLOCK_WRLOCK (&rwlock);
monitor_wrlock (monitor);
if (mailer->stream)
{
......@@ -135,8 +133,8 @@ mailer_destroy (mailer_t *pmailer)
free (mailer);
*pmailer = NULL;
RWLOCK_UNLOCK (&rwlock);
RWLOCK_DESTROY (&rwlock);
monitor_unlock (monitor);
monitor_destroy (&monitor, mailer);
}
}
......@@ -228,41 +226,3 @@ mailer_get_debug (mailer_t mailer, debug_t *pdebug)
*pdebug = mailer->debug;
return 0;
}
/* Mailer Internal Locks. Put the name of the functions in parenteses To make
they will not be redefine by the macro. If the flags was non-blocking we
should not block on the lock, so we try with pthread_rwlock_try*lock(). */
int
(mailer_wrlock) (mailer_t mailer)
{
#ifdef WITH_PTHREAD
int err = (mailer->flags & MU_STREAM_NONBLOCK) ?
RWLOCK_TRYWRLOCK (&(mailer->rwlock)) :
RWLOCK_WRLOCK (&(mailer->rwlock)) ;
if (err != 0 && err != EDEADLK)
return err;
#endif
return 0;
}
int
(mailer_rdlock) (mailer_t mailer)
{
#ifdef WITH_PTHREAD
int err = (mailer->flags & MU_STREAM_NONBLOCK) ?
RWLOCK_TRYRDLOCK (&(mailer->rwlock)) :
RWLOCK_RDLOCK (&(mailer->rwlock)) ;
if (err != 0 && err != EDEADLK)
return err;
#endif
return 0;
}
int
(mailer_unlock) (mailer_t mailer)
{
#ifdef WITH_PTHREAD
return RWLOCK_UNLOCK (&(mailer->rwlock));
#else
return 0;
#endif
}
......
......@@ -31,7 +31,7 @@ monitor_create (monitor_t *pmonitor, void *owner)
return ENOMEM;
monitor->owner = owner;
status = RWLOCK_INIT (&(monitor->lock), NULL);
if (status == 0)
if (status != 0)
{
free (monitor);
return status;
......
......@@ -30,7 +30,6 @@
#include <mailutils/stream.h>
#include <mailer0.h>
#include <registrar0.h>
#include <misc.h>
static int sendmail_init (mailer_t);
......