Граф коммитов

972 Коммитов

Автор SHA1 Сообщение Дата
Benoit Daloze 82998918ef Make Thread#join always convert its argument, as before 70f08f1eed 2020-09-21 16:22:04 +02:00
Benoit Daloze 0fa1c82bfc Make it clear the first field of join_arg is the waiting thread
* And not some list of sort.
2020-09-21 15:58:08 +02:00
Samuel Williams 70f08f1eed Make `Thread#join` non-blocking. 2020-09-21 11:48:44 +12:00
Samuel Williams 501fff14c7 When setting current thread scheduler to nil, invoke `#close`. 2020-09-21 09:51:33 +12:00
Benoit Daloze 9472d16061 Call scheduler.block instead of scheduler.kernel_sleep for blocking Queue/SizedQueue operations
* scheduler.unblock was already already called before but with no corresponding scheduler.block
* add test that Queue#pop makes the scheduler wait until it gets an element.
2020-09-17 14:59:38 +02:00
Koichi Sasada f7ccb8dd88 restart Ractor.select on intterupt
signal can interrupt Ractor.select, but if there is no exception,
Ractor.select should restart automatically.
2020-09-15 00:04:59 +09:00
Samuel Williams 8eea66a0ca Add support for Queue & SizedQueue. 2020-09-14 16:44:09 +12:00
Benoit Daloze 178c1b0922 Make Mutex per-Fiber instead of per-Thread
* Enables Mutex to be used as synchronization between multiple Fibers
  of the same Thread.
* With a Fiber scheduler we can yield to another Fiber on contended
  Mutex#lock instead of blocking the entire thread.
* This also makes the behavior of Mutex consistent across CRuby, JRuby and TruffleRuby.
* [Feature #16792]
2020-09-14 16:44:09 +12:00
Samuel Williams d387029f39 Standardised scheduler interface. 2020-09-14 16:44:09 +12:00
Samuel Williams 703e529751 Add `rb_thread_current_scheduler()`. 2020-09-14 16:44:09 +12:00
Hiroshi SHIBATA e691e671ca Removed Thread#safe_level 2020-09-11 10:52:52 +09:00
Matt Valentine-House ef22af4db0 If the GC runs before the Mutex's are initialised then we get a crash in pthread_mutex_lock.
It is possible for GC to run during initialisation due to objects being allocated
2020-09-10 08:48:51 -07:00
Koichi Sasada 79df14c04b Introduce Ractor mechanism for parallel execution
This commit introduces Ractor mechanism to run Ruby program in
parallel. See doc/ractor.md for more details about Ractor.
See ticket [Feature #17100] to see the implementation details
and discussions.

[Feature #17100]

This commit does not complete the implementation. You can find
many bugs on using Ractor. Also the specification will be changed
so that this feature is experimental. You will see a warning when
you make the first Ractor with `Ractor.new`.

I hope this feature can help programmers from thread-safety issues.
2020-09-03 21:11:06 +09:00
Nobuyoshi Nakada a90f29ebb2
procnames-start-lines [ci skip] 2020-08-17 14:27:34 +09:00
Samuel Williams f3462d99a3 Rename `rb_current_thread_scheduler` to `rb_thread_scheduler_if_nonblocking`.
Correctly capture thread before releasing GVL and pass as argument to
`rb_thread_scheduler_if_nonblocking`.
2020-07-20 13:20:58 +12:00
卜部昌平 de3e931df7 add UNREACHABLE_RETURN
Not every compilers understand that rb_raise does not return.  When a
function does not end with a return statement, such compilers can issue
warnings.  We would better tell them about reachabilities.
2020-06-29 11:05:41 +09:00
卜部昌平 8fc8912109 exec_recursive: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
Yusuke Endoh 50efa18c6c compile.c: Improve branch coverage instrumentation [Bug #16967]
Formerly, branch coverage measurement counters are generated for each
compilation traverse of the AST.  However, ensure clause node is
traversed twice; one is for normal-exit case (the resulted bytecode is
embedded in its outer scope), and the other is for exceptional case (the
resulted bytecode is used in catch table).  Two branch coverage counters
are generated for the two cases, but it is not desired.

This changeset revamps the internal representation of branch coverage
measurement.  Branch coverage counters are generated only at the first
visit of a branch node.  Visiting the same node reuses the
already-generated counter, so double counting is avoided.
2020-06-20 09:28:03 +09:00
Jeremy Evans 4e1f2283b4 Make Thread#thread_variable? similar to #thread_variable_get
Don't use rb_check_id, which only works for pinned symbols.
Switch inadvertent creation test for thread_variable? to
only check for pinned symbols, same as thread_variable_get
and thread_variable_set.

Make key variable name in thread_local_set match
thread_local_get and thread_variable?.

Fixes [Bug #16906]
2020-05-25 21:38:35 -07:00
Samuel Williams 0e3b0fcdba
Thread scheduler for light weight concurrency. 2020-05-14 22:10:55 +12:00
Kazuhiro NISHIYAMA 7cc55f4bc4
Thread#backtrace may return nil [ci skip] 2020-05-12 11:46:41 +09:00
卜部昌平 9e41a75255 sed -i 's|ruby/impl|ruby/internal|'
To fix build failures.
2020-05-11 09:24:08 +09:00
卜部昌平 d7f4d732c1 sed -i s|ruby/3|ruby/impl|g
This shall fix compile errors.
2020-05-11 09:24:08 +09:00
Koichi Sasada fd0222caed should check pending interrupts correctly.
rb_uninterruptible() disables any interrupts using handle_interrupt
feature (This function is used by `p`).
After this function, pending interrupts should be checked correctly,
however there is no chance to setup interrupt flag of working
threads, it means that nobody checks pending interrupts.
For example, it ignores terminate signal delivered at the end
of main thread and program can't stop.

This patch set interrupt flag if there are pending interrupts.
2020-04-09 12:51:51 +09:00
卜部昌平 9e6e39c351
Merge pull request #2991 from shyouhei/ruby.h
Split ruby.h
2020-04-08 13:28:13 +09:00
Koichi Sasada ff98931dac check flags passed to rb_nogvl()
RB_NOGVL_UBF_ASYNC_SAFE is wrongly specified because flags
is not checked.
[Bug #15499] 23444302
2020-03-26 01:19:35 +09:00
卜部昌平 c7e6dbd5ab fix arity mismatch
This is a ruby method with arity zero.  Which means, this function takes
one argument (that is self).
2020-02-20 11:46:54 +09:00
卜部昌平 115fec062c more on NULL versus functions.
Function pointers are not void*.  See also
ce4ea956d2
8427fca49b
2020-02-07 14:24:19 +09:00
Nobuyoshi Nakada 7584853cfe
st_delete_wrap is no longer used 2020-01-11 10:43:39 +09:00
Lourens Naudé 40c57ad4a1 Let execution context local storage be an ID table 2020-01-11 14:40:36 +13:00
Jeremy Evans beae6cbf0f Fully separate positional arguments and keyword arguments
This removes the warnings added in 2.7, and changes the behavior
so that a final positional hash is not treated as keywords or
vice-versa.

To handle the arg_setup_block splat case correctly with keyword
arguments, we need to check if we are taking a keyword hash.
That case didn't have a test, but it affects real-world code,
so add a test for it.

This removes rb_empty_keyword_given_p() and related code, as
that is not needed in Ruby 3.  The empty keyword case is the
same as the no keyword case in Ruby 3.

This changes rb_scan_args to implement keyword argument
separation for C functions when the : character is used.
For backwards compatibility, it returns a duped hash.
This is a bad idea for performance, but not duping the hash
breaks at least Enumerator::ArithmeticSequence#inspect.

Instead of having RB_PASS_CALLED_KEYWORDS be a number,
simplify the code by just making it be rb_keyword_given_p().
2020-01-02 18:40:45 -08:00
Kazuhiro NISHIYAMA bb5b8ace8d
Fix typo 2019-12-28 21:10:08 +09:00
卜部昌平 5e22f873ed decouple internal.h headers
Saves comitters' daily life by avoid #include-ing everything from
internal.h to make each file do so instead.  This would significantly
speed up incremental builds.

We take the following inclusion order in this changeset:

1.  "ruby/config.h", where _GNU_SOURCE is defined (must be the very
    first thing among everything).
2.  RUBY_EXTCONF_H if any.
3.  Standard C headers, sorted alphabetically.
4.  Other system headers, maybe guarded by #ifdef
5.  Everything else, sorted alphabetically.

Exceptions are those win32-related headers, which tend not be self-
containing (headers have inclusion order dependencies).
2019-12-26 20:45:12 +09:00
卜部昌平 33e9601938 TIMESPEC_SEC_MAX might be bigger than 53 bits.
The same as 41bc766763.  Read that commit
for what is happening.
2019-12-26 20:45:12 +09:00
卜部昌平 bf53d6c7d1 other minior internal header tweaks
These headers need no rewrite.  Just add some minor tweaks, like
addition of #include lines.  Mainly cosmetic.

TIMET_MAX_PLUS_ONE was deleted because the macro was used from only
one place (directly write expression there).
2019-12-26 20:45:12 +09:00
Eric Wong b12bc3b046 thread.c: avoid needless read after select|ppoll
We do not need to issue pipe|eventfd read(2) syscall
unless select, ppoll|poll declares the FD needs reading.
2019-12-16 18:31:08 +00:00
Jean byroot Boussier 8a80bfcfd4 Make Thread#to_s consistent with Method and Proc to_s 2019-12-11 11:59:14 +09:00
Koichi Sasada ee57920008 move an interrupt point.
`args_ptr` can be corrupted by interrupt handlers.
Pointed by nagachika <https://ruby-trunk-changes.hatenablog.com/entry/ruby_trunk_changes_20191204>.
2019-12-05 15:57:23 +09:00
Jeremy Evans 5c2c396685 Check interrupts before starting thread
Fixes a hang when Thread.new calls Thread.new in a loop.

Fixes [Bug #13688]
2019-12-03 17:27:34 +02:00
Jeremy Evans ffd0820ab3 Deprecate taint/trust and related methods, and make the methods no-ops
This removes the related tests, and puts the related specs behind
version guards.  This affects all code in lib, including some
libraries that may want to support older versions of Ruby.
2019-11-18 01:00:25 +02:00
Jeremy Evans c5c05460ac Warn on access/modify of $SAFE, and remove effects of modifying $SAFE
This removes the security features added by $SAFE = 1, and warns for access
or modification of $SAFE from Ruby-level, as well as warning when calling
all public C functions related to $SAFE.

This modifies some internal functions that took a safe level argument
to no longer take the argument.

rb_require_safe now warns, rb_require_string has been added as a
version that takes a VALUE and does not warn.

One public C function that still takes a safe level argument and that
this doesn't warn for is rb_eval_cmd.  We may want to consider
adding an alternative method that does not take a safe level argument,
and warn for rb_eval_cmd.
2019-11-18 01:00:25 +02:00
John Hawthorn ebbe396d3c Use ident hash for top-level recursion check
We track recursion in order to not infinite loop in ==, inspect, and
similar methods by keeping a thread-local 1 or 2 level hash. This allows
us to track when we have seen the same object (ex. using inspect) or
same pair of objects (ex. using ==) in this stack before and to treat
that differently.

Previously both levels of this Hash used the object's memory_id as a key
(using object_id would be slow and wasteful). Unfortunately, prettyprint
(pp.rb) uses this thread local variable to "pretend" to be inspect and
inherit its same recursion behaviour.

This commit changes the top-level hash to be an identity hash and to use
objects as keys instead of their object_ids.

I'd like to have also converted the 2nd level hash to an ident hash, but
it would have prevented an optimization which avoids allocating a 2nd
level hash for only a single element, which we want to keep because it's
by far the most common case.

So the new format of this hash is:

{ object => true } (not paired)
{ lhs_object => rhs_object_memory_id } (paired, single object)
{ lhs_object => { rhs_object_memory_id => true, ... } } (paired, many objects)

We must also update pp.rb to match this (using identity hashes).
2019-11-04 15:27:15 -08:00
Ben Woosley bb71a128eb Prefer st_is_member over st_lookup with 0
The st_is_member DEFINE has simpler semantics, for more readable code.
2019-10-09 23:46:50 +09:00
卜部昌平 7e0ae1698d avoid overflow in integer multiplication
This changeset basically replaces `ruby_xmalloc(x * y)` into
`ruby_xmalloc2(x, y)`.  Some convenient functions are also
provided for instance `rb_xmalloc_mul_add(x, y, z)` which allocates
x * y + z byes.
2019-10-09 12:12:28 +09:00
Jeremy Evans dd2068ac8d Add rb_adjust_argv_kw_splat to internal.h
We are calling this in a few other files, it is better to have it
in a header than adding prototypes to the other files.
2019-09-26 15:30:51 -07:00
Nobuyoshi Nakada 876c5fe1b2
Check the argument size
Ensure that argument array size does not overflow as `int`, before
cast in thread_do_start after new thread created.
2019-09-27 00:38:12 +09:00
Jeremy Evans 9556342838 Fix shorten-64-to-32 compile warnings/errors 2019-09-26 08:01:53 -07:00
Jeremy Evans 6b52959ef7 Fix keyword argument separation issues in Thread.new 2019-09-26 08:01:53 -07:00
Nobuyoshi Nakada 142efba93e
Adjusted directives order of a function [ci skip] 2019-09-23 02:21:54 +09:00
Lourens Naudé cadfaacb25 Lazy init thread local storage 2019-09-23 02:14:44 +09:00