- # Only create a working instance for the first invocation.
- # Degrade all other instances, make them fail "late" during
- # execution, so that users will see the errors.
- self._lib = None
- self._data = None
- if IrmpLibrary.__usable_instance is None:
- filename = self._library_filename()
- self._lib = ctypes.cdll.LoadLibrary(filename)
- self._library_setup_api()
- IrmpLibrary.__usable_instance = self
+ filename = self._library_filename()
+ self._lib = ctypes.cdll.LoadLibrary(filename)
+ self._library_setup_api()
+
+ def __del__(self):
+ '''
+ Release a disposed library instance.
+ '''
+
+ if self._inst:
+ self._lib.irmp_instance_free(self._inst)
+ self._inst = None
+
+ def __enter__(self):
+ '''
+ Enter a context (lock management).
+ '''
+
+ if self._inst is None:
+ self._inst = self._lib.irmp_instance_alloc()
+ self._lib.irmp_instance_lock(self._inst, 1)
+ return self
+
+ def __exit__(self, extype, exvalue, trace):
+ '''
+ Leave a context (lock management).
+ '''
+
+ self._lib.irmp_instance_unlock(self._inst)
+ return False
+
+ def client_id(self):
+ return self._lib.irmp_instance_id(self._inst)