Commit 87eccb42 87eccb4272e08ae729a57ca81ca25415e3c5d40c by Alain Magloire

Use the monitor for locking.

1 parent 851ed504
...@@ -24,10 +24,7 @@ ...@@ -24,10 +24,7 @@
24 24
25 #include <sys/types.h> 25 #include <sys/types.h>
26 #include <mailutils/mailer.h> 26 #include <mailutils/mailer.h>
27 #ifdef HAVE_PTHREAD_H 27 #include <mailutils/monitor.h>
28 # define __USE_UNIX98 /* ?? */
29 # include <pthread.h>
30 #endif
31 #ifdef _cplusplus 28 #ifdef _cplusplus
32 extern "C" { 29 extern "C" {
33 #endif 30 #endif
...@@ -62,9 +59,7 @@ struct _mailer ...@@ -62,9 +59,7 @@ struct _mailer
62 debug_t debug; 59 debug_t debug;
63 url_t url; 60 url_t url;
64 int flags; 61 int flags;
65 #ifdef WITH_PTHREAD 62 monitor_t monitor;
66 pthread_rwlock_t rwlock;
67 #endif
68 63
69 /* Pointer to the specific mailer data. */ 64 /* Pointer to the specific mailer data. */
70 void *data; 65 void *data;
...@@ -77,11 +72,6 @@ struct _mailer ...@@ -77,11 +72,6 @@ struct _mailer
77 int (*_send_message) __P ((mailer_t, message_t)); 72 int (*_send_message) __P ((mailer_t, message_t));
78 }; 73 };
79 74
80 /* Mail locks. */
81 extern int mailer_rdlock __P ((mailer_t));
82 extern int mailer_wrlock __P ((mailer_t));
83 extern int mailer_unlock __P ((mailer_t));
84
85 #define MAILER_NOTIFY(mailer, type) \ 75 #define MAILER_NOTIFY(mailer, type) \
86 if (mailer->observer) observer_notify (mailer->observer, type) 76 if (mailer->observer) observer_notify (mailer->observer, type)
87 77
......
...@@ -127,6 +127,7 @@ int ...@@ -127,6 +127,7 @@ int
127 list_remove (list_t list, void *item) 127 list_remove (list_t list, void *item)
128 { 128 {
129 struct list_data *current, *previous; 129 struct list_data *current, *previous;
130 int status = ENOENT;
130 if (list == NULL) 131 if (list == NULL)
131 return EINVAL; 132 return EINVAL;
132 monitor_wrlock (list->monitor); 133 monitor_wrlock (list->monitor);
...@@ -139,8 +140,8 @@ list_remove (list_t list, void *item) ...@@ -139,8 +140,8 @@ list_remove (list_t list, void *item)
139 current->next->prev = previous; 140 current->next->prev = previous;
140 free (current); 141 free (current);
141 list->count--; 142 list->count--;
142 monitor_unlock (list->monitor); 143 status = 0;
143 return 0; 144 break;
144 } 145 }
145 } 146 }
146 monitor_unlock (list->monitor); 147 monitor_unlock (list->monitor);
...@@ -152,6 +153,7 @@ list_get (list_t list, size_t index, void **pitem) ...@@ -152,6 +153,7 @@ list_get (list_t list, size_t index, void **pitem)
152 { 153 {
153 struct list_data *current; 154 struct list_data *current;
154 size_t count; 155 size_t count;
156 int status = ENOENT;
155 if (list == NULL || pitem == NULL) 157 if (list == NULL || pitem == NULL)
156 return EINVAL; 158 return EINVAL;
157 monitor_rdlock (list->monitor); 159 monitor_rdlock (list->monitor);
...@@ -161,10 +163,10 @@ list_get (list_t list, size_t index, void **pitem) ...@@ -161,10 +163,10 @@ list_get (list_t list, size_t index, void **pitem)
161 if (count == index) 163 if (count == index)
162 { 164 {
163 *pitem = current->item; 165 *pitem = current->item;
164 monitor_unlock (list->monitor); 166 status = 0;
165 return 0; 167 break;
166 } 168 }
167 } 169 }
168 monitor_unlock (list->monitor); 170 monitor_unlock (list->monitor);
169 return ENOENT; 171 return status;
170 } 172 }
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
27 27
28 #include <mailutils/registrar.h> 28 #include <mailutils/registrar.h>
29 #include <mailutils/iterator.h> 29 #include <mailutils/iterator.h>
30 #include <misc.h>
31 #include <mailer0.h> 30 #include <mailer0.h>
32 31
33 /* 32 /*
...@@ -74,7 +73,7 @@ mailer_create (mailer_t *pmailer, const char *name, int id) ...@@ -74,7 +73,7 @@ mailer_create (mailer_t *pmailer, const char *name, int id)
74 if (mailer == NULL) 73 if (mailer == NULL)
75 return ENOMEM; 74 return ENOMEM;
76 75
77 status = RWLOCK_INIT (&(mailer->rwlock), NULL); 76 status = monitor_create (&(mailer->monitor), mailer);
78 if (status != 0) 77 if (status != 0)
79 { 78 {
80 mailer_destroy (&mailer); 79 mailer_destroy (&mailer);
...@@ -109,9 +108,8 @@ mailer_destroy (mailer_t *pmailer) ...@@ -109,9 +108,8 @@ mailer_destroy (mailer_t *pmailer)
109 if (pmailer && *pmailer) 108 if (pmailer && *pmailer)
110 { 109 {
111 mailer_t mailer = *pmailer; 110 mailer_t mailer = *pmailer;
112 #ifdef WITH_PTHREAD 111 monitor_t monitor = mailer->monitor;
113 pthread_rwlock_t rwlock = mailer->rwlock; 112
114 #endif
115 if (mailer->observable) 113 if (mailer->observable)
116 { 114 {
117 observable_notify (mailer->observable, MU_EVT_MAILER_DESTROY); 115 observable_notify (mailer->observable, MU_EVT_MAILER_DESTROY);
...@@ -121,7 +119,7 @@ mailer_destroy (mailer_t *pmailer) ...@@ -121,7 +119,7 @@ mailer_destroy (mailer_t *pmailer)
121 if (mailer->_destroy) 119 if (mailer->_destroy)
122 mailer->_destroy (mailer); 120 mailer->_destroy (mailer);
123 121
124 RWLOCK_WRLOCK (&rwlock); 122 monitor_wrlock (monitor);
125 123
126 if (mailer->stream) 124 if (mailer->stream)
127 { 125 {
...@@ -135,8 +133,8 @@ mailer_destroy (mailer_t *pmailer) ...@@ -135,8 +133,8 @@ mailer_destroy (mailer_t *pmailer)
135 133
136 free (mailer); 134 free (mailer);
137 *pmailer = NULL; 135 *pmailer = NULL;
138 RWLOCK_UNLOCK (&rwlock); 136 monitor_unlock (monitor);
139 RWLOCK_DESTROY (&rwlock); 137 monitor_destroy (&monitor, mailer);
140 } 138 }
141 } 139 }
142 140
...@@ -228,41 +226,3 @@ mailer_get_debug (mailer_t mailer, debug_t *pdebug) ...@@ -228,41 +226,3 @@ mailer_get_debug (mailer_t mailer, debug_t *pdebug)
228 *pdebug = mailer->debug; 226 *pdebug = mailer->debug;
229 return 0; 227 return 0;
230 } 228 }
231
232 /* Mailer Internal Locks. Put the name of the functions in parenteses To make
233 they will not be redefine by the macro. If the flags was non-blocking we
234 should not block on the lock, so we try with pthread_rwlock_try*lock(). */
235 int
236 (mailer_wrlock) (mailer_t mailer)
237 {
238 #ifdef WITH_PTHREAD
239 int err = (mailer->flags & MU_STREAM_NONBLOCK) ?
240 RWLOCK_TRYWRLOCK (&(mailer->rwlock)) :
241 RWLOCK_WRLOCK (&(mailer->rwlock)) ;
242 if (err != 0 && err != EDEADLK)
243 return err;
244 #endif
245 return 0;
246 }
247 int
248 (mailer_rdlock) (mailer_t mailer)
249 {
250 #ifdef WITH_PTHREAD
251 int err = (mailer->flags & MU_STREAM_NONBLOCK) ?
252 RWLOCK_TRYRDLOCK (&(mailer->rwlock)) :
253 RWLOCK_RDLOCK (&(mailer->rwlock)) ;
254 if (err != 0 && err != EDEADLK)
255 return err;
256 #endif
257 return 0;
258 }
259
260 int
261 (mailer_unlock) (mailer_t mailer)
262 {
263 #ifdef WITH_PTHREAD
264 return RWLOCK_UNLOCK (&(mailer->rwlock));
265 #else
266 return 0;
267 #endif
268 }
......
...@@ -31,7 +31,7 @@ monitor_create (monitor_t *pmonitor, void *owner) ...@@ -31,7 +31,7 @@ monitor_create (monitor_t *pmonitor, void *owner)
31 return ENOMEM; 31 return ENOMEM;
32 monitor->owner = owner; 32 monitor->owner = owner;
33 status = RWLOCK_INIT (&(monitor->lock), NULL); 33 status = RWLOCK_INIT (&(monitor->lock), NULL);
34 if (status == 0) 34 if (status != 0)
35 { 35 {
36 free (monitor); 36 free (monitor);
37 return status; 37 return status;
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
30 #include <mailutils/stream.h> 30 #include <mailutils/stream.h>
31 #include <mailer0.h> 31 #include <mailer0.h>
32 #include <registrar0.h> 32 #include <registrar0.h>
33 #include <misc.h>
34 33
35 static int sendmail_init (mailer_t); 34 static int sendmail_init (mailer_t);
36 35
......