Description:
Merge branch 'master' of gitorious.org:cafe-grader/cafe-grader-judge-scripts into win-local
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r110:0bd22936dfa6 - - 1 file changed: 6 inserted, 2 deleted
@@ -13,28 +13,32 | |||||
|
13 | # directory. It locks the dir to manage critical section when |
|
13 | # directory. It locks the dir to manage critical section when |
|
14 | # updating the reference counter. |
|
14 | # updating the reference counter. |
|
15 |
|
15 | ||
|
16 | module DirInit |
|
16 | module DirInit |
|
17 |
|
17 | ||
|
18 | class Manager |
|
18 | class Manager |
|
19 |
|
19 | ||
|
20 | def initialize(dir_name, usage_filename='.usage_counter') |
|
20 | def initialize(dir_name, usage_filename='.usage_counter') |
|
21 | @dir_name = dir_name |
|
21 | @dir_name = dir_name |
|
22 | @usage_filename = usage_filename |
|
22 | @usage_filename = usage_filename |
|
23 | end |
|
23 | end |
|
24 |
|
24 | ||
|
|
25 | + def lock_filename | ||
|
|
26 | + return @dir_name + '/lockfile' | ||
|
|
27 | + end | ||
|
|
28 | + | ||
|
25 | # Check if someone has initialized the dir. If not, call block. |
|
29 | # Check if someone has initialized the dir. If not, call block. |
|
26 |
|
30 | ||
|
27 | def setup # :yields: block |
|
31 | def setup # :yields: block |
|
28 |
- dir = File.new( |
|
32 | + dir = File.new(lock_filename,"w+") |
|
29 | dir.flock(File::LOCK_EX) |
|
33 | dir.flock(File::LOCK_EX) |
|
30 | begin |
|
34 | begin |
|
31 | counter_filename = get_counter_filename |
|
35 | counter_filename = get_counter_filename |
|
32 | if File.exist? counter_filename |
|
36 | if File.exist? counter_filename |
|
33 | # someone is here |
|
37 | # someone is here |
|
34 | f = File.new(counter_filename,"r+") |
|
38 | f = File.new(counter_filename,"r+") |
|
35 | counter = f.read.to_i |
|
39 | counter = f.read.to_i |
|
36 | f.seek(0) |
|
40 | f.seek(0) |
|
37 | f.write("#{counter+1}\n") |
|
41 | f.write("#{counter+1}\n") |
|
38 | f.close |
|
42 | f.close |
|
39 | else |
|
43 | else |
|
40 | # i'm the first, create the counter file |
|
44 | # i'm the first, create the counter file |
@@ -55,25 +59,25 | |||||
|
55 | raise |
|
59 | raise |
|
56 |
|
60 | ||
|
57 | ensure |
|
61 | ensure |
|
58 | # make sure it unlock the directory |
|
62 | # make sure it unlock the directory |
|
59 | dir.flock(File::LOCK_UN) |
|
63 | dir.flock(File::LOCK_UN) |
|
60 | dir.close |
|
64 | dir.close |
|
61 | end |
|
65 | end |
|
62 | end |
|
66 | end |
|
63 |
|
67 | ||
|
64 | # Check if I am the last one using the dir. If true, call block. |
|
68 | # Check if I am the last one using the dir. If true, call block. |
|
65 |
|
69 | ||
|
66 | def teardown |
|
70 | def teardown |
|
67 |
- dir = File.new( |
|
71 | + dir = File.new(lock_filename) |
|
68 | dir.flock(File::LOCK_EX) |
|
72 | dir.flock(File::LOCK_EX) |
|
69 | begin |
|
73 | begin |
|
70 | counter_filename = get_counter_filename |
|
74 | counter_filename = get_counter_filename |
|
71 | if File.exist? counter_filename |
|
75 | if File.exist? counter_filename |
|
72 | # someone is here |
|
76 | # someone is here |
|
73 | f = File.new(counter_filename,"r+") |
|
77 | f = File.new(counter_filename,"r+") |
|
74 | counter = f.read.to_i |
|
78 | counter = f.read.to_i |
|
75 | f.seek(0) |
|
79 | f.seek(0) |
|
76 | f.write("#{counter-1}\n") |
|
80 | f.write("#{counter-1}\n") |
|
77 | f.close |
|
81 | f.close |
|
78 |
|
82 | ||
|
79 | if counter == 1 |
|
83 | if counter == 1 |
You need to be logged in to leave comments.
Login now