@@ -45,9 +45,13 @@ public IRubyObject initialize(ThreadContext context, IRubyObject value) {
45
45
}
46
46
47
47
@ JRubyMethod
48
- public IRubyObject acquire (ThreadContext context , IRubyObject value ) throws InterruptedException {
49
- this .semaphore .acquire (rubyFixnumToPositiveInt (value , "permits" ));
50
- return context .nil ;
48
+ public IRubyObject acquire (ThreadContext context ) throws InterruptedException {
49
+ return this .acquire (context , 1 );
50
+ }
51
+
52
+ @ JRubyMethod
53
+ public IRubyObject acquire (ThreadContext context , IRubyObject permits ) throws InterruptedException {
54
+ return this .acquire (context , rubyFixnumToPositiveInt (permits , "permits" ));
51
55
}
52
56
53
57
@ JRubyMethod (name = "available_permits" )
@@ -60,30 +64,32 @@ public IRubyObject drainPermits(ThreadContext context) {
60
64
return getRuntime ().newFixnum (this .semaphore .drainPermits ());
61
65
}
62
66
63
- @ JRubyMethod
64
- public IRubyObject acquire (ThreadContext context ) throws InterruptedException {
65
- this .semaphore .acquire (1 );
66
- return context .nil ;
67
- }
68
-
69
67
@ JRubyMethod (name = "try_acquire" )
70
68
public IRubyObject tryAcquire (ThreadContext context ) throws InterruptedException {
71
- return getRuntime ().newBoolean (semaphore .tryAcquire (1 ));
69
+ int permitsInt = 1 ;
70
+ boolean acquired = semaphore .tryAcquire (permitsInt );
71
+
72
+ return triedAcquire (context , acquired );
72
73
}
73
74
74
75
@ JRubyMethod (name = "try_acquire" )
75
76
public IRubyObject tryAcquire (ThreadContext context , IRubyObject permits ) throws InterruptedException {
76
- return getRuntime ().newBoolean (semaphore .tryAcquire (rubyFixnumToPositiveInt (permits , "permits" )));
77
+ int permitsInt = rubyFixnumToPositiveInt (permits , "permits" );
78
+ boolean acquired = semaphore .tryAcquire (permitsInt );
79
+
80
+ return triedAcquire (context , acquired );
77
81
}
78
82
79
83
@ JRubyMethod (name = "try_acquire" )
80
84
public IRubyObject tryAcquire (ThreadContext context , IRubyObject permits , IRubyObject timeout ) throws InterruptedException {
81
- return getRuntime ().newBoolean (
82
- semaphore .tryAcquire (
83
- rubyFixnumToPositiveInt (permits , "permits" ),
84
- rubyNumericToLong (timeout , "timeout" ),
85
- java .util .concurrent .TimeUnit .SECONDS )
86
- );
85
+ int permitsInt = rubyFixnumToPositiveInt (permits , "permits" );
86
+ boolean acquired = semaphore .tryAcquire (
87
+ permitsInt ,
88
+ rubyNumericToLong (timeout , "timeout" ),
89
+ java .util .concurrent .TimeUnit .SECONDS ,
90
+ );
91
+
92
+ return triedAcquire (context , acquired );
87
93
}
88
94
89
95
@ JRubyMethod
@@ -93,8 +99,8 @@ public IRubyObject release(ThreadContext context) {
93
99
}
94
100
95
101
@ JRubyMethod
96
- public IRubyObject release (ThreadContext context , IRubyObject value ) {
97
- this .semaphore .release (rubyFixnumToPositiveInt (value , "permits" ));
102
+ public IRubyObject release (ThreadContext context , IRubyObject permits ) {
103
+ this .semaphore .release (rubyFixnumToPositiveInt (permits , "permits" ));
98
104
return getRuntime ().newBoolean (true );
99
105
}
100
106
@@ -104,6 +110,16 @@ public IRubyObject reducePermits(ThreadContext context, IRubyObject reduction) t
104
110
return context .nil ;
105
111
}
106
112
113
+ private IRubyObject acquire (ThreadContext context , int permits ) throws InterruptedException {
114
+ this .semaphore .acquire (permits );
115
+
116
+ return context .nil ;
117
+ }
118
+
119
+ private IRubyObject triedAcquire (ThreadContext context , int permits , boolean acquired ) {
120
+ return getRuntime ().newBoolean (acquired );
121
+ }
122
+
107
123
private int rubyFixnumInt (IRubyObject value , String paramName ) {
108
124
if (value instanceof RubyFixnum ) {
109
125
RubyFixnum fixNum = (RubyFixnum ) value ;
0 commit comments