home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / Em / xkUtils.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-17  |  3.5 KB  |  167 lines

  1. #ifdef xkernel     
  2. #include "userupi.h"
  3. #include "userprocess.h"
  4. #include "debug.h"
  5. /* Eric Jul, Aug. 1989: all this is x stuff, so conditionally compile it */
  6.  
  7. #include "Kernel/h/stdTypes.h"
  8. #include "Kernel/h/kEvents.h"
  9. #include "Kernel/h/emTypes.h"
  10. extern SSPtr                    currentSSP, readyQ, removeQ();
  11. /*
  12.  * emPause:
  13.  *   Delay the calling process until something interesting happens (in UNIX
  14.  *   terms, interesting events were Signals).
  15.  */
  16.  
  17.  
  18. int handlersWaiting = 0;
  19. int xkUtilsDebugging = 0;
  20. int nWaiting = 0;
  21. short int shouldPause, reallyPause, dontPause;
  22.  
  23. emPause()
  24. {
  25.   printf("in pause\n");
  26.   abort();
  27. }
  28.  
  29.  
  30.  
  31. becomeTheEmeraldScheduler()
  32. {
  33.   xkhandlerstart();
  34.   asm("    jmp    kernel_exit");
  35. }
  36.  
  37. startAnEmeraldScheduler()
  38. {
  39.   if (xkUtilsDebugging) printf("%d: Creating em scheduler\n", xpid());
  40.   xcreateprocess(becomeTheEmeraldScheduler, 5, 0);
  41. }
  42.  
  43.  
  44. holdsigs()
  45. {
  46. }
  47.  
  48. releasesigs()
  49. {
  50. }
  51.  
  52. Semaphore *waiting;
  53. static Semaphore *mutex;
  54.  
  55. initXK()
  56. {
  57.   waiting =   xcreatesemaphore(0);
  58.   mutex     = xcreatesemaphore(1);
  59. }
  60.  
  61. xkleaving(create)
  62. int create;
  63. {
  64.   if (nWaiting > 0) {
  65.     if (xkUtilsDebugging) printf("%d: Ving waiting = %d\n", xpid(), xsemcount(waiting));
  66.     nWaiting --;
  67.     xvsem(waiting);
  68.   } else {
  69.     if (xkUtilsDebugging) printf("%d: Releasing mutex = %d\n", xpid(), xsemcount(mutex));
  70.     xvsem(mutex);
  71.     if (xkUtilsDebugging) printf("%d: After release = %d\n", xpid(), xsemcount(mutex));
  72.     if (create && handlersWaiting <= 0) {
  73.       startAnEmeraldScheduler();
  74.     }
  75.   }
  76. }
  77.  
  78. xkp(s)
  79. Semaphore *s;
  80. {
  81.   if (xkUtilsDebugging) printf("%d: xkp %x = %d\n", xpid(), s, xsemcount(s));
  82.   if (xsemcount(s) <= 0) {
  83.     xkleaving(1);
  84.     if (xkUtilsDebugging) printf("%d: Ping %x = %d\n", xpid(), s, xsemcount(s));
  85.     xpsem(s);
  86.     if (xkUtilsDebugging) printf("%d: Ping waiting = %d\n", xpid(), xsemcount(waiting));
  87.     xpsem(waiting);
  88.   } else {
  89.     xpsem(s);
  90.   }
  91. }
  92.  
  93. xkv(s)
  94. Semaphore *s;
  95. {
  96.   if (xkUtilsDebugging) printf("%d: xkv %x = %d, waiting = %d\n", xpid(), s,
  97.     xsemcount(s), xsemcount(waiting));
  98.   if (xsemcount(s) < 0) {
  99.     nWaiting++;
  100.   }
  101.   xvsem(s);
  102. }
  103.  
  104. doPause()
  105. {
  106.   printf("In pause\n");
  107.   abort();
  108. }
  109.  
  110. xkhandlerstart()
  111. {
  112.   if (xkUtilsDebugging) printf("%d: Handler starting\n", xpid());
  113.   ++handlersWaiting;
  114.   if (xkUtilsDebugging) printf("%d: Gaining mutex = %d\n", xpid(), xsemcount(mutex));
  115.   xpsem(mutex);
  116.   --handlersWaiting;
  117.   if (xkUtilsDebugging) printf("%d: After gain = %d\n", xpid(), xsemcount(mutex));
  118. }
  119.  
  120. xkhandlerend()
  121. {
  122.   asm("    jmp kernel_exit");
  123. }
  124.  
  125. char *emallocnil(x)
  126. register int x;
  127. {
  128.   register long *p, *q;
  129.   p = q = (long *) gc_malloc(x);
  130.   x >>= 2;
  131.   while (x--) {
  132.     *q++ = 0x80000000;
  133.   }
  134.   return (char *) p;
  135. }
  136.  
  137. doScheduling()
  138. {
  139.   do {
  140.     /* Check for other interested parties */
  141.     if (xkUtilsDebugging) printf("%d: checking other handlers\n", xpid());
  142.     if (handlersWaiting > 0) break;
  143.  
  144.     /* Check the task queue */
  145.     if (xkUtilsDebugging) printf("%d: checking task q\n", xpid());
  146.     if ((struct TaskQueue *)TaskQ.queue.F != &TaskQ) {
  147.       if (xkUtilsDebugging) printf("%d: calling mainloop\n", xpid());
  148.       MainLoop();
  149.       continue;
  150.     }
  151.  
  152.     /* Check for Emerald processes */
  153.     if (NonNULL(currentSSP) ||
  154.     NonNULL(currentSSP = removeQ(&readyQ))) {
  155.       if (xkUtilsDebugging) printf("%d: running process 0x%X\n", xpid(), currentSSP);
  156.       return;
  157.     }
  158.     break;
  159.   } while (1);
  160.   /* die */
  161.   if (xkUtilsDebugging) printf("%d: doScheduling done, leaving\n", xpid());
  162.   xkleaving(0);
  163.   if (xkUtilsDebugging) printf("%d: doScheduling done, exiting\n", xpid());
  164.   xexit();
  165. }
  166. #endif xkernel
  167.