nrlock.py revision 1516
1516N/A#!/usr/bin/python
1271N/A#
1271N/A# CDDL HEADER START
1271N/A#
1271N/A# The contents of this file are subject to the terms of the
1271N/A# Common Development and Distribution License (the "License").
1271N/A# You may not use this file except in compliance with the License.
1271N/A#
1271N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1271N/A# or http://www.opensolaris.org/os/licensing.
1271N/A# See the License for the specific language governing permissions
1271N/A# and limitations under the License.
1271N/A#
1271N/A# When distributing Covered Code, include this CDDL HEADER in each
1271N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1271N/A# If applicable, add the following below this CDDL HEADER, with the
1271N/A# fields enclosed by brackets "[]" replaced with your own identifying
1271N/A# information: Portions Copyright [yyyy] [name of copyright owner]
1271N/A#
1271N/A# CDDL HEADER END
1271N/A#
1271N/A# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
1271N/A# Use is subject to license terms.
1271N/A#
1271N/A
1271N/A"""Interface and implementation for Non-Reentrant locks. Derived from
1271N/ARLocks (which are reentrant locks). The default Python base locking
1271N/Atype, threading.Lock(), is non-reentrant but it doesn't support any
1271N/Aoperations other than aquire() and release(), and we'd like to be able
1271N/Ato support things like RLocks._is_owned() so that we can "assert" lock
1271N/Aownership assumptions in our code."""
1271N/A
1271N/Aimport threading
1271N/A
1271N/A# Rename some stuff so "from pkg.nrlock import *" is safe
1271N/A__all__ = [ 'NRLock' ]
1271N/A
1271N/Adef NRLock(*args, **kwargs):
1271N/A return _NRLock(*args, **kwargs)
1271N/A
1271N/Aclass _NRLock(threading._RLock):
1271N/A
1271N/A def __init__(self, verbose=None):
1271N/A self.__rlock = threading.RLock(verbose)
1271N/A
1271N/A def __repr__(self):
1271N/A self.__rlock.__repr__()
1271N/A
1271N/A def acquire(self, blocking=1):
1271N/A assert not self.__rlock._is_owned(), "recursive NRLock acquire"
1271N/A return self.__rlock.acquire(blocking)
1271N/A
1271N/A def release(self):
1271N/A return self.__rlock.release()
1271N/A
1271N/A def _is_owned(self):
1271N/A return self.__rlock._is_owned()