Enumeration

enum CatchupStyle

public enum CatchupStyle {
    | Delay
    | Burst
    | Skip
}

Description: Enumerates the catch-up policies used by a repetitive task timer.

If the execution time of a task is too long, subsequent tasks may be delayed. Different catch-up policies are applicable to different scenarios.

  • The Delay policy is applicable to the scenario where the start time of a task is not concerned and the execution interval between two tasks is fixed.
  • The Burst policy is applicable to the scenario where scheduled tasks need to be executed in sequence.
  • The Skip policy is applicable to the scenario where scheduled tasks do not need to be executed in sequence and intermediate tasks can be ignored.

Example:

In this example, Timer is created. Within the duration of Timer, three tasks will be executed. One task is executed one second after the creation, and the remaining tasks are executed at an interval of one second. Delay is the catch-up policy.

import std.sync.{Timer, sleep, CatchupStyle}
import std.time.{Duration, MonoTime}

main() {
    let start = MonoTime.now()
    Timer.repeatTimes(3, Duration.second, Duration.second, {=>
        println("Tick at: ${MonoTime.now() - start}")
    }, style: Delay)

    sleep(Duration.second * 4)
    0
}

Burst

Burst

Description: When this policy is used, tasks are started at a fixed interval. If the execution time of a task is longer than the preset task starting interval, other tasks missing the point of time are executed in sequence until catch-up.

Delay

Delay

Description: When this policy is used, the interval between the end time of the current task and the start time of the next task is fixed. That is, start time of the next task = end time of the current task + preset task starting interval.

Skip

Skip

Description: When this policy is used, tasks are started at a fixed interval. If the execution time of a task is longer than the preset task starting interval, the following missed points of time are skipped for fast catch-up.

enum MemoryOrder

public enum MemoryOrder {
    | SeqCst
}

Description: Enumerates memory order types.

The memory order refers to the sequence of memory read (load) and write (store) operations. For performance optimization, the compiler or the CPU may reorder instructions. The memory order enumeration is used to indicate a sorting policy.

SeqCst

SeqCst

Description: Indicates a consistent sequence. That is, instruction reordering is disabled to ensure that all atomic operations prior to the atomic operation are completed before the atomic operations subsequent to the atomic operation.

enum ReadWriteMutexMode

public enum ReadWriteMutexMode {
    | Unfair
    | Fair
}

Description: Enumerates the read/write lock fairness modes.

The lock fairness refers to whether a lock is acquired in sequence according to a waiting order when multiple threads are waiting for the same lock.

Fair

Fair

Description: Indicates the fair mode of read/write lock. When a lock is released, the longest-waiting thread acquires the lock first.

Unfair

Unfair

Description: Indicates the unfair mode of read/write lock. When a lock is released, any waiting thread may acquire the lock.