-
Notifications
You must be signed in to change notification settings - Fork 28
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
8 changed files
with
103 additions
and
7 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
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using dotnetCampus.Ipc.Utils.Logging; | ||
|
||
namespace dotnetCampus.Ipc.Threading | ||
{ | ||
/// <summary> | ||
/// 提供给 IPC 框架内部使用的线程池。 | ||
/// </summary> | ||
internal interface IIpcThreadPool | ||
{ | ||
/// <summary> | ||
/// 在线程池挑选一个线程执行指定代码。 | ||
/// 当任务确定已经开始执行之后就会返回第一层 <see cref="Task"/>, | ||
/// 在任务和超时时间先完成者会再次返回第二层 <see cref="Task"/>。 | ||
/// </summary> | ||
/// <param name="action"></param> | ||
/// <param name="logger"></param> | ||
/// <returns></returns> | ||
/// <remarks> | ||
/// <para>特别注意!!!</para> | ||
/// 此方法不是线程安全的,调用方必须确保此方法的调用处于临界区。 | ||
/// </remarks> | ||
Task<Task> Run(Action action, ILogger? logger); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using dotnetCampus.Ipc.Utils.Logging; | ||
|
||
namespace dotnetCampus.Ipc.Threading | ||
{ | ||
/// <summary> | ||
/// 提供给 IPC 框架内部使用的线程池。 | ||
/// 所有的调度都将确定地按顺序执行,一个执行完毕后才会执行下一个。 | ||
/// </summary> | ||
internal class IpcSingleThreadPool : IIpcThreadPool | ||
{ | ||
public async Task<Task> Run(Action action, ILogger? logger) | ||
{ | ||
// 因为此方法的调用方能保证依次执行而不并发,所以这里直接 Task.Run 也不会浪费线程。 | ||
await Task.Run(action).ConfigureAwait(false); | ||
return Task.FromResult(0); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace dotnetCampus.Ipc.Threading | ||
{ | ||
/// <summary> | ||
/// 决定 IPC 消息抵达时,以何种调度方式通知给业务代码。 | ||
/// </summary> | ||
public enum IpcTaskScheduling | ||
{ | ||
/// <summary> | ||
/// 所有的 IPC 共享同一个调度线程池。以大体上按顺序(但不保证)的并发方式通知。 | ||
/// </summary> | ||
GlobalConcurrent, | ||
|
||
/// <summary> | ||
/// 按顺序依次通知。 | ||
/// </summary> | ||
/// <remarks> | ||
/// <para>使用前请避免在业务中创造死锁条件:</para> | ||
/// <list type="bullet"> | ||
/// <item>A 向 B 发送消息,但 B 为了回 A 的这条消息,需要先向 A 请求某种值。</item> | ||
/// </list> | ||
/// <para>在这种情况下,按顺序的发送方式将导致死锁。</para> | ||
/// </remarks> | ||
OneByOne, | ||
} | ||
} |
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