aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2015-09-20 15:01:25 -0400
committerRob Austein <sra@hactrn.net>2015-09-20 15:01:25 -0400
commitfc79084886aa81e7414a4bd2aba48aa96a0b641b (patch)
treeae9a4a4a991facef564693250fb828c05ddfe581 /scripts
parentec3be7d441b7bc1888186c05db6e6b46abe63879 (diff)
Implement mutexes in py11 library using threading.Lock objects.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/py11-test.py65
1 files changed, 4 insertions, 61 deletions
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()