view dwtx/dwtxhelper/JThread.d @ 175:9e7e1a8bc813

get a JThread for a thread that was just a Thread.
author Frank Benoit <benoit@tionex.de>
date Fri, 12 Sep 2008 09:44:36 +0200
parents 862b05e0334a
children ed80c5c2b550
line wrap: on
line source

module dwtx.dwtxhelper.JThread;

import tango.core.Thread;
import dwt.dwthelper.utils;

class JThread {

    private Thread thread;
    private Runnable runnable;

    private alias ThreadLocal!(JThread) TTLS;
    private static TTLS tls;

    public static const int MAX_PRIORITY  = 10;
    public static const int MIN_PRIORITY  =  1;
    public static const int NORM_PRIORITY =  5;

    private static TTLS getTls(){
        if( tls is null ){
            synchronized( JThread.classinfo ){
                if( tls is null ){
                    tls = new TTLS();
                }
            }
        }
        return tls;
    }

    public this(){
        thread = new Thread(&internalRun);
    }
    public this( void delegate() dg ){
        thread = new Thread(&internalRun);
        runnable = dgRunnable( dg );
    }
    public this(Runnable runnable){
        thread = new Thread(&internalRun);
        this.runnable = runnable;
    }
    public this(Runnable runnable, String name){
        thread = new Thread(&internalRun);
        this.runnable = runnable;
        thread.name = name;
    }
    public this(String name){
        thread = new Thread(&internalRun);
        thread.name = name;
    }

    public void start(){
        thread.start();
    }

    public static JThread currentThread(){
        auto res = getTls().val();
        if( res is null ){
            synchronized(JThread.classinfo){
                res = getTls().val();
                if( res is null ){
                    res = new JThread();
                    res.thread = Thread.getThis();
                    getTls().val( res );
                }
            }
        }
        return res;
    }
    public int getPriority() {
        return (thread.priority-Thread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) + MIN_PRIORITY;
    }
    public void setPriority( int newPriority ) {
        thread.priority( (newPriority-MIN_PRIORITY) * (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +Thread.PRIORITY_MIN );
    }

    private void internalRun(){
        getTls().val( this );
        if( runnable !is null ){
            runnable.run();
        }
        else {
            run();
        }
    }

    public bool isAlive(){
        return thread.isRunning();
    }

    public bool isDaemon() {
        return thread.isDaemon();
    }

    public void join(){
        thread.join();
    }

    public void setDaemon(bool on) {
        thread.isDaemon(on);
    }

    public void setName(String name){
        thread.name = name;
    }
    public String getName(){
        return thread.name;
    }

    void interrupt() {
        implMissing(__FILE__,__LINE__);
    }

    static bool interrupted() {
        implMissing(__FILE__,__LINE__);
        return false;
    }

    public void run(){
        // default impl, do nothing
    }
    public static void sleep( int time ){
        Thread.sleep(time/1000.0);
    }
    public Thread nativeThread(){
        return thread;
    }
    public override char[] toString(){
        return "JThread "~thread.name;
    }
    public static void yield(){
        Thread.yield();
    }
}