thread-utils

  1. thread-utils
  2. Documentation
    1. Thread Utilities
      1. Usage
      2. print-exception-error
      3. print-exception-warning
      4. thread-warning-message
      5. thread-timeout?
      6. check-timeout
      7. error-timeout
      8. make-thread-timeout
      9. check-thread
      10. error-thread
      11. thread-created?
      12. thread-ready?
      13. thread-running?
      14. thread-blocked?
      15. thread-suspended?
      16. thread-sleeping?
      17. thread-terminated?
      18. thread-dead?
      19. thread-obstructed?
      20. thread-state=?
      21. thread-blocked-for-termination?
      22. thread-blocked-for-io?
      23. thread-blocked-for-timeout?
      24. thread-unblock!
      25. thread-thunk
      26. thread-result-list
      27. thread-block-timeout
      28. thread-state-buffer
      29. thread-end-exception
      30. thread-owned-mutexes
      31. thread-block-object
      32. thread-recipients
      33. thread-block-object-of-recipient?
      34. thread-dynamic-winds
      35. thread-standard-input
      36. thread-standard-output
      37. thread-standard-error
      38. thread-default-exception-handler
      39. thread-current-parameter-vector
    2. Thread Reaper
      1. Usage
      2. thread-reaper-shutdown?
      3. thread-reap!
      4. zombie-threads
      5. thread-reaper-stop!
      6. thread-reaper-timeout
      7. thread-reaper-timeout-set!
      8. thread-reaper-retries
      9. thread-reaper-retries-set!
      10. thread-reaper-greedy
      11. thread-reaper-greedy-set!
      12. thread-reaper-wait-seconds
      13. thread-reaper-wait-seconds-set!
      14. thread-reaper-quantum
      15. thread-reaper-quantum-set!
    3. SRFI-18 Barrier
      1. Usage
      2. make-barrier
      3. barrier?
      4. check-barrier
      5. error-barrier
      6. barrier-count
      7. barrier-limit
      8. barrier-name
      9. barrier-specific
      10. barrier-specific-set!
      11. barrier-state
      12. barrier-wait
  3. Bugs & Limitations
  4. Notes
  5. Requirements
  6. Author
  7. Repository
  8. Version history
  9. License

Documentation

SRFI 18 related utility routines; some CHICKEN specific.

Thread Utilities

Usage

(import thread-utils)
[procedure] (print-exception-error EXN [THREAD [PORT [HEADER]]])

Prints the error EXN and the current call chain. The message header is supplied by thread-warning-message. This routine duplicates the uncaught exception in a thread handling behavior of Chicken. Useful for caught exceptions that cannot in fact be handled.

The EXN is a condition or any other object. The default for THREAD is the (current-thread). The default for PORT is the (current-error-port). The default for HEADER is the "\n\tThread call history:\n".

[procedure] (print-exception-warning EXN [THREAD [PORT [HEADER]]])

Invokes print-exception-error when warnings are enabled.

The EXN is a condition or any other object. The default for THREAD is the (current-thread). The default for PORT is the (current-error-port). The default for HEADER is the "\n\tThread call history:\n".

thread-warning-message

[procedure] (thread-warning-message THREAD) -> string

Returns the string "Warning (<THREAD>): ".

thread-timeout?

check-timeout

error-timeout

[procedure] (thread-timeout? OBJECT) -> boolean
[procedure] (check-timeout LOCATION OBJECT [ARGUMENT-NAME]) -> (or #f time number)
[procedure] (error-timeout LOCATION OBJECT [ARGUMENT-NAME])

Is the OBJECT a thread-timeout object? A thread-timeout is #f, a number, or a SRFI 18 time object.

make-thread-timeout

[procedure] (make-thread-timeout OFFSET [BASE]) -> (or #f time)

Returns the SRFI 18 time object representing the OFFSET + BASE. The BASE is a thread-timeout object with default (current-time).

Should OFFSET be #f or a time object the BASE is ignored and OFFSET is returned. Assumes an absolute OFFSET indicates a timeout already calculated.

check-thread

error-thread

[procedure] (check-thread LOCATION OBJECT [ARGUMENT-NAME]) -> thread
[procedure] (error-thread LOCATION OBJECT [ARGUMENT-NAME])

Type checking for thread.

thread-created?

thread-ready?

thread-running?

thread-blocked?

thread-suspended?

thread-sleeping?

thread-terminated?

thread-dead?

[procedure] (thread-created? THREAD) -> boolean
[procedure] (thread-ready? THREAD) -> boolean
[procedure] (thread-running? THREAD) -> boolean
[procedure] (thread-blocked? THREAD) -> boolean
[procedure] (thread-suspended? THREAD) -> boolean
[procedure] (thread-sleeping? THREAD) -> boolean
[procedure] (thread-terminated? THREAD) -> boolean
[procedure] (thread-dead? THREAD) -> boolean

Is the THREAD in the indicated state?

thread-obstructed?

[procedure] (thread-obstructed? THREAD) -> boolean

Is the THREAD blocked or sleeping?

thread-state=?

[procedure] (thread-state=? THREAD TOKEN) -> boolean

Does the state token of the THREAD equal the supplied TOKEN.

thread-blocked-for-termination?

[procedure] (thread-blocked-for-termination? THREAD) -> boolean

thread-blocked-for-io?

[procedure] (thread-blocked-for-io? THREAD) -> boolean

thread-blocked-for-timeout?

[procedure] (thread-blocked-for-timeout? THREAD) -> boolean

Is the THREAD in the indicated blocking state?

thread-unblock!

[procedure] (thread-unblock! THREAD)

Attempts to cleanly unblock the THREAD.

Currently only threads blocked for timeout can be unblocked. Threads blocked for any other reason are ignored.

thread-thunk

[procedure] (thread-thunk THREAD) -> procedure

thread-result-list

[procedure] (thread-result-list THREAD) -> (or #f list)

thread-block-timeout

[procedure] (thread-block-timeout THREAD) -> (or #f float)

thread-state-buffer

[procedure] (thread-state-buffer THREAD) -> state-buffer

Returns the THREAD's STATE-BUFFER.

thread-end-exception

[procedure] (thread-end-exception THREAD) -> *

thread-owned-mutexes

[procedure] (thread-owned-mutexes THREAD) -> list

thread-block-object

[procedure] (thread-block-object THREAD) -> *

thread-recipients

[procedure] (thread-recipients THREAD) -> list

thread-block-object-of-recipient?

[procedure] (thread-block-object-of-recipient? THREAD) -> boolean

Is THREAD a recipient of itself?

thread-dynamic-winds

[procedure] (thread-dynamic-winds THREAD) -> list

thread-standard-input

[procedure] (thread-standard-input THREAD) -> port

thread-standard-output

[procedure] (thread-standard-output THREAD) -> port

thread-standard-error

[procedure] (thread-standard-error THREAD) -> port

thread-default-exception-handler

[procedure] (thread-default-exception-handler THREAD) -> procedure

thread-current-parameter-vector

[procedure] (thread-current-parameter-vector THREAD) -> vector

Thread Reaper

Allows a thread to be joined to the primordial thread asynchronously.

Usage

(import thread-reaper)

thread-reaper-shutdown?

[procedure] (thread-reaper-shutdown?) -> boolean

thread-reap!

[procedure] (thread-reap! THREAD)

Queues the supplied THREAD for reaping.

zombie-threads

[procedure] (zombie-threads) -> (list-of thread)

Returns the threads which could not be reaped, by timing out every retry.

thread-reaper-stop!

[procedure] (thread-reaper-stop!)

Processes any threads queued for reaping and terminates the reaper.

thread-reaper-timeout

thread-reaper-timeout-set!

[procedure] (thread-reaper-timeout) -> (or #f number)
[procedure] (thread-reaper-timeout-set! SECONDS)

Gets and sets the number of seconds to wait for a thread to quit. The default is #f or wait till termination. Should threads submitted to the reaper be known to "run off with the cpu" then something other than wait forever is warranted.

thread-reaper-retries

thread-reaper-retries-set!

[procedure] (thread-reaper-retries) -> fixnum
[procedure] (thread-reaper-retries-set! SECONDS)

Gets and sets the number of attempts to wait for a thread to quit.

Default is 1.

thread-reaper-greedy

thread-reaper-greedy-set!

[procedure] (thread-reaper-greedy) -> boolean
[procedure] (thread-reaper-greedy-set! FLAG)

Gets and sets the reaper greedy mode. When greedy the queue is exhausted whenever possible. Otherwise only one queue item is processed per time-slice.

Default is #f.

thread-reaper-wait-seconds

thread-reaper-wait-seconds-set!

[procedure] (thread-reaper-wait-seconds) -> (or #f number)
[procedure] (thread-reaper-wait-seconds-set! SECONDS)

Gets and sets the number of seconds to wait before another attempt by the reaper. The default is 1.0.

thread-reaper-quantum

thread-reaper-quantum-set!

[procedure] (thread-reaper-quantum) -> number
[procedure] (thread-reaper-quantum-set! MILLISECONDS)

Gets and sets the time-slice for the reaper.

Default is 2500.

Is the reaper shutting down?

SRFI-18 Barrier

Thread synchronization object.

Usage

(import (srfi-18 barrier))

make-barrier

[procedure] (make-barrier [LIMIT [NAME]]) -> barrier

Create and return a thread barrier.

LIMIT
fixnum : number of thread waiting before barrier "lifted", default 1.
NAME
* : object name, default #f.

barrier?

check-barrier

error-barrier

[procedure] (barrier? OBJ) --> boolean

Is OBJ a SRFI-18 barrier?

[procedure] (check-barrier LOC OBJ . NAM) --> barrier
[procedure] (error-barrier LOC OBJ . NAM)

barrier-count

[procedure] (barrier-count BAR) -> fixnum

Returns number of threads waiting on barrier BAR.

barrier-limit

[procedure] (barrier-limit BAR) --> fixnum

Returns maximum number of threads waiting on barrier BAR.

barrier-name

[procedure] (barrier-name BAR) --> *

Returns name of barrier BAR.

barrier-specific

[procedure] (barrier-specific BAR) -> *

Returns the specific of barrier BAR.

barrier-specific-set!

[procedure] (barrier-specific-set! BAR OBJ) -> void

Sets the specific of barrier BAR.

barrier-state

[procedure] (barrier-state BAR) -> (or thread symbol)

Returns the state of barrier BAR; see SRFI-18 mutex-state.

barrier-wait

[procedure] (barrier-wait BAR [TIMOUT]) -> boolean

Synchronize calling thread at the barrier BAR. The calling thread will block until the barrier-limit number of threads have called barrier-wait on barrier BAR.

Returns #t, and #f otherwise, to the invoking thread when the barrier is "lifted".

BAR
barrier : barrier to wait "behind."
TIMOUT
(or false real time) : timeout to abandon wait.

Bugs & Limitations

Notes

Requirements

srfi-18 queues synch miscmacros moremacros record-variants check-errors

Author

Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/thread-utils

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Version history

2.2.5
Fix barrier-name.
2.2.4
Fix barrier-specific type.
2.2.3
Fix timeout type; see barrier-wait TIMOUT argument.
2.2.2
Fix barrier-name.
2.2.1
.
2.2.0
Add (srfi-18 barrier).
2.1.2
.
2.1.1
.
2.1.0
Add zombie concept.
2.0.0
C5 port.
1.2.0
Add reaper state setters.
1.1.0
Add thread-... & *thread-... getters. Deprecated thread-blocked?/.... Added thread-blocked-for...?.
1.0.2
"Fix" for ticket #981. Added thread-reaper-wait-seconds.
1.0.1
Use compiled setup-helper (& un-hide).
1.0.0
Moved from srfi-27 into own extension.

License

Copyright (C) 2010-2022 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.