Skip to content

Commit

Permalink
Merge branch 'lupien-t push origin master add_lock_exclusive'
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Apr 28, 2015
2 parents 2526845 + aadbd33 commit e0eee7d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyvisa/ctwrapper/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def lock(library, session, lock_type, timeout, requested_key=None):
else:
access_key = create_string_buffer(256)
ret = library.viLock(session, lock_type, timeout, requested_key, access_key)
return access_key, ret
return access_key.value, ret


def map_address(library, session, map_space, map_base, map_size,
Expand Down
68 changes: 54 additions & 14 deletions pyvisa/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from __future__ import division, unicode_literals, print_function, absolute_import

import contextlib
import math
import time

Expand Down Expand Up @@ -95,6 +96,21 @@ def last_status(self):
"""
return self.visalib.get_last_status_in_session(self.session)

def _cleanup_timeout(self, timeout):
if timeout is None or math.isinf(timeout):
timeout = constants.VI_TMO_INFINITE

elif timeout < 1:
timeout = constants.VI_TMO_IMMEDIATE

elif not (1 <= timeout <= 4294967294):
raise ValueError("timeout value is invalid")

else:
timeout = int(timeout)

return timeout

@property
def timeout(self):
"""The timeout in milliseconds for all resource I/O operations.
Expand All @@ -109,18 +125,7 @@ def timeout(self):

@timeout.setter
def timeout(self, timeout):
if timeout is None or math.isinf(timeout):
timeout = constants.VI_TMO_INFINITE

elif timeout < 1:
timeout = constants.VI_TMO_IMMEDIATE

elif not (1 <= timeout <= 4294967294):
raise ValueError("timeout value is invalid")

else:
timeout = int(timeout)

timeout = self._cleanup_timeout(timeout)
self.set_visa_attribute(constants.VI_ATTR_TMO_VALUE, timeout)

@timeout.deleter
Expand Down Expand Up @@ -276,7 +281,7 @@ def enable_event(self, event_type, mechanism, context=None):
"""
self.visalib.enable_event(self.session, event_type, mechanism, context)

def lock(self, timeout=None, requested_key=None):
def lock(self, timeout='default', requested_key=None):
"""Establish a shared lock to the resource.
:param timeout: Absolute time period (in milliseconds) that a resource
Expand All @@ -288,10 +293,45 @@ def lock(self, timeout=None, requested_key=None):
:returns: A new shared access key if requested_key is None,
otherwise, same value as the requested_key
"""
timeout = self.timeout if timeout is None else timeout
timeout = self.timeout if timeout == 'default' else timeout
timeout = self._cleanup_timeout(timeout)
return self.visalib.lock(self.session, constants.AccessModes.shared_lock, timeout, requested_key)[0]

def lock_excl(self, timeout='default'):
"""Establish an exclusive lock to the resource.
:param timeout: Absolute time period (in milliseconds) that a resource
waits to get unlocked by the locking session before
returning an error. (Defaults to self.timeout)
"""
timeout = self.timeout if timeout == 'default' else timeout
timeout = self._cleanup_timeout(timeout)
self.visalib.lock(self.session, constants.AccessModes.exclusive_lock, timeout, None)

def unlock(self):
"""Relinquishes a lock for the specified resource.
"""
self.visalib.unlock(self.session)

@contextlib.contextmanager
def lock_context(self, timeout='default', requested_key='exclusive'):
"""A context that locks
:param timeout: Absolute time period (in milliseconds) that a resource
waits to get unlocked by the locking session before
returning an error. (Defaults to self.timeout)
:param requested_key: When using default of 'exclusive' the lock
is an exclusive lock.
Otherwise it is the access key for the shared lock or
None to generate a new shared access key.
The returned context is the access_key if applicable.
"""
if requested_key == 'exclusive':
self.lock_excl(timeout)
access_key = None
else:
access_key = self.lock(timeout, requested_key)
try:
yield access_key
finally:
self.unlock()

0 comments on commit e0eee7d

Please sign in to comment.