@@ -21,6 +21,9 @@ export task_result;
21
21
export tr_success;
22
22
export tr_failure;
23
23
export get_task_id;
24
+ export spawn2;
25
+ export spawn_notify2;
26
+ export spawn_joinable2;
24
27
25
28
native "rust" mod rustrt {
26
29
fn task_sleep ( time_in_us : uint ) ;
@@ -93,6 +96,47 @@ fn unpin() { rustrt::unpin_task(); }
93
96
94
97
fn set_min_stack ( stack_size : uint ) { rustrt:: set_min_stack ( stack_size) ; }
95
98
99
+ fn spawn2 < ~T > ( -data : T , f : fn #( T ) ) -> task {
100
+ spawn_inner2 ( data, f, none)
101
+ }
102
+
103
+ fn spawn_notify2 < ~T > ( -data : T , f : fn #( T ) ,
104
+ notify : comm:: chan < task_notification > ) -> task {
105
+ spawn_inner2 ( data, f, some ( notify) )
106
+ }
107
+
108
+ fn spawn_joinable2 < ~T > ( -data : T , f : fn #( T ) ) -> joinable_task {
109
+ let p = comm:: port :: < task_notification > ( ) ;
110
+ let id = spawn_notify2 ( data, f, comm:: chan :: < task_notification > ( p) ) ;
111
+ ret ( id, p) ;
112
+ }
113
+
114
+ // FIXME: To transition from the unsafe spawn that spawns a shared closure to
115
+ // the safe spawn that spawns a bare function we're going to write
116
+ // barefunc-spawn on top of unsafe-spawn. Sadly, bind does not work reliably
117
+ // enough to suite our needs (#1034, probably others yet to be discovered), so
118
+ // we're going to copy the bootstrap data into a unique pointer, cast it to an
119
+ // unsafe pointer then wrap up the bare function and the unsafe pointer in a
120
+ // shared closure to spawn.
121
+ //
122
+ // After the transition this should all be rewritten.
123
+
124
+ fn spawn_inner2 < ~T > ( -data : T , f : fn #( T ) ,
125
+ notify : option < comm:: chan < task_notification > > )
126
+ -> task_id {
127
+
128
+ fn wrapper < ~T > ( -data : * u8 , f : fn #( T ) ) {
129
+ let data: ~T = unsafe :: reinterpret_cast ( data) ;
130
+ f ( * data) ;
131
+ }
132
+
133
+ let data = ~data;
134
+ let dataptr: * u8 = unsafe :: reinterpret_cast ( data) ;
135
+ unsafe :: leak ( data) ;
136
+ let wrapped = bind wrapper ( dataptr, f) ;
137
+ ret spawn_inner ( wrapped, notify) ;
138
+ }
139
+
96
140
fn spawn ( -thunk : fn ( ) ) -> task { spawn_inner ( thunk, none) }
97
141
98
142
fn spawn_notify ( -thunk : fn ( ) , notify : comm:: chan < task_notification > ) -> task {
0 commit comments