Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading bindings #256

Merged
merged 5 commits into from
Jul 9, 2024
Merged

Threading bindings #256

merged 5 commits into from
Jul 9, 2024

Conversation

0xF6
Copy link
Member

@0xF6 0xF6 commented Jul 9, 2024

Summary by CodeRabbit

  • New Features

    • Introduced advanced thread management functionality, including creating, starting, sleeping, and joining threads.
    • Added job management features, such as starting, completing, canceling, and waiting for job status.
  • Improvements

    • Enhanced debugging in thread execution methods.
    • Added thread and job equality comparison methods for better management.
  • Updates

    • Modified thread and job creation methods to include name parameters for better identification.
    • Updated method signatures for improved parameter handling and performance.

@0xF6 0xF6 added area-vm Area of virtual machine area-corlib Area of core library priority 1 important area-runtime Area of runtime forge area-ffi Area of foreign function interface size: XS extra small task (< 3 days) labels Jul 9, 2024
@0xF6 0xF6 marked this pull request as ready for review July 9, 2024 20:37
Copy link
Contributor

coderabbitai bot commented Jul 9, 2024

Walkthrough

The changes introduce a comprehensive update to threading management in the Ishtar virtual machine's runtime. Enhancements include the addition of several new methods for thread creation, management, and synchronization, along with structural changes to relevant classes and functions. These updates improve thread handling, ensuring better performance and accuracy in multithreaded scenarios.

Changes

File Path Change Summary
runtime/ishtar.vm/__builtin/B_Threading.cs Enhanced thread management: updated methods, signatures, and logic for thread operations.
runtime/ishtar.vm/runtime/io/IshtarRawThread.cs Added name parameter to IshtarRawThread constructor and initialization.
runtime/ishtar.vm/runtime/io/IshtarThread.cs Implemented threading equality, new enums, methods for status tracking, and IshtarJob struct.
runtime/ishtar.vm/runtime/io/IshtarThreading.cs Added field for thread list, updated CreateRawThread to include name parameter, added logic for thread creation/destruction.
runtime/ishtar.vm/runtime/io/TaskScheduler.cs Introduced new enqueue method and updated CreateRawThread call with additional string parameter.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant B_Threading
    participant TaskScheduler
    participant IshtarThreading
    participant IshtarJob

    User->>+B_Threading: createThread(current, args)
    B_Threading->>+TaskScheduler: Execute Task
    TaskScheduler->>+IshtarThreading: CreateRawThread(module, frame, name)
    IshtarThreading->>-TaskScheduler: Return IshtarThread
    TaskScheduler->>-B_Threading: Task Enqueued
    B_Threading->>-User: Thread Created
    
    Note over User,B_Threading: Thread is running
    
    User->>+B_Threading: join(current, args)
    B_Threading->>+IshtarThreading: DestroyThread(thread)
    IshtarThreading->>-B_Threading: Thread Destroyed
    B_Threading->>-User: Thread Joined
Loading

Poem

In threads we weave and spin the tale,
Code once lonely now sets sail.
With create, start, and jobs that blend,
New methods guide to threads' sweet end.
Synchrony and sleep will hold the line,
Now in Ishtar, threads align.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@0xF6
Copy link
Member Author

0xF6 commented Jul 9, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jul 9, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 15

Outside diff range and nitpick comments (4)
runtime/ishtar.vm/runtime/io/IshtarThreading.cs (4)

6-6: Remove unused import.

The using static libuv.LibUV; import is not used in the provided code.

- using static libuv.LibUV;

30-30: Remove debug print statement.

Consider removing or wrapping the debug print statement in a debug flag to avoid unnecessary console output in production.

#if DEBUG
    frame->vm.println($"thread start {threadId}");
#endif

52-54: Remove debug print statements.

Consider removing or wrapping the debug print statements in a debug flag to avoid unnecessary console output in production.

#if DEBUG
    Thread.CurrentThread.Name = $"Thread [{threadData->callFrame->method->Name}]";
#endif

81-81: Remove debug print statements.

Consider removing or wrapping the debug print statements in a debug flag to avoid unnecessary console output in production.

#if DEBUG
    Thread.CurrentThread.Name = StringStorage.GetStringUnsafe(thread->Name);
#endif
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e7260e1 and 5c2ffa0.

Files selected for processing (5)
  • runtime/ishtar.vm/__builtin/B_Threading.cs (1 hunks)
  • runtime/ishtar.vm/runtime/io/IshtarRawThread.cs (1 hunks)
  • runtime/ishtar.vm/runtime/io/IshtarThread.cs (3 hunks)
  • runtime/ishtar.vm/runtime/io/IshtarThreading.cs (2 hunks)
  • runtime/ishtar.vm/runtime/io/TaskScheduler.cs (2 hunks)
Additional context used
GitHub Check: Codacy Static Code Analysis
runtime/ishtar.vm/runtime/io/IshtarThread.cs

[warning] 67-67: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L67
Implement 'IEquatable' in value type 'IshtarJobContext'.


[warning] 77-77: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L77
Implement 'IEquatable' in value type 'IshtarJob'.

Additional comments not posted (2)
runtime/ishtar.vm/runtime/io/IshtarRawThread.cs (1)

15-15: LGTM!

The addition of the Name field is appropriate and aligns with the changes in the constructor.

runtime/ishtar.vm/runtime/io/TaskScheduler.cs (1)

119-119: LGTM! Thread naming is a good practice.

The CreateRawThread call now includes a string name parameter, which is beneficial for debugging and monitoring.

@@ -6,9 +6,11 @@ namespace ishtar.io;
public readonly unsafe struct IshtarRawThread(
LibUV.uv_thread_t threadId,
delegate*<IshtarRawThread*, void> frame,
RuntimeIshtarModule* mainModule)
RuntimeIshtarModule* mainModule,
InternedString* name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add null check for the name parameter.

To ensure robustness, consider adding a null check for the name parameter in the constructor.

if (name == null)
    throw new ArgumentNullException(nameof(name));

{
public IshtarRawThread* CreateRawThread(RuntimeIshtarModule* mainModule, delegate*<IshtarRawThread*, void> frame)
public NativeList<IshtarThread>* threads = IshtarGC.AllocateList<IshtarThread>(vm.@ref);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider lazy initialization for threads field.

Lazy initialization can improve performance by delaying the creation of the threads list until it is actually needed.

public NativeList<IshtarThread>* threads;
public IshtarThreading(VirtualMachine vm)
{
    this.vm = vm;
    threads = null;
}

public NativeList<IshtarThread>* GetThreads()
{
    if (threads == null)
        threads = IshtarGC.AllocateList<IshtarThread>(vm.@ref);
    return threads;
}

LibUV.uv_thread_create(out var threadId, executeRaw, (IntPtr)thread);
*thread = new IshtarRawThread(threadId, frame, mainModule);
uv_thread_create(out var threadId, executeRaw, (IntPtr)thread);
*thread = new IshtarRawThread(threadId, frame, mainModule, StringStorage.Intern(name, mainModule));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle uv_thread_create error.

Check the return value of uv_thread_create and handle any errors appropriately.

var result = uv_thread_create(out var threadId, executeRaw, (IntPtr)thread);
if (result != 0)
    throw new InvalidOperationException($"Failed to create thread: {result}");


uv_thread_create(out var threadId, execute, (IntPtr)thread);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle uv_thread_create error.

Check the return value of uv_thread_create and handle any errors appropriately.

var result = uv_thread_create(out var threadId, execute, (IntPtr)thread);
if (result != 0)
    throw new InvalidOperationException($"Failed to create thread: {result}");

return thread;
}

public static void DestroyThread(IshtarThread* thread)
{
thread->callFrame->vm.println($"[thread] exit thread with status {thread->ctx->Status} {thread->threadId}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle uv_sem_destroy error.

Check the return value of uv_sem_destroy and handle any errors appropriately.

var result = uv_sem_destroy(ref thread->ctx->Locker);
if (result != 0)
    throw new InvalidOperationException($"Failed to destroy semaphore: {result}");

Comment on lines +61 to +62
var thread = getThread(current, args);
thread->join();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential null values in join.

Add a null check for thread before calling join.

if (thread == null)
    throw new InvalidOperationException("Thread is null.");

thread->join();

Comment on lines +68 to +70
var thread = getThread(current, args);

thread->start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential null values in start.

Add a null check for thread before calling start.

if (thread == null)
    throw new InvalidOperationException("Thread is null.");

thread->start();

Comment on lines +28 to +30
var err = uv_thread_join(threadId);
VirtualMachine.Assert(err == 0, WNE.THREAD_STATE_CORRUPTED,
$"uv_thread_join returned error status, code: {err}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle uv_thread_join error.

Check the return value of uv_thread_join and handle any errors appropriately.

var err = uv_thread_join(threadId);
if (err != 0)
    throw new InvalidOperationException($"uv_thread_join returned error status, code: {err}");

Comment on lines +59 to +65
{
CREATED,
RUNNING,
PAUSED,
CANCELED,
EXITED
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement IEquatable<T> for IshtarJobContext.

Implementing IEquatable<T> can improve performance and ensure consistent equality checks.

public struct IshtarJobContext : IEquatable<IshtarJobContext>
{
    // Existing fields...

    public bool Equals(IshtarJobContext other)
    {
        return ThreadId.Equals(other.ThreadId) && Locker.Equals(other.Locker);
    }

    public override bool Equals(object obj)
    {
        return obj is IshtarJobContext other && Equals(other);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(ThreadId, Locker);
    }
}

Comment on lines +77 to +116
public readonly unsafe struct IshtarJob(uv_work_t* workerId, CallFrame* frame, IshtarJobContext* ctx) : IEq<IshtarJob>
{
public readonly IshtarJobContext* ctx = ctx;
public readonly uv_work_t* workerId = workerId;
public readonly CallFrame* callFrame = frame;

public void start()
{
VirtualMachine.Assert(ctx->Status == IshtarJobStatus.CREATED, WNE.THREAD_STATE_CORRUPTED,
"trying start worker with out of CREATED status");
ctx->Status = IshtarJobStatus.RUNNING;
uv_sem_post(ref ctx->Locker);
}

public void complete()
=> ctx->Status = IshtarJobStatus.EXITED;

public void cancel()
{
ctx->Status = IshtarJobStatus.CANCELED;
var err = uv_cancel(workerId);
VirtualMachine.Assert(err == 0, WNE.THREAD_STATE_CORRUPTED,
$"uv_cancel work_t returned error status, code: {err}");
}

public void wait_until_complete_or_cancel()
{
begin:
if (ctx->Status is IshtarJobStatus.EXITED)
return;
if (ctx->Status is IshtarJobStatus.CANCELED)
return;

uv_sleep(1);
goto begin;
}


public static bool Eq(IshtarJob* p1, IshtarJob* p2) => p1->workerId->handle.Equals(p2->workerId->handle);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement IEquatable<T> for IshtarJob.

Implementing IEquatable<T> can improve performance and ensure consistent equality checks.

public readonly unsafe struct IshtarJob : IEquatable<IshtarJob>
{
    // Existing fields...

    public bool Equals(IshtarJob other)
    {
        return workerId->handle.Equals(other.workerId->handle);
    }

    public override bool Equals(object obj)
    {
        return obj is IshtarJob other && Equals(other);
    }

    public override int GetHashCode()
    {
        return workerId->handle.GetHashCode();
    }
}
Tools
GitHub Check: Codacy Static Code Analysis

[warning] 77-77: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L77
Implement 'IEquatable' in value type 'IshtarJob'.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e7260e1 and 5c2ffa0.

Files selected for processing (5)
  • runtime/ishtar.vm/__builtin/B_Threading.cs (1 hunks)
  • runtime/ishtar.vm/runtime/io/IshtarRawThread.cs (1 hunks)
  • runtime/ishtar.vm/runtime/io/IshtarThread.cs (3 hunks)
  • runtime/ishtar.vm/runtime/io/IshtarThreading.cs (2 hunks)
  • runtime/ishtar.vm/runtime/io/TaskScheduler.cs (2 hunks)
Additional context used
GitHub Check: Codacy Static Code Analysis
runtime/ishtar.vm/runtime/io/IshtarThread.cs

[warning] 67-67: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L67
Implement 'IEquatable' in value type 'IshtarJobContext'.


[warning] 77-77: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L77
Implement 'IEquatable' in value type 'IshtarJob'.

Additional comments not posted (23)
runtime/ishtar.vm/runtime/io/IshtarRawThread.cs (1)

9-10: LGTM!

The inclusion of the name parameter and the Name field in the IshtarRawThread struct is appropriate and aligns with the threading enhancements.

Also applies to: 15-15

runtime/ishtar.vm/runtime/io/IshtarThreading.cs (6)

10-10: LGTM!

The addition of the threads field for managing thread lists in the IshtarThreading struct is appropriate and aligns with the threading enhancements.


12-16: LGTM!

The inclusion of the name parameter in the CreateRawThread method is appropriate and aligns with the threading enhancements.


18-33: LGTM!

The CreateThread method is appropriate and aligns with the threading enhancements.


37-43: LGTM!

The DestroyThread method is appropriate and aligns with the threading enhancements.


Line range hint 44-72:
LGTM!

The execute method is appropriate and aligns with the threading enhancements.


73-83: LGTM!

The executeRaw method is appropriate and aligns with the threading enhancements.

runtime/ishtar.vm/__builtin/B_Threading.cs (6)

9-15: LGTM!

The getThreadClass method is appropriate and aligns with the threading enhancements.


17-43: LGTM!

The updates to the createThread method are appropriate and align with the threading enhancements.


45-51: LGTM!

The sleep method is appropriate and aligns with the threading enhancements.


52-58: LGTM!

The getThread method is appropriate and aligns with the threading enhancements.


59-65: LGTM!

The join method is appropriate and aligns with the threading enhancements.


66-72: LGTM!

The start method is appropriate and aligns with the threading enhancements.

runtime/ishtar.vm/runtime/io/IshtarThread.cs (9)

Line range hint 9-33:
LGTM!

The modifications to the IshtarThread struct, including the implementation of the IEq interface and the addition of the complete, join, and start methods, are appropriate and align with the threading enhancements.


Line range hint 36-43:
LGTM!

The addition of the IshtarThreadContext struct is appropriate and aligns with the threading enhancements.


48-56: LGTM!

The addition of the IshtarThreadStatus enum is appropriate and aligns with the threading enhancements.


58-65: LGTM!

The addition of the IshtarJobStatus enum is appropriate and aligns with the threading enhancements.


67-75: LGTM!

The addition of the IshtarJobContext struct is appropriate and aligns with the threading enhancements.

Tools
GitHub Check: Codacy Static Code Analysis

[warning] 67-67: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L67
Implement 'IEquatable' in value type 'IshtarJobContext'.


77-116: LGTM!

The addition of the IshtarJob struct and the implementation of the IEq interface are appropriate and align with the threading enhancements.

Tools
GitHub Check: Codacy Static Code Analysis

[warning] 77-77: runtime/ishtar.vm/runtime/io/IshtarThread.cs#L77
Implement 'IEquatable' in value type 'IshtarJob'.


83-89: LGTM!

The start method in the IshtarJob struct is appropriate and aligns with the threading enhancements.


91-93: LGTM!

The complete method in the IshtarJob struct is appropriate and aligns with the threading enhancements.


94-112: LGTM!

The cancel and wait_until_complete_or_cancel methods in the IshtarJob struct are appropriate and align with the threading enhancements.

runtime/ishtar.vm/runtime/io/TaskScheduler.cs (1)

119-119: LGTM! The addition of thread naming improves debugging.

The inclusion of the string parameter for thread naming in the CreateRawThread call enhances readability and debugging capabilities.

Comment on lines +40 to +44
public static void enqueue(CallFrame* method)
{
//uv_work_t t;
//uv_queue_work((nint)async_header, ref t)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete method: enqueue

The enqueue method is currently incomplete and contains commented-out code. Please provide the intended functionality or remove the method if it is not needed.

@0xF6 0xF6 merged commit 142c2ad into master Jul 9, 2024
11 of 15 checks passed
@0xF6 0xF6 deleted the feature/threading-bindings branch July 9, 2024 20:45
@vein-lang vein-lang locked and limited conversation to collaborators Jul 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-corlib Area of core library area-ffi Area of foreign function interface area-runtime Area of runtime forge area-vm Area of virtual machine priority 1 important size: XS extra small task (< 3 days)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant