/* Copyright (C) 1991 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
/* Display PostScript context operators */
/****** THIS FILE IS NOT IN GOOD ENOUGH SHAPE TO USE YET. ******/
/* Procedure hooks in interp.c */
extern int (*gs_interp_reschedule_proc
)(P0());
extern int (*gs_interp_time_slice_proc
)(P0());
typedef struct gs_context_s
{
int detach
; /* true if a detach has been */
/* executed for this context */
struct gs_context_s
*next
; /* next context with same status */
/* (active, waiting on same lock, */
/* waiting on same condition) */
struct gs_context_s
*joiner
; /* context waiting on a join */
struct gs_context_s
*table_next
; /* hash table chain */
/* Externally visible context state */
#define default_stacksize 50
/****** MORE STUFF HERE ******/
/* Context list structure */
typedef struct ctx_list_s
{
/* Condition structure */
typedef struct gs_condition_s
{
ctx_list waiting
; /* contexts waiting on this condition */
typedef struct gs_lock_s
{
ctx_list waiting
; /* contexts waiting for this lock */
gs_context
*holder
; /* context holding the lock, if any */
private gs_context
*ctx_current
;
#define ctx_table_size 19
private gs_context
*ctx_table
[ctx_table_size
];
private long ctx_next_index
;
private int context_create(P2(uint
, gs_context
**));
private int context_param(P2(os_ptr
, gs_context
**));
#define check_context(op, vpc)\
if ( (code = context_param(op, &vpc)) < 0 ) return code
private void context_destroy(P1(gs_context
*));
private int lock_acquire(P1(os_ptr
));
private int lock_release(P1(os_ptr
));
/* List manipulation macros */
(((pl)->head == 0 ? ((pl)->head = pc) : ((pl)->tail->next = pc)),\
(pl)->tail = pc, (pc)->next = 0)
#define add_last_all(pl,pcl) /* pcl->head != 0 */\
(((pl)->head == 0 ? ((pl)->head = (pcl)->head) :\
((pl)->tail->next = (pcl)->head)),\
(pl)->tail = (pcl)->tail, (pcl)->head = 0)
/* ------ Initialization ------ */
private int ctx_reschedule(P0());
private int ctx_time_slice(P0());
memset(ctx_table
, 0, sizeof(ctx_table
));
/* Create an initial context. */
context_create(default_stacksize
, &ctx_current
);
/* Hook into the interpreter. */
gs_interp_reschedule_proc
= ctx_reschedule
;
gs_interp_time_slice_proc
= ctx_time_slice
;
/* ------ Interpreter interface to scheduler ------ */
/* When an operator decides it is time to run a new context, */
/* it returns o_reschedule. The interpreter saves all its state in */
/* memory, calls ctx_reschedule, and then loads the state from memory. */
{ register gs_context
*pctx
;
uint ossize
, essize
, dssize
;
/* Save the state of the current context in ctx_current, */
/* if any context is current at all. */
ossize
= osp
- osbot
+ 1;
essize
= esp
- esbot
+ 1;
dssize
= dsp
- dstack
+ 1;
stackneed
= ossize
+ essize
+ dssize
;
if ( stackneed
> pctx
->stacksize
)
{ alloc_free_refs(pctx
->stacks
, pctx
->stacksize
,
newstacks
= alloc_refs(stackneed
, "ctx_reschedule");
lprintf("Can't allocate stacks!");
pctx
->stacksize
= stackneed
;
pctx
->stacks
= newstacks
;
#define save_stack(sbot, ssize)\
memcpy(stkp, sbot, ssize * sizeof(ref));\
save_stack(osbot
, ossize
);
save_stack(esbot
, essize
);
save_stack(dstack
, dssize
);
/****** MORE TO DO HERE ******/
/* Run the first ready context. */
{ lprintf("No context to run!");
ctx_current
= active
.head
;
active
.head
= active
.head
->next
;
/* Load the state of the new current context. */
#define reload_stack(sbot, ssize, sp)\
memcpy(sbot, stkp, ssize * sizeof(ref));\
sp = sbot + (ssize - 1);\
reload_stack(osbot
, ossize
, osp
);
reload_stack(esbot
, essize
, esp
);
reload_stack(dstack
, dssize
, dsp
);
/****** MORE TO DO HERE ******/
/* If the interpreter wants to time-slice, it saves its state, */
/* calls ctx_time_slice, and reloads its state. */
{ if ( active
.head
== 0 ) return 0;
add_last(&active
, ctx_current
);
/* ------ Context operators ------ */
zcurrentcontext(register os_ptr op
)
make_int(op
, ctx_current
->index
);
zdetach(register os_ptr op
)
if ( pctx
->joiner
!= 0 || pctx
->detach
)
if ( pctx
== ctx_current
)
zfork(register os_ptr op
)
uint ossize
, essize
, dssize
, stacksize
;
while ( !r_has_type(mp
, t_mark
) )
{ if ( mp
<= osbot
) return e_unmatchedmark
;
dssize
= dsp
- dstack
+ 1;
stacksize
= ossize
+ essize
+ dssize
+ 10;
code
= context_create(stacksize
, &pctx
);
if ( code
< 0 ) return code
;
memcpy(stkp
, mp
+ 1, ossize
* sizeof(ref
));
make_oper(stkp
, i_fork_done
, fork_done
);
memcpy(stkp
, dstack
, dssize
* sizeof(ref
));
pctx
->pgs
= igs
; /* ****** WRONG, MUST COPY ****** */
/****** MORE INIT HERE? ******/
make_int(mp
, pctx
->index
);
/* This gets executed when a context terminates normally. */
/****** HOW TO GET IT EXECUTED ON ERROR TERMINATION? ******/
{ if ( ctx_current
->detach
)
{ context_destroy(ctx_current
);
{ gs_context
*pctx
= ctx_current
->joiner
;
ctx_current
->status
= cs_done
;
/* Schedule the context waiting to join this one, if any. */
if ( pctx
!= 0 ) add_last(&active
, pctx
);
zjoin(register os_ptr op
)
if ( pctx
->joiner
!= 0 || pctx
== ctx_current
|| pctx
->detach
)
pctx
->joiner
= ctx_current
;
{ uint count
= pctx
->ossize
;
memcpy(++mp
, pctx
->stacks
, count
* sizeof(ref
));
zyield(register os_ptr op
)
{ if ( active
.head
== 0 ) return 0;
add_last(&active
, ctx_current
);
/* ------ Condition and lock operators ------ */
monitor_release(P1(os_ptr
)),
zcondition(register os_ptr op
)
(gs_condition
*)alloc(1, sizeof(gs_condition
), "zcondition");
if ( pcond
== 0 ) return e_VMerror
;
make_tv(op
, t_condition
, pcond
, pcond
);
zlock(register os_ptr op
)
{ gs_lock
*plock
= (gs_lock
*)alloc(1, sizeof(gs_lock
), "zlock");
if ( plock
== 0 ) return e_VMerror
;
make_tv(op
, t_lock
, plock
, plock
);
zmonitor(register os_ptr op
)
check_type(op
[-1], t_lock
);
plock
= op
[-1].value
.plock
;
if ( plock
->holder
== ctx_current
) return e_invalidcontext
;
code
= lock_acquire(op
- 1);
/****** HOW TO GUARANTEE RELEASE IF CONTEXT DIES? ******/
push_op_estack(monitor_release
, i_monitor_release
);
/* Release the monitor lock when the procedure completes. */
monitor_release(os_ptr op
)
znotify(register os_ptr op
)
check_type(*op
, t_condition
);
if ( pcond
->waiting
.head
== 0 ) return 0; /* nothing to do */
add_last_all(&active
, &pcond
->waiting
);
zwait(register os_ptr op
)
check_type(op
[-1], t_lock
);
check_type(*op
, t_condition
);
add_last(&pcond
->waiting
, ctx_current
);
push_op_estack(await_lock
, i_await_lock
);
/* When the condition is signaled, wait for acquiring the lock. */
{ int code
= lock_acquire(op
- 1);
/* ------ Internal routines ------ */
context_create(uint stacksize
, gs_context
**ppctx
)
pctx
= (gs_context
*)alloc(1, sizeof(gs_context
), "context");
if ( stacksize
< default_stacksize
) stacksize
= default_stacksize
;
stkp
= alloc_refs(stacksize
, "context(stacks)");
if ( pctx
== 0 || stkp
== 0 ) return e_VMerror
;
ctx_index
= ctx_next_index
++;
pctx
->stacksize
= stacksize
;
pctx
->status
= cs_active
;
pte
= &ctx_table
[ctx_index
% ctx_table_size
];
/* Check a context ID. Note that we do not check for context validity. */
context_param(os_ptr op
, gs_context
**ppctx
)
check_type(*op
, t_integer
);
index
= op
->value
.intval
;
if ( index
< 0 ) return e_invalidcontext
;
pctx
= ctx_table
[index
% ctx_table_size
];
for ( ; ; pctx
= pctx
->table_next
)
{ if ( pctx
== 0 ) return e_invalidcontext
;
if ( pctx
->index
== index
) break;
context_destroy(gs_context
*pctx
)
{ gs_context
**ppctx
= &ctx_table
[pctx
->index
% ctx_table_size
];
ppctx
= &(*ppctx
)->table_next
;
*ppctx
= (*ppctx
)->table_next
;
alloc_free_refs(pctx
->stacks
, pctx
->stacksize
, "context_destroy");
alloc_free((char *)pctx
, 1, sizeof(gs_context
), "context_destroy");
/* Acquire a lock. Return 0 if acquired, o_reschedule if not. */
{ gs_lock
*plock
= op
->value
.plock
;
if ( plock
->holder
== 0 )
{ plock
->holder
= ctx_current
;
add_last(&plock
->waiting
, ctx_current
);
/* Release a lock. Return 0 if OK, e_invalidcontext if not. */
{ gs_lock
*plock
= op
->value
.plock
;
if ( plock
->holder
== ctx_current
)
add_last_all(&active
, &plock
->waiting
);
/* ------ Initialization procedure ------ */
op_def zcontext_op_defs
[] = {
{"0condition", zcondition
},
{"0currentcontext", zcurrentcontext
},
{"2monitor", zmonitor
, &i_monitor
},
{"0%fork_done", fork_done
, &i_fork_done
},
{"2%monitor_release", monitor_release
, &i_monitor_release
},
{"2%await_lock", await_lock
, &i_await_lock
},
op_def_end(zcontext_init
)