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

453 Коммитов

Автор SHA1 Сообщение Дата
normal 17e4aff277 thread_pthread.c: reinitialize ubf_list at fork
It's possible for the ubf_list_head to be populated with dead
threads at fork or the ubf_list_lock to be held, so reinitialize
both at startup.

And while we're at it, use a static initializer at startup
to save a library call and kill some ifdef.

[ruby-core:88578] [Bug #15013]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64485 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-20 21:34:39 +00:00
normal d2afeb9445 thread_pthread.c: reset timeslice delay when uncontended
This matches the behavior of old timer thread more closely
and seems to fix [Bug #14999] when limited to a single CPU.
I cannot reproduce the error on a multi-core system unless
I use schedtool to force affinity to a single CPU:

schedtool -a 0x01 -e make test-spec \
	MSPECOPT='-R1000 spec/ruby/library/conditionvariable/wait_spec.rb'

While it may be good enough to pass the spec, I don't have
huge degree of confidence in the interrupt handling robustness
under extremely heavy load (these may be ancient bugs, though).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64467 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-19 00:01:08 +00:00
normal 8b6297e56d thread_pthread.c (rb_sigwait_fd_get): skip getpid check
This is not called in signal handlers, so there's no reason for
it.  glibc 2.25+ no longer caches getpid(), so it will cost a
syscall for those users.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64403 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-17 02:06:54 +00:00
normal 3872ea814c thread_pthread.c: reduce ubf_timer arming for non-signal wakeups
We do not need to rely on SIGVTALRM for non-sighandler wakeups.
This will reduce spurious wakeups in cases where sigwait_fd
is not grabbed again, soon.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64389 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-16 08:26:56 +00:00
normal 960ef493a4 thread_pthread.c: check signals from gvl.timer
For (rare) blocking functions which are not affected by signals,
we need to call the appropriate unblocking function via
`threadptr_trap_interrupt'

While we're at it, handling waitpid/SIGCHLD from gvl.timer isn't
harmful, here.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-16 08:26:51 +00:00
normal 4610d36c4c thread_pthread.c: hoist out do_gvl_timer and improve documentation
This hopefully clarifies the roles of UBF_TIMER and vm->gvl.timer

[ruby-core:88475] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-15 07:16:55 +00:00
normal 906ad1670a thread_pthread.h (native_thread_data): split list_node between ubf and gvl
Do not waste extra memory for each thread, but make
thread_pthread.c easier-to-follow as a result.

[ruby-core:88475] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64375 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-15 05:31:31 +00:00
normal 3dcf85e08e thread_pthread.c: rename rb_timer_* to ubf_timer_*
These functions will not be exported outside of thread_pthread.c
and we need to clarify the timer here is used for ubf and not
timeslice.

[ruby-core:88475] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-15 04:32:46 +00:00
normal e4fd71d64d thread_pthread.c: rename timer_thread_pipe to signal_self_pipe
This data structure has nothing to do with timers or threads.

[ruby-core:88475] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-15 04:32:41 +00:00
normal 5dca7d86b9 thread_pthread.c: additional UBF_TIMER == UBF_TIMER_PTHREAD guards
Hopefully this makes the code easier-to-follow

[ruby-core:88475] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64371 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-15 04:32:36 +00:00
normal 635294038b Revert "thread_pthread.c: use UBF_TIMER_PTHREAD on Solaris"
This reverts commit 31bfe0fe86 (r64357)

commit 17ed23bb6d (r64359,
"fix fragile spec from unpredictable errno") is the correct fix

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64361 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-14 22:06:40 +00:00
normal 31bfe0fe86 thread_pthread.c: use UBF_TIMER_PTHREAD on Solaris
I'm not sure what's causing this failure in Solaris and only
on rubyspec, since rb_io_wait_readable is a well-exercised
code path in other places.  But maybe using a pthread for
timing (similar to old timer-thread) can solve the issue.

cf. http://rubyci.s3.amazonaws.com/unstable11s/ruby-trunk/log/20180814T042506Z.fail.html.gz

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64357 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-14 06:49:25 +00:00
normal 291afc96bd thread_pthread.c: use CLOCK_REALTIME on SunOS (Solaris)
timer_create does not seem to support CLOCK_MONOTONIC on Solaris,
and CLOCK_HIRES seems like it could fail with insufficient permissions:

https://docs.oracle.com/cd/E86824_01/html/E54766/timer-create-3c.html

(Only tested on Linux and FreeBSD)

[ruby-core:88360] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64356 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-14 02:24:37 +00:00
normal 8da12db13d thread_pthread (rb_timer_arm): ignore UBF_TIMER_POSIX state 2
It looks like I forgot to account for a situation involving 3
threads.

[ruby-core:88360] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-13 22:19:54 +00:00
normal 3dd6288d73 thread_pthread: use POSIX timer or thread to get rid of races
This closes race condition where GVL is uncontended and a thread
receives a signal immediately before calling the blocking
function when releasing GVL:

	1) check interrupts
	2) release GVL
	3) blocking function

If signal fires after 1) but before 3), that thread may never
wake up if GVL is uncontended

We also need to wakeup the ubf_list unconditionally on
gvl_yield; because two threads can be yielding to each other
while waiting on IO#close while waiting on threads in IO#read or
IO#gets.

[ruby-core:88360] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64353 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-13 21:34:24 +00:00
normal 48b6bd74e2 thread_pthread.c: eliminate timer thread by restructuring GVL
This reverts commit 194a6a2c68 (r64203).

Race conditions which caused the original reversion will be fixed
in the subsequent commit.

[ruby-core:88360] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64352 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-13 21:34:20 +00:00
normal 194a6a2c68 thread_pthread.c: restore timer-thread for now :<
[ruby-core:88306]

Revert "process.c: ensure th->interrupt lock is held when migrating"

This reverts commit 5ca416bdf6 (r64201)

Revert "process.c (rb_waitpid): reduce sigwait_fd bouncing"

This reverts commit 217bdd776f (r64200).

Revert "test/ruby/test_thread.rb (test_thread_timer_and_interrupt): add timeouts"

This reverts commit 9f395f1120 (r64199).

Revert "thread_pthread.c (native_sleep): reduce ppoll sleeps"

This reverts commit b3aa256c4d (r64193).

Revert "thread.c (consume_communication_pipe): do not retry after short read"

This reverts commit 291a82f748 (r64185).

Revert "test/ruby/test_io.rb (test_race_gets_and_close): timeout each thread"

This reverts commit 3dbd8d1f66 (r64184).

Revert "thread_pthread.c (gvl_acquire_common): persist timeout across calls"

This reverts commit 8c2ae6e3ed (r64165).

Revert "test/ruby/test_io.rb (test_race_gets_and_close): use SIGABRT on timeout"

This reverts commit 931cda4db8 (r64135).

Revert "thread_pthread.c (gvl_yield): do ubf wakeups when uncontended"

This reverts commit 508f00314f (r64133).

Revert "thread_pthread.h (native_thread_data): split condvars on some platforms"

This reverts commit a038bf238b (r64124).

Revert "process.c (waitpid_nogvl): prevent conflicting use of sleep_cond"

This reverts commit 7018acc946 (r64117).

Revert "thread_pthread.c (rb_sigwait_sleep): th may be 0 from MJIT"

This reverts commit 56491afc79 (r64116).

Revert "thread*.c: waiting on sigwait_fd performs periodic ubf wakeups"

This reverts commit ab47a57a46 (r64115).

Revert "thread_pthread.c (gvl_destroy): make no-op on GVL bits"

This reverts commit 95cae74817 (r64114).

Revert "thread_pthread.c (rb_sigwait_sleep): fix uninitialized poll set in UBF case"

This reverts commit 4514362948 (r64113).

Revert "thread_pthread.c (rb_sigwait_sleep): re-fix [Bug #5343] harder"

This reverts commit 26b8a70bb3 (r64111).

Revert "thread.c: move ppoll wrapper into thread_pthread.c"

This reverts commit 3dc7727d22 (r64110).

Revert "thread.c: move ppoll wrapper before thread_pthread.c"

This reverts commit 2fa1e2e3c3 (r64109).

Revert "thread_pthread.c (ubf_select): refix [Bug #5343]"

This reverts commit 4c1ab82f06 (r64108).

Revert "thread_win32.c: suppress warnings by -Wsuggest-attribute"

This reverts commit 6a9b63e390 (r64159).

Revert "thread_pthread: remove timer-thread by restructuring GVL"

This reverts commit 708bfd2115 (r64107).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64203 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-06 05:22:00 +00:00
normal 2ba640f359 thread_pthread: stop trying to deal with cancellation
We don't use pthreads cancellation ourselves and it's painful to
use correctly.  Any cancelled threads would break
vm->living_threads, GVL, thread_sync.c, autoload, etc...

So don't bother caring; because we can't stop rogue extensions
from completely breaking the VM in other ways, either.

[ruby-core:88282] [Misc #14962]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-05 20:06:49 +00:00
normal b3aa256c4d thread_pthread.c (native_sleep): reduce ppoll sleeps
By holding into sigwait_fd until after we acquire GVL, we can
hit the faster native_cond_sleep path instead of ppoll when
another thread wants to start sleeping.  ppoll-ing on sigwait_fd
isn't really useful in program where GVL is contended

This also allows reducing vm->gvl.lock mutex contention on
waitpid sleep migrations.

                         r64170        this patch
vm_thread_condvar1        0.921        1.356

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-05 08:56:52 +00:00
mame 4cef2c8d6b thread_pthread.c: add a macro guard for PTHREAD_CANCEL_ENABLE
It seems to be unavailable on some platforms including my Android phone.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64166 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-03 05:11:20 +00:00
normal 8c2ae6e3ed thread_pthread.c (gvl_acquire_common): persist timeout across calls
Reuse old expiration time if the previous native_cond_timedwait
did not return ETIMEDOUT.  This should improve timeslice
accuracy for Timeout.timeout rubyspec without causing excessive
wakeups on uncontended GVL acquisition.

cf. http://ci.rvm.jp/results/trunk-gc-asserts@silicon-docker/1180486
    http://ci.rvm.jp/results/trunk-gc-asserts@silicon-docker/1184623

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64165 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-02 21:13:50 +00:00
normal 86d35a6b1a thread_pthread.c (unregister_ubf_list): assert unblock.func is unset
We must not allow reentry into ubf_list_head once we delete
ourselves, otherwise we could hang in there forever.

[ruby-core:88218] [Bug #14945]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64134 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 21:48:32 +00:00
normal 508f00314f thread_pthread.c (gvl_yield): do ubf wakeups when uncontended
Not having contention for GVL could mean everybody else is stuck
in blocking region without GVL, so we kick the ubf list in that
case.

I expect this to fix test_thread_fd_close timeout:
http://ci.rvm.jp/results/trunk-test@ruby-sky3/1173398

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 18:53:46 +00:00
nobu 09df9972dc thread_pthread.c: revert r64123
* thread_pthread.c (USE_NATIVE_SLEEP_COND): revised wrongly removed
  line with the ifndef guard.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64131 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 15:21:01 +00:00
normal a038bf238b thread_pthread.h (native_thread_data): split condvars on some platforms
Maybe some platforms have strange condition variable implementations
which have a "memory" of which mutexes they're associated with.

In any case, it makes documentation easier even on GNU/Linux and
FreeBSD.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64124 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 09:54:41 +00:00
nobu c307ffe64f thread_pthread.c: remove duplicate define
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64123 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 08:45:19 +00:00
normal 56491afc79 thread_pthread.c (rb_sigwait_sleep): th may be 0 from MJIT
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64116 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 06:02:22 +00:00
normal ab47a57a46 thread*.c: waiting on sigwait_fd performs periodic ubf wakeups
We need to be able to perform periodic ubf_list wakeups when a
thread is sleeping and waiting on signals.

[ruby-core:88088] [Misc #14937] [Bug #5343]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64115 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 05:51:06 +00:00
normal 95cae74817 thread_pthread.c (gvl_destroy): make no-op on GVL bits
It's possible for another thread to take vm->gvl.lock
during gvl_release at the end of thread_start_func_2
during VM shutdown, at least.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64114 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 05:51:01 +00:00
normal 4514362948 thread_pthread.c (rb_sigwait_sleep): fix uninitialized poll set in UBF case
[ruby-core:88088] [Misc #14937] [Bug #5343]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64113 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 04:28:28 +00:00
normal 26b8a70bb3 thread_pthread.c (rb_sigwait_sleep): re-fix [Bug #5343] harder
We can't always designate a timer thread, so any sleepers must
also perform ubf wakeups.  Note: a similar change needs to be
made for rb_thread_fd_select and rb_wait_for_single_fd.

[ruby-core:88088] [Misc #14937] [Bug #5343]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 02:28:00 +00:00
normal 3dc7727d22 thread.c: move ppoll wrapper into thread_pthread.c
thread_pthread.c relies on ppoll for rb_sigwait_sleep, so ensure
the compatibility wrapper is available for it.

[Bug #14950]

Reported-by: SHIBATA Hiroshi <hsbt@ruby-lang.org>
Reported-by: Greg L <Greg.mpls@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64110 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 00:31:08 +00:00
normal 4c1ab82f06 thread_pthread.c (ubf_select): refix [Bug #5343]
We still need to to designate a timer thread after registering target
thread for the ubf list.

Oops :x

Note: I was never able to reproduce
test/thread/test_queue.rb::test_thr_kill failures on my on
Debian machines.

[ruby-core:88088] [Misc #14937] [Bug #5343]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64108 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-29 22:19:15 +00:00
normal 708bfd2115 thread_pthread: remove timer-thread by restructuring GVL
To reduce resource use and reduce CI failure; remove
timer-thread.  Single-threaded Ruby processes (including forked
children) will never see extra thread overhead.  This prevents
glibc and jemalloc from going into multi-threaded mode and
initializing locks or causing fragmentation via arena explosion.

The GVL is implements its own wait-queue as a ccan/list to
permit controlling wakeup order.  Timeslice under contention is
handled by a designated timer thread (similar to choosing a
"patrol_thread" for current deadlock checking).

There is only one self-pipe, now, as wakeups for timeslice are
done independently using condition variables.  This reduces FD
pressure slightly.

Signal handling is handled directly by a Ruby Thread (instead
of timer-thread) by exposing signal self-pipe to callers of
rb_thread_fd_select, native_sleep, rb_wait_for_single_fd, etc...
Acquiring, using, and releasing the self-pipe is exposed via 4
new internal functions:

1) rb_sigwait_fd_get - exclusively acquire timer_thread_pipe.normal[0]

2) rb_sigwait_fd_sleep - sleep and wait for signal (and no other FDs)

3) rb_sigwait_fd_put - release acquired result from rb_sigwait_fd_get

4) rb_sigwait_fd_migrate - migrate signal handling to another thread
                           after calling rb_sigwait_fd_put.

rb_sigwait_fd_migrate is necessary for waitpid callers because
only one thread can wait on self-pipe at a time, otherwise a
deadlock will occur if threads fight over the self-pipe.

TRAP_INTERRUPT_MASK is now set for the main thread directly in
signal handler via rb_thread_wakeup_timer_thread.

Originally, I wanted to use POSIX timers
(timer_create/timer_settime) for this.  Unfortunately, this
proved unfeasible as Mutex#sleep resumes on spurious wakeups and
test/thread/test_cv.rb::test_condvar_timed_wait failed.  Using
pthread_sigmask to mask out SIGVTALRM fixed that test,  but
test/fiddle/test_function.rb::test_nogvl_poll proved there'd be
some unavoidable (and frequent) incompatibilities from that
approach.

Finally, this allows us to drop thread_destruct_lock and
interrupt current ec directly.

We don't need to rely on vm->thread_destruct_lock or a coherent
vm->running_thread on any platform.  Separate timer-thread for
time slice and signal handling is relegated to thread_win32.c,
now.

[ruby-core:88088] [Misc #14937]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64107 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-29 20:47:33 +00:00
normal c95467e597 thread_pthread.c: clear altstacks in thread cache at GVL destruction
Otherwise, an altstack may live past ObjectSpace destruction
and xfree-ing the altstack will segfault.

[ruby-core:85621] [Feature #14487]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-29 10:15:11 +00:00
normal c6b85fcd01 timer_thread: do not close pipes around fork
There's actually no need to close the pipes used by the
sleepy timer thread before forking, only to stop the timer
thread itself.

Instead, we only close the parent pipes in the child process,
either via close-on-exec flag or when reinitializing the timer
thread.

This change will be necessary when we allow
rb_wait_for_single_fd and rb_thread_fd_select to wait on the
timer_thread_pipe.normal[0] directly and eliminate timer thread.

I don't anticipate compatibility problems with this change
alone.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63960 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-12 23:23:25 +00:00
normal b205642518 thread_pthread: avoid redundant error message on pipe2() fail
Seeing one error for pipe creation is enough.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63949 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-11 08:49:23 +00:00
normal 23f4ba46de thread_pthread.c: use mask for timer implementation
timer-thread will continue to be supported, but future
"timer" implementation may not be a thread.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63948 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-11 08:49:18 +00:00
normal c93adfc170 mjit: get rid of memory leak in pause+resume loop
pthread_atfork is not idempotent and repeatedly calling it
causes it to register the same hook repeatedly; leading to
unbound memory growth.

Ruby already has a (confusing-named) internal API for to call
in the forked child process: rb_thread_atfork
Call the MJIT child_after_fork hook inside that to prevent
unbound growth with the following loop:

    loop do
      RubyVM::MJIT.pause
      RubyVM::MJIT.resume
    end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63884 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-08 07:27:24 +00:00
normal 44fc3d08eb unrevert r63852 but keep SIGCHLD path disabled for win32
Reading win32/win32.c waitpid implementation, maybe waitpid(-1, ...)
on that platform will never conflict with mjit use of waitpid.

In any case, I've added WAITPID_USE_SIGCHLD macro to vm_core.h
so it can be easy for Linux/BSD users to test (hopefully!)
win32-compatible code.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63855 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-05 03:02:33 +00:00
naruse df4a126d65 Revert r63758 and related commits
The change is unstable on Windows. Please re-commit it when it correctly
supports Windows.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63852 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-04 15:08:56 +00:00
normal d9dac20bb3 thread_pthread.c: pass rb_vm_t to timer_thread_sleep
I love `container_of' for generic data structures, but
in this case it's unnecessary and slightly harder-to-read.

This will make "Timeout in VM" slightly easier-to-read:
https://bugs.ruby-lang.org/issues/14859

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-04 01:26:31 +00:00
normal 45cdc834c4 thread_pthread.c (native_thread_destroy): clear native TSD pointer
mwrap <https://80x24.org/mwrap/> interposes malloc functions and
checks for GVL existence to determine Ruby source locations of
malloc calls.  pthread_getattr_np (from get_stack) may call
realloc to get the CPU set size; so when using the thread-cache,
ruby_thread_has_gvl_p() may hit a false positive on reused
threads with lingering rb_thread_t in thread-specific data.

This was causing mwrap to call rb_source_location_cstr() and
crash because it was pointed to a zero ec->cfp->iseq.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-03 08:30:16 +00:00
normal ce2a3b40ed use SIGCHLD_LOSSY to enable waitpid polling mode
Some systems lack SIGCHLD or have incomplete SIGCHLD
implementations.  So enable polling mode for them.

[ruby-core:87705] [Bug #14867]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-30 00:51:57 +00:00
normal 054a412d54 hijack SIGCHLD handler for internal use
Use a global SIGCHLD handler to guard all callers of rb_waitpid.
To work safely with multi-threaded programs, we introduce a
VM-wide waitpid_lock to be acquired BEFORE fork/vfork spawns the
process.  This is to be combined with the new ruby_waitpid_locked
function used by mjit.c in a non-Ruby thread.

Ruby-level SIGCHLD handlers registered with Signal.trap(:CHLD)
continues to work as before and there should be no regressions
in any existing use cases.

Splitting the wait queues for PID > 0 and groups (PID <= 0)
ensures we favor PID > 0 callers.

The disabling of SIGCHLD in rb_f_system is longer necessary,
as we use deferred signal handling and no longer make ANY
blocking waitpid syscalls in other threads which could "beat"
the waitpid call made by rb_f_system.

We prevent SIGCHLD from firing in normal Ruby Threads and only
enable it in the timer-thread, to prevent spurious wakeups
from in test/-ext-/gvl/test_last_thread.rb with MJIT enabled.

I've tried to guard as much of the code for RUBY_SIGCHLD==0
using C "if" statements rather than CPP "#if" so to reduce
the likelyhood of portability problems as the compiler will
see more code.

We also work to suppress false-positives from
Process.wait(-1, Process::WNOHANG) to quiets warnings from
spec/ruby/core/process/wait2_spec.rb with MJIT enabled.

Lastly, we must implement rb_grantpt for ext/pty.  We need a
MJIT-compatible way of supporting grantpt(3) which may spawn
the `pt_chown' binary and call waitpid(2) on it.

[ruby-core:87605] [Ruby trunk Bug#14867]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63758 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-27 03:14:30 +00:00
k0kubun ea0cede5aa mjit.c: initial cygwin support
thread_pthread.c: Drop pthread_attr_setscope usage. It seems that,
at least on Linux and macOS, PTHREAD_SCOPE_PROCESS is not supported
and thus PTHREAD_SCOPE_SYSTEM should be used by default.

Let's just stop calling this until we find some platform that needs
`pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)`.

[Misc #14854]

From: fd0 (Daisuke Fujimura)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63730 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-23 07:21:12 +00:00
normal b445543974 thread_pthread.c (native_sleep): do not clear unblock.arg
It is unnecessary to clear unblock.arg once unblock.func is
cleared, and unblock_function_clear in thread.c doesn't
touch it, either.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63724 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-23 03:47:54 +00:00
normal 48efa44719 thread_pthread.c: fix non-sleepy timer-thread with fork
This fixes bootstraptest/test_fork.rb for systems with
sleepy timer thread disabled.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63709 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-20 23:38:21 +00:00
normal 0e2ba8495e thread_pthread.c: microptimize vm->gvl.waiting checks
"gvl.waiting" is volatile, so the compiler won't perform
these optimizations for us.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-17 03:27:45 +00:00
normal cc6342fec3 thread_pthread.c: fix non-sleepy timer thread build
I guess everybody has poll() and fcntl() nowadays, as
the non-sleepy timer thread build has been broken for
years, now.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63682 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-17 02:56:28 +00:00
normal 832b601e49 Initialize condattr_monotonic via pthread_condattr_init
Some operating systems will work without calling
pthread_condattr_init, but some won't (such as OpenBSD). Prior
to r63238, pthread_condattr_init was always called before
calling pthread_condattr_setclock.

From: Jeremy Evans <code@jeremyevans.net>
[ruby-core:87345] [Ruby trunk Bug#14807]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-01 21:56:11 +00:00
normal 72ad081145 thread_pthread.c: avoid reading th pointer for thread cache
I suspect GC may free the rb_thread_t (th) pointer by the time
we call register_cached_thread_and_wait.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63499 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-24 02:52:46 +00:00
normal fa31e1a418 thread_pthread.c: enable thread cache by default
Since r62466 ("thread_pthread.c: shorten and fix thread cache implementation"),
our thread cache is no longer buggy with programs using fork.
This makes significant improvements in vm_thread_alive_check1
and vm_thread_create_join benchmarks and does not introduce
regressions.

Unlike old thread cache, I've changed the cache to only last 3
seconds since per-thread setup in most programs rarely takes
more than a few milliseconds to re-establish things like network
connections.  This is configurable by changing the THREAD_CACHE_TIME
variable.

I hope this allows users to simplify their code by removing the
need for thread pools in many cases.

vm_thread_alive_check1 10.872   0.150
vm_thread_close         1.988   2.027
vm_thread_condvar1      0.751   0.767
vm_thread_condvar2      0.744   0.752
vm_thread_create_join   5.296   2.343
vm_thread_mutex1        1.911   1.892
vm_thread_mutex2        1.902   1.896
vm_thread_mutex3        2.389   2.313
vm_thread_pass          0.271   0.272
vm_thread_pass_flood    0.175   0.179
vm_thread_pipe          0.460   0.436
vm_thread_queue         0.453   0.446
vm_thread_sized_queue   0.547   0.547
vm_thread_sized_queue2  1.417   1.413
vm_thread_sized_queue3  1.410   1.426
vm_thread_sized_queue4  0.787   0.791

Speedup ratio: compare with the result of `trunk' (greater is better)
name    built
vm_thread_alive_check1 72.456
vm_thread_close         0.981
vm_thread_condvar1      0.979
vm_thread_condvar2      0.990
vm_thread_create_join   2.260
vm_thread_mutex1        1.010
vm_thread_mutex2        1.003
vm_thread_mutex3        1.033
vm_thread_pass          0.994
vm_thread_pass_flood    0.980
vm_thread_pipe          1.055
vm_thread_queue         1.016
vm_thread_sized_queue   0.999
vm_thread_sized_queue2  1.003
vm_thread_sized_queue3  0.989
vm_thread_sized_queue4  0.995

[ruby-core:87030] [Feature #14757]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-23 21:16:56 +00:00
normal 0f0311df0a thread: reduce GET_THREAD calls
This allows native_sleep to use less stack (80 -> 64 bytes on
x86-64) for GVL_UNLOCK_BEGIN/END.  For future APIs, we will pass
`ec` or `th` around anyways, so the BLOCKING_REGION change
should be beneficial in the future.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-16 21:54:42 +00:00
nobu 1222560ad1 thread_pthread.c: fallback to CLOCK_REALTIME
* thread_pthread.c (Init_native_thread): fallback to the default
  CLOCK_REALTIME when failed to set to CLOCK_MONOTONIC, e.g. on
  Solaris.  [Misc #14497]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-23 02:07:11 +00:00
normal ed590bdbfc thread*: all condvars are monotonic
There's no reason to use CLOCK_REALTIME for any condvars in Ruby.
Indeed, we initialized all condvars with RB_CONDATTR_CLOCK_MONOTONIC
anyway; so simplify our code and reduce ifdefs.

[ruby-core:85639] [Misc #14497]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-22 12:09:07 +00:00
normal 475b4aa40b simplify altstack and enable reuse with thread cache
Instead of allocating and registering the altstack in different
places, do it together to reduce code and improve readability.
When thread cache is enabled, storing altstack in rb_thread_t
is wasteful and we may reuse altstack in the same pthread.

This also lets us clearly allow use of xmalloc to allow GC to
recover from ENOMEM.

[ruby-core:85621] [Feature #14487]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-20 21:38:27 +00:00
nobu 2311087b68 Tiny Fix for ASYNC BUG error message copying
The previous logic would overwrite the error message, replacing the message with the `fd` number.

This tiny update will print the message in full.

(I'm trying to debug an issue with the timer thread on my machine and the lack of error messages makes it really hard).

[Fix GH-1829]

From: Bo <bo@bowild.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62661 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-05 06:56:09 +00:00
normal 92dd6d8445 thread_pthread.c: spelling ("cancellation")
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62485 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-19 09:28:56 +00:00
normal 4651ef77d2 thread_pthread.c: fix thread cache for non-monotonic clock
I noticed this because of https://bugs.ruby-lang.org/issues/14494

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62484 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-19 09:28:51 +00:00
normal 3cadd10ac5 thread_pthread.c (native_thread_create): remove needless attrp
Followup-to: r61719 (commit e8f40bd8f8)
  ("thread_pthread: remove HAVE_PTHREAD_ATTR_INIT ifdefs")
  [ruby-core:84758] [Misc #14342]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62478 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-19 00:30:43 +00:00
normal cfeb344bf2 thread_pthread.c (rb_thread_create_mjit_thread): destroy attr
This is required on some platforms to avoid leaks.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62477 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-19 00:23:00 +00:00
normal bff57efca1 thread_pthread.c (thread cache): destroy cond after unlock
No need to hold a lock while destroying a condition variable.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62476 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-18 23:58:35 +00:00
normal 7a8460ed5a thread_pthread.c (rb_thread_create_mjit): set detach before create
This should be slightly cheaper on NPTL as it does not rely on
atomics to set pd->joinid.   We already use pthread_attr_setdetachstate,
so it won't introduce new problems by using a function we did not
use before.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62475 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-18 23:58:30 +00:00
nobu ae62ab3302 adjust indent
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-18 09:23:47 +00:00
normal 4a57b88b19 thread_pthread.c: shorten and fix thread cache implementation
Update to use ccan/list for constant-time delete on expiry and
avoid malloc.  We must also initialize th->thread_id upon thread
reuse so Thread#name= works immediately upon thread creation.

We must also reinitialize the cache mutex and list_head on
fork like we do with GVL and timer thread mutexes.
While we're at it, use monotonic clock for timeout to avoid
system time changes.

"make exam TESTS='-x test_time_tz'" passes with USE_THREAD_CACHE
enabled (but remains off, here).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62466 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-18 07:54:10 +00:00
normal 8f47542d58 thread_pthread.c (native_cond_timeout): simplify
Rely on getclockofday for CLOCK_MONOTONIC, avoid needless
variables, and rely on overflow protection from timespec_add
instead of coding our own.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62459 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-18 02:12:23 +00:00
mame 50700c4d3c thread_pthread.c: Use `getpagesize()` when `pthread_attr_getguardsize` is unavailable
This is also for emscripten.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62409 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-15 01:59:16 +00:00
nobu b0cf1c234c fix up r62272
* thread.c (timeval_for): tv_usec is suseconds_t which may be
  smaller than long.

* thread_pthread.c (native_cond_timeout): ret is now used in
  CLOCK_MONOTONIC case only.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62275 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-07 06:14:56 +00:00
normal 0abd9b7f25 thread.c: favor timespec internally
This results in fewer conversion on common modern systems with
support for clock_gettime, pthread_cond_timedwait and ppoll.
gettimeofday is declared obsolete by POSIX.1-2008, so it is yet
another reason to move away from it.  This also appears to result
in the reduction of compatibility code required for dealing
with inconsistent implementations of "struct timeval".tv_sec

In the future, this will also result in fewer conversions for
kqueue and pselect if we elect to use them.

[ruby-core:85416] [Feature #14452]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62272 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-07 01:57:14 +00:00
nobu 1f003810c2 thread_pthread.c: cast inside rb_thread_create_mjit_thread
* thread_pthread.c (rb_thread_create_mjit_thread): cast
  worker_func pointer to void pointer inside.  adjusted to the
  declaration in mjit.c and the definition in thread_win32.c.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62242 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-06 01:42:38 +00:00
shyouhei 8427fca49b assigning void* to a function pointer is a POSIXism
No implicit cast is defined between these types.  Should be explicit.
Also, NULL is defined to be ((void*)0) so not usable as a function
pointer value.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62221 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-05 04:17:44 +00:00
k0kubun fd44a5777f mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler.  See the first comment of mjit.c to know what this
file does.

mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.

This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.

`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs.  TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.

mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.

Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.

common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.

inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.

ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.

thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows.  Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes

vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.

vm_insnhelper.h: trigger MJIT on method calls during VM execution.

vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.

iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV.  TODO: unload some GCed units in some safe way.

gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.

cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.

mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.

Patch author: Vladimir Makarov <vmakarov@redhat.com>.

Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.

Part of Feature 12589 and 14235.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:58:09 +00:00
normal 0c60f22ec5 thread_pthread.c: remove dead code around "get_stack_of"
"get_stack_of" was only in a proposed patch for [Feature #8793]
https://bugs.ruby-lang.org/issues/8793 and never applied.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61750 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 23:47:19 +00:00
nobu 0b3dfa087b thread_pthread.c: round stack size
* thread_pthread.c (rb_thread_create_timer_thread): round up
  additional stack size to PTHREAD_STACK_MIN, to get rid of
  EINVAL at pthread_attr_setstacksize().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61722 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 11:52:17 +00:00
normal 043e1fd559 thread_pthread: remove checks for pthread_cond*_init
These were added for NaCL support in r36022, and we dropped NaCL
in r60374.

IMHO, any pthreads implementation without these basic functions
is not worth the time to support.

[ruby-core:84758] [Misc #14342]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61720 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 08:26:43 +00:00
normal e8f40bd8f8 thread_pthread: remove HAVE_PTHREAD_ATTR_INIT ifdefs
ifdefs make code confusing for my easily-confused mind :<
These were added for NaCL support in r36022, and we dropped NaCL
in r60374.  There are more #ifdefs to remove...

[ruby-core:84758] [Misc #14342]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61719 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 08:26:38 +00:00
mame 3ecd8ab825 Fix the position of VM_ASSERT for "pthread_create failed for time"
Fix r61706.  Thank you, Eric Wong. [ruby-core:84756]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61717 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 07:33:38 +00:00
normal 66e42f4aa6 thread_pthread: more diagnostics around timer thread creation failures
However, I don't think EAGAIN on pthread_create can really
be fixed in our code.  I suspect test machines are overloaded.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 07:15:44 +00:00
mame 6c3bf2df0d Explicit failure in VM_CHECK_MODE when failing to create timer thread
"warning: pthread_create failed for timer: Resource temporarily
unavailable, scheduling broken" still occurs randomly.  This change will
allow us to debug the issue.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61706 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 03:47:22 +00:00
normal 2244bf5716 thread_pthread.c: use container_of
It's easier to read this macro from ccan than open-coding pointer
arithmetic.

thread_pthread.c (ubf_wakeup_all_threads): use container_of

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61573 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-02 21:23:47 +00:00
shyouhei 5471bf9cc2 offsetof(type, foo.bar) is (arguably) a GCCism
TL;DR see http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2031.htm

Suppose we have:

struct X {
  struct Y {
    z_t z;
  } y;
} x;

then, you _cant_ infer offsetof(struct X, y.z). The ISO C99 section
7.17 says nothing about such situation. At least clang warns this
being an extension to the language (-Wextended-offsetof).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61560 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-02 06:41:56 +00:00
ko1 5dfdaa9299 move rb_thread_t::interrupt_flag and mask
to rb_execution_context_t.

* vm_core.h (rb_thread_t): move
  `rb_thread_t::interrupt_flag` and
  `rb_thread_t::interrupt_mask` to rb_execution_context_t.

  RUBY_VM_CHECK_INTS() accepts `ec` instead of `th`.

* cont.c (rb_fiber_terminate): to propagate interrupt information,
  add new parameter `need_interrupt`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60672 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-06 07:44:28 +00:00
ko1 837fd5e494 Use rb_execution_context_t instead of rb_thread_t
to represent execution context [Feature #14038]

* vm_core.h (rb_thread_t): rb_thread_t::ec is now a pointer.
  There are many code using `th` to represent execution context
  (such as cfp, VM stack and so on). To access `ec`, they need to
  use `th->ec->...` (adding one indirection) so that we need to
  replace them by passing `ec` instead of `th`.

* vm_core.h (GET_EC()): introduced to access current ec. Also
  remove `ruby_current_thread` global variable.

* cont.c (rb_context_t): introduce rb_context_t::thread_ptr instead of
  rb_context_t::thread_value.

* cont.c (ec_set_vm_stack): added to update vm_stack explicitly.

* cont.c (ec_switch): added to switch ec explicitly.

* cont.c (rb_fiber_close): added to terminate fibers explicitly.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60440 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 08:32:49 +00:00
normal 73c397acb5 thread_pthread: do not corrupt stack
This fixes stuck test/ruby/test_io.rb with FIBER_USE_NATIVE=0 on
GNU/Linux because linked-list pointers used by glibc get
corrupted when fiber stacks are copied.

Thanks to wanabe for finding the bug and original patch.

* thread_pthread (native_thread_init_stack): fix stack corruption
  [ruby-core:82737] [Bug #13387]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60384 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-23 21:50:08 +00:00
hsbt 0e2d2e6a79 Drop to support NaCl platform.
Because NaCl and PNaCl are already sunset status.
  see https://bugs.chromium.org/p/chromium/issues/detail?id=239656#c160

  configure.ac: Patch for this file was provided by @nobu.

  [Feature #14041][ruby-core:83497][fix GH-1726]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60374 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-23 05:56:25 +00:00
normal b860f06413 thread_pthread.c: do not wakeup inside child processes
* thread_pthread.c (rb_thread_wakeup_timer_thread): check
  ownership before incrementing
  (rb_thread_wakeup_timer_thread_low): ditto
  [Bug #13794] [ruby-core:83064]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60079 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-30 21:50:42 +00:00
ko1 4f0c9a5cc4 move th->machine to ec->machine.
* vm_core.h: move rb_thread_t::machine to rb_execution_context_t::machine.

* vm_core.h, gc.c (rb_gc_mark_machine_stack): accept ec instead of th.
  it enables to call this func from rb_execution_context_mark() in vm.c.

* cont.c (fiber_setcontext): catch up this fix.
  fiber_restore_thread() restores machine stack information too.

* gc.c: catch up structure layout changes.

* thread.c: ditto.

* thread_pthread.c: ditto.

* thread_win32.c: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59825 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-10 15:49:45 +00:00
nobu 19d692920d thread_win32.c: set thread name
* thread_win32.c (native_set_another_thread_name): set thread name
  by SetThreadDescription.

* win32/win32.c (rb_w32_set_thread_description): dynamically try
  SetThreadDescription.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59660 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-26 00:30:03 +00:00
takano32 7aa74a0d0a a64: fix crash on register stack mark/sweep pass
* thread_pthread.c: move 'register_stack_start' earlier.
  [ruby-core:79928] [Bug #13284] [Fix GH-1625]

Author:    Sergei Trofimovich <slyfox@gentoo.org>


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59307 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-10 06:57:50 +00:00
nobu e8c6254680 thread_pthread.c: workaround for valgrind on Mac
* thread.c: revert r59053.

* thread_pthread.c (rb_thread_create_timer_thread): needs more
  stack space for valgrind.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59055 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-06-09 14:42:15 +00:00
normal 410362f6a2 thread_pthread: retry timer thread creation w/o attr on EINVAL
Setting a small stack size can fail due to having 3rd-party
libraries (e.g. libkqueue) loaded, if those libraries use
thread-local-storage (__thread) heavily.  This causes
pthread_create to fail with small stacks; even if our
timer_thread function does not hit any of the TLS-using code
paths.

Today, some RubyGems are capable of using libkqueue (or __thread
storage directly), and future versions of Ruby may use kqueue
internally.

cf. https://www.akkadia.org/drepper/tls.pdf

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-24 18:59:24 +00:00
nobu 4e44f6ef86 [DOC] replace Fixnum with Integer [ci skip]
* numeric.c: [DOC] update document for Integer class.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-26 06:11:23 +00:00
nobu dcc24cef2c thread_pthread.c: check stack on co-routine case
* thread_pthread.c (ruby_init_stack): check stack bounds even if
  get_main_stack succeeded, on the "co-routine" case.
  https://github.com/ruby/ruby/commit/53953ee#commitcomment-18887413

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56060 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-03 02:28:31 +00:00
nobu a716a2474b thread_pthread.c: no fork, no gvl_atfork
* thread_pthread.c (gvl_atfork): used in rb_thread_atfork_internal
  only if HAVE_WORKING_FORK is defined.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55085 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-20 14:24:39 +00:00
nobu 4bf8fa83b7 thread.c: defer setting name in initialize
* thread.c (rb_thread_setname): defer setting native thread name
  set in initialize until the native thread is created.
  [ruby-core:74963] [Bug #12290]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54600 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 13:30:03 +00:00
naruse 88291c190a * thread_pthread.c (reserve_stack): fix reserving position where
the stack growing bottom to top. [Bug #12118]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54256 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-24 16:51:30 +00:00
odaira a2594be783 * thread_pthread.c (getstack): __pi_stacksize returned by
pthread_getthrds_np() is wrong on AIX. Use
  __pi_stackend - __pi_stackaddr instead.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54000 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-04 18:14:18 +00:00
nagachika b7573a0081 * thread_pthread.c (rb_thread_create_timer_thread): destroy attr even
if pthread_create() failed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53375 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-29 19:26:52 +00:00
normal 24c979bdea thread_pthread.c (rb_thread_create_timer_thread): fix race
This fixes an occasional [ASYNC BUG] failure in
bootstraptest/test_fork.rb '[ruby-dev:37934]'
which tests fork/pthread_create failure by setting
RLIMIT_NPROC to 1 and triggering EAGAIN on pthread_create
when attempting to recreate the timer thread.

The problem timeline is as follows:

thread 1                           thread 2
---------------------------------------------------------------
rb_thread_create_timer_thread
setup_communication_pipe
                                   rb_thread_wakeup_timer_thread_low
pthread_create fails               pipe looks valid, write!
CLOSE_INVALIDATE (x4)              EBADF -> ASYNC BUG

The checks in rb_thread_wakeup_timer_thread_low only tried to
guarantee proper ordering with native_stop_timer_thread, not
rb_thread_create_timer_thread :x

Now, this should allow rb_thread_create_timer_thread to
synchronize properly with rb_thread_wakeup_timer_thread_low by
delaying the validation marking of the timer_thread_pipe until
we are certain the timer thread is alive.

In this version, rb_thread_wakeup_timer_thread_low becomes a
noop.  Threading is still completely broken with NPROC==1, but
there's not much we can do about it beside warn the user.
We no longer spew a scary [ASYNC BUG] message or dump core
on them.

* thread_pthread.c (setup_communication_pipe): delay setting owner
  (rb_thread_create_timer_thread): until thread creation succeeds
  [ruby-core:72590] [Bug #11922]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-29 18:20:27 +00:00
hsbt 6851be0f0c * compile.c: fix typos.
[ci skip][fix GH-1140] Patch by @jutaz
* dir.c: ditto.
* gc.c: ditto.
* io.c: ditto.
* node.h: ditto.
* thread_pthread.c: ditto.
* vm_insnhelper.c: ditto.
* vsnprintf.c: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53103 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-14 02:51:13 +00:00
nobu dfe27428f1 configure.in: split SET_THREAD_NAME
* configure.in: separate SET_CURRENT_THREAD_NAME, which can set
  the name of current thread only, and SET_ANOTHER_THREAD_NAME,
  which can set the name of other threads.
* thread.c (rb_thread_setname): use SET_ANOTHER_THREAD_NAME.  OS X
  is not possible to set another thread name.
* thread_pthread.c (native_set_thread_name, thread_timer): use
  SET_CURRENT_THREAD_NAME.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52866 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-03 02:57:14 +00:00
kosaki 487748fac8 * thread_pthread.c (register_ubf_list): renamed from
add_signal_thread_list.
* thread_pthread.c (unregister_ubf_list): renamed
  from remove_signal_thread_list.
* thread_pthread.c (ubf_wakeup_all_threads): renamed from
  ping_signal_thread_list.
* thread_pthread.c (ubf_wakeup_thread): renamed from
  ubf_select_each.
* thread_pthread.c (ubf_threads_empty): renamed from
  check_signal_thread_list().
* thread_pthread.c (ubf_list_lock): renamed from
  signal_thread_list_lock.

* thread_pthread.c (register_ubf_list): large simplification
  by using ccan/list.h.
  bonus: removed malloc() and exit(EXIT_FAILURE).
* thread_pthread.c (unregister_ubf_list): ditto.
* thread_pthread.c (ubf_threads_empty): ditto.
* thread_pthread.c (ubf_wakeup_all_threads): ditto.

* thread_pthread.c (print_signal_list): removed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52818 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-30 20:33:20 +00:00
nobu e29c109d2a Haiku now best effort support
* configure.in: remove obsolete workarounds for Haiku.
* dln.c, file.c, io.c: remove obsolete Haiku workarounds.
* thread_pthread.c: add stack bounds detection for Haiku.
* signal.c: get stack pointer from signal context on Haiku.
  [ruby-core:67923] [Bug #10811] [Fix GH-1109]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52721 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-23 03:54:42 +00:00
odaira f2d679c751 * thread_pthread.c: fix compile erros when
USE_SLEEPY_TIMER_THREAD is disabled.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52280 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-25 16:07:25 +00:00
nobu 5bed80ee36 thread_pthread.c: async_bug_fd
* thread_pthread.c (async_bug_fd): show failed file descriptor too.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51779 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-09-06 15:27:25 +00:00
nobu 8a4f3a71a2 thread_pthread.c: async bug messages
* thread_pthread.c (consume_communication_pipe, CLOSE_INVALIDATE):
  adjust async bug messages.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51777 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-09-06 15:19:47 +00:00
naruse 3816f7574f * thread_pthread.c (reserve_stack): ensure the memory is really
allocated. [Bug #11457]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51626 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-08-18 11:21:50 +00:00
nobu 8b5f22c1b4 thread_pthread.c: suppress a warning
* thread_pthread.c (native_stop_timer_thread): explicit type of
  old value to suppress a pointer-sign warning.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51578 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-08-14 10:38:29 +00:00
normal f224fecc0f improve handling of timer thread shutdown
Shutting down the timer thread now always closes pipes to free FDs.
In fact, we close the write ends of the pipes is done in the main
RubyVM to signal the timer thread shutdown.

To effectively close pipes, we implement userspace locks via
atomics to force the pipe closing thread to wait on any signal
handlers which may be waking up.

While we're at it, improve robustness during resource exhaustion and
allow it to limp along non-fatally if restarting a timer thread
fails.

This reverts r51268

Note: this change is tested with VM_CHECK_MODE 1 in vm_core.h

* process.c (close_unless_reserved): add extra check
  (dup2_with_divert): remove
  (redirect_dup2): use dup2 without divert
  (before_exec_non_async_signal_safe): adjust call + comment
  (rb_f_exec): stop timer thread for all OSes
  (rb_exec_without_timer_thread): remove
* eval.c (ruby_cleanup): adjust call
* thread.c (rb_thread_stop_timer_thread): always close pipes
* thread_pthread.c (struct timer_thread_pipe): add writing field,
    mark owner_process volatile for signal handlers
  (rb_thread_wakeup_timer_thread_fd): check valid FD
  (rb_thread_wakeup_timer_thread): set writing flag to prevent close
  (rb_thread_wakeup_timer_thread_low): ditto
  (CLOSE_INVALIDATE): new macro
  (close_invalidate): new function
  (close_communication_pipe): removed
  (setup_communication_pipe_internal): make errors non-fatal
  (setup_communication_pipe): ditto
  (thread_timer): close reading ends inside timer thread
  (rb_thread_create_timer_thread): make errors non-fatal
  (native_stop_timer_thread): close write ends only, always,
   wait for signal handlers to finish
  (rb_divert_reserved_fd): remove
* thread_win32.c (native_stop_timer_thread): adjust (untested)
  (rb_divert_reserved_fd): remove
* vm_core.h: adjust prototype

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-08-14 09:44:10 +00:00
ngoto f242e17f9b * process.c (redirect_dup2): when the new FD of dup2() coflicts
with one of the timer thread FDs, the internal FD is diverted.
  [Bug #11336] [ruby-core:69886] [Bug #11350] [ruby-core:69961]
    
* process.c (dup2_with_divert): new function for the above purpose.
   
* thread_pthread.c (rb_divert_reserved_fd): new function for
  diverting reserved FD. If the given FD is the same as one of the
  reserved FDs, the reserved FD number is internally changed.
  It returns -1 when error. Otherwise, returns 0. It also returns
  0 if there is no need to change reserved FD number.

* thread_win32.c (rb_divert_reserved_fd): always returns 0 because
  of no reserved FDs.

* internal.h (rb_divert_reserved_fd): prototype declaration.
  It is Ruby internal use only.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51268 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-16 13:06:13 +00:00
ngoto d1bce9ec55 * process.c (redirect_close, parent_redirect_close): should not close
reserved FD. It should be closed in the exec system call due to the
  O_CLOEXEC or FD_CLOEXEC flag. [Bug #11353] [ruby-core:69977]
    
* process.c (close_unless_reserved): new function to close FD unless
  it is reserved for internal communication.
      
* thread_pthread.c (rb_reserved_fd_p): should check owner_process pid
  to avoid false positive in forked child process.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-15 14:26:22 +00:00
naruse 370bd884bc * configure.in: define SET_THREAD_NAME if it has pthread_set_name_np
for FreeBSD, and don't define it if both pthread_setname_np
  and pthread_set_name_np don't exist.

* thread_pthread.c (SET_THREAD_NAME): don't define if they don't exist.

* thread_pthread.c (native_set_thread_name): run if SET_THREAD_NAME
  is defined.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-11 06:56:57 +00:00
nobu 78c75612f0 thread_pthread.c: keep sp safe zone
* thread_pthread.c (reserve_stack): keep sp safe zone to get rid
  of crash by -fstack-check.  [ruby-core:68740] [Bug #11030]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50316 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-04-14 22:34:53 +00:00
nobu 40564c1e3b thread_pthread.c: use rb_pid_t
* thread_pthread.c (timer_thread_pipe.owner_process): process ID
  should be pid_t.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49796 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-01 12:27:06 +00:00
nobu 8fe95fea9d thread_pthread.c: Fix intermittent SIGBUS on Linux
* thread_pthread.c (reserve_stack): fix intermittent SIGBUS on
  Linux, by reserving the stack virtual address space at process
  start up so that it will not clash with the heap space.
  [Fix GH-822]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49452 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-31 03:06:26 +00:00
normal 8341136f07 thread.c: micro-optimize thread create/join
* thread.c (struct join_arg): restructure and make smaller
  (thread_join_sleep): avoid timeofday() call if forever
  (thread_join): pass join_arg.delay directly
  (rb_thread_inspect_msg): remove, inline into rb_thread_inspect
  (rb_thread_inspect): reduce branching and string creation
* thread_pthread.c (native_set_thread_name): create string directly
  to avoid reparsing. [Misc #10723]

This reduces time in benchmark/bm_vm_thread_create_join.rb by
a few percent.

Minor improvements only:

target 0: 2.1.5 (ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux])
target 1: trunk (ruby 2.3.0dev (2015-01-16 trunk 49282) [x86_64-linux])
target 2: built (ruby 2.3.0dev (2015-01-16 trunk 49282) [x86_64-linux])

benchmark results:
minimum results in each 3 measurements.
Execution time (sec)
name                    2.1.5   trunk   built
vm_thread_create_join   1.049   1.242   1.138

Speedup ratio: compare with the result of `2.1.5' (greater is better)
name                    trunk   built
vm_thread_create_join   0.845   0.923

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49430 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-28 17:06:40 +00:00
akr 1ea4976041 * thread_pthread.c (native_set_thread_name): New function to
set thread name visible with ps command on GNU/Linux.
  Ex. ps -o %c -L

* thread.c (thread_start_func_2): Call native_set_thread_name at
  beginning.
  (rb_thread_inspect_msg): Extract from rb_thread_inspect.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47670 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-09-21 05:14:47 +00:00
hsbt 8362f9b581 * symbian/*: removed Symbian support.
[Feature #10199][ruby-core:64725]
* dln.c: ditto.
* include/ruby/defines.h: ditto.
* thread_pthread.c: ditto.
* vm.c: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47383 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-09-04 11:09:32 +00:00
normal fdb2fc0b04 thread_pthread: prefer rb_nativethread* types/functions
This will make it easier for us to try alternative mutex/condvar
implementations while still using pthreads for thread management.
[Feature #10134]

* thread_pthread.h: define RB_NATIVETHREAD_LOCK_INIT and
  RB_NATIVETHREAD_COND_INIT macros

* thread_pthread.c (native_mutex_lock, native_mutex_unlock,
  native_mutex_trylock, native_mutex_initialize,
  native_mutex_destroy, native_cond_wait):
  use rb_nativethread_lock_t instead of pthread_mutex_t

* thread_pthread.c (native_mutex_debug): make argument type-agnostic
  to avoid later cast.

* thread_pthread.c (register_cached_thread_and_wait):
  replace PTHREAD_COND_INITIALIZER with RB_NATIVETHREAD_COND_INIT,
  use native_mutex_{lock,unlock}

* thread_pthread.c (use_cached_thread):
  use native_mutex_{lock,unlock}

* thread_pthread.c (native_sleep):
  use rb_nativethread_lock_t to match th->interrupt_lock,
  use native_mutex_{lock,unlock}

* thread_pthread.c (timer_thread_lock): use rb_nativethread_lock_t type

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47185 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-08-15 00:17:53 +00:00
nobu dfd8c5d402 thread.c: fix for non-scalar pthread_t
* configure.in (rb_cv_scalar_pthread_t): pthread_t is not required
  to be a scalar type.
* thread.c (fill_thread_id_string, thread_id_str): dump pthread_t
  in hexadecimal form if it is not a scalar type, assume it can be
  represented in a pointer form otherwise.  based on the patch by
  Rei Odaira at [ruby-core:62867].  [ruby-core:62857] [Bug #9884]
* thread_pthread.c (Init_native_thread, thread_start_func_1),
  (native_thread_create): set thread_id_str if needed.
* vm_core.h (rb_thread_t): add thread_id_string if needed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46406 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-06-11 08:38:09 +00:00
nobu e2b10b6d13 thread_pthread.c: timer thread flag
* thread_pthread.c (timer_thread): add a flag to tell timer thread
  is created, since 0 may be a valid value as pthread_t.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46405 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-06-11 08:37:44 +00:00
nobu a5b9624fdc thread_pthread.c: variable for errno
* thread_pthread.c (rb_thread_wakeup_timer_thread_fd): use a local
  variable for errno.

* thread_pthread.c (consume_communication_pipe): ditto.  add
  EWOULDBLOCK case.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-10 15:52:45 +00:00
nobu 3385f6a0d4 thread_pthread.c: timer_thread_pipe struct
* thread_pthread.c (timer_thread_pipe): aggregate timer thread
  pipe stuff into a struct.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45907 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-10 14:48:58 +00:00
nobu 779ce6b00e thread_pthread.c: compare with pthread_equal
* thread_pthread.c (ubf_select): compare thread_id with
  pthread_equal() but not directly.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45906 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-10 14:47:04 +00:00
nobu a46e21c86f thread_pthread.c: variable for errno
* thread_pthread.c (timer_thread_sleep): use a local variable for
  errno.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45898 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-10 04:32:10 +00:00
nobu ed37579c6a thread_*.c: constify
* thread_{pthread,win32}.c (native_cond_timedwait): constify.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-05-09 14:31:34 +00:00
nobu 6245e02cd3 configure.in: define SET_THREAD_NAME
* configure.in (SET_THREAD_NAME): define according to
  pthread_setname_np variations.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45190 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-02-27 07:10:11 +00:00
nobu bb7ae984f8 configure.in: use pthread_setname_np only if available
* configure.in: check if pthread_setname_np is available.
* thread_pthread.c: pthread_setname_np is not available on old
  Darwins.  [ruby-core:60524] [Bug #9492]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44925 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-02-13 02:06:36 +00:00
nobu f423a63cfa thread_pthread.c: use get_stack
* thread_pthread.c (ruby_init_stack, ruby_stack_overflowed_p):
  place get_stack above others to get stack boundary information.
  [ruby-core:60113] [Bug #9454]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44726 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-28 07:33:20 +00:00
naruse 239c994243 * thread_pthread.c: rlimit is only available on Linux.
At least r44712 breaks FreeBSD.
  [ruby-core:60113] [Bug #9454]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44725 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-28 06:29:51 +00:00
nobu 628f75b752 vm_core.h: rb_thread_struct::machine
* vm_core.h (rb_thread_struct): aggregate cpu stuff into a struct,
  so that a debugger can show its content at once.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44722 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-28 06:09:58 +00:00
nobu d7b106db6f thread_pthread.c: cast to size_t
* thread_pthread.c (ruby_stack_overflowed_p): range of rlim_cur
  may be larger than size_t.  [ruby-core:60113] [Bug #9454]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44715 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-27 13:16:25 +00:00
nobu 60e85501f2 thread_pthread.c: get current main thread stack size
* thread_pthread.c: get current main thread stack size, which may
  be expanded than allocated size at initialization, by rlimit().
  [ruby-core:60113] [Bug #9454]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44712 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-27 12:53:48 +00:00
nobu 6802a3f6f2 thread_pthread.c: fix for dynamic PTHREAD_STACK_MIN
* thread_pthread.c (rb_thread_create_timer_thread): fix for platforms
  where PTHREAD_STACK_MIN is a dynamic value and not a compile-time
  constant.  [ruby-dev:47911] [Bug #9436]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-21 11:14:54 +00:00
nobu 03a89f5bc3 * thread_pthread.c (rb_thread_create_timer_thread): fix missing paren.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44673 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-21 08:26:17 +00:00
nobu ad9d4f0211 thread_pthread.c: expand timer thread stack size
* thread_pthread.c (rb_thread_create_timer_thread): expand timer
  thread stack size to get rid of segfault on FreeBSD/powerpc64.
  based on the patch by Steve Wills at [ruby-core:59923].
  [ruby-core:56590] [Bug #8783]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44671 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-21 07:26:47 +00:00
nobu a4963d464b thread_pthread.c: timer thread stack size
* thread_pthread.c (rb_thread_create_timer_thread): define the stack
  size for timer thread at compile time.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44670 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-01-21 07:26:46 +00:00
nobu 53953eee7c thread_pthread.c: use get_main_stack properly
* thread_pthread.c (ruby_init_stack): set stack_start properly by
  get_main_stack() if possible.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43706 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-18 13:47:12 +00:00
akr fbdd08b8c2 * thread_pthread.c (rb_thread_create_timer_thread): Show error
message instead of error number.

* cont.c (fiber_machine_stack_alloc): Ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43694 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-11-16 16:49:39 +00:00
nobu 09c96a7858 thread_pthread.c: set thread name
* thread_pthread.c (thread_timer): set timer thread name on OSX too.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43128 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-10-03 01:37:00 +00:00
nobu 1908874bf4 thread_pthread.c: fix compile error on HP-UX
* thread_pthread.c (sys/dyntune.h): for gettune().
* thread_pthread.c (hpux_attr_getstackaddr): fix missing *.
  [ruby-core:56983] [Feature #8793]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-09-03 12:35:22 +00:00
nobu 93024c6377 thread_pthread.c: fill stack info by creator thread
* thread_pthread.c (native_thread_init_stack): wait the creator thread
  to fill machine stack info, if get_stack_of() is available.
* thread_pthread.c (native_thread_create): fill the created thread
  stack info after starting, if get_stack_of() is available.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42732 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-30 13:37:59 +00:00
nobu b59b1b9bd9 thread_pthread.c: define attr only if used
* thread_pthread.c (native_thread_create): define attr only if it is
  used, and merge pthread_create() calls.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42731 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-30 13:37:51 +00:00
nobu 7703ba8544 thread_pthread.c: get_main_stack
* thread_pthread.c (get_main_stack): separate function to get stack of
  main thread.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42730 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-30 13:37:46 +00:00
nobu f8b3123eb9 thread_pthread.c: fix vps_pagesize
* thread_pthread.c (hpux_attr_getstackaddr): vps_pagesize is defaulted
  to 16 and in Kbytes.  [ruby-core:56863]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42728 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-30 01:26:05 +00:00
nobu 806ddf6713 thread_pthread.c: get_stack on HP-UX
* thread_pthread.c (hpux_attr_getstackaddr): basic support for the
  get_stack() under HP-UX.  based on the patch by michal@rokos.cz
  (Michal Rokos) at [ruby-core:56645].  [Feature #8793]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-28 08:20:13 +00:00
nobu b7aa49b9a7 thread_pthread.c: dup code
* thread_pthread.c (get_stack): merge duplicated code split by ifdef.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42715 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-28 08:20:10 +00:00
nobu 50b17c701f thread_pthread.c: simplify
* thread_pthread.c (register_cached_thread_and_wait): simplify and
  reduce branches.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-28 05:59:13 +00:00
ko1 4d3feac974 * thread_(pthread|win32).h: rename rb_thread_cond_t to
rb_nativethread_cond_t.
* thread.c, thread_pthread.c, thread_win32.c, vm_core.h: catch up
  renaming.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42138 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-07-23 10:50:32 +00:00