1 6f4d00ee 2013-09-23 0intro #include "stdinc.h"
3 6f4d00ee 2013-09-23 0intro #include "9.h"
5 6f4d00ee 2013-09-23 0intro static struct {
6 4b576658 2013-09-23 0intro QLock lock;
8 6f4d00ee 2013-09-23 0intro Excl* head;
9 6f4d00ee 2013-09-23 0intro Excl* tail;
12 6f4d00ee 2013-09-23 0intro struct Excl {
13 6f4d00ee 2013-09-23 0intro Fsys* fsys;
14 6f4d00ee 2013-09-23 0intro uvlong path;
15 6f4d00ee 2013-09-23 0intro ulong time;
17 6f4d00ee 2013-09-23 0intro Excl* next;
18 6f4d00ee 2013-09-23 0intro Excl* prev;
22 6f4d00ee 2013-09-23 0intro LifeTime = (5*60),
26 6f4d00ee 2013-09-23 0intro exclAlloc(Fid* fid)
29 6f4d00ee 2013-09-23 0intro Excl *excl;
31 6f4d00ee 2013-09-23 0intro assert(fid->excl == nil);
33 6f4d00ee 2013-09-23 0intro t = time(0L);
34 4b576658 2013-09-23 0intro qlock(&ebox.lock);
35 6f4d00ee 2013-09-23 0intro for(excl = ebox.head; excl != nil; excl = excl->next){
36 6f4d00ee 2013-09-23 0intro if(excl->fsys != fid->fsys || excl->path != fid->qid.path)
39 6f4d00ee 2013-09-23 0intro * Found it.
40 6f4d00ee 2013-09-23 0intro * Now, check if it's timed out.
41 6f4d00ee 2013-09-23 0intro * If not, return error, it's locked.
42 6f4d00ee 2013-09-23 0intro * If it has timed out, zap the old
43 6f4d00ee 2013-09-23 0intro * one and continue on to allocate a
44 6f4d00ee 2013-09-23 0intro * a new one.
46 6f4d00ee 2013-09-23 0intro if(excl->time >= t){
47 4b576658 2013-09-23 0intro qunlock(&ebox.lock);
48 4b576658 2013-09-23 0intro werrstr("exclusive lock");
51 6f4d00ee 2013-09-23 0intro excl->fsys = nil;
55 6f4d00ee 2013-09-23 0intro * Not found or timed-out.
56 6f4d00ee 2013-09-23 0intro * Alloc a new one and initialise.
58 4b576658 2013-09-23 0intro excl = vtmallocz(sizeof(Excl));
59 6f4d00ee 2013-09-23 0intro excl->fsys = fid->fsys;
60 6f4d00ee 2013-09-23 0intro excl->path = fid->qid.path;
61 6f4d00ee 2013-09-23 0intro excl->time = t+LifeTime;
62 6f4d00ee 2013-09-23 0intro if(ebox.tail != nil){
63 6f4d00ee 2013-09-23 0intro excl->prev = ebox.tail;
64 6f4d00ee 2013-09-23 0intro ebox.tail->next = excl;
67 6f4d00ee 2013-09-23 0intro ebox.head = excl;
68 6f4d00ee 2013-09-23 0intro excl->prev = nil;
70 6f4d00ee 2013-09-23 0intro ebox.tail = excl;
71 6f4d00ee 2013-09-23 0intro excl->next = nil;
72 4b576658 2013-09-23 0intro qunlock(&ebox.lock);
74 6f4d00ee 2013-09-23 0intro fid->excl = excl;
79 6f4d00ee 2013-09-23 0intro exclUpdate(Fid* fid)
82 6f4d00ee 2013-09-23 0intro Excl *excl;
84 6f4d00ee 2013-09-23 0intro excl = fid->excl;
86 6f4d00ee 2013-09-23 0intro t = time(0L);
87 4b576658 2013-09-23 0intro qlock(&ebox.lock);
88 6f4d00ee 2013-09-23 0intro if(excl->time < t || excl->fsys != fid->fsys){
89 4b576658 2013-09-23 0intro qunlock(&ebox.lock);
90 4b576658 2013-09-23 0intro werrstr("exclusive lock broken");
93 6f4d00ee 2013-09-23 0intro excl->time = t+LifeTime;
94 4b576658 2013-09-23 0intro qunlock(&ebox.lock);
100 6f4d00ee 2013-09-23 0intro exclFree(Fid* fid)
102 6f4d00ee 2013-09-23 0intro Excl *excl;
104 6f4d00ee 2013-09-23 0intro if((excl = fid->excl) == nil)
106 6f4d00ee 2013-09-23 0intro fid->excl = nil;
108 4b576658 2013-09-23 0intro qlock(&ebox.lock);
109 6f4d00ee 2013-09-23 0intro if(excl->prev != nil)
110 6f4d00ee 2013-09-23 0intro excl->prev->next = excl->next;
112 6f4d00ee 2013-09-23 0intro ebox.head = excl->next;
113 6f4d00ee 2013-09-23 0intro if(excl->next != nil)
114 6f4d00ee 2013-09-23 0intro excl->next->prev = excl->prev;
116 6f4d00ee 2013-09-23 0intro ebox.tail = excl->prev;
117 4b576658 2013-09-23 0intro qunlock(&ebox.lock);
119 4b576658 2013-09-23 0intro vtfree(excl);
123 6f4d00ee 2013-09-23 0intro exclInit(void)