001: package org.jacorb.transaction;
002:
003: /*
004: * JacORB transaction service - a free TS for JacORB
005: *
006: * Copyright (C) 1999-2004 LogicLand group Alex Sinishin.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022: import java.util.*;
023:
024: public class Timer extends Thread {
025:
026: private boolean stoped;
027:
028: class Channel {
029: Sleeper slp;
030: int time;
031:
032: Channel(Sleeper _slp, int _time) {
033: slp = _slp;
034: time = _time;
035: }
036: }
037:
038: private Channel[] channels;
039: private int top;
040:
041: Timer(int max_of_chan) {
042: stoped = true;
043: channels = new Channel[max_of_chan];
044: for (int i = 0; i < channels.length; i++) {
045: channels[i] = null;
046: }
047: top = 0;
048: start();
049: }
050:
051: private int find_free() {
052: for (int i = 0; i < channels.length; i++) {
053: if (channels[i] == null) {
054: return i;
055: }
056: }
057: throw new org.omg.CORBA.INTERNAL();
058: }
059:
060: void pause() {
061: synchronized (channels) {
062: if (!stoped) {
063: stoped = true;
064: }
065: }
066: }
067:
068: void go() {
069: synchronized (channels) {
070: if (stoped) {
071: stoped = false;
072: resume();
073: }
074: }
075: }
076:
077: void add_channel(Sleeper slp, int time) {
078: if (time <= 0) {
079: throw new org.omg.CORBA.INTERNAL();
080: }
081: Channel ch = new Channel(slp, time);
082:
083: synchronized (channels) {
084: int ix = find_free();
085: if (ix > top) {
086: throw new org.omg.CORBA.INTERNAL();
087: }
088: if (ix == top) {
089: top++;
090: }
091: channels[ix] = ch;
092: }
093:
094: go();
095: }
096:
097: private void destroy_channel(int ix) {
098: synchronized (channels) {
099: channels[ix] = null;
100: if ((top - 1) == ix) {
101: top--;
102: }
103: if (top == 0) {
104: stoped = true;
105: }
106: }
107: }
108:
109: void kill_channel(Sleeper slp) {
110: synchronized (channels) {
111: for (int i = 0; i < top; i++) {
112: if (channels[i] != null) {
113: if (channels[i].slp == slp) {
114: destroy_channel(i);
115: return;
116: }
117: }
118: }
119: }
120: }
121:
122: private void count() {
123: int sz = top;
124: for (int i = 0; i < sz; i++) {
125: if (channels[i] != null) {
126: if (channels[i].time != 0) {
127: channels[i].time--;
128: if (channels[i].time == 0) {
129: channels[i].slp.wakeup();
130: }
131: }
132: }
133: }
134: }
135:
136: public void run() {
137: try {
138: for (;;) {
139: if (stoped) {
140: suspend();
141: } else {
142: sleep(1000);
143: count();
144: }
145: }
146: } catch (Throwable e) {
147: e.printStackTrace();
148: System.exit(1);
149: }
150: }
151:
152: }
|