Olivier Houchard | cea46c0 | 2019-09-23 17:42:42 +0200 | [diff] [blame] | 1 | #include <pthread.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #define USE_THREAD |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 5 | #include <haproxy/list.h> |
Olivier Houchard | cea46c0 | 2019-09-23 17:42:42 +0200 | [diff] [blame] | 6 | |
| 7 | /* Stress test the mt_lists. |
| 8 | * Compile from the haproxy directory with : |
| 9 | * cc -Iinclude tests/test-list.c -lpthread -O2 -o test-list |
| 10 | * The only argument it takes is the number of threads to be used. |
| 11 | * ./test-list 4 |
| 12 | */ |
| 13 | |
| 14 | struct mt_list pouet_list = MT_LIST_HEAD_INIT(pouet_list); |
| 15 | #define MAX_ACTION 5000000 |
| 16 | |
| 17 | __thread unsigned int tid; |
| 18 | struct pouet_lol { |
| 19 | struct mt_list list_elt; |
| 20 | }; |
| 21 | |
| 22 | void *thread(void *pouet) |
| 23 | { |
| 24 | struct pouet_lol *lol; |
| 25 | struct mt_list *elt1, elt2; |
| 26 | tid = (uintptr_t)pouet; |
| 27 | int i = 0; |
| 28 | |
| 29 | for (int i = 0; i < MAX_ACTION; i++) { |
| 30 | struct pouet_lol *lol; |
| 31 | struct mt_list *elt1, elt2; |
| 32 | switch (random() % 4) { |
| 33 | case 0: |
| 34 | lol = malloc(sizeof(*lol)); |
| 35 | MT_LIST_INIT(&lol->list_elt); |
Willy Tarreau | de4db17 | 2020-07-10 08:10:29 +0200 | [diff] [blame^] | 36 | MT_LIST_TRY_ADD(&pouet_list, &lol->list_elt); |
Olivier Houchard | cea46c0 | 2019-09-23 17:42:42 +0200 | [diff] [blame] | 37 | break; |
| 38 | case 1: |
| 39 | lol = malloc(sizeof(*lol)); |
| 40 | MT_LIST_INIT(&lol->list_elt); |
Willy Tarreau | de4db17 | 2020-07-10 08:10:29 +0200 | [diff] [blame^] | 41 | MT_LIST_TRY_ADDQ(&pouet_list, &lol->list_elt); |
Olivier Houchard | cea46c0 | 2019-09-23 17:42:42 +0200 | [diff] [blame] | 42 | break; |
| 43 | |
| 44 | case 2: |
| 45 | lol = MT_LIST_POP(&pouet_list, struct pouet_lol *, list_elt); |
| 46 | if (lol) |
| 47 | free(lol); |
| 48 | break; |
| 49 | case 3: |
| 50 | |
| 51 | mt_list_for_each_entry_safe(lol, &pouet_list, list_elt, elt1, elt2) |
| 52 | |
| 53 | { |
| 54 | if (random() % 2) { |
| 55 | MT_LIST_DEL_SAFE(elt1); |
| 56 | free(lol); |
| 57 | } |
| 58 | if (random() % 2) { |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | break; |
| 63 | default: |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int main(int argc, char *argv[]) |
| 70 | { |
| 71 | int nb; |
| 72 | pthread_t *pth; |
| 73 | |
| 74 | srandom(time(NULL)); |
| 75 | if (argc != 2) { |
| 76 | printf("Usage: %s <nb_threads>\n", argv[0]); |
| 77 | exit(1); |
| 78 | } |
| 79 | nb = atoi(argv[1]); |
| 80 | #if 0 |
| 81 | if (nb < 2) { |
| 82 | printf("Need at least 2 threads.\n"); |
| 83 | exit(1); |
| 84 | } |
| 85 | #endif |
| 86 | pth = malloc(nb * sizeof(*pth)); |
| 87 | if (pth == NULL) { |
| 88 | printf("Shot failed to connect.\n"); |
| 89 | exit(1); |
| 90 | } |
| 91 | for (int i = 0; i < nb; i++) { |
| 92 | pthread_create(&pth[i], NULL, thread, (void *)(uintptr_t)i); |
| 93 | |
| 94 | } |
| 95 | for (int i = 0; i < nb; i++) |
| 96 | pthread_join(pth[i], NULL); |
| 97 | return 0; |
| 98 | } |