-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,69 @@ | ||
const std = @import("std"); | ||
const sustenet = @import("root").sustenet; | ||
|
||
const RwLock = std.Thread.RwLock; | ||
const ArrayList = std.ArrayList; | ||
const Action = sustenet.utils.Extensions.Action; | ||
|
||
pub const ThreadManager = @This(); | ||
const ThreadManager = @This(); | ||
var instance: ?ThreadManager = null; | ||
|
||
mainPool: ArrayList(Action), | ||
mainPoolCopied: ArrayList(Action), | ||
executeAction: bool = false, | ||
main_pool_lock: RwLock = .{}, | ||
main_pool: ArrayList(Action), | ||
main_pool_copied: ArrayList(Action), | ||
execute_action: bool = false, | ||
|
||
/// Get the singleton instance. | ||
pub fn getInstance(allocator: std.mem.Allocator) !ThreadManager { | ||
if (instance == null) { | ||
instance = ThreadManager{ | ||
.main_pool = ArrayList(Action).init(allocator), | ||
.main_pool_copied = ArrayList(Action).init(allocator), | ||
.execute_action = false, | ||
}; | ||
} | ||
|
||
return instance.?; | ||
} | ||
|
||
//#region Execution Functions | ||
/// Sets an action to be executed on the main thread. | ||
pub fn executeOnMainThread( | ||
self: *ThreadManager, | ||
/// The action to be executed on the main thread. | ||
action: Action, | ||
) void { | ||
action.invoke(); | ||
self.main_pool_lock.lock(); | ||
defer self.main_pool_lock.unlock(); | ||
|
||
self.main_pool.append(action) catch unreachable; | ||
self.execute_action = true; | ||
} | ||
|
||
/// Execute all code meant to run on the main thread. Should only be called from the main thread. | ||
pub fn updateMain(self: ThreadManager) void { | ||
if (self.executeAction) { | ||
self.mainPoolCopied.clearAndFree(); | ||
// RWLock mainPool | ||
pub fn updateMain(self: *ThreadManager) void { | ||
if (self.execute_action) { | ||
self.main_pool_lock.lock(); | ||
defer self.main_pool_lock.unlock(); | ||
|
||
self.main_pool_copied.clearAndFree(); | ||
{ | ||
self.mainPoolCopied.appendSlice(self.mainPool); | ||
self.mainPool.clear(); | ||
self.executeAction = false; | ||
const main_pool_slice = self.main_pool.toOwnedSlice() catch unreachable; | ||
self.main_pool_copied.appendSlice(main_pool_slice) catch unreachable; | ||
self.execute_action = false; | ||
} | ||
|
||
for (self.mainPoolCopied.items()) |action| { | ||
for (self.main_pool_copied.items) |action| { | ||
action.invoke(); | ||
} | ||
} | ||
} | ||
//#endregion | ||
|
||
//#region Memory Functions | ||
pub fn deinit(self: *ThreadManager) void { | ||
self.main_pool.deinit(); | ||
self.main_pool_copied.deinit(); | ||
instance = null; | ||
} | ||
//#endregion |
This file contains 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
This file contains 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