configure: add --disable-sse and add mmx/sse detection for OS X
[melted] / src / framework / mlt_events.c
index e42dabb..b0efd6b 100644 (file)
@@ -1,40 +1,46 @@
-/*
- * mlt_events.h -- event handling 
- * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
- * Author: Charles Yates <charles.yates@pandora.be>
+/**
+ * \file mlt_events.c
+ * \brief event handling
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Copyright (C) 2004-2008 Ushodaya Enterprises Limited
+ * \author Charles Yates <charles.yates@pandora.be>
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <pthread.h>
 
 #include "mlt_properties.h"
 #include "mlt_events.h"
 
-/** Memory leak checks.
-*/
+/** Memory leak checks. */
 
-//#define _MLT_EVENT_CHECKS_
+#undef _MLT_EVENT_CHECKS_
 
 #ifdef _MLT_EVENT_CHECKS_
 static int events_created = 0;
 static int events_destroyed = 0;
 #endif
 
+/** \brief Events class
+ *
+ */
+
 struct mlt_events_struct
 {
        mlt_properties owner;
@@ -43,6 +49,10 @@ struct mlt_events_struct
 
 typedef struct mlt_events_struct *mlt_events;
 
+/** \brief Event class
+ *
+ */
+
 struct mlt_event_struct
 {
        mlt_events owner;
@@ -57,7 +67,7 @@ struct mlt_event_struct
 
 void mlt_event_inc_ref( mlt_event this )
 {
-       if ( this != NULL && this->owner != NULL )
+       if ( this != NULL )
                this->ref_count ++;
 }
 
@@ -84,7 +94,7 @@ void mlt_event_unblock( mlt_event this )
 
 void mlt_event_close( mlt_event this )
 {
-       if ( this != NULL && this->owner != NULL )
+       if ( this != NULL )
        {
                if ( -- this->ref_count == 1 )
                        this->owner = NULL;
@@ -99,7 +109,7 @@ void mlt_event_close( mlt_event this )
        }
 }
 
-/** Forward declaration to private functions.
+/* Forward declaration to private functions.
 */
 
 static mlt_events mlt_events_fetch( mlt_properties );
@@ -227,11 +237,10 @@ mlt_event mlt_events_listen( mlt_properties this, void *service, char *id, mlt_l
                                        event->listener = listener;
                                        event->service = service;
                                        mlt_properties_set_data( listeners, temp, event, 0, ( mlt_destructor )mlt_event_close, NULL );
+                                       mlt_event_inc_ref( event );
                                }
                        }
 
-                       if ( event != NULL )
-                               mlt_event_inc_ref( event );
                }
        }
        return event;
@@ -319,6 +328,58 @@ void mlt_events_disconnect( mlt_properties this, void *service )
        }
 }
 
+/** \brief private to mlt_events_struct, used by mlt_events_wait_for() */
+
+typedef struct
+{
+       int done;
+       pthread_cond_t cond;
+       pthread_mutex_t mutex;
+}
+condition_pair;
+
+static void mlt_events_listen_for( mlt_properties this, condition_pair *pair )
+{
+       pthread_mutex_lock( &pair->mutex );
+       if ( pair->done == 0 )
+       {
+               pthread_cond_signal( &pair->cond );
+               pthread_mutex_unlock( &pair->mutex );
+       }
+}
+
+mlt_event mlt_events_setup_wait_for( mlt_properties this, char *id )
+{
+       condition_pair *pair = malloc( sizeof( condition_pair ) );
+       pair->done = 0;
+       pthread_cond_init( &pair->cond, NULL );
+       pthread_mutex_init( &pair->mutex, NULL );
+       pthread_mutex_lock( &pair->mutex );
+       return mlt_events_listen( this, pair, id, ( mlt_listener )mlt_events_listen_for );
+}
+
+void mlt_events_wait_for( mlt_properties this, mlt_event event )
+{
+       if ( event != NULL )
+       {
+               condition_pair *pair = event->service;
+               pthread_cond_wait( &pair->cond, &pair->mutex );
+       }
+}
+
+void mlt_events_close_wait_for( mlt_properties this, mlt_event event )
+{
+       if ( event != NULL )
+       {
+               condition_pair *pair = event->service;
+               event->owner = NULL;
+               pair->done = 0;
+               pthread_mutex_unlock( &pair->mutex );
+               pthread_mutex_destroy( &pair->mutex );
+               pthread_cond_destroy( &pair->cond );
+       }
+}
+
 /** Fetch the events object.
 */