forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.rs
More file actions
1409 lines (1228 loc) · 38.6 KB
/
task.rs
File metadata and controls
1409 lines (1228 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Task management.
*
* An executing Rust program consists of a tree of tasks, each with their own
* stack, and sole ownership of their allocated heap data. Tasks communicate
* with each other using ports and channels.
*
* When a task fails, that failure will propagate to its parent (the task
* that spawned it) and the parent will fail as well. The reverse is not
* true: when a parent task fails its children will continue executing. When
* the root (main) task fails, all tasks fail, and then so does the entire
* process.
*
* Tasks may execute in parallel and are scheduled automatically by the
* runtime.
*
* # Example
*
* ~~~
* do spawn {
* log(error, "Hello, World!");
* }
* ~~~
*/
import result::result;
import dvec::extensions;
import dvec_iter::extensions;
export task;
export task_result;
export notification;
export sched_mode;
export sched_opts;
export task_opts;
export builder;
export default_task_opts;
export get_opts;
export set_opts;
export set_sched_mode;
export add_wrapper;
export run;
export future_result;
export future_task;
export unsupervise;
export run_listener;
export spawn;
export spawn_with;
export spawn_listener;
export spawn_sched;
export try;
export yield;
export failing;
export get_task;
export unkillable;
export local_data_key;
export local_data_pop;
export local_data_get;
export local_data_set;
export local_data_modify;
export single_threaded;
export thread_per_core;
export thread_per_task;
export manual_threads;
export osmain;
/* Data types */
/// A handle to a task
enum task = task_id;
/**
* Indicates the manner in which a task exited.
*
* A task that completes without failing and whose supervised children
* complete without failing is considered to exit successfully.
*
* FIXME (See #1868): This description does not indicate the current behavior
* for linked failure.
*/
enum task_result {
success,
failure,
}
/// A message type for notifying of task lifecycle events
enum notification {
/// Sent when a task exits with the task handle and result
exit(task, task_result)
}
/// Scheduler modes
enum sched_mode {
/// All tasks run in the same OS thread
single_threaded,
/// Tasks are distributed among available CPUs
thread_per_core,
/// Each task runs in its own OS thread
thread_per_task,
/// Tasks are distributed among a fixed number of OS threads
manual_threads(uint),
/**
* Tasks are scheduled on the main OS thread
*
* The main OS thread is the thread used to launch the runtime which,
* in most cases, is the process's initial thread as created by the OS.
*/
osmain
}
/**
* Scheduler configuration options
*
* # Fields
*
* * sched_mode - The operating mode of the scheduler
*
* * foreign_stack_size - The size of the foreign stack, in bytes
*
* Rust code runs on Rust-specific stacks. When Rust code calls foreign
* code (via functions in foreign modules) it switches to a typical, large
* stack appropriate for running code written in languages like C. By
* default these foreign stacks have unspecified size, but with this
* option their size can be precisely specified.
*/
type sched_opts = {
mode: sched_mode,
foreign_stack_size: option<uint>
};
/**
* Task configuration options
*
* # Fields
*
* * supervise - Do not propagate failure to the parent task
*
* All tasks are linked together via a tree, from parents to children. By
* default children are 'supervised' by their parent and when they fail
* so too will their parents. Settings this flag to false disables that
* behavior.
*
* * notify_chan - Enable lifecycle notifications on the given channel
*
* * sched - Specify the configuration of a new scheduler to create the task
* in
*
* By default, every task is created in the same scheduler as its
* parent, where it is scheduled cooperatively with all other tasks
* in that scheduler. Some specialized applications may want more
* control over their scheduling, in which case they can be spawned
* into a new scheduler with the specific properties required.
*
* This is of particular importance for libraries which want to call
* into foreign code that blocks. Without doing so in a different
* scheduler other tasks will be impeded or even blocked indefinitely.
*/
type task_opts = {
supervise: bool,
notify_chan: option<comm::chan<notification>>,
sched: option<sched_opts>,
};
/**
* The task builder type.
*
* Provides detailed control over the properties and behavior of new tasks.
*/
// NB: Builders are designed to be single-use because they do stateful
// things that get weird when reusing - e.g. if you create a result future
// it only applies to a single task, so then you have to maintain some
// potentially tricky state to ensure that everything behaves correctly
// when you try to reuse the builder to spawn a new task. We'll just
// sidestep that whole issue by making builders uncopyable and making
// the run function move them in.
enum builder {
builder_({
mut opts: task_opts,
mut gen_body: fn@(+fn~()) -> fn~(),
can_not_copy: option<comm::port<()>>
})
}
/* Task construction */
fn default_task_opts() -> task_opts {
/*!
* The default task options
*
* By default all tasks are supervised by their parent, are spawned
* into the same scheduler, and do not post lifecycle notifications.
*/
{
supervise: true,
notify_chan: none,
sched: none
}
}
fn builder() -> builder {
//! Construct a builder
let body_identity = fn@(+body: fn~()) -> fn~() { body };
builder_({
mut opts: default_task_opts(),
mut gen_body: body_identity,
can_not_copy: none
})
}
fn get_opts(builder: builder) -> task_opts {
//! Get the task_opts associated with a builder
builder.opts
}
fn set_opts(builder: builder, opts: task_opts) {
/*!
* Set the task_opts associated with a builder
*
* To update a single option use a pattern like the following:
*
* set_opts(builder, {
* supervise: false
* with get_opts(builder)
* });
*/
builder.opts = opts;
}
fn set_sched_mode(builder: builder, mode: sched_mode) {
set_opts(builder, {
sched: some({
mode: mode,
foreign_stack_size: none
})
with get_opts(builder)
});
}
fn add_wrapper(builder: builder, gen_body: fn@(+fn~()) -> fn~()) {
/*!
* Add a wrapper to the body of the spawned task.
*
* Before the task is spawned it is passed through a 'body generator'
* function that may perform local setup operations as well as wrap
* the task body in remote setup operations. With this the behavior
* of tasks can be extended in simple ways.
*
* This function augments the current body generator with a new body
* generator by applying the task body which results from the
* existing body generator to the new body generator.
*/
let prev_gen_body = builder.gen_body;
builder.gen_body = fn@(+body: fn~()) -> fn~() {
gen_body(prev_gen_body(body))
};
}
fn run(-builder: builder, +f: fn~()) {
/*!
* Creates and exucutes a new child task
*
* Sets up a new task with its own call stack and schedules it to run
* the provided unique closure. The task has the properties and behavior
* specified by `builder`.
*
* # Failure
*
* When spawning into a new scheduler, the number of threads requested
* must be greater than zero.
*/
let body = builder.gen_body(f);
spawn_raw(builder.opts, body);
}
/* Builder convenience functions */
fn future_result(builder: builder) -> future::future<task_result> {
/*!
* Get a future representing the exit status of the task.
*
* Taking the value of the future will block until the child task
* terminates.
*
* Note that the future returning by this function is only useful for
* obtaining the value of the next task to be spawning with the
* builder. If additional tasks are spawned with the same builder
* then a new result future must be obtained prior to spawning each
* task.
*/
// FIXME (#1087, #1857): Once linked failure and notification are
// handled in the library, I can imagine implementing this by just
// registering an arbitrary number of task::on_exit handlers and
// sending out messages.
let po = comm::port();
let ch = comm::chan(po);
set_opts(builder, {
notify_chan: some(ch)
with get_opts(builder)
});
do future::from_fn {
alt comm::recv(po) {
exit(_, result) { result }
}
}
}
fn future_task(builder: builder) -> future::future<task> {
//! Get a future representing the handle to the new task
import future::future_pipe;
let (po, ch) = future_pipe::init();
let ch = ~mut some(ch);
do add_wrapper(builder) |body, move ch| {
let ch = { let mut t = none;
t <-> *ch;
~mut t};
fn~(move ch) {
let mut po = none;
po <-> *ch;
future_pipe::server::completed(option::unwrap(po),
get_task());
body();
}
}
future::from_port(po)
}
fn unsupervise(builder: builder) {
//! Configures the new task to not propagate failure to its parent
set_opts(builder, {
supervise: false
with get_opts(builder)
});
}
fn run_with<A:send>(-builder: builder,
+arg: A,
+f: fn~(+A)) {
/*!
* Runs a task, while transfering ownership of one argument to the
* child.
*
* This is useful for transfering ownership of noncopyables to
* another task.
*
*/
let arg = ~mut some(arg);
do run(builder) {
let mut my_arg = none;
my_arg <-> *arg;
f(option::unwrap(my_arg))
}
}
fn run_listener<A:send>(-builder: builder,
+f: fn~(comm::port<A>)) -> comm::chan<A> {
/*!
* Runs a new task while providing a channel from the parent to the child
*
* Sets up a communication channel from the current task to the new
* child task, passes the port to child's body, and returns a channel
* linked to the port to the parent.
*
* This encapsulates some boilerplate handshaking logic that would
* otherwise be required to establish communication from the parent
* to the child.
*/
let setup_po = comm::port();
let setup_ch = comm::chan(setup_po);
do run(builder) {
let po = comm::port();
let mut ch = comm::chan(po);
comm::send(setup_ch, ch);
f(po);
}
comm::recv(setup_po)
}
/* Spawn convenience functions */
fn spawn(+f: fn~()) {
/*!
* Creates and executes a new child task
*
* Sets up a new task with its own call stack and schedules it to run
* the provided unique closure.
*
* This function is equivalent to `run(new_builder(), f)`.
*/
run(builder(), f);
}
fn spawn_with<A:send>(+arg: A, +f: fn~(+A)) {
/*!
* Runs a task, while transfering ownership of one argument to the
* child.
*
* This is useful for transfering ownership of noncopyables to
* another task.
*
* This function is equivalent to `run_with(builder(), arg, f)`.
*/
run_with(builder(), arg, f)
}
fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
/*!
* Runs a new task while providing a channel from the parent to the child
*
* Sets up a communication channel from the current task to the new
* child task, passes the port to child's body, and returns a channel
* linked to the port to the parent.
*
* This encapsulates some boilerplate handshaking logic that would
* otherwise be required to establish communication from the parent
* to the child.
*
* The simplest way to establish bidirectional communication between
* a parent in child is as follows:
*
* let po = comm::port();
* let ch = comm::chan(po);
* let ch = do spawn_listener |po| {
* // Now the child has a port called 'po' to read from and
* // an environment-captured channel called 'ch'.
* };
* // Likewise, the parent has both a 'po' and 'ch'
*
* This function is equivalent to `run_listener(builder(), f)`.
*/
run_listener(builder(), f)
}
fn spawn_sched(mode: sched_mode, +f: fn~()) {
/*!
* Creates a new scheduler and executes a task on it
*
* Tasks subsequently spawned by that task will also execute on
* the new scheduler. When there are no more tasks to execute the
* scheduler terminates.
*
* # Failure
*
* In manual threads mode the number of threads requested must be
* greater than zero.
*/
let mut builder = builder();
set_sched_mode(builder, mode);
run(builder, f);
}
fn try<T:send>(+f: fn~() -> T) -> result<T,()> {
/*!
* Execute a function in another task and return either the return value
* of the function or result::err.
*
* # Return value
*
* If the function executed successfully then try returns result::ok
* containing the value returned by the function. If the function fails
* then try returns result::err containing nil.
*/
let po = comm::port();
let ch = comm::chan(po);
let mut builder = builder();
unsupervise(builder);
let result = future_result(builder);
do run(builder) {
comm::send(ch, f());
}
alt future::get(result) {
success { result::ok(comm::recv(po)) }
failure { result::err(()) }
}
}
/* Lifecycle functions */
fn yield() {
//! Yield control to the task scheduler
let task_ = rustrt::rust_get_task();
let mut killed = false;
rustrt::rust_task_yield(task_, killed);
if killed && !failing() {
fail "killed";
}
}
fn failing() -> bool {
//! True if the running task has failed
rustrt::rust_task_is_unwinding(rustrt::rust_get_task())
}
fn get_task() -> task {
//! Get a handle to the running task
task(rustrt::get_task_id())
}
/**
* Temporarily make the task unkillable
*
* # Example
*
* ~~~
* do task::unkillable {
* // detach / yield / destroy must all be called together
* rustrt::rust_port_detach(po);
* // This must not result in the current task being killed
* task::yield();
* rustrt::rust_port_destroy(po);
* }
* ~~~
*/
unsafe fn unkillable(f: fn()) {
class allow_failure {
let i: (); // since a class must have at least one field
new(_i: ()) { self.i = (); }
drop { rustrt::rust_task_allow_kill(); }
}
let _allow_failure = allow_failure(());
rustrt::rust_task_inhibit_kill();
f();
}
/* Internal */
type sched_id = int;
type task_id = int;
// These are both opaque runtime/compiler types that we don't know the
// structure of and should only deal with via unsafe pointer
type rust_task = libc::c_void;
type rust_closure = libc::c_void;
fn spawn_raw(opts: task_opts, +f: fn~()) {
let mut f = if opts.supervise {
f
} else {
// FIXME (#1868, #1789): The runtime supervision API is weird here
// because it was designed to let the child unsupervise itself,
// when what we actually want is for parents to unsupervise new
// children.
fn~() {
rustrt::unsupervise();
f();
}
};
unsafe {
let fptr = ptr::addr_of(f);
let closure: *rust_closure = unsafe::reinterpret_cast(fptr);
let new_task = alt opts.sched {
none {
rustrt::new_task()
}
some(sched_opts) {
new_task_in_new_sched(sched_opts)
}
};
assert !new_task.is_null();
do option::iter(opts.notify_chan) |c| {
// FIXME (#1087): Would like to do notification in Rust
rustrt::rust_task_config_notify(new_task, c);
}
rustrt::start_task(new_task, closure);
unsafe::forget(f);
}
fn new_task_in_new_sched(opts: sched_opts) -> *rust_task {
if opts.foreign_stack_size != none {
fail "foreign_stack_size scheduler option unimplemented";
}
let num_threads = alt opts.mode {
single_threaded { 1u }
thread_per_core {
fail "thread_per_core scheduling mode unimplemented"
}
thread_per_task {
fail "thread_per_task scheduling mode unimplemented"
}
manual_threads(threads) {
if threads == 0u {
fail "can not create a scheduler with no threads";
}
threads
}
osmain { 0u /* Won't be used */ }
};
let sched_id = if opts.mode != osmain {
rustrt::rust_new_sched(num_threads)
} else {
rustrt::rust_osmain_sched_id()
};
rustrt::rust_new_task_in_sched(sched_id)
}
}
/****************************************************************************
* Task local data management
*
* Allows storing boxes with arbitrary types inside, to be accessed anywhere
* within a task, keyed by a pointer to a global finaliser function. Useful
* for task-spawning metadata (tracking linked failure state), dynamic
* variables, and interfacing with foreign code with bad callback interfaces.
*
* To use, declare a monomorphic global function at the type to store, and use
* it as the 'key' when accessing. See the 'tls' tests below for examples.
*
* Casting 'Arcane Sight' reveals an overwhelming aura of Transmutation magic.
****************************************************************************/
/**
* Indexes a task-local data slot. The function itself is used to
* automatically finalise stored values; also, its code pointer is used for
* comparison. Recommended use is to write an empty function for each desired
* task-local data slot (and use class destructors, instead of code inside the
* finaliser, if specific teardown is needed). DO NOT use multiple
* instantiations of a single polymorphic function to index data of different
* types; arbitrary type coercion is possible this way. The interface is safe
* as long as all key functions are monomorphic.
*/
type local_data_key<T> = fn@(+@T);
// We use dvec because it's the best data structure in core. If TLS is used
// heavily in future, this could be made more efficient with a proper map.
type task_local_element = (*libc::c_void, *libc::c_void, fn@(+*libc::c_void));
// Has to be a pointer at outermost layer; the foreign call returns void *.
type task_local_map = @dvec::dvec<option<task_local_element>>;
extern fn cleanup_task_local_map(map_ptr: *libc::c_void) unsafe {
assert !map_ptr.is_null();
// Get and keep the single reference that was created at the beginning.
let map: task_local_map = unsafe::reinterpret_cast(map_ptr);
for (*map).each |entry| {
alt entry {
// Finaliser drops data. We drop the finaliser implicitly here.
some((_key, data, finalise_fn)) { finalise_fn(data); }
none { }
}
}
}
// Gets the map from the runtime. Lazily initialises if not done so already.
unsafe fn get_task_local_map(task: *rust_task) -> task_local_map {
// Relies on the runtime initialising the pointer to null.
// NOTE: The map's box lives in TLS invisibly referenced once. Each time
// we retrieve it for get/set, we make another reference, which get/set
// drop when they finish. No "re-storing after modifying" is needed.
let map_ptr = rustrt::rust_get_task_local_data(task);
if map_ptr.is_null() {
let map: task_local_map = @dvec::dvec();
// Use reinterpret_cast -- transmute would take map away from us also.
rustrt::rust_set_task_local_data(task, unsafe::reinterpret_cast(map));
rustrt::rust_task_local_data_atexit(task, cleanup_task_local_map);
// Also need to reference it an extra time to keep it for now.
unsafe::bump_box_refcount(map);
map
} else {
let map = unsafe::transmute(map_ptr);
unsafe::bump_box_refcount(map);
map
}
}
unsafe fn key_to_key_value<T>(key: local_data_key<T>) -> *libc::c_void {
// Keys are closures, which are (fnptr,envptr) pairs. Use fnptr.
// Use reintepret_cast -- transmute would leak (forget) the closure.
let pair: (*libc::c_void, *libc::c_void) = unsafe::reinterpret_cast(key);
tuple::first(pair)
}
// If returning some(..), returns with @T with the map's reference. Careful!
unsafe fn local_data_lookup<T>(map: task_local_map, key: local_data_key<T>)
-> option<(uint, *libc::c_void, fn@(+*libc::c_void))> {
let key_value = key_to_key_value(key);
let map_pos = (*map).position(|entry|
alt entry { some((k,_,_)) { k == key_value } none { false } }
);
do map_pos.map |index| {
// .get() is guaranteed because of "none { false }" above.
let (_, data_ptr, finaliser) = (*map)[index].get();
(index, data_ptr, finaliser)
}
}
unsafe fn local_get_helper<T>(task: *rust_task, key: local_data_key<T>,
do_pop: bool) -> option<@T> {
let map = get_task_local_map(task);
// Interpret our findings from the map
do local_data_lookup(map, key).map |result| {
// A reference count magically appears on 'data' out of thin air.
// 'data' has the reference we originally stored it with. We either
// need to erase it from the map or artificially bump the count.
let (index, data_ptr, _) = result;
let data: @T = unsafe::transmute(data_ptr);
if do_pop {
(*map).set_elt(index, none);
} else {
unsafe::bump_box_refcount(data);
}
data
}
}
unsafe fn local_pop<T>(task: *rust_task,
key: local_data_key<T>) -> option<@T> {
local_get_helper(task, key, true)
}
unsafe fn local_get<T>(task: *rust_task,
key: local_data_key<T>) -> option<@T> {
local_get_helper(task, key, false)
}
unsafe fn local_set<T>(task: *rust_task, key: local_data_key<T>, -data: @T) {
let map = get_task_local_map(task);
// Store key+data as *voids. Data is invisibly referenced once; key isn't.
let keyval = key_to_key_value(key);
let data_ptr = unsafe::transmute(data);
// Finaliser is called at task exit to de-reference up remaining entries.
let finaliser: fn@(+*libc::c_void) = unsafe::reinterpret_cast(key);
// Construct new entry to store in the map.
let new_entry = some((keyval, data_ptr, finaliser));
// Find a place to put it.
alt local_data_lookup(map, key) {
some((index, old_data_ptr, old_finaliser)) {
// Key already had a value set, old_data_ptr, whose reference we
// need to drop. After that, overwriting its slot will be safe.
// (The heap-allocated finaliser will be freed in the overwrite.)
// FIXME(#2734): just transmuting old_data_ptr to @T doesn't work,
// similarly to the sample there (but more our/unsafety's fault?).
old_finaliser(old_data_ptr);
(*map).set_elt(index, new_entry);
}
none {
// Find an empty slot. If not, grow the vector.
alt (*map).position(|x| x == none) {
some(empty_index) {
(*map).set_elt(empty_index, new_entry);
}
none {
(*map).push(new_entry);
}
}
}
}
}
unsafe fn local_modify<T>(task: *rust_task, key: local_data_key<T>,
modify_fn: fn(option<@T>) -> option<@T>) {
// Could be more efficient by doing the lookup work, but this is easy.
let newdata = modify_fn(local_pop(task, key));
if newdata.is_some() {
local_set(task, key, option::unwrap(newdata));
}
}
/* Exported interface for task-local data (plus local_data_key above). */
/**
* Remove a task-local data value from the table, returning the
* reference that was originally created to insert it.
*/
unsafe fn local_data_pop<T>(key: local_data_key<T>) -> option<@T> {
local_pop(rustrt::rust_get_task(), key)
}
/**
* Retrieve a task-local data value. It will also be kept alive in the
* table until explicitly removed.
*/
unsafe fn local_data_get<T>(key: local_data_key<T>) -> option<@T> {
local_get(rustrt::rust_get_task(), key)
}
/**
* Store a value in task-local data. If this key already has a value,
* that value is overwritten (and its destructor is run).
*/
unsafe fn local_data_set<T>(key: local_data_key<T>, -data: @T) {
local_set(rustrt::rust_get_task(), key, data)
}
/**
* Modify a task-local data value. If the function returns 'none', the
* data is removed (and its reference dropped).
*/
unsafe fn local_data_modify<T>(key: local_data_key<T>,
modify_fn: fn(option<@T>) -> option<@T>) {
local_modify(rustrt::rust_get_task(), key, modify_fn)
}
extern mod rustrt {
#[rust_stack]
fn rust_task_yield(task: *rust_task, &killed: bool);
fn rust_get_sched_id() -> sched_id;
fn rust_new_sched(num_threads: libc::uintptr_t) -> sched_id;
fn get_task_id() -> task_id;
#[rust_stack]
fn rust_get_task() -> *rust_task;
fn new_task() -> *rust_task;
fn rust_new_task_in_sched(id: sched_id) -> *rust_task;
fn rust_task_config_notify(
task: *rust_task, &&chan: comm::chan<notification>);
fn start_task(task: *rust_task, closure: *rust_closure);
fn rust_task_is_unwinding(rt: *rust_task) -> bool;
fn unsupervise();
fn rust_osmain_sched_id() -> sched_id;
fn rust_task_inhibit_kill();
fn rust_task_allow_kill();
#[rust_stack]
fn rust_get_task_local_data(task: *rust_task) -> *libc::c_void;
#[rust_stack]
fn rust_set_task_local_data(task: *rust_task, map: *libc::c_void);
#[rust_stack]
fn rust_task_local_data_atexit(task: *rust_task, cleanup_fn: *u8);
}
#[test]
fn test_spawn_raw_simple() {
let po = comm::port();
let ch = comm::chan(po);
do spawn_raw(default_task_opts()) {
comm::send(ch, ());
}
comm::recv(po);
}
#[test]
#[ignore(cfg(windows))]
fn test_spawn_raw_unsupervise() {
let opts = {
supervise: false
with default_task_opts()
};
do spawn_raw(opts) {
fail;
}
}
#[test]
#[ignore(cfg(windows))]
fn test_spawn_raw_notify() {
let task_po = comm::port();
let task_ch = comm::chan(task_po);
let notify_po = comm::port();
let notify_ch = comm::chan(notify_po);
let opts = {
notify_chan: some(notify_ch)
with default_task_opts()
};
do spawn_raw(opts) {
comm::send(task_ch, get_task());
}
let task_ = comm::recv(task_po);
assert comm::recv(notify_po) == exit(task_, success);
let opts = {
supervise: false,
notify_chan: some(notify_ch)
with default_task_opts()
};
do spawn_raw(opts) {
comm::send(task_ch, get_task());
fail;
}
let task_ = comm::recv(task_po);
assert comm::recv(notify_po) == exit(task_, failure);
}
#[test]
fn test_run_basic() {
let po = comm::port();
let ch = comm::chan(po);
let buildr = builder();
do run(buildr) {
comm::send(ch, ());
}
comm::recv(po);
}
#[test]
fn test_add_wrapper() {
let po = comm::port();
let ch = comm::chan(po);
let buildr = builder();
do add_wrapper(buildr) |body| {
fn~() {
body();
comm::send(ch, ());
}
}
do run(buildr) { }
comm::recv(po);
}
#[test]
#[ignore(cfg(windows))]
fn test_future_result() {
let buildr = builder();
let result = future_result(buildr);
do run(buildr) { }
assert future::get(result) == success;
let buildr = builder();
let result = future_result(buildr);
unsupervise(buildr);
do run(buildr) { fail }
assert future::get(result) == failure;
}
#[test]
fn test_future_task() {
let po = comm::port();
let ch = comm::chan(po);
let buildr = builder();
let task1 = future_task(buildr);
do run(buildr) { comm::send(ch, get_task()) }
assert future::get(task1) == comm::recv(po);
}
#[test]
fn test_spawn_listiner_bidi() {
let po = comm::port();
let ch = comm::chan(po);
let ch = do spawn_listener |po| {
// Now the child has a port called 'po' to read from and
// an environment-captured channel called 'ch'.
let res = comm::recv(po);
assert res == "ping";
comm::send(ch, "pong");
};
// Likewise, the parent has both a 'po' and 'ch'
comm::send(ch, "ping");
let res = comm::recv(po);
assert res == "pong";
}
#[test]
fn test_try_success() {
alt do try {
"Success!"
} {
result::ok("Success!") { }
_ { fail; }
}
}