summaryrefslogtreecommitdiff
path: root/fibers (follow)
Commit message (Collapse)AuthorAgeFilesLines
* Garbage collect synchronized items from channels put/get queues.Christopher Allan Webber2017-08-104-35/+90
| | | | | | | | | | | | | * fibers/conditions.scm (make-counter, %steps-till-gc, counter-decrement!) (counter-reset!): Moved to new module, counter.scm. * fibers/counter.scm: New file. Rename `%steps-till-gc' to `%countdown-steps'. * Makefile.am: Add counter.scm. * fibers/channels.scm (<channel>, make-channel): Add new slots `getq-gc-counter' and `putq-gc-counter'. (put-operation, get-operation): Garbage collect synchronized items from queues. * fibers/deque.scm (dequeue-filter, dequeue-filter!): New procedures.
* Update copyright headers, adding Chris Webber where appropriate.Christopher Allan Webber2017-08-102-0/+2
| | | | | * fibers/conditions.scm: * fibers/stack.scm: Update copyright headers.
* Lighten up fibers by installing "catch" in schedulerAndy Wingo2017-08-061-21/+36
| | | | This takes the load off of each fiber and speeds things up.
* Fix typo in schedule-task-when-fd-writableAndy Wingo2017-08-061-1/+1
|
* Rebase fibers on top of schedulers and tasksAndy Wingo2017-08-066-551/+428
| | | | | | | | | | This commit refactors fibers to be based on lighter-weight "tasks", and makes the scheduler API more orthogonal. Now there are no more fiber objects (although the fibers layer could re-add them if they are useful), and fibers no longer have names (although again this could be re-added at an upper layer). Also it's the current scheduler that's bound by a parameter, not the current fiber, and as it's a thread-safe parameter it doesn't need to be bound in each fiber.
* Garbage collect old condition waiters.Christopher Allan Webber2017-07-312-5/+56
| | | | | | | | * fibers/stack.scm (stack-filter!): New variable. * fibers/conditions.scm (make-counter, counter-increment!) (counter-reset!, %steps-till-gc): New variables. (<condition>, make-condition): Update to take gc-step argument. (wait-operation): Occasionally garbage collect old condition waiters.
* Add block-asyncs in fiber->thread resume pathAndy Wingo2017-02-201-4/+6
| | | | | | * fibers/operations.scm (perform-operation): Block asyncs in resume callback; otherwise the fiber doing the resuming could be scheduled away.
* Fix deprecation warning in (fibers operations)Andy Wingo2017-02-201-0/+5
| | | | * fibers/operations.scm: Use (ice-9 threads).
* Fix epoll wakeupAndy Wingo2017-02-201-5/+7
| | | | | | * fibers/epoll.scm (epoll): Like a complete idiot, I broke epoll wakeup. Fixed by calling expiry->timeout while epoll-state is waiting.
* Update REPL support for run-fibers changes.Andy Wingo2017-02-201-11/+26
| | | | * fibers/repl.scm (fibers): Update for new run-fibers changes.
* run-fibers returns when initial fiber finishesAndy Wingo2017-02-192-65/+71
| | | | | | | | | | | | | | | | | | | | | * epoll.c (scm_primitive_epoll_wait): Accept timeout in internal time units instead of milliseconds. Avoid some overhead if the timeout is zero. * fibers/epoll.scm (epoll): Adapt to primitive-epoll-wait chance. Change get-timeout callback to an "expiry" argument and an update-expiry last-minute function. Tighten the window around the "waiting" epoll-state. * fibers.scm (%run-fibers): Remove current-read-waiter / current-write-waiter parameterization, given that fibers individually each have this parameterization. (run-fibers): Add #:drain? argument. * fibers/internal.scm (schedule-runnables-for-next-turn): Simplify a bit, inlining scheduler-poll-timeout and adapting to epoll change. (scheduler-work-pending?): New function. (run-scheduler): Simplify finish? logic. * tests/basic.scm: * tests/speedup.scm: Update to drain where needed. * fibers.texi: Update for new run-fibers termination condition.
* Lower preemption overheadAndy Wingo2017-02-171-7/+9
| | | | | | | * fibers/interrupts.scm (with-interrupts/sigprof): (with-interrupts/thread-cputime, with-interrupts): Separate predicate from interrupt routine, so that predicate can run off-thread. * fibers.scm (%run-fibers): Adapt.
* Prevent spurious preemptionAndy Wingo2017-02-171-2/+14
| | | | | | | * fibers/internal.scm (<scheduler>, make-scheduler): (scheduler-runcount, run-scheduler): Maintain a count of run fibers. * fibers.scm (%run-fibers): Only cause the current fiber to suspend if the runcount isn't advancing.
* Take advantage of accept4 interface.Andy Wingo2017-02-151-2/+2
| | | | | * fibers/web/server.scm (client-loop): Setvbuf here. (socket-loop): Use new accept4 interface.
* Tweak fibers web socket optionsAndy Wingo2017-02-151-7/+4
| | | | | | | * fibers/web/server.scm (client-loop): Move NODELAY things here and actually enable NODELAY (before we were... disabling it???). (socket-loop): Remove SNDBUF munging; let's assume it doesn't matter rather than the reverse.
* Fix epoll-modify logicAndy Wingo2017-02-151-66/+52
| | | | | | | | | | | | | | | | Because this case wasn't being hit before, there were bugs. * fibers/internal.scm (<scheduler>): Remove active fd count; we'll add a #:forever? option to run-fibers instead. Simplify sources. (make-scheduler): Adapt. (schedule-fibers-for-fd): Use resume callbacks, to allow for other kinds of callbacks. (scheduler-poll-timeout): Don't wait for active fd count to go to zero. (finalize-fd): Avoid mucking with hash tables from this potentially-gc callback. (add-fd-event-waiter): New function. (resume-on-fd-events): Call new function.
* Add epoll-add*!Andy Wingo2017-02-151-1/+8
| | | | | * fibers/epoll.scm (epoll-add*!): New function, tries an epoll-modify! then falls back to epoll-add!.
* Fix spurious epoll trafficAndy Wingo2017-02-141-1/+1
| | | | | | | | | | * fibers/internal.scm (schedule-fibers-for-fd): Fix sense of the POLLERR test. This was causing us to register and unregister our fd's with the kernel all the time. This was a scalability bottleneck as adding or removing an fd from an epoll set is serialized in the kernel through one mutex (!!!!), the "epmutex"; because you can nest epoll instances the kernel uses this to prevent cycles. Hint found in Voellmy's paper on the MIO system for Haskell.
* Minor tweak to web serverAndy Wingo2017-02-141-2/+3
| | | | | * fibers/web/server.scm (socket-loop): Move set-nonblocking! call to client where it parallelizes better.
* Gracefully handle EPIPEAndy Wingo2017-02-121-3/+4
| | | | | | * fibers/web/server.scm (client-loop): Avoid error when reading from shut-down socket (EPIPE). Perhaps a read that gives EPIPE should produce an EOF object anyway.
* Add concurrent web serverAndy Wingo2017-02-111-0/+282
| | | | | | | | * fibers.texi (Concurrent Web Server): New section. * fibers/web/server.scm: Add web server that can run handlers concurrently. * examples/concurrent-web-hello.scm: New file. * Makefile.am (SOURCES): Add (fibers web server).
* Add condition variable implementationAndy Wingo2017-02-112-3/+107
| | | | | | | | | * fibers/conditions.scm: * tests/conditions.scm: New files. * Makefile.am: Add new files. * fibers.texi (Conditions): New section. * fibers/timers.scm (sleep-operation): Rename from wait-operation. * tests/foreign.scm: Adapt to sleep-operation change.
* Fix channel CAS logicAndy Wingo2017-02-081-4/+4
| | | | | * fibers/channels.scm (put-operation, get-operation): Fix bogus CAS that could cause us to spin in an allocation loop.
* Better exception handlingAndy Wingo2017-02-071-4/+17
| | | | | | | * fibers/internal.scm (create-fiber): Add a start-stack and a catch-all around the fiber thunk. Note that in practice this needs the recent post-2.1.6 patch in Guile (498f3f95684361f3591106a8f9cb9065fd649288) to work well.
* Better findability for epoll extension.Andy Wingo2017-01-192-1/+33
| | | | | | | * fibers/config.scm.in: New file. * Makefile.am: Wire up new file. * env.in: Define FIBERS_BUILD_DIR. * fibers/epoll.scm: Use (fibers config) to get dir for epoll extension.
* Randomized round-robin work sharing/stealingAndy Wingo2017-01-181-28/+54
| | | | | | | | | | | | | * fibers.scm (start-auxiliary-threads, stop-auxiliary-threads): Adapt to scheduler-remote-peers change. (spawn-fiber): Adapt to use choose-parallel-scheduler. * fibers/internal.scm (<scheduler>): Add choose-parallel-scheduler field. (shuffle, make-selector): New helpers. (make-scheduler): Adapt to initialize choose-parallel-scheduler field. (choose-parallel-scheduler): New public function. (run-scheduler): Use fiber-stealer.
* create-fiber always captures dynamic stateAndy Wingo2017-01-082-11/+11
| | | | | | | * fibers/internal.scm (create-fiber): Remove dynamic-state argument; always capture the current dynamic state. * fibers/timers.scm (timer-operation): * fibers.scm (spawn-fiber): Adapt.
* Peer schedulers share a prompt tagAndy Wingo2017-01-081-15/+17
| | | | | | | | * fibers/internal.scm (make-scheduler): Peer schedulers share a prompt tag. This eliminates TOCTTOU bugs in suspend-current-fiber when computing the suspension prompt tag. (run-scheduler): Inline run-fiber and just use the same prompt tag for all fibers.
* Fix race between with-interrupts and thread startAndy Wingo2017-01-081-4/+10
| | | | * fibers/interrupts.scm (with-interrupts/thread-cputime): Fix race.
* Fix clock-nanosleep wrapperAndy Wingo2017-01-081-9/+7
| | | | | * fibers/posix-clocks.scm (clock-nanosleep): clock_nanosleep returns the error value directly; it doesn't set errno. Fix.
* Add support for operations from foreign threadsAndy Wingo2017-01-052-2/+51
| | | | | | | | | | * tests/foreign.scm: New file. * Makefile.am: Add new file. * fibers/operations.scm (perform-operation): Support blocking operations from foreign threads (without a scheduler). * fibers/timers.scm (timer-sched, *timer-sched*, timer-operation): Add support for timeouts that use an auxiliary thread instead of relying on the current scheduler.
* Remove epoll dep on suspendable portsAndy Wingo2017-01-041-17/+5
| | | | | | | | | * epoll.c (scm_primitive_epoll_wake): New function. (scm_primitive_epoll_wait): If the epoll was woken, drain the read pipe directly. (init_fibers_epoll): Adapt prototypes. * fibers/epoll.scm (epoll-wake!): Use primitive-epoll-wake. (epoll): Let primitive-epoll-wait handle wakeup.
* Centralize operation wrap handlingAndy Wingo2017-01-043-28/+28
| | | | | | | * fibers/operations.scm (<base-op>): Update comment. (perform-operation): Wrap resume proc before calling block function. * fibers/channels.scm (put-operation, get-operation): * fibers/timers.scm (timer-operation): Adapt to pre-wrapped resume proc.
* Prepare for support for blocking operationsAndy Wingo2017-01-044-62/+58
| | | | | | | | | | | | * fibers/operations.scm (perform-operation): Block function takes sched and resume function, not fiber. Will allow blocking operations in the future. * fibers/internal.scm (add-timer): Rename from resume-on-timer; just call a thunk after a time has passed. * fibers/timers.scm (timer-operation): Adapt to resume-on-timer and operation changes. * fibers/channels.scm (put-operation, get-operation): Adapt to operation changes.
* Fix ,spawn-fiberAndy Wingo2016-12-301-1/+1
| | | | | * fibers/repl.scm (spawn-fiber): Use spawn-fiber instead of create-fiber to get right dynamic-state.
* Flesh out remote peers interfaceAndy Wingo2016-12-291-3/+14
| | | | | | | * fibers/internal.scm (<scheduler>): Add remote-peers setter, and expose getter publically. (make-scheduler): Change remote-peers arg to "parallelism", and use setter to initialize remote peers.
* C-c works on threads stuck in epollAndy Wingo2016-12-291-1/+3
| | | | | | | | * epoll.c (scm_primitive_epoll_wait): Add wakefd argument, and use facilities from git guile to ensure that C-c will wake a thread sleeping in epoll. (init_fibers_epoll): Declare additional primitive-epoll-wait arg. * fibers/epoll.scm (epoll): Tell Guile about the wake fd.
* Fix yield-current-fiberAndy Wingo2016-12-231-5/+8
| | | | | | * fibers/internal.scm (yield-current-fiber): Fix to allow yield-current-fiber to be run from an interrupt tick, when it's possible that no fiber is current.
* Enable work stealing in run-schedulerAndy Wingo2016-12-221-37/+53
| | | | | | | | | | | | | | | | * fibers.scm (run-fibers): Store result in an atomic box, given that the initial fiber could migrate. * fibers/internal.scm (<scheduler>): Add remote-peers field. (<fiber>): Add set-fiber-scheduler!. (make-scheduler): Adapt to <scheduler> change. (scheduler-finished?): New helper. (scheduler-poll-timeout): Take a finished? predicate to know when to wait and when to return directly. (schedule-runnables-for-next-turn): Plumb finished? predicate through. (run-scheduler): Avoid returning until finished? is true. Refactor to add work stealing. (steal-fiber!): New helper.
* Get current scheduler from current fiberAndy Wingo2016-12-221-27/+25
| | | | | | | | | | | | | | | | | | | * fibers.scm (run-fibers): Refine #:keep-scheduler? default. (current-fiber-scheduler): Rename from require-current-scheduler, and use the current fiber to get at the scheduler. (spawn-fiber): Let create-fiber handle dynamic state shenanigans, as it needs to ensure that current-fiber is bound in the thunk. * fibers/internal.scm (with-scheduler): Don't parameterize current-scheduler, as that binding for a given scheduler needs to change over time, and the with-dynamic-state prevents this from happening. (schedule-fiber!): Wake schedulers not on the current kernel thread. (run-fiber): Don't parameterize current-fiber; it's lost across the with-dynamic-state. (create-fiber): Instead set current-fiber inside the with-dynamic-state. (suspend-current-fiber, yield-current-fiber): Get current scheduler from current fiber.
* run-scheduler pops one fiber at a timeAndy Wingo2016-12-221-13/+15
| | | | * fibers/internal.scm (run-scheduler): Pop one fiber at a time.
* Rework runqueue as pair of stacksAndy Wingo2016-12-222-23/+92
| | | | | | | | | | * fibers/stack.scm: New file. * Makefile.am: Add new file. * fibers/internal.scm: Rework runqueue to have separate current and next stacks. We want to expose the current runqueue so that remote schedulers can steal work, but we also need to know when we flip the next runqueue to the current runqueue so that we can pull in additional wakeups from epoll and timers.
* Beginnings of work stealingAndy Wingo2016-12-212-4/+26
| | | | | | | | * fibers/deque.scm (update!): Don't spin if value didn't change. (dequeue!): New function. * fibers/internal.scm (schedule-fibers-for-fd): Add comment about stealing and fd's. (steal-work!): New internal function.
* Add REPL commands to manualAndy Wingo2016-12-161-1/+1
|
* Factor interrupt generation to separate facilityAndy Wingo2016-12-162-50/+105
| | | | | | | | | | * fibers/interrupts.scm: New file. * fibers/internal.scm (<scheduler>): Remove Hz field and adapt users. (run-scheduler): Remove preemption; (fibers) handles this now. (yield-current-fiber): New primitive. * fibers.scm (run-fibers): Use interrupts module and wrap run-scheduler in `with-interrupts'. * Makefile.am: Add new file.
* Add posix-clocks.scmAndy Wingo2016-12-161-0/+166
| | | | | * fibers/posix-clocks.scm: New file. * Makefile.am: Add new file.
* Add support for preemptionAndy Wingo2016-12-121-18/+49
| | | | | | | | | | | | | * Makefile.am: Add new test. * TODO.md: Remove stale TODO entries. * fibers.scm (run-fibers): Add #:hz parameter. * fibers.texi (Design): Mention that we support preemption. (Using Fibers): Update for preemption. * fibers/internal.scm (<scheduler>): Add hz field. (make-scheduler): Add #:hz initarg. (maybe-preemptive): New helper. (run-scheduler): Use maybe-preemptive. * tests/preemption.scm: New test.
* Error on attempt to suspend unsuspendable continuationsAndy Wingo2016-12-121-2/+9
| | | | | * fibers/internal.scm (suspend-current-fiber): Error if the fiber is not suspendable.
* Import (ice-9 threads) to avoid deprecation warningsAndy Wingo2016-12-122-0/+2
| | | | | * fibers/internal.scm: * fibers/repl.scm: Explicitly import (ice-9 threads).
* Merge pull request #4 from vyp/typoAndy Wingo2016-10-191-2/+2
|\ | | | | fibers/internal: comment typos