Project

General

Profile

Bug #15438 ยป 0001-2.6-fix-handling-of-negative-priorities.patch

Patch for 2.6 - sylvain.joyeux (Sylvain Joyeux), 01/15/2019 12:08 PM

View differences:

thread.c
80 80
#ifndef USE_NATIVE_THREAD_PRIORITY
81 81
#define USE_NATIVE_THREAD_PRIORITY 0
82 82
#define RUBY_THREAD_PRIORITY_MAX 3
83
#define RUBY_THREAD_PRIORITY_MIN -3
83
#define RUBY_THREAD_PRIORITY_MIN -5
84 84
#endif
85 85

  
86 86
#ifndef THREAD_DEBUG
......
341 341
    rb_thread_wakeup_timer_thread(0);
342 342
}
343 343

  
344
static int thread_time_quantum_usec_from_priority(int priority);
345

  
344 346
#if   defined(_WIN32)
345 347
#include "thread_win32.c"
346 348

  
......
364 366
#error "unsupported thread type"
365 367
#endif
366 368

  
369
static int thread_time_quantum_usec_from_priority(int priority) {
370
    if (priority > 0) {
371
        return TIME_QUANTUM_USEC_BASE << priority;
372
    }
373
    else {
374
        return TIME_QUANTUM_USEC_BASE >> -priority;
375
    }
376
}
377

  
367 378
/*
368 379
 * TODO: somebody with win32 knowledge should be able to get rid of
369 380
 * timer-thread by busy-waiting on signals.  And it should be possible
......
2216 2227
	}
2217 2228

  
2218 2229
	if (timer_interrupt) {
2219
	    uint32_t limits_us = TIME_QUANTUM_USEC;
2220

  
2221
	    if (th->priority > 0)
2222
		limits_us <<= th->priority;
2223
	    else
2224
		limits_us >>= -th->priority;
2230
	    uint32_t limits_us = thread_time_quantum_usec_from_priority(th->priority);
2225 2231

  
2226 2232
	    if (th->status == THREAD_RUNNABLE)
2227 2233
		th->running_time_us += TIME_QUANTUM_USEC;
......
3600 3606
    else if (priority < RUBY_THREAD_PRIORITY_MIN) {
3601 3607
	priority = RUBY_THREAD_PRIORITY_MIN;
3602 3608
    }
3609
    native_update_quantum(priority);
3603 3610
    target_th->priority = (int8_t)priority;
3604 3611
#endif
3605 3612
    return INT2NUM(target_th->priority);
......
3888 3895
sigwait_timeout(rb_thread_t *th, int sigwait_fd, const rb_hrtime_t *orig,
3889 3896
                int *drained_p)
3890 3897
{
3891
    static const rb_hrtime_t quantum = TIME_QUANTUM_USEC * 1000;
3892

  
3893 3898
    if (sigwait_fd >= 0 && (!ubf_threads_empty() || BUSY_WAIT_SIGNALS)) {
3894 3899
        *drained_p = check_signals_nogvl(th, sigwait_fd);
3895
        if (!orig || *orig > quantum)
3896
            return &quantum;
3900
        if (!orig || *orig > TIME_QUANTUM_NSEC)
3901
            return &TIME_QUANTUM_NSEC;
3897 3902
    }
3898 3903

  
3899 3904
    return orig;
thread_pthread.c
175 175
/* 100ms.  10ms is too small for user level thread scheduling
176 176
 * on recent Linux (tested on 2.6.35)
177 177
 */
178
#define TIME_QUANTUM_MSEC (100)
179
#define TIME_QUANTUM_USEC (TIME_QUANTUM_MSEC * 1000)
180
#define TIME_QUANTUM_NSEC (TIME_QUANTUM_USEC * 1000)
178
#define TIME_QUANTUM_USEC_BASE (100 * 1000)
179
static rb_hrtime_t TIME_QUANTUM_MSEC = TIME_QUANTUM_USEC_BASE / 1000;
180
static rb_hrtime_t TIME_QUANTUM_USEC = TIME_QUANTUM_USEC_BASE;
181
static rb_hrtime_t TIME_QUANTUM_NSEC = TIME_QUANTUM_USEC_BASE * 1000;
181 182

  
182 183
static rb_hrtime_t native_cond_timeout(rb_nativethread_cond_t *, rb_hrtime_t);
183 184

  
185
static void
186
native_update_quantum(int priority)
187
{
188
    rb_hrtime_t new_quantum_usec;
189
    new_quantum_usec = thread_time_quantum_usec_from_priority(priority);
190
    if (TIME_QUANTUM_USEC > new_quantum_usec)
191
    {
192
        TIME_QUANTUM_USEC = new_quantum_usec;
193
        TIME_QUANTUM_MSEC = new_quantum_usec / 1000;
194
        TIME_QUANTUM_NSEC = new_quantum_usec * 1000;
195
    }
196
}
197

  
184 198
/*
185 199
 * Designate the next gvl.timer thread, favor the last thread in
186 200
 * the waitq since it will be in waitq longest
thread_win32.c
13 13

  
14 14
#include <process.h>
15 15

  
16
#define TIME_QUANTUM_USEC (10 * 1000)
16
#define TIME_QUANTUM_USEC_BASE (100 * 1000)
17
#define TIME_QUANTUM_USEC TIME_QUANTUM_USEC
18
static const rb_hrtime_t TIME_QUANTUM_NSEC = &TIME_QUANTUM_USEC * 1000;
17 19
#define RB_CONDATTR_CLOCK_MONOTONIC 1 /* no effect */
18 20

  
21
void native_update_quantum(int priority) { }
22

  
19 23
#undef Sleep
20 24

  
21 25
#define native_thread_yield() Sleep(0)
22
-