1 // SPDX-License-Identifier: GPL-2.0+
3 * Helper functions to sync execution between parent and child processes.
5 * Copyright 2018, Thiago Jung Bauermann, IBM Corporation.
12 * Information in a shared memory location for synchronization between child and
16 /* The parent waits on this semaphore. */
19 /* If true, the child should give up as well. */
22 /* The child waits on this semaphore. */
25 /* If true, the parent should give up as well. */
29 #define CHILD_FAIL_IF(x, sync) \
33 "[FAIL] Test FAILED on line %d\n", __LINE__); \
34 (sync)->child_gave_up = true; \
40 #define PARENT_FAIL_IF(x, sync) \
44 "[FAIL] Test FAILED on line %d\n", __LINE__); \
45 (sync)->parent_gave_up = true; \
51 #define PARENT_SKIP_IF_UNSUPPORTED(x, sync) \
53 if ((x) == -1 && (errno == ENODEV || errno == EINVAL)) { \
54 (sync)->parent_gave_up = true; \
60 int init_child_sync(struct child_sync *sync)
64 ret = sem_init(&sync->sem_parent, 1, 0);
66 perror("Semaphore initialization failed");
70 ret = sem_init(&sync->sem_child, 1, 0);
72 perror("Semaphore initialization failed");
79 void destroy_child_sync(struct child_sync *sync)
81 sem_destroy(&sync->sem_parent);
82 sem_destroy(&sync->sem_child);
85 int wait_child(struct child_sync *sync)
89 /* Wait until the child prods us. */
90 ret = sem_wait(&sync->sem_parent);
92 perror("Error waiting for child");
96 return sync->child_gave_up;
99 int prod_child(struct child_sync *sync)
103 /* Unblock the child now. */
104 ret = sem_post(&sync->sem_child);
106 perror("Error prodding child");
113 int wait_parent(struct child_sync *sync)
117 /* Wait until the parent prods us. */
118 ret = sem_wait(&sync->sem_child);
120 perror("Error waiting for parent");
124 return sync->parent_gave_up;
127 int prod_parent(struct child_sync *sync)
131 /* Unblock the parent now. */
132 ret = sem_post(&sync->sem_parent);
134 perror("Error prodding parent");