# Threads of Diarkis

This page explains the threads used in Diarkis. The threads within the Diarkis Module are created during the initialization of `DiarkisInterfaceBase::DiarkisInit()`. When `IDiarkisTransport::Connect()` is called, threads for UDP/TCP/P2P are created. The number of runtime library threads will increase according to the Diarkis server you connect to.

* Runtime Library
  * When connecting to a UDP server, the following two are created:
    * Send Thread: A thread for sending packets accumulated in the Send Pedding Buffer.
    * Receive Thread: A thread to push events to the Event Scheduler from the packets received by the socket.
  * When connecting to a TCP server, the following one is created:
    * Network Thread: A thread for sending and receiving packets.
  * For P2P connections, the following is created for each connection partner:
    * Holepunch Thread: A temporarily created thread used during hole punching.
* Diarkis Module
  * Runtime Thread: A thread for invoking response/notification events from the Diarkis server.
  * Logger Backend Thread: A thread for buffering and processing logs in Diarkis.

### Thread Sequence Diagram during UDP Connection

{% @mermaid/diagram content="sequenceDiagram
box Application
participant App Thread
participant Runtime Thread
end
box Library
participant Pending Buffer
participant Event Scheduler
participant Send Thread
participant Receive Thread
end
box Diarkis Server
participant Server
end

```
Note over Receive Thread: Waits with the socket<br/>open for up to 100 ms
Note over Send Thread: Waits up to 100 ms<br/>and is activated<br/>when something is pushed<br/>to the Pending Buffer
Note over Runtime Thread: Waits up to 100 ms or<br/>is activated<br/>when something is pushed<br/>to the Event Scheduler

 Activate App Thread
```

App Thread->>Pending Buffer: Push message to Send()/RSend() Pending Buffer
Activate Pending Buffer
loop Send Loop
Pending Buffer->>Send Thread: Wake the thread from wait
Activate Send Thread
Send Thread->>Pending Buffer: Check and retrieve message to continue processing
Deactivate Pending Buffer
Send Thread->>Server: Send
Activate Server
Deactivate Send Thread
end

loop Receive Loop
Server->>Receive Thread: Receive
Deactivate Server
Activate Receive Thread
Receive Thread->>Event Scheduler: Push event to Event Scheduler
Activate Event Scheduler
Deactivate Receive Thread
Event Scheduler->>Runtime Thread: Wake thread from wait
Activate Runtime Thread
end
loop Runtime Loop
Runtime Thread->>Event Scheduler: Check and retrieve event to continue processing
Deactivate Event Scheduler
Runtime Thread->>App Thread: Callback
Deactivate Runtime Thread
end

```
Send Thread->>Pending Buffer: Message check<br/>Does nothing and waits if Pending Buffer is empty.
 Activate Send Thread
Note over Send Thread: At max interval of 100 ms
Deactivate Send Thread
 Activate Receive Thread
Receive Thread->>Receive Thread: Even without receiving messages<br/>check for abnormalities or completion<br/>looping once every max 100 ms
Deactivate Receive Thread

Runtime Thread->>Event Scheduler: Event check<br/>Waits if no event 
 Activate Runtime Thread
Note over Runtime Thread: At max interval of 100 ms
Deactivate Runtime Thread
```

Deactivate App Thread" %}

#### In Case Multiple Diarkis Servers Are Needed

For instance, if you need to prepare UDP servers for both matching and TURN purposes, you need to have two instances of `DiarkisInterfaceBase` for each server. Therefore, four threads will be created in total for the runtime library (two `Send Thread` and two `Receive Thread`).

In this scenario, only one thread each for the Diarkis Module (`Runtime Thread` and `Logger Backend`) is created, resulting in a total of 6: 2 for matching, 2 for TURN, and 2 for Diarkis Module. When making a P2P connection, a temporary thread is created for each connection peer.
