Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

concur/Threads.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature concur.Threads
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------


# Threads -- effect that provides concurrent thread
#
# lifecycle: While effect is installed, threads can be spawned.
# When the threads effect is uninstated all threads are joined.
#
public Threads ref : effect is


  # all non-detached child threads
  #
  # NYI: UNDER DEVELOPMENT: Set concur.Thread?
  #
  public children Sequence concur.Thread => abstract


  # the current thread
  #
  public current concur.Thread  => abstract


  # spawn a new thread, teleporting the current effect environment to
  # the new thread.
  #
  # This is to be used in environments where code interacts with data
  # shared with the current thread, e.g., mutable variables that where
  # created with a thread-safe mutate effect.
  #
  public spawn(# code to run in the new thread with the teleported effect environment
               code ()->unit
              ) concur.Thread => abstract


  # spawn a new thread without inheriting the current thread's
  # effect environment, i.e., using a new, empty effect environment.
  #
  # This is intended for use in, e.g., thread pools that are used to perform
  # tasks that may require different effects.
  #
  public spawn_without_inherited_effects(# code to run in the new thread without an new, empty effect environment
                                         code ()->unit
                                        ) concur.Thread
  => abstract


  # detach a child thread
  #
  public detach(thrd concur.Thread) unit
    pre debug: children.contains thrd
  => abstract


  # join all child threads
  #
  public redef finally unit =>
    children.for_each (.join)


  # default implementation of threads
  #
  # this will get instated automatically at startup
  #
  public redef fixed type.default_value option concur.Threads
  =>
    initial := concur.Thread fuzion.sys.thread.current (uid 0)
    default [] initial


universe.default(
  public redef children Sequence concur.Thread,
  public redef current concur.Thread) : concur.Threads is

  # spawn a new thread, teleporting the current effect environment
  # the new thread.
  #
  # This is to be used in environments where code interacts with data
  # shared with the current thread, e.g., mutable variables that where
  # created with a thread-safe mutate effect.
  #
  public redef spawn(# code to run in the new thread with the teleported effect environment
                      code ()->unit
                     ) concur.Thread =>
    ewh := effect_wormhole
    tid := unique_id  # NYI: CLEANUP: use thread id
    spawn_without_inherited_effects tid ()->
      ewh.teleport ()->
        st := concur.Thread fuzion.sys.thread.current  tid
        concur.Threads.replace (default (children++[st]).as_array st)
        code()


  # spawn a new thread without inheriting the current thread's
  # effect environment, i.e., using a new, empty effect environment.
  #
  # This is intended for use in, e.g., thread pools that are used to perform
  # tasks that may require different effects.
  #
  public redef spawn_without_inherited_effects(# code ot run int he new thread without an new, empty effect environment
                                                code ()->unit
                                               ) concur.Thread
  =>
    tid := unique_id  # NYI: CLEANUP: use thread id
    spawn_without_inherited_effects tid code


  # helper for spawn and spawn_without_inherited_effects to spawn a thread
  # with given tid.
  #
  spawn_without_inherited_effects(# NYI: CLEANUP: remove this argument and use system thread id
                                  tid uid,

                                  # code ot run int he new thread without an new, empty effect environment
                                  code ()->unit
                                 ) concur.Thread =>
    st := spawn tid code
    concur.Threads.replace (default (children++[st]).as_array current)
    st


  # spawn a new thread using given code
  #
  spawn(# NYI: CLEANUP: remove this argument and use system thread id
        tid uid,
        code ()->unit)
  =>
    concur.Thread (fuzion.sys.thread.spawn code) tid


  # detach a child thread
  #
  public redef detach(thrd concur.Thread) unit
  =>
    # NYI: UNDER DEVELOPMENT: pthread_detach
    concur.Threads.replace (default (children.filter (!=thrd)).as_array current)

last changed: 2026-07-23