Project

General

Profile

Feature #10341 ยป 0002-Code-cleanup-in-fiber_switch-fiber_store.patch

nome (Knut Franke), 10/08/2014 11:59 AM

View differences:

cont.c
1337 1337
	th->root_fiber = th->fiber = fib->cont.self;
1338 1338
    }
1339 1339

  
1340
#if !FIBER_USE_NATIVE
1341
    cont_save_machine_stack(th, &fib->cont);
1342
#endif
1343

  
1344
    if (FIBER_USE_NATIVE || ruby_setjmp(fib->cont.jmpbuf)) {
1345 1340
#if FIBER_USE_NATIVE
1346
	fiber_setcontext(next_fib, fib);
1341
    fiber_setcontext(next_fib, fib);
1342
    /* restored */
1347 1343
#ifndef _WIN32
1348
	if (terminated_machine_stack.ptr) {
1349
	    if (machine_stack_cache_index < MAX_MACHINE_STACK_CACHE) {
1350
		machine_stack_cache[machine_stack_cache_index].ptr = terminated_machine_stack.ptr;
1351
		machine_stack_cache[machine_stack_cache_index].size = terminated_machine_stack.size;
1352
		machine_stack_cache_index++;
1344
    if (terminated_machine_stack.ptr) {
1345
	if (machine_stack_cache_index < MAX_MACHINE_STACK_CACHE) {
1346
	    machine_stack_cache[machine_stack_cache_index].ptr = terminated_machine_stack.ptr;
1347
	    machine_stack_cache[machine_stack_cache_index].size = terminated_machine_stack.size;
1348
	    machine_stack_cache_index++;
1349
	}
1350
	else {
1351
	    if (terminated_machine_stack.ptr != fib->cont.machine.stack) {
1352
		munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
1353 1353
	    }
1354 1354
	    else {
1355
		if (terminated_machine_stack.ptr != fib->cont.machine.stack) {
1356
		    munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
1357
		}
1358
		else {
1359
		    rb_bug("terminated fiber resumed");
1360
		}
1355
		rb_bug("terminated fiber resumed");
1361 1356
	    }
1362
	    terminated_machine_stack.ptr = NULL;
1363
	    terminated_machine_stack.size = 0;
1364 1357
	}
1365
#endif
1366
#endif
1358
	terminated_machine_stack.ptr = NULL;
1359
	terminated_machine_stack.size = 0;
1360
    }
1361
    GetFiberPtr(th->fiber, fib);
1362
    if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
1363
    return fib->cont.value;
1364
#endif /* not _WIN32 */
1365

  
1366
#else /* FIBER_USE_NATIVE */
1367
    cont_save_machine_stack(th, &fib->cont);
1368
    if (ruby_setjmp(fib->cont.jmpbuf)) {
1367 1369
	/* restored */
1368 1370
	GetFiberPtr(th->fiber, fib);
1369 1371
	if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
1372
	if (nextfib->cont.value == Qundef) {
1373
	    cont_restore_0(nextfib->cont, &nextfib->cont.value);
1374
	    rb_bug("rb_fiber_resume: unreachable");
1375
	}
1370 1376
	return fib->cont.value;
1371 1377
    }
1372
#if !FIBER_USE_NATIVE
1373 1378
    else {
1374
	return Qundef;
1379
	VALUE undef = Qundef;
1380
	cont_restore_0(nextfib->cont, &undef);
1381
	rb_bug("rb_fiber_resume: unreachable");
1375 1382
    }
1376
#endif
1383
#endif /* FIBER_USE_NATIVE */
1377 1384
}
1378 1385

  
1379 1386
static inline VALUE
......
1402 1409
    }
1403 1410
    else if (fib->status == TERMINATED) {
1404 1411
	value = rb_exc_new2(rb_eFiberError, "dead fiber called");
1405
	if (th->fiber != fibval) {
1406
	    GetFiberPtr(th->fiber, fib);
1407
	    if (fib->status != TERMINATED) rb_exc_raise(value);
1408
	    fibval = th->root_fiber;
1409
	}
1410
	else {
1411
	    fibval = fib->prev;
1412
	    if (NIL_P(fibval)) fibval = th->root_fiber;
1413
	}
1414
	GetFiberPtr(fibval, fib);
1412

  
1413
	GetFiberPtr(th->fiber, fib);
1414
	if (fib->status != TERMINATED) rb_exc_raise(value);
1415

  
1416
	/* th->fiber is also dead => switch to root fiber */
1417
	/* (this means we're being called from rb_fiber_terminate, */
1418
	/* and the terminated fiber's return_fiber() is already dead) */
1419
	GetFiberPtr(th->root_fiber, fib);
1415 1420
	cont = &fib->cont;
1416 1421
	cont->argc = -1;
1417 1422
	cont->value = value;
1418 1423
#if FIBER_USE_NATIVE
1419 1424
	{
1420
	    VALUE oldfibval;
1425
	    VALUE oldfibval = th->fiber;
1421 1426
	    rb_fiber_t *oldfib;
1422
	    oldfibval = rb_fiber_current();
1423 1427
	    GetFiberPtr(oldfibval, oldfib);
1424 1428
	    fiber_setcontext(fib, oldfib);
1425 1429
	}
1426 1430
#else
1427 1431
	cont_restore_0(cont, &value);
1428 1432
#endif
1433
	/* unreachable */
1429 1434
    }
1430 1435

  
1431 1436
    if (is_resume) {
......
1440 1445
    cont->value = make_passing_arg(argc, argv);
1441 1446

  
1442 1447
    value = fiber_store(fib);
1443
#if !FIBER_USE_NATIVE
1444
    if (value == Qundef) {
1445
	cont_restore_0(cont, &value);
1446
	rb_bug("rb_fiber_resume: unreachable");
1447
    }
1448
#endif
1449 1448
    RUBY_VM_CHECK_INTS(th);
1450 1449

  
1451 1450
    return value;
1452
-