File: test.code

package info (click to toggle)
python-extended-threading 0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 124 kB
  • ctags: 139
  • sloc: python: 867; makefile: 32
file content (141 lines) | stat: -rw-r--r-- 3,550 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import threading
import extended_threading
import thread
from time import sleep

class reader(threading.Thread):
  __lock = None
  __mode = "w" #"w"ait, "g"rab, "r"elease, "e"xit
  __SID  = 0
  __has  = 0
  __waiting = False
  __this = None
  ID  = None
  def __init__(self, lock):
    threading.Thread.__init__(self)
    self.__lock = lock
    self.__mode = "w"
    self.ID = self.__SID
    type(self).__SID += 1
    self.__has = 0
    self.__waiting = False
  #def __init__(self, lock):
  def mode(self, m):
    self.__mode = m
  #def mode(self, m):
  def run(self):
    self.__this = thread.get_ident()
    while(self.__mode != "e"):
      while(self.__mode == "g"): #grab once per second
        self.__waiting = True
        wait = 1
        if(self.__has):
          wait = 0
        if(self.__lock.acquire(self.__lock.READ, wait)):
          print "Reader #" + str(self.ID) + " acquired"
          self.__has += 1
        self.__waiting = False
        sleep(1)
      #while(self.__mode == "g"): #grab once per second
      while(self.__mode == "r" and self.__has > 0):
        self.__lock.release()
        self.__has -= 1
        print "Reader #" + str(self.ID) + " released"
      #while(self.__mode == "r" and self.__has > 0):
    #while(self.__mode != "e"):
  #def run(self):
  def ident(self):
    print "Reader            #" + str(self.ID)
    print "           Thread: " + str(self.__this)
    print " Waiting on lock?: " + str(self.__waiting)
    print "      Total locks: " + str(self.__has)
  #def ident(self):
#class reader(threading.Thread):



class writer(threading.Thread):
  __lock = None
  __mode = "w" #"w"ait, "g"rab, "r"elease, "e"xit
  __SID  = 0
  __has  = 0
  __waiting = False
  __this = None
  ID  = None
  def __init__(self, lock):
    threading.Thread.__init__(self)
    self.__lock = lock
    self.__mode = "w"
    self.ID = self.__SID
    type(self).__SID += 1
    self.__has = 0
    self.__waiting = False
  #def __init__(self, lock):
  def mode(self, m):
    self.__mode = m
  #def mode(self, m):
  def run(self):
    self.__this = thread.get_ident()
    while(self.__mode != "e"):
      while(self.__mode == "g"): #grab once per second
        self.__waiting = True
        wait = 1
        if(self.__has):
          wait = 0
        if(self.__lock.acquire(self.__lock.WRITE, wait)):
          print "Writer #" + str(self.ID) + " acquired"
          self.__has += 1
        self.__waiting = False
        sleep(1)
      #while(self.__mode == "g"): #grab once per second
      while(self.__mode == "r" and self.__has > 0):
        self.__lock.release()
        self.__has -= 1
        print "Writer #" + str(self.ID) + " released"
      #while(self.__mode == "r" and self.__has > 0):
    #while(self.__mode != "e"):
  #def run(self):
  def ident(self):
    print "Writer            #" + str(self.ID)
    print "           Thread: " + str(self.__this)
    print " Waiting on lock?: " + str(self.__waiting)
    print "      Total locks: " + str(self.__has)
  #def ident(self):
#class writer(threading.Thread):



l = extended_threading.RWrlock()

threads = []
threads.append(reader(l))
threads.append(reader(l))
threads.append(reader(l))
threads.append(writer(l))
threads.append(writer(l))
threads.append(writer(l))

def grab():
  for t in threads:
    t.mode("g")

def wait():
  for t in threads:
    t.mode("w")

def release():
  for t in threads:
    t.mode("r")

def status():
  for t in threads:
    t.ident()

def stop():
  for t in threads:
    t.mode("e")

def start():
  for t in threads:
    t.start()