-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIdleTimeoutExecutor.cs
97 lines (89 loc) · 3.21 KB
/
IdleTimeoutExecutor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TSJ.Gemini.Slack
{
/**
* not the cleanest implementation ever of a way to have thread safety around a future action
* whose execution time may change. Thread safety is dependent on proper use of the locking
* around the given mutex from outside the object. Lock around creation, be sure to invalidate references
* on "removed" being called, and otherwise check the "Dead" property before adjusting the timeout
* **/
public class IdleTimeoutExecutor
{
private DateTime _timeout;
//dangerous, always lock in this order
//this is done for thread safety outside of the object
//on the mutex given, yet also on a private mutex to
//use for notification - we don't want to reuse the provided
//mutex for internal notification purposes
private volatile object _mutex;
private volatile object _privateMutex = new object();
public DateTime Timeout
{
get
{
lock (_mutex)
lock (_privateMutex)
{ return _timeout; }
}
set
{
lock (_mutex)
lock (_privateMutex)
{
if (Dead) throw new Exception("IdleTimeoutExecutor already fired");
_timeout = value;
//alert the waiting thread
Monitor.PulseAll(_privateMutex);
}
}
}
public bool Dead
{
get;
private set;
}
public IdleTimeoutExecutor(DateTime timeout, Action onTimeoutExpired, Action cleanup, object mutex = null)
{
DateTime created = DateTime.Now;
if (DateTime.Now > timeout)
{
onTimeoutExpired();
}
else
{
_timeout = timeout;
_mutex = mutex ?? new object();
new Thread(() =>
{
lock (_mutex)
lock (_privateMutex)
{
try
{
DateTime now = DateTime.Now;
while (_timeout > now)
{
//release the lock while we wait for timeout or notification
Monitor.Wait(_privateMutex, _timeout - now);
now = DateTime.Now;
}
try
{
onTimeoutExpired();
}
catch { } //just let it go, nothing we can do
Dead = true;
cleanup();
} catch
{ } //FFS what now!? avoid exception to OS thread.
}
}).Start();
}
}
}
}