wasCSharpSQLite – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * TimerHandler.java --
3 *
4 * The API for defining timer event handler.
5 *
6 * Copyright (c) 1997 Cornell University.
7 * Copyright (c) 1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and
10 * redistribution of this file, and for a DISCLAIMER OF ALL
11 * WARRANTIES.
12 *
13 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
14 *
15 * RCS @(#) $Id: TimerHandler.java,v 1.1.1.1 1998/10/14 21:09:21 cvsadmin Exp $
16 *
17 */
18 using System;
19 namespace tcl.lang
20 {
21  
22 /*
23 * This abstract class is used to define timer handlers.
24 */
25  
26 abstract public class TimerHandler
27 {
28  
29 /*
30 * Back pointer to the notifier that will fire this timer.
31 */
32  
33 internal Notifier notifier;
34  
35 /*
36 * System time at (of after) which the timer should be fired.
37 */
38  
39 internal long atTime;
40  
41 /*
42 * True if the cancel() method has been called.
43 */
44  
45 internal bool isCancelled;
46  
47 /*
48 * Used to distinguish older idle handlers from recently-created ones.
49 */
50  
51 internal int generation;
52  
53 public TimerHandler( Notifier n, int milliseconds )
54 {
55 int i;
56  
57 atTime = ( System.DateTime.Now.Ticks - 621355968000000000 ) / 10000 + milliseconds;
58 notifier = (Notifier)n;
59 isCancelled = false;
60  
61 /*
62 * Add the event to the queue in the correct position (ordered by
63 * event firing time).
64 *
65 * NOTE: it's very important that if two timer handlers have the
66 * same atTime, the newer timer handler always goes after the
67 * older handler in the list. See comments in
68 * Notifier.TimerEvent.processEvent() for details.
69 */
70  
71 lock ( notifier )
72 {
73 generation = notifier.timerGeneration;
74  
75 for ( i = 0; i < notifier.timerList.Count; i++ )
76 {
77 TimerHandler q = (TimerHandler)notifier.timerList[i];
78 if ( atTime < q.atTime )
79 {
80 break;
81 }
82 }
83 notifier.timerList.Insert( i, this );
84  
85 if ( System.Threading.Thread.CurrentThread != notifier.primaryThread )
86 {
87 System.Threading.Monitor.PulseAll( notifier );
88 }
89 }
90 }
91 public void cancel()
92 {
93 lock ( this )
94 {
95 if ( isCancelled )
96 {
97 return;
98 }
99  
100 isCancelled = true;
101  
102 lock ( notifier )
103 {
104 for ( int i = 0; i < notifier.timerList.Count; i++ )
105 {
106 if ( notifier.timerList[i] == this )
107 {
108 notifier.timerList.RemoveAt( i );
109  
110 /*
111 * We can return now because the same timer can be
112 * registered only once in the list of timers.
113 */
114  
115 return;
116 }
117 }
118 }
119 }
120 }
121 internal int invoke()
122 {
123 lock ( this )
124 {
125 /*
126 * The timer may be cancelled after it was put on the
127 * event queue. Check its isCancelled field to make sure it's
128 * not cancelled.
129 */
130  
131 if ( !isCancelled )
132 {
133 processTimerEvent();
134 return 1;
135 }
136 else
137 {
138 return 0;
139 }
140 }
141 }
142 abstract public void processTimerEvent();
143 } // end TimerHandler
144 }