OpenVMS Source Code Demos

demo_lock_dlm_100.bas

1000	%title	"DEMO_LOCK_DLM_100.BAS"
	%ident	"version_100.2"
	!========================================================================================================================
	! Title  : DEMO_LOCK_DLM_100.BAS
	! Author : Neil Rieck
	! Created: 00.04.03
	! Purpose: to demonstrate the use of the DISTIRBUTED LOCK MANAGER method to control access to a shared resource
	! Notes  : in this demo it is assumed that this process needs exclusive write access to an array in memory (this would
	! almost certainly be a global page file where OpenVMS was used to map memory over a data file). The location we wish
	! to lock is row 33 column 66 so we will just make up a unique name that reflects that cell.
	!
	! ver who when   what
	! --- --- ------ --------------------------------------------------------------------------------------------------------
        ! 100 NSR 000403 1. original work
        !     NSR 050901 2. modified to compile correctly without needing "[.inc]VMS_Externals.inc"
	!========================================================================================================================
	! calls:	$enq		enqueue		(async)
	!		$enqw		enqueue wait	(sync)
	!		$deq		dequeue
	!		$getlki		get lock info
	!
	! lock modes:	lck$m_nlmode	null
	!		lck$m_crmode	concurrent read		allows shared reading
	!		lck$m_cwmode	concurrent write	allows shared writing
	!		lck$m_prmode	protected read		allows shared read but no writers
	!		lck$m_pwmode	protected write		allows shared read but no other writers (other than self)
	!		lck$m_exmode	exclusive		allows no sharing with others
	!========================================================================================================================
	option type=explicit							! no kid stuff
	!
        %include "starlet"      %from %library "sys$library:basic$starlet"      ! system services
        %include "$ssdef"       %from %library "sys$library:basic$starlet"      ! ss$
        %include "$lckdef"      %from %library "sys$library:basic$starlet"      ! lck$
        %include "$lkidef"      %from %library "sys$library:basic$starlet"      ! lki$
	!
	declare long	rc%			 !
	!
	record lock_block_rec			 ! define a new data structure
		word	lock_condition		,!						&
			reserved		,!						&
		long	lock_ident		,! lock id#					&
		byte	lock_value(16)		 ! (only required with flag: lck$m_valblk)
	end record lock_block_rec
	!
	declare lock_block_rec lock_block	! now declare our variable
	!
	! SYS$ENQW [efn] ,lkmode ,lksb ,[flags] ,[resnam] ,[parid] ,[astadr] ,[astprm] ,[blkast] ,[acmode] ,[rsdm_id]
	!
	!	place a "null" lock (OpenVMS will make an entry in the lock manager if one doesn't exist)
	!
	print "-i-enqw nl"
	rc% = sys$enqw(				,! efn:							&
			lck$k_nlmode		,! lkmode: null; just indicates an interest		&
			lock_block		,! lksb:						&
						,! flags:						&
			"DEMO_LOCK_DLM_3366"	,! resname: name of the protected resource 		&
			,,,,,,,	)
	print "-e-enqw nl rc:",str$(rc%)	if (rc% and 1%) <> 1%
	!
	!	convert our "null" lock to a "protected write" lock
	!
	print "-i-enqw pw"
	rc% = sys$enqw(				,! efn:							&
			lck$k_pwmode		,! lkmode: protected write				&
			lock_block		,! lksb:						&
			lck$m_convert		,! flags:						&
			"DEMO_LOCK_DLM_3366"	,! resname: name of the protected resource 		&
			,,,,,,, )
	print "-e-enqw pw rc:",str$(rc%)	if (rc% and 1%) <> 1%
	!
	!	convert our "protected write" lock to an "exclusive" lock
	!
	print "-i-enqw ex"
	rc% = sys$enqw(				,! efn:							&
			lck$k_exmode		,! lkmode: exclusive					&
			lock_block		,! lksb:						&
			lck$m_convert		,! flags:						&
			"DEMO_LOCK_DLM_3366"	,! resname: name of the protected resource 		&
			,,,,,,, )
	print "-e-enqw ex rc:",str$(rc%)	if (rc% and 1%) <> 1%
	!
	print "starting work (30 seconds)"
	sleep 30									! do some work
	print "finished work"
	!
	!	convert our "exclusive" lock back to a "null" lock
	!
	goto dequeue if 1=1
	print "-i-enqw nl"
	rc% = sys$enqw(				,! efn:							&
			lck$k_nlmode		,! lkmode: null;					&
			lock_block		,! lksb:						&
						,! flags:						&
			"DEMO_LOCK_DLM_3366"	,! resname: name of the protected resource 		&
			,,,,,,,	)
	print "-e-enqw nl rc:",str$(rc%)	if (rc% and 1%) <> 1%
	!
	!	remove "our interest" in this resource
	!
	! SYS$DEQ [lkid] ,[valblk] ,[acmode] ,[flags]
	!
	dequeue:
	print "-i-deq all"
	rc% = sys$deq(			,! lkid:	&
					,! valblk:	&
					,! acmode:	&
			LCK$M_DEQALL	 ! flags:	&
			)
	print "-e-deq all rc:",str$(rc%)	if (rc% and 1%) <> 1%
	!
	end

Back to Home
Neil Rieck
Waterloo, Ontario, Canada.