|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */ |
| 2 | +/* |
| 3 | + * See COPYRIGHT in top-level directory. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include "abt.h" |
| 9 | +#include "abttest.h" |
| 10 | + |
| 11 | +#define DEFAULT_NUM_THREADS 4 |
| 12 | + |
| 13 | +void thread_func(void *arg) |
| 14 | +{ |
| 15 | + int ret; |
| 16 | + void *stackaddr1 = NULL, *stackaddr2 = NULL; |
| 17 | + size_t stacksize1 = 0, stacksize2 = 0; |
| 18 | + ABT_thread self; |
| 19 | + ABT_thread_attr attr; |
| 20 | + |
| 21 | + /* Get self */ |
| 22 | + ret = ABT_thread_self(&self); |
| 23 | + ATS_ERROR(ret, "ABT_thread_self"); |
| 24 | + |
| 25 | + /* Method 1: New direct API */ |
| 26 | + ret = ABT_thread_get_stack(self, &stackaddr1, &stacksize1); |
| 27 | + ATS_ERROR(ret, "ABT_thread_get_stack"); |
| 28 | + |
| 29 | + /* Method 2: Old method via attributes (for comparison) */ |
| 30 | + ret = ABT_thread_get_attr(self, &attr); |
| 31 | + ATS_ERROR(ret, "ABT_thread_get_attr"); |
| 32 | + |
| 33 | + ret = ABT_thread_attr_get_stack(attr, &stackaddr2, &stacksize2); |
| 34 | + ATS_ERROR(ret, "ABT_thread_attr_get_stack"); |
| 35 | + |
| 36 | + ret = ABT_thread_attr_free(&attr); |
| 37 | + ATS_ERROR(ret, "ABT_thread_attr_free"); |
| 38 | + |
| 39 | + /* Verify both methods return the same values */ |
| 40 | + if (stackaddr1 != stackaddr2) { |
| 41 | + ATS_ERROR(ABT_ERR_OTHER, "Stack addresses don't match"); |
| 42 | + } |
| 43 | + if (stacksize1 != stacksize2) { |
| 44 | + ATS_ERROR(ABT_ERR_OTHER, "Stack sizes don't match"); |
| 45 | + } |
| 46 | + |
| 47 | + ATS_printf(2, "Thread: stackaddr=%p, stacksize=%zu\n", stackaddr1, |
| 48 | + stacksize1); |
| 49 | + |
| 50 | + /* Test partial queries */ |
| 51 | + void *stackaddr_only = NULL; |
| 52 | + ret = ABT_thread_get_stack(self, &stackaddr_only, NULL); |
| 53 | + ATS_ERROR(ret, "ABT_thread_get_stack (addr only)"); |
| 54 | + if (stackaddr_only != stackaddr1) { |
| 55 | + ATS_ERROR(ABT_ERR_OTHER, "Stackaddr-only query mismatch"); |
| 56 | + } |
| 57 | + |
| 58 | + size_t stacksize_only = 0; |
| 59 | + ret = ABT_thread_get_stack(self, NULL, &stacksize_only); |
| 60 | + ATS_ERROR(ret, "ABT_thread_get_stack (size only)"); |
| 61 | + if (stacksize_only != stacksize1) { |
| 62 | + ATS_ERROR(ABT_ERR_OTHER, "Stacksize-only query mismatch"); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +int main(int argc, char *argv[]) |
| 67 | +{ |
| 68 | + int ret, i; |
| 69 | + int num_threads = DEFAULT_NUM_THREADS; |
| 70 | + ABT_xstream xstream; |
| 71 | + ABT_pool pool; |
| 72 | + ABT_thread *threads; |
| 73 | + |
| 74 | + /* Initialize */ |
| 75 | + ATS_read_args(argc, argv); |
| 76 | + if (argc > 1) { |
| 77 | + num_threads = ATS_get_arg_val(ATS_ARG_N_ULT); |
| 78 | + } |
| 79 | + ATS_init(argc, argv, 1); |
| 80 | + |
| 81 | + ATS_printf(1, "# of ULTs: %d\n", num_threads); |
| 82 | + |
| 83 | + threads = (ABT_thread *)malloc(num_threads * sizeof(ABT_thread)); |
| 84 | + |
| 85 | + /* Get the primary execution stream */ |
| 86 | + ret = ABT_xstream_self(&xstream); |
| 87 | + ATS_ERROR(ret, "ABT_xstream_self"); |
| 88 | + |
| 89 | + /* Get the pool */ |
| 90 | + ret = ABT_xstream_get_main_pools(xstream, 1, &pool); |
| 91 | + ATS_ERROR(ret, "ABT_xstream_get_main_pools"); |
| 92 | + |
| 93 | + /* Create ULTs */ |
| 94 | + for (i = 0; i < num_threads; i++) { |
| 95 | + ret = ABT_thread_create(pool, thread_func, NULL, ABT_THREAD_ATTR_NULL, |
| 96 | + &threads[i]); |
| 97 | + ATS_ERROR(ret, "ABT_thread_create"); |
| 98 | + } |
| 99 | + |
| 100 | + /* Join and free ULTs */ |
| 101 | + for (i = 0; i < num_threads; i++) { |
| 102 | + ret = ABT_thread_free(&threads[i]); |
| 103 | + ATS_ERROR(ret, "ABT_thread_free"); |
| 104 | + } |
| 105 | + |
| 106 | + /* Test with external thread (should return NULL/0) */ |
| 107 | + void *ext_stackaddr = (void *)0xdeadbeef; |
| 108 | + size_t ext_stacksize = 12345; |
| 109 | + ABT_thread ext_thread; |
| 110 | + |
| 111 | + ret = ABT_thread_self(&ext_thread); |
| 112 | + ATS_ERROR(ret, "ABT_thread_self"); |
| 113 | + |
| 114 | + ret = ABT_thread_get_stack(ext_thread, &ext_stackaddr, &ext_stacksize); |
| 115 | + ATS_ERROR(ret, "ABT_thread_get_stack (external)"); |
| 116 | + |
| 117 | + ATS_printf(2, "External thread: stackaddr=%p, stacksize=%zu\n", |
| 118 | + ext_stackaddr, ext_stacksize); |
| 119 | + |
| 120 | + /* Note: External thread may have a stack or not depending on version */ |
| 121 | + /* Just verify the call succeeds */ |
| 122 | + |
| 123 | + /* Finalize */ |
| 124 | + ret = ATS_finalize(0); |
| 125 | + |
| 126 | + free(threads); |
| 127 | + |
| 128 | + return ret; |
| 129 | +} |
0 commit comments