	.TITLE MUTEX - enqueues and dequeues locks for ED1 program
	.IDENT /01A/

; Externals
	.EXTERNAL	ED1_INUSE

; Macro library calls
	$SSDEF
	$LCKDEF

	.macro	errorck,?l1
	blbs	r0,l1
	$exit_s	code=r0
l1:
	.endm

; Read-Write data program section
	.PSECT	MUTEX_RWDATA,RD,WRT,NOEXE

LKSB:		.BLKQ	1
RESLEN:		.BLKL	1
RESPTR:		.BLKL	1

	.PSECT	MUTEX_CODE,EXE,RD,NOWRT

	.entry	ed1$acquire_mutex,^m<R2,R3,R4,R5,R6>

	movl	4(AP),reslen		; get descriptor block
      	movl	reslen,resptr		; length and address
	movl	@reslen,reslen		; get the length
	addl 	#4,resptr		; get buffer pointer
	movl	@resptr,resptr		; and address of buffer

	cmpw	reslen,#31		; see if a problem with length
	blequ	length_ok		; ... if no problem, skip
	movw	#31,reslen		; ... if too long, shorten

length_ok:
	$enq_s -
		efn=#1,-		; use event flag 0
		lkmode=#LCK$K_EXMODE,-	; lock at exclusive access
		lksb=LKSB,-		; address of lock status block
		flags=#LCK$M_NOQUEUE,-
		resnam=RESLEN		; name of resource to lock

	cmpl	R0,#SS$_NOTQUEUED	; did we get it?
	beql	no_lock			; no, sorry
	errorck				; check for other errors
	ret				; yes, return
no_lock:
	pushl	#ED1_INUSE		; file is in-use
	calls	#1,G^LIB$SIGNAL		; signal, and end
	ret

	.entry ed1$release_mutex,^m<R2,R3,R4>

	$deq_s -
		lkid=LKSB+4		; the lock id is in lock status block
	movl	#SS$_NORMAL,R0
	ret
	.end
