| 1 |
// Copyright 2007 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace NDesk.DBus |
| 9 |
{ |
| 10 |
class PendingCall |
| 11 |
{ |
| 12 |
Connection conn; |
| 13 |
Message reply = null; |
| 14 |
object lockObj = new object (); |
| 15 |
|
| 16 |
public PendingCall (Connection conn) |
| 17 |
{ |
| 18 |
this.conn = conn; |
| 19 |
} |
| 20 |
|
| 21 |
int waiters = 0; |
| 22 |
|
| 23 |
public Message Reply |
| 24 |
{ |
| 25 |
get { |
| 26 |
if (Thread.CurrentThread == conn.mainThread) { |
| 27 |
/* |
| 28 |
while (reply == null) |
| 29 |
conn.Iterate (); |
| 30 |
*/ |
| 31 |
|
| 32 |
while (reply == null) |
| 33 |
conn.HandleMessage (conn.ReadMessage ()); |
| 34 |
|
| 35 |
conn.DispatchSignals (); |
| 36 |
} else { |
| 37 |
lock (lockObj) { |
| 38 |
Interlocked.Increment (ref waiters); |
| 39 |
|
| 40 |
while (reply == null) |
| 41 |
Monitor.Wait (lockObj); |
| 42 |
|
| 43 |
Interlocked.Decrement (ref waiters); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return reply; |
| 48 |
} set { |
| 49 |
lock (lockObj) { |
| 50 |
reply = value; |
| 51 |
|
| 52 |
if (waiters > 0) |
| 53 |
Monitor.PulseAll (lockObj); |
| 54 |
|
| 55 |
if (Completed != null) |
| 56 |
Completed (reply); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public event Action<Message> Completed; |
| 62 |
} |
| 63 |
} |