core/thread

thread

Types

12

Pool

Pool :: struct { allocator: mem.Allocator, mutex: sync.Mutex, sem_available: sync.Sema, // the following values are atomic num_waiting: int, num_in_processing: int, num_outstanding: int, num_done: int, // end of atomics // called once per thread at startup thread_init_proc: Thread_Init_Proc, thread_init_data: rawptr, // called once per thread at shutdown thread_fini_proc: Thread_Fini_Proc, thread_fini_data: rawptr, is_running: bool, threads: []^Thread, tasks: queue.Queue(Task), tasks_done: [dynamic]Task, }Source

Do not access the pool's members directly while the pool threads are running, since they use different kinds of locking and mutual exclusion devices. Careless access can and will lead to nasty bugs. Once initialized, the pool's memory address is not allowed to change until it is destroyed.

Thread

Thread :: struct { specific: Thread_Os_Specific, flags: bit_set[Thread_State; u8], // Thread ID. Depending on the platform, may start out as 0 (zero) until the thread // has had a chance to run. id: int, // The thread procedure. procedure: Thread_Proc, // User-supplied pointer, that will be available to the thread once it is // started. Should be set after the thread has been created, but before // it is started. data: rawptr, // User-supplied integer, that will be available to the thread once it is // started. Should be set after the thread has been created, but before // it is started. user_index: int, // User-supplied array of arguments, that will be available to the thread, // once it is started. Should be set after the thread has been created, // but before it is started. user_args: [8]rawptr, // The thread context. // This field can be assigned to directly, after the thread has been // created, but __before__ the thread has been started. This field must // not be changed after the thread has started. // // **Note**: If this field is **not** set, the temp allocator will be managed // automatically. If it is set, the allocators must be handled manually. // // **IMPORTANT**: // By default, the thread proc will get the same context as `main()` gets. // In this situation, the thread will get a new temporary allocator which // will be cleaned up when the thread dies. ***This does NOT happen when // `init_context` field is initialized***. // // If `init_context` is initialized, and `temp_allocator` field is set to // the default temp allocator, then `runtime.default_temp_allocator_destroy()` // procedure needs to be called from the thread procedure, in order to prevent // any memory leaks. init_context: Maybe(runtime.Context), // The allocator used to allocate data for the thread. creation_allocator: mem.Allocator, }Source

Type representing a thread handle and the associated with that thread data.

Thread_Os_Specific

Thread_Os_Specific :: struct { unix_thread: posix.pthread_t, start_ok: sync.Sema, }Source

NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t. Also see core/sys/darwin/mach_darwin.odin/semaphore_t.

Constants

4

Variables

1

Procedures

55

create

create :: proc(procedure: Thread_Proc, priority = Thread_Priority.Normal) -> (^Thread)Source

Create a thread in a suspended state with the given priority.

This procedure creates a thread that will be set to run the procedure specified by procedure parameter with a specified priority. The returned thread will be in a suspended state, until start() procedure is called.

To start the thread, call start(). Also the create_and_start() procedure can be called to create and start the thread immediately.

create_and_start

create_and_start :: proc(fn: proc(), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal, self_cleanup: untyped boolean = false) -> (t: ^Thread)Source

Run a procedure on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

If self_cleanup is specified, after the thread finishes the execution of the fn procedure, the resources associated with the thread are going to be automatically freed.

Do not dereference the ^Thread pointer, if this flag is specified. That includes calling join, which needs to dereference ^Thread`.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

create_and_start_with_data

create_and_start_with_data :: proc(data: rawptr, fn: proc(data: rawptr), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal, self_cleanup: untyped boolean = false) -> (t: ^Thread)Source

Run a procedure with one pointer parameter on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

If self_cleanup is specified, after the thread finishes the execution of the fn procedure, the resources associated with the thread are going to be automatically freed.

Do not dereference the ^Thread pointer, if this flag is specified. That includes calling join, which needs to dereference ^Thread`.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

create_and_start_with_poly_data

create_and_start_with_poly_data :: proc(data: T, fn: proc(data: T), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal, self_cleanup: untyped boolean = false) -> (t: ^Thread)Source

Run a procedure with one polymorphic parameter on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

If self_cleanup is specified, after the thread finishes the execution of the fn procedure, the resources associated with the thread are going to be automatically freed.

Do not dereference the ^Thread pointer, if this flag is specified. That includes calling join, which needs to dereference ^Thread`.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

create_and_start_with_poly_data2

create_and_start_with_poly_data2 :: proc( arg1: T1, arg2: T2, fn: proc(), init_context: Maybe(runtime.Context), priority: _ = Thread_Priority.Normal, self_cleanup: untyped boolean = false, ) -> (t: ^Thread)Source

Run a procedure with two polymorphic parameters on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

If self_cleanup is specified, after the thread finishes the execution of the fn procedure, the resources associated with the thread are going to be automatically freed.

Do not dereference the ^Thread pointer, if this flag is specified. That includes calling join, which needs to dereference ^Thread`.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

create_and_start_with_poly_data3

create_and_start_with_poly_data3 :: proc( arg1: T1, arg2: T2, arg3: T3, fn: proc(arg1: T1, arg2: T2, arg3: T3), init_context: Maybe(runtime.Context), priority: _ = Thread_Priority.Normal, self_cleanup: untyped boolean = false, ) -> (t: ^Thread)Source

Run a procedure with three polymorphic parameters on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

If self_cleanup is specified, after the thread finishes the execution of the fn procedure, the resources associated with the thread are going to be automatically freed.

Do not dereference the ^Thread pointer, if this flag is specified. That includes calling join, which needs to dereference ^Thread`.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

create_and_start_with_poly_data4

create_and_start_with_poly_data4 :: proc( arg1: T1, arg2: T2, arg3: T3, arg4: T4, fn: proc(arg1: T1, arg2: T2, arg3: T3, arg4: T4), init_context: Maybe(runtime.Context), priority: _ = Thread_Priority.Normal, self_cleanup: untyped boolean = false, ) -> (t: ^Thread)Source

Run a procedure with four polymorphic parameters on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

If self_cleanup is specified, after the thread finishes the execution of the fn procedure, the resources associated with the thread are going to be automatically freed.

Do not dereference the ^Thread pointer, if this flag is specified. That includes calling join, which needs to dereference ^Thread`.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

pool_add_task

pool_add_task :: proc(pool: ^Pool, allocator: mem.Allocator, procedure: Task_Proc, data: rawptr, user_index: int)Source

Add a task to the thread pool.

Tasks can be added from any thread, not just the thread that created the thread pool. You can even add tasks from inside other tasks.

Each task also needs an allocator which it either owns, or which is thread safe.

pool_is_empty

pool_is_empty :: proc(pool: ^Pool) -> (bool)Source

If tasks are only being added from one thread, and this procedure is being called from that same thread, it will reliably tell if the thread pool is empty or not. Empty in this case means there are no tasks waiting, being processed, or _done_.

pool_join

pool_join :: proc(pool: ^Pool)Source

Finish tasks that have already started processing, then shut down all pool threads. Might leave over waiting tasks, any memory allocated for the user data of those tasks will not be freed.

pool_num_done

pool_num_done :: proc(pool: ^Pool) -> (int)Source

Number of tasks which are done processing. Only informational, mostly for debugging. Don't rely on this value being consistent with other num_* values.

pool_num_outstanding

pool_num_outstanding :: proc(pool: ^Pool) -> (int)Source

Outstanding tasks are all tasks that are not done, that is, tasks that are waiting, as well as tasks that are currently being processed. Only informational, mostly for debugging. Don't rely on this value being consistent with other num_* values.

pool_shutdown

pool_shutdown :: proc(pool: ^Pool, exit_code: int)Source

Force the pool to stop all of its threads and put it into a state where it will no longer run any more tasks.

The pool must still be destroyed after this.

pool_stop_task

pool_stop_task :: proc(pool: ^Pool, user_index: int, exit_code: int) -> (bool)Source

Forcibly stop a running task by its user index.

This will terminate the underlying thread. Ideally, you should use some means of communication to stop a task, as thread termination may leave resources unclaimed.

The thread will be restarted to accept new tasks.

Returns true if the task was found and terminated.

run

run :: proc(fn: proc(), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal)Source

Run a procedure on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

run_with_data

run_with_data :: proc(data: rawptr, fn: proc(data: rawptr), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal)Source

Run a procedure with one pointer parameter on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

run_with_poly_data

run_with_poly_data :: proc(data: T, fn: proc(data: T), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal)Source

Run a procedure with one polymorphic parameter on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

run_with_poly_data2

run_with_poly_data2 :: proc(arg1: T1, arg2: T2, fn: proc(), init_context: Maybe(runtime.Context), priority = Thread_Priority.Normal)Source

Run a procedure with two polymorphic parameters on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

run_with_poly_data3

run_with_poly_data3 :: proc( arg1: T1, arg2: T2, arg3: T3, fn: proc(arg1: T1, arg2: T2, arg3: T3), init_context: Maybe(runtime.Context), priority: _ = Thread_Priority.Normal, )Source

Run a procedure with three polymorphic parameters on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

run_with_poly_data4

run_with_poly_data4 :: proc( arg1: T1, arg2: T2, arg3: T3, arg4: T4, fn: proc(arg1: T1, arg2: T2, arg3: T3, arg4: T4), init_context: Maybe(runtime.Context), priority: _ = Thread_Priority.Normal, )Source

Run a procedure with four polymorphic parameters on a different thread.

This procedure runs the given procedure on another thread. The context specified by init_context will be used as the context in which fn is going to execute. The thread will have priority specified by the priority parameter.

IMPORTANT: If init_context is specified and the default temporary allocator is used, the thread procedure needs to call runtime.default_temp_allocator_destroy() in order to free the resources associated with the temporary allocations.

yield

yield :: proc()Source

Yield the execution of the current thread to another OS thread or process.

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.