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)
#
# -----------------------------------------------------------------------

# type for a spawned thread
#
module:public thread(thrd Thread) : property.equatable is

  # NYI: CLEANUP: use thread id
  id := unique_id

  joined := concur.atomic trit .new trit.no

  # wait for this thread to finish execution
  #
  public join unit ! atomic_access =>
    if joined.compare_and_set trit.no trit.unknown
      fuzion.sys.thread.join0 thrd
      joined.write trit.yes
    else
      # NYI: UNDER DEVELOPMENT:
      # better busy loop
      while joined.read.is_no_or_unknown

    # NYI: UNDER DEVELOPMENT:
    # should we send message to the thread that spawned this
    # thread that it can remove its list of running threads?


  public set_policy(policy choice
                            concur.scheduling_policy.sched_other
                            concur.scheduling_policy.sched_fifo
                            concur.scheduling_policy.sched_rr,
                    reset_on_fork bool,
                    priority i32) outcome unit
    pre
      safety: 0 ? priority ? 99
  =>
    p :=
      match policy
        concur.scheduling_policy.sched_other => 0
        concur.scheduling_policy.sched_fifo => 1
        concur.scheduling_policy.sched_rr => 2
    p2 :=
      if reset_on_fork
        p | 0x40000000
      else
        p
    e := fuzion.sys.thread.set_policy thrd p2 priority
    if e = 0
      unit
    else
      error "fzE_thread_setschedparam error {e}"


  public set_affinity(core i32) outcome unit
  =>
    e := fuzion.sys.thread.set_affinity thrd core
    if e = 0
      unit
    else
      error "fzE_thread_setaffinity error {e}"


  # equality
  #
  public fixed redef type.equality(a, b thread.this) bool
  =>
    a.id = b.id

last changed: 2026-04-16