From fc79084886aa81e7414a4bd2aba48aa96a0b641b Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sun, 20 Sep 2015 15:01:25 -0400 Subject: Implement mutexes in py11 library using threading.Lock objects. --- scripts/py11-test.py | 65 ++++------------------------------------------------ 1 file changed, 4 insertions(+), 61 deletions(-) (limited to 'scripts') diff --git a/scripts/py11-test.py b/scripts/py11-test.py index a5e9c88..1719f8b 100644 --- a/scripts/py11-test.py +++ b/scripts/py11-test.py @@ -71,72 +71,15 @@ if False: print "Not using MUTEXes at all" p11.C_Initialize() -elif True: +elif False: print "Using OS MUTEXes" p11.C_Initialize(CKF_OS_LOCKING_OK) else: print "Using our own pseudo-MUTEXes" - - # This class probably belongs in the library. - - class Mutexes(object): - - format = "=L" - length = len(pack(format, 0)) - - def __init__(self, verbose = True, flags = 0): - self.init_args = (flags, self.create, self.destroy, self.lock, self.unlock) - self.next_mutex = 0 - self.mutexes = {} - self.verbose = verbose - - def find(self): - if len(self.mutexes) > 0xFFFFFFFF: - raise RuntimeError - while self.next_mutex in self.mutexes: - self.next_mutex = (self.next_mutex + 1) & 0xFFFFFFFF - - def encode(self, n): - return (CK_BYTE * self.length)(*pack(self.format, n)) - - def decode(self, m): - return unpack(self.format, m[:self.length])[0] - - def create(self, mm): - self.find() - if self.verbose: - print "Creating mutex", self.next_mutex - m = self.encode(self.next_mutex) - self.mutexes[self.next_mutex] = [False, m] - mm[0] = m - return CKR_OK - - def destroy(self, m): - mutex = self.decode(m) - if self.verbose: - print "Destroying mutex", mutex - del self.mutexes[mutex] - return CKR_OK - - def lock(self, m): - mutex = self.decode(m) - if self.verbose: - print "Locking mutex", mutex - self.mutexes[mutex][0] = True - return CKR_OK - - def unlock(self, m): - mutex = self.decode(m) - if self.verbose: - print "Unlocking mutex", mutex - self.mutexes[mutex][0] = False - return CKR_OK - - mutexes = Mutexes() - p11.C_Initialize(*mutexes.init_args) - -# Dun with MUTEX insanit and C_Initialize() call + from py11.mutex import MutexDB + mdb = MutexDB() + p11.C_Initialize(0, mdb.create, mdb.destroy, mdb.lock, mdb.unlock) slots = p11.C_GetSlotList() -- cgit v1.2.3