socsvn commit: r236215 - soc2012/scher/par_ports/head/Mk

scher at FreeBSD.org scher at FreeBSD.org
Wed May 23 17:03:03 UTC 2012


Author: scher
Date: Wed May 23 17:03:01 2012
New Revision: 236215
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=236215

Log:
  [new_feature] directory locking targets
  
  Two directory locking targets (do-lock, do-unlock) are implemented in
  bsd.parallel.mk file.
  This targets provide atomic locking/unlocking technique to lock/unlock
  a directory specified by ${_parv_DIR_TO_LOCK} variable.
  Submitted by:   Alexander Pronin

Added:
  soc2012/scher/par_ports/head/Mk/bsd.parallel.mk

Added: soc2012/scher/par_ports/head/Mk/bsd.parallel.mk
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2012/scher/par_ports/head/Mk/bsd.parallel.mk	Wed May 23 17:03:01 2012	(r236215)
@@ -0,0 +1,83 @@
+#-*- mode: Fundamental; tab-width: 4; -*-
+# ex:ts=4
+#
+# $FreeBSD$
+#
+# Please view me with 4 column tabs!
+
+.include <bsd.commands.mk>
+
+
+_dparv_=
+_parv_=
+#################
+# Debugging specific tools and variable declarations
+_dparv_START_OUTPUT_MESSAGE= "=================_PAR_PORTS_SPECIFIC_OUTPUT_=============="
+_dparv_END_OUTPUT_MESSAGE= "==============_END_OF_PAR_PORTS_SPECIFIC_OUTPUT_=============="
+
+_dparv_START_OUTPUT= ${ECHO_CMD} ${_dparv_START_OUTPUT_MESSAGE}
+_dparv_END_OUTPUT= ${ECHO_CMD} ${_dparv_END_OUTPUT_MESSAGE}
+#################
+
+
+#################
+# Commands
+_parv_KILL= /bin/kill
+_parv_KILL_FLAGS= --
+#################
+# Locking variables and tools
+_parv_DIR_TO_LOCK?=${PORTSDIR}/tmp
+_parv_LOCK_FILE= ${_parv_DIR_TO_LOCK}/.lock
+
+_parv_WAIT_FOR_LOCK_TIME= 5
+
+# Senquence of commands to lock a directory using ${_parv_LOCK_FILE}.
+# During evaluation of the following commands lockf(1) is holding lock on ${_parv_LOCK_FILE} file.
+# Hence NO other process is able to evaluate any commands using lockf(1)
+# locking on the same ${_parv_LOCK_FILE} file.
+# 
+# Stalled locks cheking enabled.
+#
+# If the directory is locked then make process will be terminated with 143 exit code.
+#
+_parv_LOCK_SEQ= \
+	pid=$$(${CAT} ${_parv_LOCK_FILE}); \
+	if [ $${pid} ]; then \
+		pgrep -P $${pid} > /dev/null; \
+		status=$$(${ECHO_CMD} $$?); \
+		if [ $${status} -eq 0 ]; then \
+			${_dparv_START_OUTPUT}; \
+			${ECHO_CMD} Unable to lock ${_parv_DIR_TO_LOCK}; \
+			${ECHO_CMD} Dir: ${_parv_DIR_TO_LOCK}   is already locked by another working process ...; \
+			${_dparv_END_OUTPUT}; \
+			${_parv_KILL} ${_parv_KILL_FLAGS} -${.MAKE.PID}; \
+		else \
+			${_dparv_START_OUTPUT}; \
+			${ECHO_CMD} Dir: ${_parv_DIR_TO_LOCK}   Stalled lock Detected!; \
+			${ECHO_CMD} Deleting stalled lock; \
+			${ECHO_CMD} Locking: ${_parv_DIR_TO_LOCK}; \
+			${_dparv_END_OUTPUT}; \
+			${ECHO_CMD} ${.MAKE.PID} > ${_parv_LOCK_FILE}; \
+		fi; \
+	else \
+		${_dparv_START_OUTPUT}; \
+		${ECHO_CMD} Locking: ${_parv_DIR_TO_LOCK}; \
+		${_dparv_END_OUTPUT}; \
+		${ECHO_CMD} ${.MAKE.PID} > ${_parv_LOCK_FILE}; \
+	fi; \
+#################
+
+
+do-lock:
+	@lockf -k -t ${_parv_WAIT_FOR_LOCK_TIME} ${_parv_LOCK_FILE} ${SH} -c '${_parv_LOCK_SEQ}' || ( \
+		${_dparv_START_OUTPUT}; \
+		${ECHO_CMD} "Unable to lock ${_parv_DIR_TO_LOCK}"; \
+		${ECHO_CMD} "Lock file: ${_parv_LOCK_FILE} is alredy locked by another working process ..."; \
+		${_dparv_END_OUTPUT}; \
+		${_parv_KILL} ${_parv_KILL_FLAGS} -${.MAKE.PID}; \
+	); \
+
+# In this target there is no timeout, because if it is necessary to unlock dir then
+# this process has to wait until all other registered to lockf(1) operation processes do their stuff.
+do-unlock:
+	@lockf -k ${_parv_LOCK_FILE} ${RM} ${_parv_LOCK_FILE}


More information about the svn-soc-all mailing list