Revision: 15337
Author: oleg.kulikoff
Date: Tue Nov 16 22:05:58 2010
Log: Issue 2108
http://code.google.com/p/mobicents/source/detail?r=15337
Added:
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/Semaphore.java
Modified:
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/HighResTimerImpl.java
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/Scheduler.java
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/TimerImpl.java
=======================================
--- /dev/null
+++
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/Semaphore.java
Tue Nov 16 22:05:58 2010
@@ -0,0 +1,64 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and individual contributors as
indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a full listing
+ * of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License, v. 2.0.
+ * This program is distributed in the hope that it will be useful, but
WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License,
+ * v. 2.0 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+package org.mobicents.media.server.impl.clock;
+
+import org.mobicents.media.server.spi.clock.Timer;
+
+/**
+ *
+ * @author kulikov
+ */
+public class Semaphore {
+
+ // timer instance
+ private Timer timer;
+
+ //precision for time measurement
+ private long precision;
+
+ //time interval around finish time
+ private long low;
+
+ //current time
+ private long now;
+
+ /**
+ * Creates new instance of semaphore.
+ *
+ * @param timer timer which will be used for time measurement.
+ * @param precision the precision for time measurement in microseconds
+ */
+ public Semaphore(Timer timer, long precision) {
+ this.timer = timer;
+ this.precision = precision;
+ }
+
+ /**
+ * Blocks thread for specified amount of time.
+ *
+ * @param amount the amount of time in milliseconds. must be positive.
+ */
+ public void block(long amount) {
+ low = timer.getTimestamp() + amount * 1000000L - precision * 1000L;
+ //poll timer in cycle till required time will be reached
+ while ((now < low)) {
+ now = timer.getTimestamp();
+ }
+ }
+}
=======================================
---
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/HighResTimerImpl.java
Wed Jun 30 09:26:03 2010
+++
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/HighResTimerImpl.java
Tue Nov 16 22:05:58 2010
@@ -38,7 +38,7 @@
* @see org.mobicents.media.server.spi.clock.Timer#getTimestamp().
*/
public long getTimestamp() {
- return p.highResCounter()* 1000/frequency;
- }
-
-}
+ return p.highResCounter() * 1000000000L/frequency;
+ }
+
+}
=======================================
---
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/Scheduler.java
Wed Oct 6 06:44:55 2010
+++
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/Scheduler.java
Tue Nov 16 22:05:58 2010
@@ -34,9 +34,13 @@
private long second;
private Timer timer;
+ private Semaphore semaphore;
public Scheduler() {
timer = new TimerImpl();
+
+ //creating semaphore with 10 microseconds precision
+ semaphore = new Semaphore(timer, 10);
}
public void setTimer(Timer timer) {
@@ -103,7 +107,7 @@
Taskset temp = new Taskset();
//fixing current time when we start execution
- start = timer.getTimestamp();
+ start = timer.getTimestamp()/1000000L;
//running task for selected task set
@@ -181,23 +185,15 @@
}
//checking time when we are finished.
- finish = timer.getTimestamp();
+ finish = timer.getTimestamp()/1000000L;
//calculating time spent for execution all above and
//correcting the amaont of time to wait.
drift = (finish - start);
latency = delay - drift;
- //it makes sense to wait if latency greater then 5 ms
- //but if latency less then of equals to zero then it means
- //that we have to wait infinetely until new task will be
scheduled.
- if (latency >= 5) {
- try {
- Thread.currentThread().sleep(latency);
- } catch (InterruptedException e) {
- return;
- }
- }
+ semaphore.block(latency);
+
time += delay;
if (time >= 1000) {
time -= 1000;
@@ -205,7 +201,21 @@
}
}
+ /**
+ * Returns time in milliseconds.
+ *
+ * @return time in milliseconds.
+ */
public long getTimestamp() {
+ return timer.getTimestamp()/1000000L;
+ }
+
+ /**
+ * Returns time in nanoseconds.
+ *
+ * @return time in nanoseconds.
+ */
+ public long getNanotime() {
return timer.getTimestamp();
}
}
=======================================
---
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/TimerImpl.java
Wed Jun 30 09:26:03 2010
+++
/trunk/servers/media/core/server-impl/src/main/java/org/mobicents/media/server/impl/clock/TimerImpl.java
Tue Nov 16 22:05:58 2010
@@ -31,7 +31,7 @@
* @see org.mobicents.media.server.spi.clock.Timer#getTimestamp().
*/
public long getTimestamp() {
- return System.currentTimeMillis();
+ return System.nanoTime();
}
}