• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DemoBytom / DemoEngine / 13838022014

13 Mar 2025 03:10PM UTC coverage: 9.384% (-0.07%) from 9.454%
13838022014

push

coveralls.net

DemoBytom
Adding cached empty task completion source

DoEvents request will be invoked many times, and each time reset was called it would create new task completion source, so added a way to have it be resettable to an empty/completed state without extra allocations

227 of 2419 relevant lines covered (9.38%)

16013.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Demo.Engine.Core/Features/StaThread/StaThreadRequests.cs
1
// Copyright © Michał Dembski and contributors.
2
// Distributed under MIT license. See LICENSE file in the root for more information.
3

4
using Demo.Engine.Core.Interfaces.Platform;
5
using Demo.Engine.Core.Interfaces.Rendering;
6
using Demo.Engine.Core.ValueObjects;
7
using Microsoft.Extensions.ObjectPool;
8

9
namespace Demo.Engine.Core.Features.StaThread;
10

11
internal abstract record StaThreadRequests
12
{
13
    public static DoEventsOkRequest DoEventsOk(
14
        RenderingSurfaceId renderingSurfaceId)
15
        => new(renderingSurfaceId);
×
16

17
    public static CreateSurfaceRequest CreateSurface()
18
        => new();
×
19

20
    public abstract bool Invoke(
21
            IRenderingEngine renderingEngine,
22
            IOSMessageHandler osMessageHandler);
23

24
    internal abstract record StaThreadWorkInner<TResult>
25
        : StaThreadRequests
26
    {
27
        private static TaskCompletionSource<TResult> Empty { get; }
×
28
        private TaskCompletionSource<TResult> _tcs;
29
        private CancellationTokenRegistration? _tcsRegistration;
30

31
        static StaThreadWorkInner()
32
        {
33
            Empty = new();
×
34
            Empty.SetResult(default!);
×
35
        }
×
36

37
        protected StaThreadWorkInner(
×
38
            bool startCompleted = false)
×
39
            => _tcs = startCompleted
×
40
                ? Empty
×
41
                : new(TaskCreationOptions.RunContinuationsAsynchronously);
×
42

43
        public Task<TResult> Invoked
44
            => _tcs.Task;
×
45

46
        protected abstract TResult InvokeFuncInternal(
47
            IRenderingEngine renderingEngine,
48
            IOSMessageHandler osMessageHandler);
49

50
        public override bool Invoke(
51
            IRenderingEngine renderingEngine,
52
            IOSMessageHandler osMessageHandler)
53
        {
54
            if (Invoked.IsCompleted)
×
55
            {
56
                return Invoked.IsCompletedSuccessfully;
×
57
            }
58

59
            try
60
            {
61
                var returnValue = InvokeFuncInternal(renderingEngine, osMessageHandler);
×
62
                _tcs.SetResult(returnValue);
×
63
                _ = _tcsRegistration?.Unregister();
×
64
                return true;
×
65
            }
66
            catch (Exception ex)
×
67
            {
68
                _ = _tcsRegistration?.Unregister();
×
69
                _tcs.SetException(ex);
×
70
            }
×
71
            return false;
×
72
        }
×
73

74
        protected void Reset(
75
            CancellationToken cancellationToken)
76
        {
77
            if (!_tcs.Task.IsCompleted)
×
78
            {
79
                _ = _tcs.TrySetException(
×
80
                    new TaskCanceledException("Message reset!"));
×
81
            }
82
            _ = _tcsRegistration?.Unregister();
×
83
            _tcsRegistration = null;
×
84
            _tcs = new TaskCompletionSource<TResult>(
×
85
                TaskCreationOptions.RunContinuationsAsynchronously);
×
86
            _tcsRegistration = cancellationToken.Register(()
×
87
                => _tcs.SetCanceled());
×
88
        }
×
89

90
        protected void ResetCompleted()
91
        {
92
            if (!_tcs.Task.IsCompleted)
×
93
            {
94
                _ = _tcs.TrySetException(
×
95
                    new TaskCanceledException("Message reset!"));
×
96
            }
97
            _ = _tcsRegistration?.Unregister();
×
98
            _tcsRegistration = null;
×
99
            _tcs = Empty;
×
100
        }
×
101
    }
102
    internal sealed record CreateSurfaceRequest
103
        : StaThreadWorkInner<RenderingSurfaceId>
104
    {
105
        protected override RenderingSurfaceId InvokeFuncInternal(
106
            IRenderingEngine renderingEngine,
107
            IOSMessageHandler osMessageHandler)
108
            => renderingEngine.CreateSurface();
×
109
    }
110

111
    internal sealed record DoEventsOkRequest
112
        : StaThreadWorkInner<bool>,
113
          IResettable
114
    {
115
        private RenderingSurfaceId _renderingSurfaceId;
116

117
        public DoEventsOkRequest()
118
            : this(RenderingSurfaceId.Empty)
×
119
        {
120

121
        }
×
122

123
        internal DoEventsOkRequest(
124
            RenderingSurfaceId renderingSurfaceId)
125
            : base(false)
×
126
            => _renderingSurfaceId = renderingSurfaceId;
×
127

128
        protected override bool InvokeFuncInternal(
129
            IRenderingEngine renderingEngine,
130
            IOSMessageHandler osMessageHandler)
131
            => renderingEngine.TryGetRenderingSurface(
×
132
                _renderingSurfaceId,
×
133
                out var renderingSurface)
×
134
            ? osMessageHandler.DoEvents(
×
135
                renderingSurface.RenderingControl)
×
136
            : throw new InvalidOperationException("No RenderingSurface provided!");
×
137

138
        public void Reset(
139
            RenderingSurfaceId renderingSurfaceId,
140
            CancellationToken cancellationToken)
141
        {
142
            if (renderingSurfaceId == RenderingSurfaceId.Empty)
×
143
            {
144
                ResetCompleted();
×
145
            }
146
            else
147
            {
148
                Reset(cancellationToken);
×
149
            }
150
            _renderingSurfaceId = renderingSurfaceId;
×
151
        }
×
152

153
        public bool TryReset()
154
        {
155
            if (Invoked.IsCompleted == false)
×
156
            {
157
                return false;
×
158
            }
159

160
            Reset(RenderingSurfaceId.Empty, CancellationToken.None);
×
161

162
            return true;
×
163
        }
164
    }
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc