Files
mercury/tests/hard_coded/thread_sbrk.m
Zoltan Somogyi 33eb3028f5 Clean up the tests in half the test directories.
tests/accumulator/*.m:
tests/analysis_*/*.m:
tests/benchmarks*/*.m:
tests/debugger*/*.{m,exp,inp}:
tests/declarative_debugger*/*.{m,exp,inp}:
tests/dppd*/*.m:
tests/exceptions*/*.m:
tests/general*/*.m:
tests/grade_subdirs*/*.m:
tests/hard_coded*/*.m:
    Make these tests use four-space indentation, and ensure that
    each module is imported on its own line. (I intend to use the latter
    to figure out which subdirectories' tests can be executed in parallel.)

    These changes usually move code to different lines. For the debugger tests,
    specify the new line numbers in .inp files and expect them in .exp files.
2015-02-14 20:14:03 +11:00

114 lines
2.8 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
% This program performs Mercury allocations in one thread (e.g. using Boehm GC)
% and simulates malloc calls in another thread that indirectly call sbrk.
% If both allocators use sbrk and are invoked simulataneously then
% memory corruption can result.
:- module thread_sbrk.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module bool.
:- import_module int.
:- import_module list.
:- import_module string.
:- import_module thread.
:- import_module thread.semaphore.
:- pragma foreign_decl("C", "
#ifdef MR_HAVE_SBRK
#include <unistd.h>
#endif
").
:- type tree
---> nil
; node(int, tree, tree).
%---------------------------------------------------------------------------%
main(!IO) :-
( can_spawn ->
semaphore.init(Sem, !IO),
thread.spawn(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)
).
:- pred sbrk_loop(semaphore::in, io::di, io::uo) is det.
sbrk_loop(Sem, !IO) :-
thread.yield(!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)
).
:- pred sbrk(int::in, io::di, io::uo) is det.
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.
alloc_thread(Sem, !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) :-
( 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
;
build(Depth - 1, L, Id0 + 1, Id2),
build(Depth - 1, R, Id2, Id),
T = node(Id0, L, R)
).
:- func size(tree) = int.
size(nil) = 1.
size(node(_, L, R)) = 1 + size(L) + size(R).