Skip to content

Commit 825ef93

Browse files
committed
Updated CAtomicFixnum to use long long. Added max/min constants.
1 parent 41d05c7 commit 825ef93

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

ext/concurrent_ruby_ext/atomic_fixnum.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ VALUE atomic_fixnum_allocate(VALUE klass) {
1212
}
1313

1414
VALUE method_atomic_fixnum_initialize(int argc, VALUE* argv, VALUE self) {
15-
VALUE value = INT2NUM(0);
15+
VALUE value = LL2NUM(0);
1616
rb_check_arity(argc, 0, 1);
1717
if (argc == 1) {
1818
Check_Type(argv[0], T_FIXNUM);
@@ -33,13 +33,13 @@ VALUE method_atomic_fixnum_value_set(VALUE self, VALUE value) {
3333
}
3434

3535
VALUE method_atomic_fixnum_increment(VALUE self) {
36-
long value = NUM2INT((VALUE) DATA_PTR(self));
37-
return method_atomic_fixnum_value_set(self, INT2NUM(value + 1));
36+
long long value = NUM2LL((VALUE) DATA_PTR(self));
37+
return method_atomic_fixnum_value_set(self, LL2NUM(value + 1));
3838
}
3939

4040
VALUE method_atomic_fixnum_decrement(VALUE self) {
41-
long value = NUM2INT((VALUE) DATA_PTR(self));
42-
return method_atomic_fixnum_value_set(self, INT2NUM(value - 1));
41+
long long value = NUM2LL((VALUE) DATA_PTR(self));
42+
return method_atomic_fixnum_value_set(self, LL2NUM(value - 1));
4343
}
4444

4545
VALUE method_atomic_fixnum_compare_and_set(VALUE self, VALUE rb_expect, VALUE rb_update) {

ext/concurrent_ruby_ext/rb_concurrent.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void Init_concurrent_ruby_ext() {
4343
rb_define_method(rb_cAtomicBoolean, "make_false", method_atomic_boolean_make_false, 0);
4444

4545
// CAtomicFixnum
46+
rb_define_const(rb_cAtomicFixnum, "MIN_VALUE", LL2NUM(LLONG_MIN));
47+
rb_define_const(rb_cAtomicFixnum, "MAX_VALUE", LL2NUM(LLONG_MAX));
4648
rb_define_alloc_func(rb_cAtomicFixnum, atomic_fixnum_allocate);
4749
rb_define_method(rb_cAtomicFixnum, "initialize", method_atomic_fixnum_initialize, -1);
4850
rb_define_method(rb_cAtomicFixnum, "value", method_atomic_fixnum_value, 0);

lib/concurrent/atomic/atomic_fixnum.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module Concurrent
1010
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
1111
class MutexAtomicFixnum
1212

13+
# http://stackoverflow.com/questions/535721/ruby-max-integer
14+
MIN_VALUE = -(2**(0.size * 8 -2))
15+
MAX_VALUE = (2**(0.size * 8 -2) -1)
16+
1317
# @!macro [attach] atomic_fixnum_method_initialize
1418
#
1519
# Creates a new `AtomicFixnum` with the given initial value.
@@ -106,6 +110,9 @@ def compare_and_set(expect, update)
106110
# @!macro atomic_fixnum
107111
class JavaAtomicFixnum
108112

113+
MIN_VALUE = Java::JavaLang::Long::MIN_VALUE
114+
MAX_VALUE = Java::JavaLang::Long::MAX_VALUE
115+
109116
# @!macro atomic_fixnum_method_initialize
110117
#
111118
def initialize(init = 0)

0 commit comments

Comments
 (0)