Import Upstream version 20180207
[hcoop/debian/mlton.git] / runtime / gc / copy-thread.c
CommitLineData
7f918cf1
CE
1/* Copyright (C) 2011-2012 Matthew Fluet.
2 * Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 * Copyright (C) 1997-2000 NEC Research Institute.
5 *
6 * MLton is released under a BSD-style license.
7 * See the file MLton-LICENSE for details.
8 */
9
10GC_thread copyThread (GC_state s, GC_thread from, size_t used) {
11 GC_thread to;
12
13 if (DEBUG_THREADS)
14 fprintf (stderr, "copyThread ("FMTPTR")\n", (uintptr_t)from);
15 /* newThread may do a GC, which invalidates from.
16 * Hence we need to stash from someplace that the GC can find it.
17 */
18 assert (s->savedThread == BOGUS_OBJPTR);
19 s->savedThread = pointerToObjptr((pointer)from - offsetofThread (s), s->heap.start);
20 to = newThread (s, alignStackReserved(s, used));
21 from = (GC_thread)(objptrToPointer(s->savedThread, s->heap.start) + offsetofThread (s));
22 s->savedThread = BOGUS_OBJPTR;
23 if (DEBUG_THREADS) {
24 fprintf (stderr, FMTPTR" = copyThread ("FMTPTR")\n",
25 (uintptr_t)to, (uintptr_t)from);
26 }
27 copyStack (s,
28 (GC_stack)(objptrToPointer(from->stack, s->heap.start)),
29 (GC_stack)(objptrToPointer(to->stack, s->heap.start)));
30 to->bytesNeeded = from->bytesNeeded;
31 to->exnStack = from->exnStack;
32 return to;
33}
34
35void GC_copyCurrentThread (GC_state s) {
36 GC_thread fromThread;
37 GC_stack fromStack;
38 GC_thread toThread;
39 LOCAL_USED_FOR_ASSERT GC_stack toStack;
40
41 if (DEBUG_THREADS)
42 fprintf (stderr, "GC_copyCurrentThread\n");
43 enter (s);
44 fromThread = (GC_thread)(objptrToPointer(s->currentThread, s->heap.start)
45 + offsetofThread (s));
46 fromStack = (GC_stack)(objptrToPointer(fromThread->stack, s->heap.start));
47 toThread = copyThread (s, fromThread, fromStack->used);
48 toStack = (GC_stack)(objptrToPointer(toThread->stack, s->heap.start));
49 assert (toStack->reserved == alignStackReserved (s, toStack->used));
50 leave (s);
51 if (DEBUG_THREADS)
52 fprintf (stderr, FMTPTR" = GC_copyCurrentThread\n", (uintptr_t)toThread);
53 assert (s->savedThread == BOGUS_OBJPTR);
54 s->savedThread = pointerToObjptr((pointer)toThread - offsetofThread (s), s->heap.start);
55}
56
57pointer GC_copyThread (GC_state s, pointer p) {
58 GC_thread fromThread;
59 GC_stack fromStack;
60 GC_thread toThread;
61 LOCAL_USED_FOR_ASSERT GC_stack toStack;
62
63 if (DEBUG_THREADS)
64 fprintf (stderr, "GC_copyThread ("FMTPTR")\n", (uintptr_t)p);
65 enter (s);
66 fromThread = (GC_thread)(p + offsetofThread (s));
67 fromStack = (GC_stack)(objptrToPointer(fromThread->stack, s->heap.start));
68 toThread = copyThread (s, fromThread, fromStack->used);
69 toStack = (GC_stack)(objptrToPointer(toThread->stack, s->heap.start));
70 assert (toStack->reserved == alignStackReserved (s, toStack->used));
71 leave (s);
72 if (DEBUG_THREADS)
73 fprintf (stderr, FMTPTR" = GC_copyThread ("FMTPTR")\n",
74 (uintptr_t)toThread, (uintptr_t)fromThread);
75 return ((pointer)toThread - offsetofThread (s));
76}