]> git.sesse.net Git - vlc/blobdiff - src/misc/objects.c
Remove useless mutex (pthread_once is enough)
[vlc] / src / misc / objects.c
index 79a64d203bc75c6ce5f5e25531631491a2c6ca12..483098585b4314a151dc950117e583bfb2bb1b03 100644 (file)
@@ -64,6 +64,7 @@
 #else
 # include <io.h>
 # include <fcntl.h>
+# include <errno.h> /* ENOSYS */
 #endif
 
 /*****************************************************************************
@@ -466,6 +467,59 @@ void __vlc_object_unlock( vlc_object_t *obj )
     vlc_mutex_unlock( &obj->object_lock );
 }
 
+#ifdef WIN32
+# include <winsock2.h>
+# include <ws2tcpip.h>
+
+/**
+ * select()-able pipes emulated using Winsock
+ */
+static int pipe (int fd[2])
+{
+    SOCKADDR_IN addr;
+    int addrlen = sizeof (addr);
+
+    SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
+           c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
+    if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
+        goto error;
+
+    memset (&addr, 0, sizeof (addr));
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
+    if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
+     || getsockname (l, (PSOCKADDR)&addr, &addrlen)
+     || listen (l, 1)
+     || connect (c, (PSOCKADDR)&addr, addrlen))
+        goto error;
+
+    a = accept (l, NULL, NULL);
+    if (a == INVALID_SOCKET)
+        goto error;
+
+    closesocket (l);
+    shutdown (a, 0);
+    shutdown (c, 1);
+    fd[0] = c;
+    fd[1] = a;
+    return 0;
+
+error:
+    if (l != INVALID_SOCKET)
+        closesocket (l);
+    if (c != INVALID_SOCKET)
+        closesocket (c);
+    return -1;
+}
+
+#undef  read
+#define read( a, b, c )  recv (a, b, c, 0)
+#undef  write
+#define write( a, b, c ) send (a, b, c, 0)
+#undef  close
+#define close( a )       closesocket (a)
+#endif /* WIN32 */
+
 /**
  * Returns the readable end of a pipe that becomes readable whenever
  * an object is signaled. This can be used to wait for VLC object events
@@ -481,7 +535,7 @@ void __vlc_object_unlock( vlc_object_t *obj )
  * @param obj object that would be signaled (object lock MUST hold)
  * @return a readable pipe descriptor, or -1 on error.
  */
-int vlc_object_waitpipe( vlc_object_t *obj )
+int __vlc_object_waitpipe( vlc_object_t *obj )
 {
     int *pipes = obj->p_internals->pipes;
     vlc_assert_locked( &obj->object_lock );
@@ -491,11 +545,7 @@ int vlc_object_waitpipe( vlc_object_t *obj )
         /* This can only ever happen if someone killed us without locking */
         assert( pipes[0] == -1 );
 
-#ifndef WIN32
         if( pipe( pipes ) )
-#else
-        if( _pipe( pipes, 1, _O_BINARY ) )
-#endif
             return -1;
     }
 
@@ -552,6 +602,36 @@ int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
 }
 
 
+/**
+ * Checks whether an object has been "killed".
+ * The object lock must be held.
+ *
+ * Typical code for an object thread could be:
+ *
+   vlc_object_lock (self);
+   ...initialization...
+   while (vlc_object_alive (self))
+   {
+       ...preprocessing...
+
+       if (vlc_object_wait (self))
+           continue;
+
+       ...postprocessing...
+   }
+   ...deinitialization...
+   vlc_object_unlock (self);
+ *
+ *
+ * @return true iff the object has not been killed yet
+ */
+vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
+{
+    vlc_assert_locked( &obj->object_lock );
+    return !obj->b_die;
+}
+
+
 /**
  * Signals an object for which the lock is held.
  */