home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZCONTEXT.C < prev    next >
C/C++ Source or Header  |  1992-09-04  |  13KB  |  518 lines

  1. /* Copyright (C) 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zcontext.c */
  21. /* Display PostScript context operators */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "alloc.h"
  27. #include "dict.h"
  28. #include "dstack.h"
  29. #include "estack.h"
  30. #include "state.h"
  31. #include "store.h"
  32.  
  33. /****** THIS FILE IS NOT IN GOOD ENOUGH SHAPE TO USE YET. ******/
  34.  
  35. /* Procedure hooks in interp.c */
  36. extern int (*gs_interp_reschedule_proc)(P0());
  37. extern int (*gs_interp_time_slice_proc)(P0());
  38.  
  39. /* Context structure */
  40. typedef enum {
  41.     cs_invalid,
  42.     cs_active,
  43.     cs_done
  44. } ctx_status;
  45. typedef struct gs_context_s {
  46.     ctx_status status;
  47.     long index;
  48.     int detach;            /* true if a detach has been */
  49.                     /* executed for this context */
  50.     struct gs_context_s *next;    /* next context with same status */
  51.                     /* (active, waiting on same lock, */
  52.                     /* waiting on same condition) */
  53.     struct gs_context_s *joiner;    /* context waiting on a join */
  54.                     /* for this one */
  55.     struct gs_context_s *table_next;    /* hash table chain */
  56.         /* Externally visible context state */
  57.     ref *stacks;
  58. #define default_stacksize 50
  59.     uint stacksize;
  60.     uint ossize;
  61.     uint essize;
  62.     uint dssize;
  63.     gs_state *pgs;
  64.     /****** MORE STUFF HERE ******/
  65. } gs_context;
  66.  
  67. /* Context list structure */
  68. typedef struct ctx_list_s {
  69.     gs_context *head;
  70.     gs_context *tail;
  71. } ctx_list;
  72.  
  73. /* Condition structure */
  74. typedef struct gs_condition_s {
  75.     ctx_list waiting;    /* contexts waiting on this condition */
  76. } gs_condition;
  77.  
  78. /* Lock structure */
  79. typedef struct gs_lock_s {
  80.     ctx_list waiting;    /* contexts waiting for this lock */
  81.     gs_context *holder;        /* context holding the lock, if any */
  82. } gs_lock;
  83.  
  84. /* Global state */
  85. private gs_context *ctx_current;
  86. private ctx_list active;
  87. #define ctx_table_size 19
  88. private gs_context *ctx_table[ctx_table_size];
  89. private long ctx_next_index;
  90.  
  91. /* Forward references */
  92. private int context_create(P2(uint, gs_context **));
  93. private int context_param(P2(os_ptr, gs_context **));
  94. #define check_context(op, vpc)\
  95.   if ( (code = context_param(op, &vpc)) < 0 ) return code
  96. private void context_destroy(P1(gs_context *));
  97. private int lock_acquire(P1(os_ptr));
  98. private int lock_release(P1(os_ptr));
  99.  
  100. /* List manipulation macros */
  101. #define add_last(pl,pc)\
  102.   (((pl)->head == 0 ? ((pl)->head = pc) : ((pl)->tail->next = pc)),\
  103.    (pl)->tail = pc, (pc)->next = 0)
  104. #define add_last_all(pl,pcl)        /* pcl->head != 0 */\
  105.   (((pl)->head == 0 ? ((pl)->head = (pcl)->head) :\
  106.     ((pl)->tail->next = (pcl)->head)),\
  107.    (pl)->tail = (pcl)->tail, (pcl)->head = 0)
  108.  
  109. /* ------ Initialization ------ */
  110.  
  111. private int ctx_reschedule(P0());
  112. private int ctx_time_slice(P0());
  113. private void
  114. zcontext_init()
  115. {    ctx_current = 0;
  116.     active.head = 0;
  117.     memset(ctx_table, 0, sizeof(ctx_table));
  118.     ctx_next_index = 1;
  119.     /* Create an initial context. */
  120.     context_create(default_stacksize, &ctx_current);
  121.     /* Hook into the interpreter. */
  122.     gs_interp_reschedule_proc = ctx_reschedule;
  123.     gs_interp_time_slice_proc = ctx_time_slice;
  124. }
  125.  
  126. /* ------ Interpreter interface to scheduler ------ */
  127.  
  128. /* When an operator decides it is time to run a new context, */
  129. /* it returns o_reschedule.  The interpreter saves all its state in */
  130. /* memory, calls ctx_reschedule, and then loads the state from memory. */
  131. private int
  132. ctx_reschedule()
  133. {    register gs_context *pctx;
  134.     ref *stkp;
  135.     uint ossize, essize, dssize;
  136.     /* Save the state of the current context in ctx_current, */
  137.     /* if any context is current at all. */
  138.     pctx = ctx_current;
  139.     if ( pctx != 0 )
  140.        {    uint stackneed;
  141.         ref *newstacks;
  142.         ossize = osp - osbot + 1;
  143.         essize = esp - esbot + 1;
  144.         dssize = dsp - dstack + 1;
  145.         stackneed = ossize + essize + dssize;
  146.         if ( stackneed > pctx->stacksize )
  147.            {    alloc_free_refs(pctx->stacks, pctx->stacksize,
  148.                     "ctx_reschedule");
  149.             newstacks = alloc_refs(stackneed, "ctx_reschedule");
  150.             if ( newstacks == 0 )
  151.                {    /* Punt. */
  152.                 lprintf("Can't allocate stacks!");
  153.                 return_error(e_Fatal);
  154.                }
  155.             pctx->stacksize = stackneed;
  156.             pctx->stacks = newstacks;
  157.            }
  158.         stkp = pctx->stacks;
  159. #define save_stack(sbot, ssize)\
  160.   memcpy(stkp, sbot, ssize * sizeof(ref));\
  161.   pctx->ssize = ssize;\
  162.   stkp += ssize
  163.         save_stack(osbot, ossize);
  164.         save_stack(esbot, essize);
  165.         save_stack(dstack, dssize);
  166. #undef save_stack
  167.         pctx->pgs = igs;
  168.         /****** MORE TO DO HERE ******/
  169.        }
  170.     /* Run the first ready context. */
  171.     if ( active.head == 0 )
  172.        {    lprintf("No context to run!");
  173.         return_error(e_Fatal);
  174.        }
  175.     ctx_current = active.head;
  176.     active.head = active.head->next;
  177.     /* Load the state of the new current context. */
  178.     pctx = ctx_current;
  179.     stkp = pctx->stacks;
  180. #define reload_stack(sbot, ssize, sp)\
  181.   ssize = pctx->ssize;\
  182.   memcpy(sbot, stkp, ssize * sizeof(ref));\
  183.   sp = sbot + (ssize - 1);\
  184.   stkp += ssize
  185.     reload_stack(osbot, ossize, osp);
  186.     reload_stack(esbot, essize, esp);
  187.     esfile = 0;
  188.     reload_stack(dstack, dssize, dsp);
  189. #undef reload_stack
  190.     igs = pctx->pgs;
  191.     /****** MORE TO DO HERE ******/
  192.     return 0;
  193. }
  194.  
  195. /* If the interpreter wants to time-slice, it saves its state, */
  196. /* calls ctx_time_slice, and reloads its state. */
  197. private int
  198. ctx_time_slice()
  199. {    if ( active.head == 0 ) return 0;
  200.     add_last(&active, ctx_current);
  201.     return ctx_reschedule();
  202. }
  203.  
  204. /* ------ Context operators ------ */
  205.  
  206. private int
  207.   fork_done(P1(os_ptr)),
  208.   i_fork_done;
  209.  
  210. /* currentcontext */
  211. int
  212. zcurrentcontext(register os_ptr op)
  213. {    push(1);
  214.     make_int(op, ctx_current->index);
  215.     return 0;
  216. }
  217.  
  218. /* detach */
  219. int
  220. zdetach(register os_ptr op)
  221. {    gs_context *pctx;
  222.     int code;
  223.     check_context(op, pctx);
  224.     if ( pctx->joiner != 0 || pctx->detach )
  225.         return e_invalidcontext;
  226.     pop(1);
  227.     switch ( pctx->status )
  228.        {
  229.     case cs_active:
  230.         pctx->detach = 1;
  231.         break;
  232.     case cs_done:
  233.         context_destroy(pctx);
  234.         if ( pctx == ctx_current )
  235.            {    ctx_current = 0;
  236.             return o_reschedule;
  237.            }
  238.        }
  239.     return 0;
  240. }
  241.  
  242. /* fork */
  243. int
  244. zfork(register os_ptr op)
  245. {    os_ptr mp = op - 1;
  246.     gs_context *pctx;
  247.     uint ossize, essize, dssize, stacksize;
  248.     int code;
  249.     ref *stkp;
  250.     check_proc(*op);
  251.     while ( !r_has_type(mp, t_mark) )
  252.        {    if ( mp <= osbot ) return e_unmatchedmark;
  253.         mp--;
  254.        }
  255.     ossize = op - mp - 1;
  256.     essize = 2;
  257.     dssize = dsp - dstack + 1;
  258.     stacksize = ossize + essize + dssize + 10;
  259.     code = context_create(stacksize, &pctx);
  260.     if ( code < 0 ) return code;
  261.     stkp = pctx->stacks;
  262.     pctx->ossize = ossize;
  263.     memcpy(stkp, mp + 1, ossize * sizeof(ref));
  264.     stkp += ossize;
  265.     pctx->essize = essize;
  266.     make_oper(stkp, i_fork_done, fork_done);
  267.     stkp++;
  268.     *stkp = *op;
  269.     stkp++;
  270.     pctx->dssize = dssize;
  271.     memcpy(stkp, dstack, dssize * sizeof(ref));
  272.     pctx->pgs = igs;        /* ****** WRONG, MUST COPY ****** */
  273.     /****** MORE INIT HERE? ******/
  274.     add_last(&active, pctx);
  275.     osp = mp;
  276.     make_int(mp, pctx->index);
  277.     return 0;
  278. }
  279. /* This gets executed when a context terminates normally. */
  280. /****** HOW TO GET IT EXECUTED ON ERROR TERMINATION? ******/
  281. private int
  282. fork_done(os_ptr op)
  283. {    if ( ctx_current->detach )
  284.        {    context_destroy(ctx_current);
  285.         ctx_current = 0;
  286.        }
  287.     else
  288.        {    gs_context *pctx = ctx_current->joiner;
  289.         ctx_current->status = cs_done;
  290.         /* Schedule the context waiting to join this one, if any. */
  291.         if ( pctx != 0 ) add_last(&active, pctx);
  292.        }
  293.     return o_reschedule;
  294. }
  295.  
  296. /* join */
  297. int
  298. zjoin(register os_ptr op)
  299. {    gs_context *pctx;
  300.     int code;
  301.     check_context(op, pctx);
  302.     if ( pctx->joiner != 0 || pctx == ctx_current || pctx->detach )
  303.         return e_invalidcontext;
  304.     switch ( pctx->status )
  305.        {
  306.     case cs_active:
  307.         pctx->joiner = ctx_current;
  308.         return o_reschedule;
  309.     case cs_done:
  310.        {    uint count = pctx->ossize;
  311.         os_ptr mp = op;
  312.         push(count);
  313.         make_mark(mp);
  314.         memcpy(++mp, pctx->stacks, count * sizeof(ref));
  315.         context_destroy(pctx);
  316.        }
  317.        }
  318.     return 0;
  319. }
  320.  
  321. /* yield */
  322. int
  323. zyield(register os_ptr op)
  324. {    if ( active.head == 0 ) return 0;
  325.     add_last(&active, ctx_current);
  326.     return o_reschedule;
  327. }
  328.  
  329. /* ------ Condition and lock operators ------ */
  330.  
  331. private int
  332.   i_monitor,
  333.   monitor_release(P1(os_ptr)),
  334.   i_monitor_release,
  335.   await_lock(P1(os_ptr)),
  336.   i_await_lock;
  337.  
  338. /* condition */
  339. int
  340. zcondition(register os_ptr op)
  341. {    gs_condition *pcond =
  342.         (gs_condition *)alloc(1, sizeof(gs_condition), "zcondition");
  343.     if ( pcond == 0 ) return e_VMerror;
  344.     pcond->waiting.head = 0;
  345.     push(1);
  346.     make_tv(op, t_condition, pcond, pcond);
  347.     return 0;
  348. }
  349.  
  350. /* lock */
  351. int
  352. zlock(register os_ptr op)
  353. {    gs_lock *plock = (gs_lock *)alloc(1, sizeof(gs_lock), "zlock");
  354.     if ( plock == 0 ) return e_VMerror;
  355.     plock->holder = 0;
  356.     plock->waiting.head = 0;
  357.     push(1);
  358.     make_tv(op, t_lock, plock, plock);
  359.     return 0;
  360. }
  361.  
  362. /* monitor */
  363. int
  364. zmonitor(register os_ptr op)
  365. {    gs_lock *plock;
  366.     int code;
  367.     check_type(op[-1], t_lock);
  368.     check_proc(*op);
  369.     plock = op[-1].value.plock;
  370.     check_estack(2);
  371.     if ( plock->holder == ctx_current ) return e_invalidcontext;
  372.     code = lock_acquire(op - 1);
  373.     /****** HOW TO GUARANTEE RELEASE IF CONTEXT DIES? ******/
  374.     push_op_estack(monitor_release, i_monitor_release);
  375.     *++esp = op[-1];
  376.     pop(2);
  377.     return code;
  378. }
  379. /* Release the monitor lock when the procedure completes. */
  380. private int
  381. monitor_release(os_ptr op)
  382. {    es_ptr ep = esp--;
  383.     return lock_release(ep);
  384. }
  385.  
  386. /* notify */
  387. int
  388. znotify(register os_ptr op)
  389. {    gs_condition *pcond;
  390.     check_type(*op, t_condition);
  391.     pcond = op->value.pcond;
  392.     pop(1); op--;
  393.     if ( pcond->waiting.head == 0 ) return 0;    /* nothing to do */
  394.     add_last_all(&active, &pcond->waiting);
  395.     return zyield(op);
  396. }
  397.  
  398. /* wait */
  399. int
  400. zwait(register os_ptr op)
  401. {    gs_condition *pcond;
  402.     check_type(op[-1], t_lock);
  403.     check_type(*op, t_condition);
  404.     pcond = op->value.pcond;
  405.     check_estack(1);
  406.     lock_release(op - 1);
  407.     add_last(&pcond->waiting, ctx_current);
  408.     push_op_estack(await_lock, i_await_lock);
  409.     return o_reschedule;
  410. }
  411. /* When the condition is signaled, wait for acquiring the lock. */
  412. private int
  413. await_lock(os_ptr op)
  414. {    int code = lock_acquire(op - 1);
  415.     pop(2);
  416.     return code;
  417. }
  418.  
  419. /* ------ Internal routines ------ */
  420.  
  421. /* Create a context. */
  422. private int
  423. context_create(uint stacksize, gs_context **ppctx)
  424. {    gs_context *pctx;
  425.     ref *stkp;
  426.     long ctx_index;
  427.     gs_context **pte;
  428.     pctx = (gs_context *)alloc(1, sizeof(gs_context), "context");
  429.     if ( stacksize < default_stacksize ) stacksize = default_stacksize;
  430.     stkp = alloc_refs(stacksize, "context(stacks)");
  431.     if ( pctx == 0 || stkp == 0 ) return e_VMerror;
  432.     ctx_index = ctx_next_index++;
  433.     pctx->stacks = stkp;
  434.     pctx->stacksize = stacksize;
  435.     pctx->status = cs_active;
  436.     pctx->index = ctx_index;
  437.     pctx->detach = 0;
  438.     pctx->next = 0;
  439.     pctx->joiner = 0;
  440.     pte = &ctx_table[ctx_index % ctx_table_size];
  441.     pctx->table_next = *pte;
  442.     *pte = pctx;
  443.     *ppctx = pctx;
  444.     return 0;
  445. }
  446.  
  447. /* Check a context ID.  Note that we do not check for context validity. */
  448. private int
  449. context_param(os_ptr op, gs_context **ppctx)
  450. {    gs_context *pctx;
  451.     long index;
  452.     check_type(*op, t_integer);
  453.     index = op->value.intval;
  454.     if ( index < 0 ) return e_invalidcontext;
  455.     pctx = ctx_table[index % ctx_table_size];
  456.     for ( ; ; pctx = pctx->table_next )
  457.        {    if ( pctx == 0 ) return e_invalidcontext;
  458.         if ( pctx->index == index ) break;
  459.        }
  460.     *ppctx = pctx;
  461.     return 0;
  462. }
  463.  
  464. /* Destroy a context. */
  465. private void
  466. context_destroy(gs_context *pctx)
  467. {    gs_context **ppctx = &ctx_table[pctx->index % ctx_table_size];
  468.     while ( *ppctx != pctx )
  469.         ppctx = &(*ppctx)->table_next;
  470.     *ppctx = (*ppctx)->table_next;
  471.     alloc_free_refs(pctx->stacks, pctx->stacksize, "context_destroy");
  472.     alloc_free((char *)pctx, 1, sizeof(gs_context), "context_destroy");
  473. }
  474.  
  475. /* Acquire a lock.  Return 0 if acquired, o_reschedule if not. */
  476. private int
  477. lock_acquire(os_ptr op)
  478. {    gs_lock *plock = op->value.plock;
  479.     if ( plock->holder == 0 )
  480.        {    plock->holder = ctx_current;
  481.         return 0;
  482.        }
  483.     add_last(&plock->waiting, ctx_current);
  484.     return o_reschedule;
  485. }
  486.  
  487. /* Release a lock.  Return 0 if OK, e_invalidcontext if not. */
  488. private int
  489. lock_release(os_ptr op)
  490. {    gs_lock *plock = op->value.plock;
  491.     if ( plock->holder == ctx_current )
  492.        {    plock->holder = 0;
  493.         add_last_all(&active, &plock->waiting);
  494.         return 0;
  495.        }
  496.     return e_invalidcontext;
  497. }
  498.  
  499. /* ------ Initialization procedure ------ */
  500.  
  501. op_def zcontext_op_defs[] = {
  502.     {"0condition", zcondition},
  503.     {"0currentcontext", zcurrentcontext},
  504.     {"1detach", zdetach},
  505.     {"2fork", zfork},
  506.     {"1join", zjoin},
  507.     {"0lock", zlock},
  508.     {"2monitor", zmonitor, &i_monitor},
  509.     {"1notify", znotify},
  510.     {"2wait", zwait},
  511.     {"0yield", zyield},
  512.         /* Internal operators */
  513.     {"0%fork_done", fork_done, &i_fork_done},
  514.     {"2%monitor_release", monitor_release, &i_monitor_release},
  515.     {"2%await_lock", await_lock, &i_await_lock},
  516.     op_def_end(zcontext_init)
  517. };
  518.