home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZCONTEXT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  15.0 KB  |  560 lines

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