/*
 * Copyright 2014-2019 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.agrona.concurrent;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.spi.MidiDeviceProvider;
import java.lang.reflect.Method;

/**
 * Control the supporting of high resolution timers for scheduled events by enabling the {@link MidiDevice}.
 */
public class HighResolutionTimer
{
    private static final MidiDevice MIDI_DEVICE;

    static
    {
        MidiDevice midiDevice = null;
        try
        {
            final Class<?> mediaProviderClass = Class.forName("com.sun.media.sound.MidiOutDeviceProvider");
            final Method getDeviceInfoMethod = mediaProviderClass.getMethod("getDeviceInfo");
            final MidiDeviceProvider provider = (MidiDeviceProvider)mediaProviderClass.getConstructor().newInstance();
            final MidiDevice.Info[] info = (MidiDevice.Info[])getDeviceInfoMethod.invoke(provider);

            midiDevice = info.length > 0 ? provider.getDevice(info[0]) : null;
        }
        catch (final Exception ignore)
        {
        }

        MIDI_DEVICE = midiDevice;
    }

    private static volatile boolean isEnabled = false;

    /**
     * Has the high resolution timer been enabled?
     *
     * @return true if the we believe it is enabled otherwise false.
     */
    public static boolean isEnabled()
    {
        return isEnabled;
    }

    /**
     * Attempt to enable the {@link MidiDevice} which requires the high resolution timer.
     */
    public static synchronized void enable()
    {
        if (null != MIDI_DEVICE && !MIDI_DEVICE.isOpen())
        {
            try
            {
                MIDI_DEVICE.open();
            }
            catch (final MidiUnavailableException ignore)
            {
                isEnabled = false;
                return;
            }

            isEnabled = true;
        }
    }

    /**
     * Attempt to disable the high resolution timer by closing the {@link MidiDevice}.
     */
    public static synchronized void disable()
    {
        if (MIDI_DEVICE != null && MIDI_DEVICE.isOpen())
        {
            MIDI_DEVICE.close();
        }

        isEnabled = false;
    }
}
@@ -629,176 +629,8 @@
 t;%0A%0A
-import javax.sound.midi.MidiDevice;%0Aimport javax.sound.midi.MidiUnavailableException;%0Aimport javax.sound.midi.spi.MidiDeviceProvider;%0Aimport java.lang.reflect.Method;%0A%0A
 /**%0A
@@ -685,63 +685,27 @@
 ers 
-for scheduled events by enabling the %7B@link MidiDevice%7D
+by a bit of hackery
 .%0A *
@@ -764,781 +764,21 @@
 tic 
-final MidiDevice MIDI_DEVICE;%0A%0A    static%0A    %7B%0A        MidiDevice midiDevice = null;%0A        try%0A        %7B%0A            final Class%3C?%3E mediaProviderClass = Class.forName(%22com.sun.media.sound.MidiOutDeviceProvider%22);%0A            final Method getDeviceInfoMethod = mediaProviderClass.getMethod(%22getDeviceInfo%22);%0A            final MidiDeviceProvider provider = (MidiDeviceProvider)mediaProviderClass.getConstructor().newInstance();%0A            final MidiDevice.Info%5B%5D info = (MidiDevice.Info%5B%5D)getDeviceInfoMethod.invoke(provider);%0A%0A            midiDevice = info.length %3E 0 ? provider.getDevice(info%5B0%5D) : null;%0A        %7D%0A        catch (final Exception ignore)%0A        %7B%0A        %7D%0A%0A        MIDI_DEVICE = midiDevice;%0A    %7D%0A%0A    private static volatile boolean isEnabled = false
+Thread thread
 ;%0A%0A 
@@ -978,24 +978,29 @@
  return 
-isEnable
+null != threa
 d;%0A    %7D
@@ -1038,50 +1038,8 @@
 ble 
-the %7B@link MidiDevice%7D which requires the 
 high
@@ -1055,16 +1055,17 @@
 on timer
+s
 .%0A     *
@@ -1141,119 +1141,82 @@
  != 
-MIDI_DEVICE && !MIDI_DEVICE.isOpen())%0A        %7B%0A            try%0A            %7B%0A                MIDI_DEVICE.open(
+thread)%0A        %7B%0A            thread = new Thread(HighResolutionTimer::run
 );%0A 
@@ -1228,69 +1228,33 @@
     
-  
-%7D%0A            catch (final MidiUnavailableException ignor
+  thread.setDaemon(tru
 e)
+;
 %0A   
@@ -1266,112 +1266,80 @@
     
-%7B%0A                isEnabled = false;%0A                return;%0A            %7D%0A%0A            isEnabled = true
+thread.setName(%22high-resolution-timer-hack%22);%0A            thread.start()
 ;%0A  
@@ -1416,42 +1416,9 @@
 imer
- by closing the %7B@link MidiDevice%7D
+s
 .%0A  
@@ -1491,95 +1491,279 @@
 if (
-MIDI_DEVICE != null && MIDI_DEVICE.isOpen())%0A        %7B%0A            MIDI_DEVICE.close();
+null != thread)%0A        %7B%0A            thread.interrupt();%0A            thread = null;%0A        %7D%0A    %7D%0A%0A    private static void run()%0A    %7B%0A        try%0A        %7B%0A            Thread.sleep(Long.MAX_VALUE);%0A        %7D%0A        catch (final InterruptedException ignore)%0A        %7B
 %0A   
@@ -1782,25 +1782,21 @@
     
-isEnabled = false
+thread = null
 ;%0A  

Add high resolution timer hack which does not require midi device.