Created
October 21, 2025 15:35
-
-
Save marvhus/934b32c07325ae90a15c40fbd567ef59 to your computer and use it in GitHub Desktop.
Simple library for dealing with a Multi-Core by Default development style.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (C) 2025 marvhus <martin@marvhus.xyz> | |
| // See end of file for extended copyright information. | |
| Lane_Context :: struct { | |
| index, count: int; | |
| barrier: *Barrier; | |
| broadcast_memory: *int; | |
| } | |
| #add_context lane: Lane_Context; | |
| lane_sync :: () { | |
| lock(*context.lane.barrier.lock); | |
| context.lane.barrier.total_thread_count += 1; | |
| local_sense := !context.lane.barrier.flag; | |
| if context.lane.barrier.total_thread_count >= context.lane.barrier.thread_barrier_number { | |
| context.lane.barrier.total_thread_count = 0; | |
| context.lane.barrier.flag = local_sense; | |
| unlock(*context.lane.barrier.lock); | |
| return; | |
| } | |
| unlock(*context.lane.barrier.lock); | |
| while context.lane.barrier.flag != local_sense {} | |
| } | |
| lane_broadcast :: (value: *int, broadcast_source_lane_index: int) { | |
| if (context.lane.index == broadcast_source_lane_index) { | |
| context.lane.broadcast_memory.* = value.*; | |
| } | |
| lane_sync(); | |
| if (context.lane.index != broadcast_source_lane_index) { | |
| value.* = context.lane.broadcast_memory.*; | |
| } | |
| lane_sync(); | |
| } | |
| lane_range :: (total_range: int) -> start: int, end: int { | |
| indexes_per_lane := total_range / context.lane.count; | |
| leftover_indexes_count := total_range - indexes_per_lane * context.lane.count; | |
| leftover_indexes_before_this_lane_count := min(context.lane.index, leftover_indexes_count); | |
| start := indexes_per_lane * context.lane.index + leftover_indexes_before_this_lane_count; | |
| end := start + indexes_per_lane; | |
| if context.lane.index < leftover_indexes_count then end += 1; | |
| return start, end; | |
| } | |
| ////// | |
| Barrier :: struct { | |
| lock: Mutex; | |
| thread_barrier_number: int; | |
| total_thread_count: int; | |
| flag: bool; | |
| } | |
| init :: (barrier: *Barrier, thread_barrier_number: int) { | |
| assert(thread_barrier_number > 0); | |
| init(*barrier.lock); | |
| barrier.thread_barrier_number = thread_barrier_number; | |
| barrier.total_thread_count = 0; | |
| barrier.flag = false; | |
| } | |
| destroy :: (barrier: *Barrier) { | |
| destroy(*barrier.lock); | |
| } | |
| #scope_module; | |
| #import "Basic"; | |
| #import "Thread"; | |
| // Lane.jai - Simple library for dealing with a Multi-Core by Default development style. | |
| // Copyright (C) 2025 marvhus <martin@marvhus.xyz> | |
| // | |
| // This program 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, either version 3 of the License, or | |
| // (at your option) any later version. | |
| // | |
| // This program 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 this program. If not, see <https://www.gnu.org/licenses/>. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment