summaryrefslogtreecommitdiff
path: root/gs/src/zcontext.c
blob: c570f54ceeb42d65e07bb2ea1dcfd8eb47cddd2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/* Copyright (C) 1991, 1995, 1997 Aladdin Enterprises.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

/* zcontext.c */
/* Display PostScript context operators */
#include "memory_.h"
#include "ghost.h"
#include "gp.h"			/* for usertime */
#include "errors.h"
#include "oper.h"
#include "gsexit.h"
#include "gsstruct.h"
#include "gsutil.h"
#include "gxalloc.h"
#include "idict.h"
#include "igstate.h"
#include "icontext.h"
#include "istruct.h"
#include "dstack.h"
#include "estack.h"
#include "ostack.h"
#include "store.h"

/* Still needs:
   fork: copy gstate stack
   fork_done: do all necessary restores
 */

/* Scheduling hooks in interp.c */
extern int (*gs_interp_reschedule_proc)(P0());
extern int (*gs_interp_time_slice_proc)(P0());
extern int gs_interp_time_slice_ticks;

/* Per-context state stored in statics */
extern ref ref_binary_object_format;

/* Context state structure */
/* (defined in icontext.h) */
extern_st(st_ref_stack);
private_st_context_state();
#define pcst ((gs_context_state_t *)vptr)
private ENUM_PTRS_BEGIN(context_state_enum_ptrs) return 0;
	ENUM_REF(0, gs_context_state_t, dstack);
	ENUM_REF(1, gs_context_state_t, estack);
	ENUM_REF(2, gs_context_state_t, ostack);
	ENUM_PTR(3, gs_context_state_t, pgs);
ENUM_PTRS_END
private RELOC_PTRS_BEGIN(context_state_reloc_ptrs) {
	RELOC_REF(gs_context_state_t, dstack);
	RELOC_REF(gs_context_state_t, estack);
	RELOC_REF(gs_context_state_t, ostack);
	RELOC_PTR(gs_context_state_t, pgs);
} RELOC_PTRS_END
#undef pcst

/* Context structure */
typedef enum {
	cs_active,
	cs_done
} ctx_status;
typedef struct gs_context_s gs_context;
struct gs_context_s {
	gs_context_state_t state;	/* (must be first for subclassing) */
		/* Private state */
	ctx_status status;
	ulong index;
	bool detach;			/* true if a detach has been */
					/* executed for this context */
	gs_context *next;		/* next context with same status */
					/* (active, waiting on same lock, */
					/* waiting on same condition, */
					/* waiting to be destroyed) */
	gs_context *joiner;		/* context waiting on a join */
					/* for this one */
	gs_context *table_next;		/* hash table chain */
};
gs_private_st_suffix_add3(st_context, gs_context, "context",
  context_enum_ptrs, context_reloc_ptrs, st_context_state,
  next, joiner, table_next);

/* Context list structure */
typedef struct ctx_list_s {
	gs_context *head;
	gs_context *tail;
} ctx_list;

/* Condition structure */
typedef struct gs_condition_s {
	ctx_list waiting;	/* contexts waiting on this condition */
} gs_condition;
gs_private_st_ptrs2(st_condition, gs_condition, "conditiontype",
  condition_enum_ptrs, condition_reloc_ptrs, waiting.head, waiting.tail);

/* Lock structure */
typedef struct gs_lock_s {
	ctx_list waiting;		/* contexts waiting for this lock, */
					/* must be first for subclassing */
	gs_context *holder;		/* context holding the lock, if any */
} gs_lock;
gs_private_st_suffix_add1(st_lock, gs_lock, "locktype",
  lock_enum_ptrs, lock_reloc_ptrs, st_condition, holder);

/* Global state */
typedef struct gs_scheduler_s {
  gs_context *current;
  long usertime_initial;	/* usertime when current started running */
  ctx_list active;
  gs_context *dead;
#define ctx_table_size 19
  gs_context *table[ctx_table_size];
} gs_scheduler_t;
private gs_scheduler_t *gs_scheduler;
#define ctx_current gs_scheduler->current
#define ctx_active gs_scheduler->active
#define ctx_dead gs_scheduler->dead
#define ctx_table gs_scheduler->table
private gs_gc_root_t gs_scheduler_root;
/* Structure definition */
gs_private_st_composite(st_scheduler, gs_scheduler_t, "gs_scheduler",
  scheduler_enum_ptrs, scheduler_reloc_ptrs);
private ENUM_PTRS_BEGIN(scheduler_enum_ptrs) {
  index -= 4;
  if ( index < ctx_table_size )
    ENUM_RETURN_PTR(gs_scheduler_t, table[index]);
  return 0;
}
  ENUM_PTR(0, gs_scheduler_t, current);
  ENUM_PTR(1, gs_scheduler_t, active.head);
  ENUM_PTR(2, gs_scheduler_t, active.tail);
  ENUM_PTR(3, gs_scheduler_t, dead);
ENUM_PTRS_END
private RELOC_PTRS_BEGIN(scheduler_reloc_ptrs) {
  RELOC_PTR(gs_scheduler_t, current);
  RELOC_PTR(gs_scheduler_t, active.head);
  RELOC_PTR(gs_scheduler_t, active.tail);
  RELOC_PTR(gs_scheduler_t, dead);
  { int i;
    for ( i = 0; i < ctx_table_size; ++i )
      RELOC_PTR(gs_scheduler_t, table[i]);
  }
} RELOC_PTRS_END

/* Forward references */
private int context_create(P2(gs_context **, gs_ref_memory_t *));
private long context_usertime(P0());
#define context_load(psched, pctx)\
  do {\
    if ( (pctx)->state.keep_usertime )\
      (psched)->usertime_initial = context_usertime();\
    context_state_load(&(pctx)->state);\
  } while (0)
#define context_store(psched, pctx)\
  do {\
    context_state_store(&(pctx)->state);\
    if ( (pctx)->state.keep_usertime )\
      (pctx)->state.usertime_total +=\
        context_usertime() - (psched)->usertime_initial;\
  } while (0)
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 void stack_copy(P4(ref_stack *, const ref_stack *, uint, uint));
private int lock_acquire(P1(os_ptr));
private int lock_release(P1(ref *));

/* List manipulation macros */
#define add_last(pl,pc)\
  (((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 = (pcl)->tail = 0)

/* ------ Initialization ------ */

private int ctx_reschedule(P0());
private int ctx_time_slice(P0());
private void
zcontext_init(void)
{	gs_ref_memory_t *imem = iimemory_global;

	gs_scheduler = gs_alloc_struct((gs_memory_t *)imem, gs_scheduler_t,
				       &st_scheduler, "gs_scheduler");
	gs_register_struct_root((gs_memory_t *)imem, &gs_scheduler_root,
				(void **)&gs_scheduler, "gs_scheduler");
	ctx_current = 0;
	ctx_active.head = ctx_active.tail = 0;
	ctx_dead = 0;
	memset(ctx_table, 0, sizeof(ctx_table));
	/* Create an initial context. */
	if ( context_create(&ctx_current, imem) < 0 )
	  { lprintf("Can't create initial context!");
	    gs_abort();
	  }
	ctx_current->state.memory = gs_imemory;
	/* Hook into the interpreter. */
	gs_interp_reschedule_proc = ctx_reschedule;
	gs_interp_time_slice_proc = ctx_time_slice;
	gs_interp_time_slice_ticks = 100;
}

/* ------ 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. */
private int
ctx_reschedule(void)
{	/* Save the state of the current context in ctx_current, */
	/* if any context is current at all. */
	if ( ctx_current != 0 )
	  context_store(gs_scheduler, ctx_current);
	/* If there are any dead contexts waiting to be released, */
	/* take care of that now. */
	while ( ctx_dead != 0 )
	  { gs_context *next = ctx_dead->next;

	    context_destroy(ctx_dead);
	    ctx_dead = next;
	  }
	/* Run the first ready context. */
	if ( ctx_active.head == 0 )
	  { lprintf("No context to run!");
	    return_error(e_Fatal);
	  }
	ctx_current = ctx_active.head;
	if ( !(ctx_active.head = ctx_active.head->next) )
	  ctx_active.tail = 0;
	/* Load the state of the new current context. */
	context_load(gs_scheduler, ctx_current);
	esfile_clear_cache();
	dict_set_top();		/* reload dict stack cache */
	return 0;
}

/* If the interpreter wants to time-slice, it saves its state, */
/* calls ctx_time_slice, and reloads its state. */
private int
ctx_time_slice(void)
{	if ( ctx_active.head == 0 )
	  return 0;
	add_last(&ctx_active, ctx_current);
	return ctx_reschedule();
}

/* ------ Context operators ------ */

private int fork_done(P1(os_ptr));

/* - currentcontext <context> */
private int
zcurrentcontext(register os_ptr op)
{	push(1);
	make_int(op, ctx_current->index);
	return 0;
}

/* <context> detach - */
private int
zdetach(register os_ptr op)
{	gs_context *pctx;
	int code;

	check_context(op, pctx);
	if ( pctx->joiner != 0 || pctx->detach )
	  return_error(e_invalidcontext);
	switch ( pctx->status )
	  {
	  case cs_active:
		pctx->detach = true;
		break;
	  case cs_done:
		context_destroy(pctx);
	  }
	pop(1);
	return 0;
}

/* <mark> <obj1> ... <objN> <proc> fork <context> */
private int
zfork(register os_ptr op)
{	uint mcount = ref_stack_counttomark(&o_stack);
	gs_context *pctx;
	int code;

	check_proc(*op);
	if ( mcount == 0 )
	  return_error(e_unmatchedmark);
	if ( gs_imemory.save_level )
	  return_error(e_invalidcontext);
	code = context_create(&pctx, iimemory_global);
	if ( code < 0 )
	  return code;
	/* Share local and global VM. */
	pctx->state.memory = gs_imemory;
	{ int i;
	  for ( i = 0; i < countof(gs_imemory.spaces.indexed); ++i )
	    if ( gs_imemory.spaces.indexed[i] != 0 )
	      gs_imemory.spaces.indexed[i]->num_contexts++;
	}
	/* Initialize the stacks. */
	{ ref_stack *dstack = r_ptr(&pctx->state.dstack, ref_stack);
	  uint count = ref_stack_count(&d_stack);

	  ref_stack_push(dstack, count - ref_stack_count(dstack));
	  stack_copy(dstack, &d_stack, count, 0);
	}
	{ ref_stack *estack = r_ptr(&pctx->state.estack, ref_stack);

	  ref_stack_push(estack, 3);
	  /* fork_done must be executed in both normal and error cases. */
	  make_mark_estack(estack->p - 2, es_other, fork_done);
	  make_oper(estack->p - 1, 0, fork_done);
	  *estack->p = *op;
	}
	{ ref_stack *ostack = r_ptr(&pctx->state.ostack, ref_stack);
	  uint count = mcount - 2;

	  ref_stack_push(ostack, count);
	  stack_copy(ostack, &o_stack, count, 1);
	}
	/****** COPY GSTATE STACK ******/
	pctx->state.binary_object_format = ref_binary_object_format;
	add_last(&ctx_active, pctx);
	pop(mcount - 1);
	op = osp;
	make_int(op, pctx->index);
	return 0;
}
/* This gets executed when a context terminates normally */
/* or when the stack is being unwound for an error termination. */
/****** MUST DO ALL RESTORES ******/
/****** WHAT IF invalidrestore? ******/
private int
fork_done(os_ptr op)
{	if ( ctx_current->detach )
	  { /*
	     * We can't free the context's memory yet, because the
	     * interpreter still has references to it.  Instead, queue the
	     * context to be freed the next time we reschedule.
	     */
	    context_store(gs_scheduler, ctx_current);
	    ctx_current->next = ctx_dead;
	    ctx_dead = ctx_current;
	    ctx_current = 0;
	  }
	else
	  { 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(&ctx_active, pctx);
	  }
	return o_reschedule;
}

/* <context> join <mark> <obj1> ... <objN> */
private int
zjoin(register os_ptr op)
{	gs_context *pctx;
	int code;

	check_context(op, pctx);
	if ( pctx->joiner != 0 || pctx->detach || pctx == ctx_current ||
	     pctx->state.memory.space_global !=
	       ctx_current->state.memory.space_global ||
	     pctx->state.memory.space_local !=
	       ctx_current->state.memory.space_local ||
	     gs_imemory.save_level != 0
	   )
	  return_error(e_invalidcontext);
	switch ( pctx->status )
	   {
	case cs_active:
		pctx->joiner = ctx_current;
		return o_reschedule;
	case cs_done:
	   {	const ref_stack *ostack =
		  r_ptr(&pctx->state.ostack, ref_stack);
		uint count = ref_stack_count(ostack);

		push(count);
		{ ref *rp = ref_stack_index(&o_stack, count);
		  make_mark(rp);
		}
		stack_copy(&o_stack, ostack, count, 0);
		context_destroy(pctx);
	   }
	   }
	return 0;
}

/* - yield - */
private int
zyield(register os_ptr op)
{	if ( ctx_active.head == 0 )
	  return 0;
	add_last(&ctx_active, ctx_current);
	return o_reschedule;
}

/* ------ Condition and lock operators ------ */

private int
  monitor_cleanup(P1(os_ptr)),
  monitor_release(P1(os_ptr)),
  await_lock(P1(os_ptr));

/* - condition <condition> */
private int
zcondition(register os_ptr op)
{	gs_condition *pcond =
	  ialloc_struct(gs_condition, &st_condition, "zcondition");

	if ( pcond == 0 )
	  return_error(e_VMerror);
	pcond->waiting.head = pcond->waiting.tail = 0;
	push(1);
	make_istruct(op, a_all, pcond);
	return 0;
}

/* - lock <lock> */
private int
zlock(register os_ptr op)
{	gs_lock *plock = ialloc_struct(gs_lock, &st_lock, "zlock");

	if ( plock == 0 )
	  return_error(e_VMerror);
	plock->holder = 0;
	plock->waiting.head = plock->waiting.tail = 0;
	push(1);
	make_istruct(op, a_all, plock);
	return 0;
}

/* <lock> <proc> monitor - */
private int
zmonitor(register os_ptr op)
{	gs_lock *plock;
	int code;

	check_stype(op[-1], st_lock);
	check_proc(*op);
	plock = r_ptr(op - 1, gs_lock);
	if ( plock->holder == ctx_current ||
	     (gs_imemory.save_level != 0 &&
	      plock->holder != 0 &&
	      plock->holder->state.memory.space_local ==
	        ctx_current->state.memory.space_local)
	   )
	  return_error(e_invalidcontext);
	/*
	 * We push on the e-stack:
	 *	The lock object
	 *	An e-stack mark with monitor_cleanup, to release the lock
	 *	  in case of an error
	 *	monitor_release, to release the lock in the normal case
	 *	The procedure to execute
	 */
	check_estack(4);
	code = lock_acquire(op - 1);
	if ( code != 0 )
	  { /* We didn't acquire the lock.  Re-execute this later. */
	    push_op_estack(zmonitor);
	    return code;	/* o_reschedule */
	  }
	*++esp = op[-1];
	push_mark_estack(es_other, monitor_cleanup);
	push_op_estack(monitor_release);
	*++esp = *op;
	pop(2);
	return code;
}
/* Release the monitor lock when unwinding for an error or exit. */
private int
monitor_cleanup(os_ptr op)
{	int code = lock_release(esp);
	--esp;
	return code;
}
/* Release the monitor lock when the procedure completes. */
private int
monitor_release(os_ptr op)
{	--esp;
	return monitor_cleanup(op);
}

/* <condition> notify - */
private int
znotify(register os_ptr op)
{	gs_condition *pcond;

	check_stype(*op, st_condition);
	pcond = r_ptr(op, gs_condition);
	pop(1); op--;
	if ( pcond->waiting.head == 0 )		/* nothing to do */
	  return 0;
	add_last_all(&ctx_active, &pcond->waiting);
	return zyield(op);
}

/* <lock> <condition> wait - */
private int
zwait(register os_ptr op)
{	gs_lock *plock;
	gs_condition *pcond;

	check_stype(op[-1], st_lock);
	plock = r_ptr(op - 1, gs_lock);
	check_stype(*op, st_condition);
	pcond = r_ptr(op, gs_condition);
	if ( plock->holder != ctx_current ||
	     (gs_imemory.save_level != 0 &&
	      (r_space(op - 1) == avm_local || r_space(op) == avm_local))
	   )
	  return_error(e_invalidcontext);
	check_estack(1);
	lock_release(op - 1);
	add_last(&pcond->waiting, ctx_current);
	push_op_estack(await_lock);
	return o_reschedule;
}
/* When the condition is signaled, wait for acquiring the lock. */
private int
await_lock(os_ptr op)
{	int code = lock_acquire(op - 1);

	if ( code == 0 )
	  { pop(2);
	    return code;
	  }
	/* We didn't acquire the lock.  Re-execute the wait. */
	push_op_estack(await_lock);
	return code;		/* o_reschedule */
}

/* ------ Miscellaneous operators ------ */

/* - usertime <int> */
private int
zusertime_context(register os_ptr op)
{	long utime = context_usertime();

	push(1);
	if ( !ctx_current->state.keep_usertime ) {
	  /*
	   * This is the first time this context has executed usertime:
	   * we must track its execution time from now on.
	   */
	  gs_scheduler->usertime_initial = utime;
	  ctx_current->state.keep_usertime = true;
	}
	make_int(op, ctx_current->state.usertime_total + utime -
		 gs_scheduler->usertime_initial);
	return 0;
}

/* ------ Internal routines ------ */

/* Create a context. */
private int
context_create(gs_context **ppctx, gs_ref_memory_t *mem)
{	gs_context *pctx;
	int code;
	long ctx_index;
	gs_context **pte;

	pctx = gs_alloc_struct((gs_memory_t *)mem, gs_context, &st_context,
			       "context_create");
	if ( pctx == 0 )
	  return_error(e_VMerror);
	code = context_state_alloc(&pctx->state, mem);
	if ( code < 0 )
	  { gs_free_object((gs_memory_t *)mem, pctx, "context_create");
	    return code;
	  }
	ctx_index = gs_next_ids(1);
	pctx->status = cs_active;
	pctx->index = ctx_index;
	pctx->detach = false;
	pctx->next = 0;
	pctx->joiner = 0;
	pte = &ctx_table[ctx_index % ctx_table_size];
	pctx->table_next = *pte;
	*pte = pctx;
	*ppctx = pctx;
	return 0;
}

/* Check a context ID.  Note that we do not check for context validity. */
private int
context_param(os_ptr op, gs_context **ppctx)
{	gs_context *pctx;
	long index;

	check_type(*op, t_integer);
	index = op->value.intval;
	if ( index < 0 )
	  return_error(e_invalidcontext);
	pctx = ctx_table[index % ctx_table_size];
	for ( ; ; pctx = pctx->table_next )
	  { if ( pctx == 0 )
	      return_error(e_invalidcontext);
	    if ( pctx->index == index )
	      break;
	  }
	*ppctx = pctx;
	return 0;
}

/* Read the usertime as a single value. */
private long
context_usertime(void)
{	long secs_ns[2];

	gp_get_usertime(secs_ns);
	return secs_ns[0] * 1000 + secs_ns[1] / 1000000;
}

/* Destroy a context. */
private void
context_destroy(gs_context *pctx)
{	gs_context **ppctx = &ctx_table[pctx->index % ctx_table_size];

	while ( *ppctx != pctx )
	  ppctx = &(*ppctx)->table_next;
	*ppctx = (*ppctx)->table_next;
	context_state_free(&pctx->state, iimemory);
	ifree_object(pctx, "context_destroy");
}

/* Copy the top elements of one stack to another. */
/* Note that this does not push the elements: */
/* the destination stack must have enough space preallocated. */
private void
stack_copy(ref_stack *to, const ref_stack *from, uint count, uint from_index)
{	long i;

	for ( i = (long)count - 1; i >= 0; --i )
	  *ref_stack_index(to, i) = *ref_stack_index(from, i + from_index);
}

/* Acquire a lock.  Return 0 if acquired, o_reschedule if not. */
private int
lock_acquire(os_ptr op)
{	gs_lock *plock = r_ptr(op, gs_lock);

	if ( plock->holder == 0 )
	  { plock->holder = ctx_current;
	    return 0;
	  }
	add_last(&plock->waiting, ctx_current);
	return o_reschedule;
}

/* Release a lock.  Return 0 if OK, e_invalidcontext if not. */
private int
lock_release(ref *op)
{	gs_lock *plock = r_ptr(op, gs_lock);

	if ( plock->holder == ctx_current )
	  { plock->holder = 0;
	    add_last_all(&ctx_active, &plock->waiting);
	    return 0;
	  }
	return_error(e_invalidcontext);
}

/* ------ Initialization procedure ------ */

BEGIN_OP_DEFS(zcontext_op_defs) {
	{"0condition", zcondition},
	{"0currentcontext", zcurrentcontext},
	{"1detach", zdetach},
	{"2fork", zfork},
	{"1join", zjoin},
	{"0lock", zlock},
	{"2monitor", zmonitor},
	{"1notify", znotify},
	{"2wait", zwait},
	{"0yield", zyield},
		/* Note that the following replace prior definitions */
		/* in the indicated files: */
	{"0usertime", zusertime_context},	/* zmisc.c */
		/* Internal operators */
	{"0%fork_done", fork_done},
	{"0%monitor_cleanup", monitor_cleanup},
	{"0%monitor_release", monitor_release},
	{"2%await_lock", await_lock},
END_OP_DEFS(zcontext_init) }