|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <tc_util.h> |
| 8 | +#include <pthread.h> |
| 9 | + |
| 10 | +int pthread_cancel(pthread_t pthread); |
| 11 | +#define N_THR 4 |
| 12 | +#define STACKSZ 1024 |
| 13 | +#define ONE_SECOND 1 |
| 14 | +#define THREAD_PRIORITY 3 |
| 15 | + |
| 16 | +K_THREAD_STACK_ARRAY_DEFINE(stacks, N_THR, STACKSZ); |
| 17 | +int exit_count; |
| 18 | + |
| 19 | +void *thread_top(void *p1) |
| 20 | +{ |
| 21 | + pthread_t self; |
| 22 | + int oldstate; |
| 23 | + int val = (u32_t) p1; |
| 24 | + |
| 25 | + if (val % 2) { |
| 26 | + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); |
| 27 | + } |
| 28 | + |
| 29 | + self = pthread_self(); |
| 30 | + TC_PRINT("Canceling thread %d\n", (s32_t) p1); |
| 31 | + pthread_cancel(self); |
| 32 | + TC_PRINT("Thread %d could not be canceled\n", (s32_t) p1); |
| 33 | + sleep(ONE_SECOND); |
| 34 | + exit_count++; |
| 35 | + TC_PRINT("Exiting thread %d\n", (s32_t)p1); |
| 36 | + pthread_exit(p1); |
| 37 | + TC_PRINT("Exited thread %d\n", (s32_t)p1); |
| 38 | + return NULL; |
| 39 | +} |
| 40 | + |
| 41 | +void main(void) |
| 42 | +{ |
| 43 | + s32_t i, ret, status = TC_FAIL; |
| 44 | + pthread_attr_t attr[N_THR]; |
| 45 | + struct sched_param schedparam; |
| 46 | + pthread_t newthread[N_THR]; |
| 47 | + void *retval; |
| 48 | + |
| 49 | + TC_START("POSIX thread cancel APIs\n"); |
| 50 | + /* Creating 4 threads with lowest application priority */ |
| 51 | + for (i = 0; i < N_THR; i++) { |
| 52 | + pthread_attr_init(&attr[i]); |
| 53 | + |
| 54 | + if (i == 0) { |
| 55 | + pthread_attr_setdetachstate(&attr[i], |
| 56 | + PTHREAD_CREATE_JOINABLE); |
| 57 | + } else if (i == 1) { |
| 58 | + pthread_attr_setdetachstate(&attr[i], |
| 59 | + PTHREAD_CREATE_JOINABLE); |
| 60 | + } else if (i == 2) { |
| 61 | + pthread_attr_setdetachstate(&attr[i], |
| 62 | + PTHREAD_CREATE_DETACHED); |
| 63 | + } else if (i == 3) { |
| 64 | + pthread_attr_setdetachstate(&attr[i], |
| 65 | + PTHREAD_CREATE_DETACHED); |
| 66 | + } |
| 67 | + schedparam.priority = 2; |
| 68 | + pthread_attr_setschedparam(&attr[i], &schedparam); |
| 69 | + pthread_attr_setstack(&attr[i], &stacks[i][0], STACKSZ); |
| 70 | + ret = pthread_create(&newthread[i], &attr[i], thread_top, |
| 71 | + (void *)i); |
| 72 | + if (ret != 0) { |
| 73 | + TC_ERROR("Number of threads exceeds Maximum Limit\n"); |
| 74 | + goto done; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (i = 0; i < N_THR; i++) { |
| 79 | + TC_PRINT("Waiting for pthread %d to Join\n", i); |
| 80 | + pthread_join(newthread[i], &retval); |
| 81 | + TC_PRINT("Pthread %d joined to %s\n", i, __func__); |
| 82 | + } |
| 83 | + |
| 84 | + TC_PRINT("pthread join test over\n"); |
| 85 | + |
| 86 | + /* Test PASS if all threads have exited before main exit */ |
| 87 | + status = exit_count == 1 ? TC_PASS : TC_FAIL; |
| 88 | + sleep(ONE_SECOND); |
| 89 | +done: |
| 90 | + TC_END_REPORT(status); |
| 91 | +} |
0 commit comments