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

concur/thread.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.thread
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# thread -- effect that provides concurrent thread
#
public thread (
  # the handler this effect uses to spawn threads
  p Thread_Handler
  ) : simple_effect
is

  # type for a spawned thread
  #
  private:public spawned(private id i64) is

    # wait for this thread to finish execution
    #
    # NYI it should be made impossible to call this more than once
    #
    public join =>
      fuzion.sys.thread.join0 id
      _ := thread p



  # spawn a new thread
  #
  public spawn(code ()->unit) =>
    st := p.spawn code
    _ := thread p
    st


  type.install_default =>
    (concur.thread default_thread).default


  # default thread handler
  #
  type.default_thread : concur.Thread_Handler is

    # spawn a new thread using given code
    #
    spawn(code ()->unit) => concur.thread.spawned (fuzion.sys.thread.spawn code)


# thread with no argument returns thread.env, i.e., the currently installed
# source of thread concurrency.
#
public thread =>
  thread.install_default
  thread.env


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

  # spawn a new thread using given code
  #
  spawn(code ()->unit) concur.thread.spawned => abstract