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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
// TODO:
// DispatcherObject returned by BeginInvoke must allow:
// * Waiting until delegate is invoked.
// See: BeginInvoke documentation for details
//
// Implement the "Invoke" methods, they are currently not working.
//
// Add support for disabling the dispatcher and resuming it.
// Add support for Waiting for new tasks to be pushed, so that we dont busy loop.
// Add support for aborting an operation (emit the hook.operationaborted too)
//
// Very confusing information about Shutdown: it states that shutdown is
// not over, until all events are unwinded, and also states that all events
// are aborted at that point. See 'Dispatcher.InvokeShutdown' docs,
//
// Testing reveals that
// -> InvokeShutdown() stops processing, even if events are available,
// there is no "unwinding" of events, even of higher priority events,
// they are just ignored.
//
// The documentation for the Dispatcher family is poorly written, complete
// sections are cut-and-pasted that add no value and the important pieces
// like (what is a frame) is not on the APIs, but scattered everywhere else
//
// -----------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Miguel de Icaza (miguel@novell.com)
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security;
using System.Threading;
namespace System.Windows.Threading {
[Flags]
internal enum Flags {
ShutdownStarted = 1,
Shutdown = 2,
Disabled = 4
}
public sealed class Dispatcher {
static Dictionary<Thread, Dispatcher> dispatchers = new Dictionary<Thread, Dispatcher> ();
static object olock = new object ();
static DispatcherFrame main_execution_frame = new DispatcherFrame ();
const int TOP_PRIO = (int)DispatcherPriority.Send;
Thread base_thread;
PokableQueue [] priority_queues = new PokableQueue [TOP_PRIO+1];
Flags flags;
int queue_bits;
//
// Used to notify the dispatcher thread that new data is available
//
EventWaitHandle wait;
//
// The hooks for this Dispatcher
//
DispatcherHooks hooks;
//
// The current DispatcherFrame active in a given Dispatcher, we use this to
// keep a linked list of all active frames, so we can "ExitAll" frames when
// requested
DispatcherFrame current_frame;
Dispatcher (Thread t)
{
base_thread = t;
for (int i = 1; i <= (int) DispatcherPriority.Send; i++)
priority_queues [i] = new PokableQueue ();
wait = new EventWaitHandle (false, EventResetMode.AutoReset);
hooks = new DispatcherHooks (this);
}
[EditorBrowsable (EditorBrowsableState.Never)]
public bool CheckAccess ()
{
return Thread.CurrentThread == base_thread;
}
[EditorBrowsable (EditorBrowsableState.Never)]
public void VerifyAccess ()
{
if (Thread.CurrentThread != base_thread)
throw new InvalidOperationException ("Invoked from a different thread");
}
public static void ValidatePriority (DispatcherPriority priority, string parameterName)
{
if (priority < DispatcherPriority.Inactive || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException (parameterName);
}
public DispatcherOperation BeginInvoke (Delegate method, object[] args)
{
throw new NotImplementedException ();
}
public DispatcherOperation BeginInvoke (Delegate method, DispatcherPriority priority, object[] args)
{
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public DispatcherOperation BeginInvoke (DispatcherPriority priority, Delegate method)
{
if (priority < 0 || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException ("priority");
if (priority == DispatcherPriority.Inactive)
throw new ArgumentException ("priority can not be inactive", "priority");
if (method == null)
throw new ArgumentNullException ("method");
DispatcherOperation op = new DispatcherOperation (this, priority, method);
Queue (priority, op);
return op;
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public DispatcherOperation BeginInvoke (DispatcherPriority priority, Delegate method, object arg)
{
if (priority < 0 || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException ("priority");
if (priority == DispatcherPriority.Inactive)
throw new ArgumentException ("priority can not be inactive", "priority");
if (method == null)
throw new ArgumentNullException ("method");
DispatcherOperation op = new DispatcherOperation (this, priority, method, arg);
Queue (priority, op);
return op;
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public DispatcherOperation BeginInvoke (DispatcherPriority priority, Delegate method, object arg, params object [] args)
{
if (priority < 0 || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException ("priority");
if (priority == DispatcherPriority.Inactive)
throw new ArgumentException ("priority can not be inactive", "priority");
if (method == null)
throw new ArgumentNullException ("method");
DispatcherOperation op = new DispatcherOperation (this, priority, method, arg, args);
Queue (priority, op);
return op;
}
public object Invoke (Delegate method, object[] args)
{
throw new NotImplementedException ();
}
public object Invoke (Delegate method, TimeSpan timeout, object[] args)
{
throw new NotImplementedException ();
}
public object Invoke (Delegate method, TimeSpan timeout, DispatcherPriority priority, object[] args)
{
throw new NotImplementedException ();
}
public object Invoke (Delegate method, DispatcherPriority priority, object[] args)
{
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public object Invoke (DispatcherPriority priority, Delegate method)
{
if (priority < 0 || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException ("priority");
if (priority == DispatcherPriority.Inactive)
throw new ArgumentException ("priority can not be inactive", "priority");
if (method == null)
throw new ArgumentNullException ("method");
DispatcherOperation op = new DispatcherOperation (this, priority, method);
Queue (priority, op);
PushFrame (new DispatcherFrame ());
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public object Invoke (DispatcherPriority priority, Delegate method, object arg)
{
if (priority < 0 || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException ("priority");
if (priority == DispatcherPriority.Inactive)
throw new ArgumentException ("priority can not be inactive", "priority");
if (method == null)
throw new ArgumentNullException ("method");
Queue (priority, new DispatcherOperation (this, priority, method, arg));
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public object Invoke (DispatcherPriority priority, Delegate method, object arg, params object [] args)
{
if (priority < 0 || priority > DispatcherPriority.Send)
throw new InvalidEnumArgumentException ("priority");
if (priority == DispatcherPriority.Inactive)
throw new ArgumentException ("priority can not be inactive", "priority");
if (method == null)
throw new ArgumentNullException ("method");
Queue (priority, new DispatcherOperation (this, priority, method, arg, args));
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public object Invoke (DispatcherPriority priority, TimeSpan timeout, Delegate method)
{
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public object Invoke (DispatcherPriority priority, TimeSpan timeout, Delegate method, object arg)
{
throw new NotImplementedException ();
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public object Invoke (DispatcherPriority priority, TimeSpan timeout, Delegate method, object arg, params object [] args)
{
throw new NotImplementedException ();
}
void Queue (DispatcherPriority priority, DispatcherOperation x)
{
int p = ((int) priority);
PokableQueue q = priority_queues [p];
lock (q){
int flag = 1 << p;
q.Enqueue (x);
queue_bits |= flag;
}
hooks.EmitOperationPosted (x);
if (Thread.CurrentThread != base_thread)
wait.Set ();
}
internal void Reprioritize (DispatcherOperation op, DispatcherPriority oldpriority)
{
int oldp = (int) oldpriority;
PokableQueue q = priority_queues [oldp];
lock (q){
q.Remove (op);
}
Queue (op.Priority, op);
hooks.EmitOperationPriorityChanged (op);
}
public static Dispatcher CurrentDispatcher {
get {
lock (olock){
Thread t = Thread.CurrentThread;
Dispatcher dis = FromThread (t);
if (dis != null)
return dis;
dis = new Dispatcher (t);
dispatchers [t] = dis;
return dis;
}
}
}
public static Dispatcher FromThread (Thread thread)
{
Dispatcher dis;
if (dispatchers.TryGetValue (thread, out dis))
return dis;
return null;
}
public Thread Thread {
get {
return base_thread;
}
}
[SecurityCritical]
public static void Run ()
{
PushFrame (main_execution_frame);
}
[SecurityCritical]
public static void PushFrame (DispatcherFrame frame)
{
if (frame == null)
throw new ArgumentNullException ("frame");
Dispatcher dis = CurrentDispatcher;
if (dis.HasShutdownFinished)
throw new InvalidOperationException ("The Dispatcher has shut down");
if (frame.dispatcher != null)
throw new InvalidOperationException ("Frame is already running on a different dispatcher");
if ((dis.flags & Flags.Disabled) != 0)
throw new InvalidOperationException ("Dispatcher processing has been disabled");
frame.ParentFrame = dis.current_frame;
dis.current_frame = frame;
frame.dispatcher = dis;
dis.RunFrame (frame);
}
void PerformShutdown ()
{
EventHandler h;
h = ShutdownStarted;
if (h != null)
h (this, new EventArgs ());
flags |= Flags.Shutdown;
h = ShutdownFinished;
if (h != null)
h (this, new EventArgs ());
priority_queues = null;
wait = null;
}
void RunFrame (DispatcherFrame frame)
{
do {
while (queue_bits != 0){
for (int i = TOP_PRIO; i > 0 && queue_bits != 0; i--){
int current_bit = queue_bits & (1 << i);
if (current_bit != 0){
PokableQueue q = priority_queues [i];
do {
DispatcherOperation task;
lock (q){
task = (DispatcherOperation) q.Dequeue ();
}
task.Invoke ();
//
// call hooks.
//
if (task.Status == DispatcherOperationStatus.Aborted)
hooks.EmitOperationAborted (task);
else
hooks.EmitOperationCompleted (task);
if (!frame.Continue)
return;
if (HasShutdownStarted){
PerformShutdown ();
return;
}
// if we are done with this queue, leave.
lock (q){
if (q.Count == 0){
queue_bits &= ~(1 << i);
break;
}
}
//
// If a higher-priority task comes in, go do that
//
if (current_bit < (queue_bits & ~current_bit))
break;
} while (true);
}
}
}
hooks.EmitInactive ();
wait.WaitOne ();
wait.Reset ();
} while (frame.Continue);
}
[EditorBrowsable (EditorBrowsableState.Advanced)]
public DispatcherHooks Hooks {
[SecurityCritical]
get { throw new NotImplementedException (); }
}
public bool HasShutdownStarted {
get {
return (flags & Flags.ShutdownStarted) != 0;
}
}
public bool HasShutdownFinished {
get {
return (flags & Flags.Shutdown) != 0;
}
}
//
// Do no work here, so that any events are thrown on the owning thread
//
[SecurityCritical]
public void InvokeShutdown ()
{
flags |= Flags.ShutdownStarted;
}
[SecurityCritical]
public void BeginInvokeShutdown (DispatcherPriority priority)
{
throw new NotImplementedException ();
}
[SecurityCritical]
public static void ExitAllFrames ()
{
Dispatcher dis = CurrentDispatcher;
for (DispatcherFrame frame = dis.current_frame; frame != null; frame = frame.ParentFrame){
if (frame.exit_on_request)
frame.Continue = false;
else {
//
// Stop unwinding the frames at the first frame that is
// long running
break;
}
}
}
public DispatcherProcessingDisabled DisableProcessing ()
{
throw new NotImplementedException ();
}
public event EventHandler ShutdownStarted;
public event EventHandler ShutdownFinished;
public event DispatcherUnhandledExceptionEventHandler UnhandledException;
public event DispatcherUnhandledExceptionFilterEventHandler UnhandledExceptionFilter;
}
internal class PokableQueue {
const int initial_capacity = 32;
int size, head, tail;
object [] array;
internal PokableQueue (int capacity)
{
array = new object [capacity];
}
internal PokableQueue () : this (initial_capacity)
{
}
public void Enqueue (object obj)
{
if (size == array.Length)
Grow ();
array[tail] = obj;
tail = (tail+1) % array.Length;
size++;
}
public object Dequeue ()
{
if (size < 1)
throw new InvalidOperationException ();
object result = array[head];
array [head] = null;
head = (head + 1) % array.Length;
size--;
return result;
}
void Grow () {
int newc = array.Length * 2;
object[] new_contents = new object[newc];
array.CopyTo (new_contents, 0);
array = new_contents;
head = 0;
tail = head + size;
}
public int Count {
get {
return size;
}
}
public void Remove (object obj)
{
for (int i = 0; i < size; i++){
if (array [(head+i) % array.Length] == obj){
for (int j = i; j < size-i; j++)
array [(head +j) % array.Length] = array [(head+j+1) % array.Length];
size--;
if (size < 0)
size = array.Length-1;
tail--;
}
}
}
}
}
|