51 Commits

Author SHA1 Message Date
Matt Valentine-House
cb3b126eee Better feature detection for malloc counter locks 2026-05-20 22:14:47 +01:00
John Hawthorn
4263f1d718 Store fiber serial as Ractor-local 2025-11-25 13:48:35 -08:00
Nobuyoshi Nakada
ec05bc39a3 Fix the fallback using mutex 2025-11-25 13:47:45 +09:00
Nobuyoshi Nakada
bbf4bde75e Reapply "Fix stdatomic case in rbimpl_atomic_u64_fetch_add"
This reverts commit 8a68dc7bdd3d1c97677a6633a4f2b5e524c492ae.
2025-11-25 13:47:45 +09:00
Max Bernstein
8a68dc7bdd
Revert "Fix stdatomic case in rbimpl_atomic_u64_fetch_add" (#15311)
This reverts commit d3b6f835d565ec1590059773fc87589ddf8adc37.

This broke the Docker builds and presumably also 32-bit machines that
don't already have libatomic installed.
2025-11-24 15:28:30 -05:00
Nobuyoshi Nakada
d3b6f835d5
Fix stdatomic case in rbimpl_atomic_u64_fetch_add
On some platoforms, 64bit atomic operations need the dedicated helper
library.
2025-11-22 01:20:04 +09:00
John Hawthorn
5f5da2c293 Fix stdatomic case in rbimpl_atomic_u64_fetch_add
This was failing on crossruby, likely because HAVE_GCC_ATOMIC_BUILTINS
was true, but HAVE_GCC_ATOMIC_BUILTINS_64 was false. We probably should
have feature detection of 64-bit stdatomics like we do for GCC, but for
now let's keep rbimpl_atomic_u64_fetch_add in sync with load/set.
2025-11-21 00:43:48 -08:00
John Hawthorn
ff1d23eccb Use a serial to keep track of Mutex-owning Fiber
Previously this held a pointer to the Fiber itself, which requires
marking it (which was only implemented recently, prior to that it was
buggy). Using a monotonically increasing integer instead allows us to
avoid having a free function and keeps everything simpler.

My main motivations in making this change are that the root fiber lazily
allocates self, which makes the writebarrier implementation challenging
to do correctly, and wanting to avoid sending Mutexes to the remembered
set when locked by a short-lived Fiber.
2025-11-20 14:06:33 -08:00
John Hawthorn
cb360b0b4b Implement rbimpl_atomic_value_load
This only adds the rbimpl_ version to include/ruby/atomic.h so that it
is not a new public interface.

We were already using RUBY_ATOMIC_VALUE_LOAD in a few locations. This
will allow us to use other memory orders internally when desired.
2025-08-12 10:38:00 -07:00
John Hawthorn
77d29ef73c Convert ATOMIC_LOAD_RELAXED to use new rbimpl_* 2025-08-12 10:38:00 -07:00
Peter Zhu
aed7a95f9d Move RUBY_ATOMIC_VALUE_LOAD to ruby_atomic.h
Deduplicates RUBY_ATOMIC_VALUE_LOAD by moving it to ruby_atomic.h.
2025-06-25 13:04:25 -04:00
Nobuyoshi Nakada
7cfdcde069
Expect aligned pointer for the atomic operations 2025-06-13 11:14:05 +09:00
Nobuyoshi Nakada
9f112afcde
Allow volatile pointer relaxed atomic operations 2025-06-05 17:32:24 +09:00
Nobuyoshi Nakada
9e84a278a3
Win: Cast of qualifier in rbimpl_atomic_u64_load_relaxed 2025-06-05 17:09:52 +09:00
Nobuyoshi Nakada
c5f7d274d7
Check for 64bit atomic operations
May not be supported on some 32bit architectures.

```
/usr/lib/gcc-cross/m68k-linux-gnu/14/../../../../m68k-linux-gnu/bin/ld: ../../libruby-static.a(vm.o): in function `rbimpl_atomic_u64_set_relaxed':
/home/ubuntu/build/ruby/master/m68k-linux/../src/ruby_atomic.h:60:(.text+0x2468): undefined reference to `__atomic_store_8'
/usr/lib/gcc-cross/m68k-linux-gnu/14/../../../../m68k-linux-gnu/bin/ld: ../../libruby-static.a(vm.o): in function `rbimpl_atomic_u64_load_relaxed':
/home/ubuntu/build/ruby/master/m68k-linux/../src/ruby_atomic.h:43:(.text+0x2950):
undefined reference to `__atomic_load_8'
```
2025-06-04 15:31:28 +09:00
Jean Boussier
e27404af9e Use all 32bits of shape_id_t on all platforms
Followup: https://github.com/ruby/ruby/pull/13341 / [Feature #21353]

Even thought `shape_id_t` has been make 32bits, we were still limited
to use only the lower 16 bits because they had to fit alongside `attr_index_t`
inside a `uintptr_t` in inline caches.

By enlarging inline caches we can unlock the full 32bits on all
platforms, allowing to use these extra bits for tagging.
2025-06-03 21:15:41 +02:00
John Hawthorn
05e0e7223a Use atomic load to read interrupt mask 2025-05-20 09:56:31 -07:00
Nobuyoshi Nakada
6ed6b85ece Expose atomic operation macros with RUBY prefix
Now we need atomic operations, which are lighter than mutex, more
widely for extension libraries because of Ractor.
2020-12-24 11:43:21 +09:00
NARUSE, Yui
ce92d15596 fix typo [Bug #17194] 2020-09-26 18:24:28 +09:00
Nobuyoshi Nakada
257e01b61d
Solaris does not provide atomic_sub family 2020-04-26 22:39:36 +09:00
Nobuyoshi Nakada
1760c60174
Clarified return value of atomic macros 2020-04-26 19:41:12 +09:00
Nobuyoshi Nakada
e571bb8436
Removed phony atomic operations for void* and VALUE 2020-04-20 21:14:04 +09:00
Nobuyoshi Nakada
20773a1090
Bail out if no atomic operation found
As atomic operations are mandatory now, not-working phony fallback
definitions are not only useless but confusing and harmful.
2020-04-20 12:33:37 +09:00
卜部昌平
34fd7241e4 fine-grained #ifdef guard for Sparc systems
There are cases when sizeof(int) == sizeof(long) == sizeof(size_t).  On
such cases however int and long are incompatible types in theory.  We
should not assume typedef long size_t, because on Solaris size_t is
actually a typedef of int.

This reduces compiler warnings on such situations.
2020-02-06 11:46:51 +09:00
卜部昌平
0958e19ffb add several __has_something macro
With these macros implemented we can write codes just like we can assume
the compiler being clang.  MSC_VERSION_SINCE is defined to implement
those macros, but turned out to be handy for other places.  The -fdeclspec
compiler flag is necessary for clang to properly handle __has_declspec().
2019-12-26 20:45:12 +09:00
shyouhei
e62214be87 move atomic logic from mjit.c from ruby_atomic.h
This macro shall be shared among other files, not just for mjit.c.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-01 03:40:37 +00:00
shyouhei
740a1706c8 (cosmetic) add blank lines for better readability
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-01 02:55:11 +00:00
shyouhei
a6b50f379d statement experssion is a GCCism
should mark as such.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61557 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-02 06:41:53 +00:00
nobu
b3377eaa13 Revert r52995
revert slow atomic operations.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52997 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-09 07:30:44 +00:00
nobu
0599d7de58 use atomic operations
* regcomp.c (onig_chain_link_add): use atomic operation instead of
  mutex.
* regint.h (ONIG_STATE_{INC,DEC}_THREAD): ditto.
* regparse.c (PopFreeNode, node_recycle): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52995 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-09 06:48:15 +00:00
nobu
e65f38b97d ruby_atomic.h: fix old value type of ATOMIC_CAS
* ruby_atomic.h (ATOMIC_CAS): old value to be swapped should be
  same as the destination.  immediate value may need type
  promotion.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52893 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-05 00:56:52 +00:00
nobu
c5d2ef80a4 ruby_atomic.h: fix the argument order
* ruby_atomic.h (ATOMIC_SIZE_CAS): fix the argument order of
  InterlockedCompareExchange64.  new value and then old value is
  the last.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52892 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-05 00:25:43 +00:00
nobu
22fa19d1dc ruby_atomic.h: fix typo
* ruby_atomic.h (ATOMIC_VALUE_CAS): fix typo.
  TODO: make arguments of all CAS macros consistent.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51781 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-09-07 06:55:01 +00:00
nobu
ec10c033a7 ruby_atomic.h: atomic VALUE operations
* ruby_atomic.h (ATOMIC_VALUE_EXCHANGE, ATOMIC_VALUE_CAS): add
  atomic operations for VALUE.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-08-06 01:44:03 +00:00
nobu
9767eb3001 ruby_atomic.h: refine ATOMIC_PTR_CAS
* ruby_atomic.h (ATOMIC_PTR_CAS): define by generic CAS macro, not
  via size_t, to suppress a warning by mingw gcc.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-21 09:32:40 +00:00
nobu
30c8f9dd83 ruby_atomic.h (ATOMIC_OR): should return previous value
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48036 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-10-19 17:34:02 +00:00
nobu
ff00300834 iseq.c: potential memory leak
* iseq.c (iseq_load): keep type_map to get rid of memory leak.
  based on a patch by Eric Wong at [ruby-core:59699].  [Bug #9399]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-12 07:41:10 +00:00
nobu
fb29aefc7a ruby_atomic.h: ATOMIC_PTR_EXCHANGE
* ruby_atomic.h (ATOMIC_PTR_EXCHANGE): atomic exchange function for
  a generic pointer.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43996 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-05 01:48:10 +00:00
nobu
b8434daf6b ruby_atomic.h: duplicate code
* ruby_atomic.h (ATOMIC_SIZE_CAS): remove duplicate code as
  ATOMIC_CAS().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43995 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-05 01:47:58 +00:00
glass
3ff90426d9 * ruby_atomic.h: remove duplicate definisions between ATOMIC_XXX
and ATOMIC_SIZE_XXX.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43883 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-27 15:13:08 +00:00
glass
b983511292 * ruby_atomic.h: define ATOMIC_SIZE_CAS() with
__atomic_compare_exchange_n() and refactoring.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43882 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-27 14:57:33 +00:00
glass
0791c940ad * ruby_atomic.h: use __atomic builtin functions supported by GCC.
__sync family are legacy functions now and it is recommended
  that new code use the __atomic functions.
  http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

* configure.in: check existence of __atomic functions.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43851 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-26 02:45:53 +00:00
ngoto
c3c06ff78c * ruby_atomic.h (ATOMIC_SIZE_CAS): fix compile error on Solaris since r43460.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-14 11:40:15 +00:00
nobu
1058f20672 ruby_atomic.h: ATOMIC_SIZE_CAS
* ruby_atomic.h (ATOMIC_SIZE_CAS): new macro, compare and swap size_t.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43460 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-10-29 06:46:33 +00:00
nobu
9f9b476710 ruby_atomic.h: suppress a warning
* ruby_atomic.h (ATOMIC_SIZE_SUB): add cast to suppress a warning by VC++.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43356 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-10-18 14:23:50 +00:00
nobu
15d4450907 ruby_atomic.h: ATOMIC_CAS on VC6
* ruby_atomic.h (rb_w32_atomic_cas): call InterlockedCompareExchange
  directly.
* ruby_atomic.h (ATOMIC_CAS): fix missing function call.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41760 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-07-03 15:13:06 +00:00
nobu
b12ffcb15d ruby_atomic.h: suppress warnigns
* ruby_atomic.h (ATOMIC_CAS): suppress C4022 and C4047 warnings in
  VC6.  only InterlockedCompareExchange is declared using PVOID.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41759 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-07-03 14:47:42 +00:00
ngoto
f70a7cd20f * configure.in (AC_CHECK_HEADERS): atomic.h for Solaris atomic_ops.
* ruby_atomic.h: Skip using Solaris10 atomic_ops on Solaris 9 or earlier if atomic.h is not available. [ruby-dev:47229] [Bug #8228]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40163 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-06 15:00:44 +00:00
ngoto
5faf4cc77b * ruby_atomic.h (ATOMIC_CAS): added for Solaris and other platforms.
* ruby_atomic.h, signal.c (NEED_RUBY_ATOMIC_OPS): renamed from
  NEED_RUBY_ATOMIC_EXCHANGE.
* signal.c (ruby_atomic_compare_and_swap): naive, non-atomic
  compare-and-swap implementation only used for platforms without
  valid support for atomic operations.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-11-27 10:21:59 +00:00
kosaki
6190bb4d8a * ruby_atomic.h (ATOMIC_CAS): new macro for compare-and-exchange.
* vm_core.h (struct rb_thread_struct): add interrupt_mask member.
* thread.c (thread_create_core, Init_Thread): initialize
  th->thread_mask.

* vm_core.h (RUBY_VM_INTERRUPTED_ANY): new macro for avoiding
  bare th->interrupt_flag.
* vm_core.h (RUBY_VM_INTERRUPTED, RUBY_VM_INTERRUPTED): check
  th->interrupt_mask.
* thread.c (set_unblock_function, rb_thread_schedule): replace
  th->interrupt_flag with RUBY_VM_INTERRUPTED_ANY()

* signal.c (signal_exec): set up thread->interrupt_mask for
  preventing recursive trap handler.
* vm_core.h (RUBY_VM_CHECK_INTS, RUBY_VM_CHECK_INTS_BLOCKING): ditto.

* thread.c (rb_threadptr_execute_interrupts):
  don't process interrupt if it is masked.
  [Bug #6009] [ruby-core:42524]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37861 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-11-26 10:57:39 +00:00