- Fix cothread pointers and allocation so full 2M stack space available:
authorDavid I. Lehn <dlehn@users.sourceforge.net>
Wed, 16 May 2001 07:16:47 +0000 (07:16 +0000)
committerDavid I. Lehn <dlehn@users.sourceforge.net>
Wed, 16 May 2001 07:16:47 +0000 (07:16 +0000)
Original commit message from CVS:
- Fix cothread pointers and allocation so full 2M stack space available:
- Double maximums to use full stack space:
- COTHREAD_STACKSIZE = 16k
- COTHREAD_MAXTHREADS = 128
- cothread changes only verified on x86
- cothread_create returns NULL if nthreads == MAXTHREADS though not yet handled by gstschedule caller

gst/cothreads.c
gst/gstscheduler.c

index 108fd87..2de2b3e 100644 (file)
@@ -36,8 +36,8 @@
 #include "gstarch.h"
 
 
-#define COTHREAD_STACKSIZE 8192
-#define COTHREAD_MAXTHREADS 64
+#define COTHREAD_STACKSIZE 16384
+#define COTHREAD_MAXTHREADS 128
 #define STACK_SIZE 0x200000
 
 
@@ -86,7 +86,7 @@ cothread_init (void)
   ctx->threads[0]->argc = 0;
   ctx->threads[0]->argv = NULL;
   ctx->threads[0]->flags = COTHREAD_STARTED;
-  ctx->threads[0]->sp = (int *)CURRENT_STACK_FRAME;
+  ctx->threads[0]->sp = (void *)CURRENT_STACK_FRAME;
   ctx->threads[0]->pc = 0;
 
   GST_INFO (GST_CAT_COTHREADS,"0th thread is %p at sp:%p",ctx->threads[0], ctx->threads[0]->sp);
@@ -105,26 +105,32 @@ cothread_init (void)
  *
  * Create a new cothread state in the given context
  *
- * Returns: the new cothread state
+ * Returns: the new cothread state or NULL on error
  */
 cothread_state*
 cothread_create (cothread_context *ctx) 
 {
   cothread_state *s;
 
+  if (ctx->nthreads == COTHREAD_MAXTHREADS) {
+    GST_DEBUG (0, "attempt to create > COTHREAD_MAXTHREADS\n");
+    return NULL;
+  }
   GST_DEBUG (0,"pthread_self() %ld\n",pthread_self());
   //if (0) {
   if (pthread_self() == 0) {
-    s = (cothread_state *)malloc(sizeof(int) * COTHREAD_STACKSIZE);
+    s = (cothread_state *)malloc(COTHREAD_STACKSIZE);
     GST_DEBUG (0,"new stack (case 1) at %p\n",s);
   } else {
-    char *sp = CURRENT_STACK_FRAME;
-    unsigned long *stack_end = (unsigned long *)((unsigned long)sp &
-      ~(STACK_SIZE - 1));
+    void *sp = CURRENT_STACK_FRAME;
+    // FIXME this may not be 64bit clean
+    //       could use casts to uintptr_t from inttypes.h
+    //       if only all platforms had inttypes.h
+    void *stack_end = (void *)((unsigned long)sp & ~(STACK_SIZE - 1));
     s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
                            COTHREAD_STACKSIZE));
     GST_DEBUG (0,"new stack (case 2) at %p\n",s);
-    if (mmap((char *)s,COTHREAD_STACKSIZE*(sizeof(int)),
+    if (mmap((void *)s,COTHREAD_STACKSIZE,
              PROT_READ|PROT_WRITE|PROT_EXEC,MAP_FIXED|MAP_PRIVATE|MAP_ANON,
              -1,0) < 0) {
       perror("mmap'ing cothread stack space");
@@ -135,7 +141,7 @@ cothread_create (cothread_context *ctx)
   s->ctx = ctx;
   s->threadnum = ctx->nthreads;
   s->flags = 0;
-  s->sp = ((int *)s + COTHREAD_STACKSIZE);
+  s->sp = ((void *)s + COTHREAD_STACKSIZE);
   // is this needed anymore?
   s->top_sp = s->sp;
 
@@ -164,7 +170,7 @@ cothread_setfunc (cothread_state *thread,
   thread->func = func;
   thread->argc = argc;
   thread->argv = argv;
-  thread->pc = (int *)func;
+  thread->pc = (void *)func;
 }
 
 /**
index cc6fe46..e4e1d79 100644 (file)
@@ -277,6 +277,7 @@ gst_schedule_cothreaded_chain (GstBin *bin, _GstBinChain *chain) {
     // need to set up the cothread now
     if (wrapper_function != NULL) {
       if (element->threadstate == NULL) {
+        // FIXME handle cothread_create returning NULL
         element->threadstate = cothread_create (bin->threadcontext);
         GST_DEBUG (0,"created cothread %p for '%s'\n",element->threadstate,GST_ELEMENT_NAME(element));
       }