home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Programming / tek / kn / elate / exec / initthread.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-12  |  3.0 KB  |  138 lines

  1.  
  2. #include "tek/kn/elate/exec.h"
  3. #include <stdio.h>
  4.  
  5. extern pid_t kn_proc_exec_local(ELATE_SPAWN *, ELATE_PCB *)
  6.     __ELATE_QCALL__(("qcall sys/kn/proc/exec/local"));
  7.  
  8. extern void kn_mem_free(void *)
  9.     __ELATE_QCALL__(("qcall sys/kn/mem/free"));
  10.  
  11. /* 
  12. **    TEKlib
  13. **    (C) 2001 TEK neoscientists
  14. **    all rights reserved.
  15. **
  16. **    TBOOL kn_initthread(TKNOB *thread, TVOID (*threadfunc)(TAPTR data), TAPTR data)
  17. **
  18. **    init thread.
  19. **
  20. */
  21.  
  22. TBOOL kn_initthread(TKNOB *thread, TVOID (*threadfunc)(TAPTR data), TAPTR selfdata)
  23. {
  24.     if (sizeof(TKNOB) >= sizeof(struct elatethread))
  25.     {
  26.         struct elatethread *t = (struct elatethread *) thread;
  27.  
  28.         kn_memset(t, sizeof(struct elatethread), 0);
  29.  
  30.         if (kn_proc_getparams(kn_proc_pid_get(), &t->pcb) == 0)
  31.         {
  32.             strcpy(t->toolname, "dispatch");
  33.             sprintf(t->adrstr, "%d", (int) t);
  34.             t->argvblock[0] = t->toolname;
  35.             t->argvblock[1] = t->adrstr;
  36.             t->argvblock[2] = TNULL;
  37.             t->data = selfdata;
  38.             t->function = threadfunc;
  39.  
  40.             t->globaldata = t;            /* self reference via global data */
  41.     
  42.             t->spawn = kn_proc_spawn_make("lib/tek/kn/exec/dispatch", 
  43.                 t->argvblock,
  44.                 NULL,                /* stackname */
  45.                 NULL,                /* dataname */
  46.                 t->globaldata,        /* global */
  47.                 sizeof(TAPTR),        /* globalsize */
  48.                 1,1);
  49.  
  50.             if (t->spawn)
  51.             {
  52.                 t->initok = 0;
  53.                 t->pid = kn_proc_exec_local(t->spawn, &t->pcb);
  54.                 if (t->pid > 0)
  55.                 {
  56.                     /* 
  57.                     **    setup self reference.
  58.                     */
  59.                     
  60.                     void *ndadata;
  61.  
  62.                     sprintf(t->globalname, "tektask%08x", t->pid);
  63.                     ndadata = kn_nda_name(t, t->globalname);
  64.                     if (ndadata == t)
  65.                     {
  66.                         t->initok = 1;
  67.                         kn_proc_wake(t->pid);
  68.                         return TTRUE;
  69.                     }
  70.  
  71.                     kn_proc_chld(t->pid, 0, NULL);
  72.                     kn_proc_delete(t->pid);
  73.                 }
  74.                 kn_mem_free(t->spawn);
  75.             }
  76.         }
  77.     }
  78.     else
  79.     {
  80.         struct elatethread *t = kn_alloc0(sizeof(struct elatethread));
  81.         if (t)
  82.         {
  83.             if (kn_proc_getparams(kn_proc_pid_get(), &t->pcb) == 0)
  84.             {
  85.                 strcpy(t->toolname, "dispatch");
  86.                 sprintf(t->adrstr, "%d", (int) t);
  87.                 t->argvblock[0] = t->toolname;
  88.                 t->argvblock[1] = t->adrstr;
  89.                 t->argvblock[2] = TNULL;
  90.                 t->data = selfdata;
  91.                 t->function = threadfunc;
  92.     
  93.                 t->globaldata = t;            /* self reference via global data */
  94.         
  95.                 t->spawn = kn_proc_spawn_make("lib/tek/kn/exec/dispatch", 
  96.                     t->argvblock,
  97.                     NULL,                /* stackname */
  98.                     NULL,                /* dataname */
  99.                     t->globaldata,        /* global */
  100.                     sizeof(TAPTR),        /* globalsize */
  101.                     1,1);
  102.  
  103.                 if (t->spawn)
  104.                 {
  105.                     t->initok = 0;
  106.                     t->pid = kn_proc_exec_local(t->spawn, &t->pcb);
  107.                     if (t->pid > 0)
  108.                     {
  109.                         /* 
  110.                         **    setup self reference.
  111.                         */
  112.                         
  113.                         void *ndadata;
  114.     
  115.                         sprintf(t->globalname, "tektask%08x", t->pid);
  116.                         ndadata = kn_nda_name(t, t->globalname);
  117.                         if (ndadata == t)
  118.                         {
  119.                             *((struct elatethread **) thread) = t;
  120.                             t->initok = 1;
  121.                             kn_proc_wake(t->pid);
  122.                             return TTRUE;
  123.                         }
  124.     
  125.                         kn_proc_chld(t->pid, 0, NULL);
  126.                         kn_proc_delete(t->pid);
  127.                     }
  128.                     kn_mem_free(t->spawn);
  129.                 }
  130.             }
  131.             kn_free(t);
  132.         }
  133.     }
  134.  
  135.     dbkprintf(10,"*** TEKLIB kernel: could not create thread\n");
  136.     return TFALSE;
  137. }
  138.