forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniqueid.r2py
56 lines (37 loc) · 1.18 KB
/
uniqueid.r2py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Author: Justin Cappos
Module: A simple library that provides a unique ID for each call
Start date: November 11th, 2008
This is a really, really simple module, only broken out to avoid duplicating
functionality.
NOTE: This will give unique ids PER FILE. If you have multiple python
modules that include this, they will have the potential to generate the
same ID.
"""
# This is a list to prevent using part of the user's mycontext dict
# We use getruntime() instead of a list starting with 0, as this
# library may get imported multiple times.
# See ticket #1319 and #1318 for more details.
current_time = getruntime()
uniqueid_idlist = [int((current_time - int(current_time)) * 2**32)]
uniqueid_idlock = createlock()
def uniqueid_getid():
"""
<Purpose>
Return a unique ID in a threadsafe way
<Arguments>
None
<Exceptions>
None
<Side Effects>
None.
<Returns>
The ID (an integer)
"""
uniqueid_idlock.acquire(True)
# I'm using a list because I need a global, but don't want to use the
# programmer's dict
myid = uniqueid_idlist[0]
uniqueid_idlist[0] = uniqueid_idlist[0] + 1
uniqueid_idlock.release()
return myid