~ubuntu-branches/ubuntu/oneiric/haxe/oneiric

« back to all changes in this revision

Viewing changes to haxe/std/cpp/vm/Thread.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2010-01-31 23:08:43 UTC
  • mfrom: (5.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100131230843-1cxte2x20ypk9c25
Tags: 1:2.5-1
New upstream version, taken from new upstream subversion repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
typedef ThreadHandle = Dynamic;
28
28
 
 
29
#if HXCPP_MULTI_THREADED
29
30
class Thread {
30
31
 
31
32
        var handle : ThreadHandle;
38
39
                Send a message to the thread queue. This message can be readed by using [readMessage].
39
40
        **/
40
41
        public function sendMessage( msg : Dynamic ) {
41
 
                thread_send(handle,msg);
 
42
                untyped __global__.__hxcpp_thread_send(handle,msg);
42
43
        }
43
44
 
44
45
 
46
47
                Returns the current thread.
47
48
        **/
48
49
        public static function current() {
49
 
                return new Thread(thread_current());
 
50
                return new Thread(untyped __global__.__hxcpp_thread_current());
50
51
        }
51
52
 
52
53
        /**
53
54
                Creates a new thread that will execute the [callb] function, then exit.
54
55
        **/
55
56
        public static function create( callb : Void -> Void ) {
56
 
                return new Thread(thread_create(function(_) { callb(); },[]));
 
57
                return new Thread(untyped __global__.__hxcpp_thread_create(callb));
57
58
        }
58
59
 
59
60
        /**
62
63
                returns [null] if no message is available.
63
64
        **/
64
65
        public static function readMessage( block : Bool ) : Dynamic {
65
 
                return thread_read_message(block);
 
66
                return untyped __global__.__hxcpp_thread_read_message(block);
66
67
        }
67
68
 
68
69
        function __compare(t) {
69
70
                return untyped handle == t.handle;
70
71
        }
71
72
 
72
 
        /**
73
 
                Starts an OS message loop after [osInitialize] has been done.
74
 
                In that state, the UI handled by this thread will be updated and
75
 
                [sync] calls can be performed. The loop returns when [exitLoop] is
76
 
                called for this thread.
77
 
        **
78
 
        public static function osLoop() {
79
 
                if( os_loop == null ) throw "Please call osInitialize() first";
80
 
                os_loop();
81
 
        }
82
 
 
83
 
        /**
84
 
                The function [f] will be called by this thread if it's in [osLoop].
85
 
                [sync] returns immediatly. See [osInitialize] remarks.
86
 
        **
87
 
        public function sync( f : Void -> Void ) {
88
 
                os_sync(handle,f);
89
 
        }
90
 
 
91
 
        /**
92
 
                The function [f] will be called by this thread and the calling thread
93
 
                will wait until the result is available then return its value.
94
 
        **
95
 
        public function syncResult<T>( f : Void -> T ) : T {
96
 
                if( this == current() )
97
 
                        return f();
98
 
                var v = new cpp.vm.Lock();
99
 
                var r = null;
100
 
                sync(function() {
101
 
                        r = f();
102
 
                        v.release();
103
 
                });
104
 
                v.wait();
105
 
                return r;
106
 
        }
107
 
 
108
 
        /**
109
 
                Exit from [osLoop].
110
 
        **
111
 
        public function exitLoop() {
112
 
                os_loop_stop(handle);
113
 
        }
114
 
 
115
 
        /**
116
 
                If you want to use the [osLoop], [sync] and [syncResult] methods, you
117
 
                need to call [osInitialize] before creating any thread or calling [current].
118
 
                This will load [os.ndll] library and initialize UI methods for each thread.
119
 
        **
120
 
        public static function osInitialize() {
121
 
                os_loop = cpp.Lib.load("os","os_loop",0);
122
 
                os_loop_stop = cpp.Lib.load("os","os_loop_stop",1);
123
 
                os_sync = cpp.Lib.load("os","os_sync",2);
124
 
        }
125
 
 
126
 
        static var os_loop = null;
127
 
        static var os_loop_stop = null;
128
 
        static var os_sync = null;
129
 
        */
130
 
 
131
 
        static var thread_create = cpp.Lib.load("std","thread_create",2);
132
 
        static var thread_current = cpp.Lib.load("std","thread_current",0);
133
 
        static var thread_send = cpp.Lib.load("std","thread_send",2);
134
 
        static var thread_read_message = cpp.Lib.load("std","thread_read_message",1);
135
 
 
136
73
}
 
74
#else
 
75
You_need_to_define_HXCPP_MULTI_THREADED_to_use_the_Thread_class
 
76
#end