This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
PendingCall.cs
111 lines (86 loc) · 1.86 KB
/
PendingCall.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2007 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details
using System;
using System.Threading;
namespace DBus
{
class PendingCall : IAsyncResult
{
Connection conn;
Message reply = null;
//AutoResetEvent waitHandle = new AutoResetEvent (false);
ManualResetEvent waitHandle;
public PendingCall (Connection conn)
{
this.conn = conn;
}
public Message Reply
{
get {
if (reply != null)
return reply;
if (Thread.CurrentThread == conn.mainThread) {
/*
while (reply == null)
conn.Iterate ();
*/
while (reply == null)
conn.HandleMessage (conn.Transport.ReadMessage ());
completedSync = true;
conn.DispatchSignals ();
} else {
if (waitHandle == null)
Interlocked.CompareExchange (ref waitHandle, new ManualResetEvent (false), null);
// TODO: Possible race condition?
while (reply == null)
waitHandle.WaitOne ();
completedSync = false;
}
return reply;
} set {
if (reply != null)
throw new Exception ("Cannot handle reply more than once");
reply = value;
if (waitHandle != null)
waitHandle.Set ();
if (Completed != null)
Completed (reply);
}
}
public event Action<Message> Completed;
bool completedSync;
public void Cancel ()
{
throw new NotImplementedException ();
}
#region IAsyncResult Members
object IAsyncResult.AsyncState
{
get {
return conn;
}
}
WaitHandle IAsyncResult.AsyncWaitHandle
{
get {
if (waitHandle == null)
waitHandle = new ManualResetEvent (false);
return waitHandle;
}
}
bool IAsyncResult.CompletedSynchronously
{
get {
return reply != null && completedSync;
}
}
bool IAsyncResult.IsCompleted
{
get {
return reply != null;
}
}
#endregion
}
}