aboutsummaryrefslogtreecommitdiff
path: root/test/test-stop-threads.c
blob: be0787da6bd770490991292ce17efe30bb3f7170 (plain) (blame)
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
#include "substitute-internal.h"
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
static void *some_thread(void *ip) {
    long i = (long) ip;
    while (1) {
        printf("Hello from %ld\n", i);
        sleep(1);
    }
}
static void replacement() {
    printf("Bye\n");
    pthread_exit(NULL);
}
static uintptr_t patch_callback(void *ctx, UNUSED uintptr_t pc) {
    assert(!ctx);
    return (uintptr_t) replacement;
}

int main() {
    pthread_t pts[10];
    for (long i = 0; i < 10; i++)
        pthread_create(&pts[i], NULL, some_thread, (void *) i);
    sleep(1);
    void *stop_token;
    assert(!stop_other_threads(&stop_token));
    assert(!apply_pc_patch_callback(stop_token, patch_callback, NULL));
    assert(!resume_other_threads(stop_token));
    void *out;
    for (long i = 0; i < 10; i++)
        assert(!pthread_join(pts[i], &out));
}