- while (*prv != NULL) {
- if (*prv != evt)
- prv = &(*prv)->next;
- else {
- /* valid, unlink */
- *prv = evt->next;
-
- /* removes all watchers */
- while(evt->watchs != NULL)
- remove_watch(evt->watchs);
-
- /* free */
- free(evt);
- break;
+ while (*prv && !(found = (*prv == evt)))
+ prv = &(*prv)->next;
+ if (found)
+ *prv = evt->next;
+ pthread_mutex_unlock(&events_mutex);
+
+ /* destroys the event */
+ if (found) {
+ /* removes all watchers */
+ while(evt->watchs != NULL) {
+ listener = evt->watchs->listener;
+ pthread_mutex_lock(&listener->mutex);
+ pthread_mutex_lock(&evt->mutex);
+ remove_watch(evt->watchs);
+ pthread_mutex_unlock(&evt->mutex);
+ pthread_mutex_unlock(&listener->mutex);