aboutsummaryrefslogtreecommitdiff
path: root/test/test-stop-threads.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/test-stop-threads.c')
-rw-r--r--test/test-stop-threads.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/test-stop-threads.c b/test/test-stop-threads.c
new file mode 100644
index 0000000..be0787d
--- /dev/null
+++ b/test/test-stop-threads.c
@@ -0,0 +1,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));
+}