[gst-devel] cothreads.[ch] patch

Thomas Vander Stichele thomas at urgent.rug.ac.be
Tue Jul 2 11:30:08 CEST 2002


sure.

> On Tue, 02 Jul 2002, Thomas Vander Stichele wrote:
> 
> > Hi,
> > 
> > attached is a patch that tries to abstract the COTHREAD_ATOMIC - dependent 
> > code.  I am open for suggestions to do it in a better way but this looks 
> > like a good start to me.
> 
> Would you mind redoing this as a unified diff (diff -u) ?
> 
> Thanks.
> 
> wingo.
> 
> 
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> gstreamer-devel mailing list
> gstreamer-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gstreamer-devel
> 

-- 

The Dave/Dina Project : future TV today ! - http://davedina.apestaart.org/
<-*-                      -*->
"Don't worry, Joey, it's unisex."
"Maybe you need sex, I just had some a couple of days ago !"
"No, no, U-N-I-sex !"
"Well, I ain't gonna say no to that !"
<-*- thomas at apestaart.org -*->
URGent, the best radio on the Internet - 24/7 ! - http://urgent.rug.ac.be/
-------------- next part --------------
Index: cothreads.c
===================================================================
RCS file: /cvsroot/gstreamer/gstreamer/gst/cothreads.c,v
retrieving revision 1.75
diff -u -r1.75 cothreads.c
--- cothreads.c	29 Jun 2002 16:26:47 -0000	1.75
+++ cothreads.c	2 Jul 2002 18:18:51 -0000
@@ -102,11 +102,7 @@
   ctx->threads[0]->pc = 0;
 
   /* initialize the lock */
-#ifdef COTHREAD_ATOMIC
-  atomic_set (&ctx->threads[0]->lock, 0);
-#else
-  ctx->threads[0]->lock = g_mutex_new ();
-#endif
+  _lock_new (ctx->threads[0]->lock);
 
   GST_INFO (GST_CAT_COTHREADS, "0th thread is %p at sp:%p", ctx->threads[0], ctx->threads[0]->sp);
 
@@ -214,11 +210,7 @@
   thread->top_sp = thread->sp; /* for debugging purposes to detect stack overruns */
 
   /* initialize the lock */
-#ifdef COTHREAD_ATOMIC
-  atomic_set (thread->lock, 0);
-#else
-  thread->lock = g_mutex_new ();
-#endif
+  _lock_new (thread->lock);
 
   GST_INFO (GST_CAT_COTHREADS, 
             "created cothread #%d in slot %d: %p at sp:%p lock:%p", 
@@ -263,9 +255,7 @@
   /* we have to unlock here because we might be switched out with the lock held */
   cothread_unlock (thread);
 
-#ifndef COTHREAD_ATOMIC
-  g_mutex_free (thread->lock);
-#endif
+  _lock_free (thread->lock);
 
   if (threadnum == 0) 
   {
@@ -306,12 +296,7 @@
     g_assert (thread->lock != NULL);
 
     /* FIXME: I'm pretty sure we have to do something to the lock, no ? */
-#ifdef COTHREAD_ATOMIC
-    /* FIXME: I don't think we need to do anything to an atomic lock */
-#else
-    //g_mutex_free (thread->lock);
-    //thread->lock = NULL;
-#endif
+    _lock_free (thread->lock);
 
     GST_DEBUG (GST_CAT_COTHREADS, 
                "munmap cothread slot stack from %p to %p (size 0x%lx)", 
@@ -586,18 +571,10 @@
     goto selfswitch;
 
   /* unlock the current thread, we're out of that context now */
-#ifdef COTHREAD_ATOMIC
-  /* do something to unlock the cothread */
-#else
-  g_mutex_unlock (current->lock);
-#endif
+  _lock_unlock (current->lock);
 
   /* lock the next cothread before we even switch to it */
-#ifdef COTHREAD_ATOMIC
-  /* do something to lock the cothread */
-#else
-  g_mutex_lock (thread->lock);
-#endif
+  _lock_lock (thread->lock);
 
   /* find the number of the thread to switch to */
   GST_INFO (GST_CAT_COTHREAD_SWITCH, "switching from cothread #%d to cothread #%d",
@@ -665,12 +642,7 @@
 void
 cothread_lock (cothread_state * thread)
 {
-#ifdef COTHREAD_ATOMIC
-  /* do something to lock the cothread */
-#else
-  if (thread->lock)
-    g_mutex_lock (thread->lock);
-#endif
+  _lock_lock (thread->lock);
 }
 
 /**
@@ -684,14 +656,7 @@
 gboolean
 cothread_trylock (cothread_state * thread)
 {
-#ifdef COTHREAD_ATOMIC
-  /* do something to try to lock the cothread */
-#else
-  if (thread->lock)
-    return g_mutex_trylock (thread->lock);
-  else
-    return FALSE;
-#endif
+  _lock_trylock (thread->lock);
 }
 
 /**
@@ -703,10 +668,5 @@
 void
 cothread_unlock (cothread_state * thread)
 {
-#ifdef COTHREAD_ATOMIC
-  /* do something to unlock the cothread */
-#else
-  if (thread->lock)
-    g_mutex_unlock (thread->lock);
-#endif
+  _lock_unlock (thread->lock);
 }
Index: cothreads.h
===================================================================
RCS file: /cvsroot/gstreamer/gstreamer/gst/cothreads.h,v
retrieving revision 1.24
diff -u -r1.24 cothreads.h
--- cothreads.h	28 Jun 2002 10:58:05 -0000	1.24
+++ cothreads.h	2 Jul 2002 18:18:51 -0000
@@ -44,6 +44,56 @@
 #define COTHREAD_STARTED	0x01
 #define COTHREAD_DESTROYED	0x02
 
+/* moved the COTHREAD_ATOMIC dependent stuff here instead of littered around
+ * the code as previous */
+
+/* _lock_t type */
+#ifdef COTHREAD_ATOMIC
+typedef atomic_t _lock_t;
+#else
+typedef GMutex * _lock_t;
+#endif
+
+/* _lock_new macro */
+#ifdef COTHREAD_ATOMIC
+#  define _lock_new(x)  atomic_set (x, 0);
+#else
+#  define _lock_new(x)  x = g_mutex_new ();
+#endif
+    
+/* _lock_free macro */
+#ifdef COTHREAD_ATOMIC
+#  define _lock_free(x) atomic_set (x, 0);
+#else
+#  define _lock_free(x) g_mutex_free (x);
+#endif
+
+/* _lock_lock macro */
+#ifdef COTHREAD_ATOMIC
+#  define _lock_lock(x) atomic_set (x, 1);
+#else
+#  define _lock_lock(x) if (x) { g_mutex_lock (x); }
+#endif
+
+/* _lock_unlock macro */
+#ifdef COTHREAD_ATOMIC
+#  define _lock_unlock(x) atomic_set (x, 0);
+#else
+#  define _lock_unlock(x) if (x) { g_mutex_unlock (x); }
+#endif
+
+/* _lock_trylock macro */
+#ifdef COTHREAD_ATOMIC
+#  define _lock_trylock(x) \
+  if (atomic_read (x) == 1) \
+    return FALSE; \
+  else { \
+    _lock_lock(x); \
+    return TRUE; }
+#else
+#  define _lock_trylock(x) return g_mutex_trylock (x);
+#endif
+    
 struct _cothread_state {
   cothread_context *ctx;
   int threadnum;
@@ -61,11 +111,7 @@
   void *pc;
 
   int magic_number;
-#ifdef COTHREAD_ATOMIC
-  atomic_t lock;
-#else
-  GMutex *lock;
-#endif
+  _lock_t lock;
 };
 
 


More information about the gstreamer-devel mailing list