File: ThreadLabels.c

package info (click to toggle)
ghc 9.10.3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 169,076 kB
  • sloc: haskell: 713,554; ansic: 84,184; cpp: 30,255; javascript: 9,003; sh: 7,870; fortran: 3,527; python: 3,228; asm: 2,523; makefile: 2,324; yacc: 1,570; lisp: 532; xml: 196; perl: 111; csh: 2
file content (70 lines) | stat: -rw-r--r-- 1,971 bytes parent folder | download
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
/* -----------------------------------------------------------------------------
 * ThreadLabels.c
 *
 * (c) The GHC Team 2002-2003
 *
 * Table of thread labels.
 *
 * ---------------------------------------------------------------------------*/

#include "rts/PosixSource.h"
#include "Rts.h"

#include "ThreadLabels.h"
#include "RtsUtils.h"
#include "RtsFlags.h"
#include "Hash.h"
#include "Trace.h"

#include <stdlib.h>
#include <string.h>

/*
 * Note [Thread Labels]
 * ~~~~~~~~~~~~~~~~~~~~
 * The user may assign a textual label to a thread using the labelThread#
 * primop to help identify the thread. This label is represented by StgTSO's
 * label field which contains a pointer to a ByteArray# containing a
 * UTF-8 string.
 *
 * Note that this string isn't necessary NULL terminated; rather, its length is
 * determined by the ByteArray# length.
 */

static StgArrBytes *
allocateArrBytes(Capability *cap, size_t size_in_bytes)
{
    /* round up to a whole number of words */
    uint32_t data_size_in_words  = ROUNDUP_BYTES_TO_WDS(size_in_bytes);
    uint32_t total_size_in_words = sizeofW(StgArrBytes) + data_size_in_words;

    StgArrBytes *arr = (StgArrBytes *) allocate(cap, total_size_in_words);
    SET_ARR_HDR(arr, &stg_ARR_WORDS_info, cap->r.rCCCS, size_in_bytes);
    return arr;
}

void
setThreadLabel(Capability  *cap,
               StgTSO      *tso,
               char *label)
{
    int len = strlen(label);
    StgArrBytes *arr = allocateArrBytes(cap, len);
    memcpy(&arr->payload, label, len);
    labelThread(cap, tso, arr);
}

void
labelThread(Capability  *cap,
            StgTSO      *tso,
            StgArrBytes *label)
{
    if (tso->label) {
        IF_NONMOVING_WRITE_BARRIER_ENABLED {
            updateRemembSetPushClosure(cap, (StgClosure *) tso->label);
        }
    }
    recordClosureMutated(cap, (StgClosure*)tso);
    RELEASE_STORE(&tso->label, label);
    traceThreadLabel(cap, tso, (char *) label->payload, label->bytes);
}