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 (

  # the handler this effect uses to spawn threads
  p Thread_Handler,

  # NYI: UNDER DEVELOPMENT: use more suitable data structure
  public running array concur.Thread,

  # the currently running thread
  public current concur.Thread
  ) : effect
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 spawn(# code to run in the new thread with the teleported effect environemnt
               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
        (threads p (running++[st]).as_array st).replace
        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 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.
  #
  public 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 := p.spawn tid code
    (threads p (running++[st]).as_array current).replace
    st


  # detach a running thread
  #
  public detach(thrd concur.Thread) unit
  pre debug: running.contains thrd
  =>
    # NYI: UNDER DEVELOPMENT: pthread_detach
    (threads p (running.filter (t->t!=thrd)).as_array current).replace


  # join all running threads
  #
  public redef finally unit =>
    running.for_each t->
      t.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)
    concur.threads concur.default_thread_handler [initial] initial


# default thread handler
#
default_thread_handler : Thread_Handler is

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

# Thread_Handler -- abstract source of concurrency
#
private:public Thread_Handler ref is

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

last changed: 2026-06-17