~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to cs/test/Ice/dispatcher/Dispatcher.cs

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// **********************************************************************
 
2
//
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
 
4
//
 
5
// This copy of Ice is licensed to you under the terms described in the
 
6
// ICE_LICENSE file included in this distribution.
 
7
//
 
8
// **********************************************************************
 
9
 
 
10
using System;
 
11
using System.Diagnostics;
 
12
using System.Reflection;
 
13
using System.Collections.Generic;
 
14
using System.Threading;
 
15
 
 
16
public class Dispatcher
 
17
{
 
18
    private static void test(bool b)
 
19
    {
 
20
        if(!b)
 
21
        {
 
22
            throw new System.Exception();
 
23
        }
 
24
    }
 
25
 
 
26
    public Dispatcher()
 
27
    {
 
28
        Debug.Assert(_instance == null);
 
29
        _instance = this;
 
30
        _thread = new Thread(run);
 
31
        _thread.Start();
 
32
    }
 
33
 
 
34
    public void 
 
35
    run()
 
36
    {
 
37
        while(true)
 
38
        {
 
39
            System.Action call = null;
 
40
            lock(this)
 
41
            {
 
42
                if(!_terminated && _calls.Count == 0)
 
43
                {
 
44
                    Monitor.Wait(this);
 
45
                }
 
46
                
 
47
                if(_calls.Count > 0)
 
48
                {
 
49
                    call = _calls.Dequeue();
 
50
                }
 
51
                else if(_terminated)
 
52
                {
 
53
                    // Terminate only once all calls are dispatched.
 
54
                    return;
 
55
                }
 
56
            }
 
57
            
 
58
            if(call != null)
 
59
            {
 
60
                try
 
61
                {
 
62
                    call();
 
63
                }
 
64
                catch(System.Exception)
 
65
                {
 
66
                    // Exceptions should never propagate here.
 
67
                    test(false);
 
68
                }
 
69
            }
 
70
        }
 
71
    }
 
72
    
 
73
    public void
 
74
    dispatch(System.Action call, Ice.Connection con)
 
75
    {
 
76
        lock(this)
 
77
        {
 
78
            _calls.Enqueue(call);
 
79
            if(_calls.Count == 1)
 
80
            {
 
81
                Monitor.Pulse(this);
 
82
            }
 
83
        }
 
84
    }
 
85
 
 
86
    static public void
 
87
    terminate()
 
88
    {
 
89
        lock(_instance)
 
90
        {
 
91
            _instance._terminated = true;
 
92
            Monitor.Pulse(_instance);
 
93
        }
 
94
 
 
95
        _instance._thread.Join();
 
96
    }
 
97
    
 
98
    static public bool
 
99
    isDispatcherThread()
 
100
    {
 
101
        return Thread.CurrentThread == _instance._thread;
 
102
    }
 
103
 
 
104
    static Dispatcher _instance; 
 
105
 
 
106
    private Queue<System.Action> _calls = new Queue<System.Action>();
 
107
    Thread _thread;
 
108
    bool _terminated = false;
 
109
};