[m-rev.] diff: Use spawn_native in thread_sbrk test.

Peter Wang novalazy at gmail.com
Thu Aug 9 18:49:37 AEST 2018


The thread_sbrk test is only meaningful with OS threads so use
thread.spawn_native instead of thread.spawn now that it is available.

It was harmless to run the test with only concurrent Mercury threads
anyway, except that calling thread.yield leads to a crash in deep
profiling grades (whether or not concurrent Mercury threads *should*
work in deep profiling grades is another matter).

tests/hard_coded/thread_sbrk.m:
tests/hard_coded/thread_sbrk.exp2:
    Use thread.spawn_native instead of thread.spawn.

    Delete unnecessary thread.yield calls.
---
 tests/hard_coded/thread_sbrk.exp2 |  2 +-
 tests/hard_coded/thread_sbrk.m    | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/tests/hard_coded/thread_sbrk.exp2 b/tests/hard_coded/thread_sbrk.exp2
index 900f82ada..ff161d491 100644
--- a/tests/hard_coded/thread_sbrk.exp2
+++ b/tests/hard_coded/thread_sbrk.exp2
@@ -1 +1 @@
-spawn not supported.
+spawn_native not supported.
diff --git a/tests/hard_coded/thread_sbrk.m b/tests/hard_coded/thread_sbrk.m
index 2d6de81c7..35a477ef4 100644
--- a/tests/hard_coded/thread_sbrk.m
+++ b/tests/hard_coded/thread_sbrk.m
@@ -32,34 +32,34 @@
 #endif
 ").
 
 :- type tree
     --->    nil
     ;       node(int, tree, tree).
 
 %---------------------------------------------------------------------------%
 
 main(!IO) :-
-    ( can_spawn ->
+    ( can_spawn_native ->
         semaphore.init(Sem, !IO),
-        thread.spawn(alloc_thread(Sem), !IO),
+        thread.spawn_native(alloc_thread(Sem), _, !IO),
         semaphore.wait(Sem, !IO),
         sbrk_loop(Sem, !IO),
         io.write_string("done.\n", !IO)
     ;
-        io.write_string("spawn not supported.\n", !IO)
+        io.write_string("spawn_native not supported.\n", !IO)
     ).
 
 :- pred sbrk_loop(semaphore::in, io::di, io::uo) is det.
 
 sbrk_loop(Sem, !IO) :-
-    thread.yield(!IO),
+    % io.write_string("sbrk thread\n", !IO),
     semaphore.try_wait(Sem, Success, !IO),
     (
         Success = yes
     ;
         Success = no,
         % It is hard to trigger a crash by calling malloc because not every
         % call ends up calling sbrk.  Therefore we call sbrk directly.
         sbrk(0x100, !IO),
         sbrk_loop(Sem, !IO)
     ).
@@ -70,35 +70,35 @@ sbrk(_, !IO).
 
 :- pragma foreign_proc("C",
     sbrk(Increment::in, _IO0::di, _IO::uo),
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
 #ifdef MR_HAVE_SBRK
     sbrk(Increment);
 #endif
 ").
 
-:- pred alloc_thread(semaphore::in, io::di, io::uo) is cc_multi.
+:- pred alloc_thread(semaphore::in, thread::in, io::di, io::uo) is cc_multi.
 
-alloc_thread(Sem, !IO) :-
+alloc_thread(Sem, _Thread, !IO) :-
     semaphore.signal(Sem, !IO),
     alloc_loop(Sem, 1, !IO).
 
 :- pred alloc_loop(semaphore::in, int::in, io::di, io::uo) is cc_multi.
 
 alloc_loop(Sem, Depth, !IO) :-
+    % io.write_string("alloc thread\n", !IO),
     ( Depth > 20 ->
         semaphore.signal(Sem, !IO)
     ;
         build(Depth, T, 0, _Id),
         io.format("depth %d, size %d\n", [i(Depth), i(size(T))], !IO),
-        thread.yield(!IO),
         alloc_loop(Sem, Depth + 1, !IO)
     ).
 
 :- pred build(int::in, tree::out, int::in, int::out) is det.
 
 build(Depth, T, Id0, Id) :-
     ( Depth = 1 ->
         T = nil,
         Id = Id0
     ;
-- 
2.18.0



More information about the reviews mailing list