blob: fbd06625de97d9b2fa6de3dee8209a57d67ba3ed (
plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// **********************************************************************
//
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace IceInternal
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading;
public class AsyncIOThread
{
internal AsyncIOThread(Instance instance)
{
_instance = instance;
_thread = new HelperThread(this);
if(instance.initializationData().properties.getProperty("Ice.ThreadPriority").Length > 0)
{
ThreadPriority priority = IceInternal.Util.stringToThreadPriority(
instance.initializationData().properties.getProperty("Ice.ThreadPriority"));
_thread.Start(priority);
}
else
{
_thread.Start(ThreadPriority.Normal);
}
}
public void queue(ThreadPoolWorkItem callback)
{
_m.Lock();
try
{
Debug.Assert(!_destroyed);
_queue.AddLast(callback);
_m.Notify();
}
finally
{
_m.Unlock();
}
}
public void destroy()
{
_m.Lock();
try
{
Debug.Assert(!_destroyed);
_destroyed = true;
_m.Notify();
}
finally
{
_m.Unlock();
}
}
public void joinWithThread()
{
if(_thread != null)
{
_thread.Join();
}
}
public void run()
{
LinkedList<ThreadPoolWorkItem> queue = new LinkedList<ThreadPoolWorkItem>();
while(true)
{
_m.Lock();
try
{
if(_destroyed && _queue.Count == 0)
{
break;
}
while(!_destroyed && _queue.Count == 0)
{
_m.Wait();
}
LinkedList<ThreadPoolWorkItem> tmp = queue;
queue = _queue;
_queue = tmp;
}
finally
{
_m.Unlock();
}
foreach(ThreadPoolWorkItem cb in queue)
{
try
{
cb();
}
catch(Ice.LocalException ex)
{
string s = "exception in asynchronous IO thread:\n" + ex;
_instance.initializationData().logger.error(s);
}
catch(System.Exception ex)
{
string s = "unknown exception in asynchronous IO thread:\n" + ex;
_instance.initializationData().logger.error(s);
}
}
queue.Clear();
}
}
private Instance _instance;
private bool _destroyed;
private LinkedList<ThreadPoolWorkItem> _queue = new LinkedList<ThreadPoolWorkItem>();
private sealed class HelperThread
{
internal HelperThread(AsyncIOThread asyncIOThread)
{
_asyncIOThread = asyncIOThread;
_name = _asyncIOThread._instance.initializationData().properties.getProperty("Ice.ProgramName");
if(_name.Length > 0)
{
_name += "-";
}
_name += "Ice.AsyncIOThread";
}
public void Join()
{
_thread.Join();
}
public void Start(ThreadPriority priority)
{
_thread = new Thread(new ThreadStart(Run));
_thread.IsBackground = true;
_thread.Name = _name;
_thread.Priority = priority;
_thread.Start();
}
public void Run()
{
_asyncIOThread.run();
}
private AsyncIOThread _asyncIOThread;
private string _name;
private Thread _thread;
}
private HelperThread _thread;
private readonly IceUtilInternal.Monitor _m = new IceUtilInternal.Monitor();
}
}
|