Skip to content

Commit 597a90d

Browse files
weihsinyehidoleat
andcommitted
Simplify the operation of test and set
1. When creating the future, set the future's flag, which is akin to assigning the job. Afterward, transfer the ownership to the worker. Once the worker completes the job, clear the flag and return the ownership, which is akin to submitting a job. Then, the main thread can regain ownership. By doing this, the main thread can wait directly for the result through test and set without checking if the result is NULL. This avoids the situation where the flag could be set to true by the main thread before the worker starts the job. Additionally, the worker does not need to check with test and set before performing the job. 2. Drop the `atomic_flag_clear` in `tpool_future_wait` function and then directly free the pointer of future and its result in `tpool_future_destroy` function. 3. Rename the variable 'lock' in the future structure to 'flag'. Rename the function name `tpool_future_get` to `tpool_future_wait`. Co-authored-by: Chih-Wei Chien <[email protected]> Signed-off-by: Wei-Hsin Yeh <[email protected]>
1 parent 5a060d3 commit 597a90d

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

examples/rmw_example.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <assert.h>
77

88
#include <math.h>
9-
#include <stddef.h>
109

1110
#define PRECISION 100 /* upper bound in BPP sum */
1211

@@ -15,7 +14,7 @@
1514

1615
struct tpool_future {
1716
void *result;
18-
atomic_flag lock;
17+
atomic_flag flag;
1918
};
2019

2120
typedef struct job {
@@ -48,32 +47,22 @@ static struct tpool_future *tpool_future_create(void)
4847
struct tpool_future *future = malloc(sizeof(struct tpool_future));
4948
if (future) {
5049
future->result = NULL;
51-
atomic_flag_clear(&future->lock);
50+
atomic_flag_clear(&future->flag);
51+
atomic_flag_test_and_set(&future->flag);
5252
}
5353
return future;
5454
}
5555

56-
void tpool_future_get(struct tpool_future *future)
56+
void tpool_future_wait(struct tpool_future *future)
5757
{
58-
while (future->result == NULL)
58+
while (atomic_flag_test_and_set(&future->flag))
5959
;
60-
while (atomic_flag_test_and_set(&future->lock))
61-
;
62-
atomic_flag_clear(&future->lock);
6360
}
6461

65-
int tpool_future_destroy(struct tpool_future *future)
62+
void tpool_future_destroy(struct tpool_future *future)
6663
{
67-
if (future) {
68-
while (atomic_flag_test_and_set(&future->lock))
69-
;
70-
if (future->result != NULL) {
71-
free(future);
72-
} else {
73-
atomic_flag_clear(&future->lock);
74-
}
75-
}
76-
return 0;
64+
free(future->result);
65+
free(future);
7766
}
7867

7968
static int worker(void *args)
@@ -95,12 +84,8 @@ static int worker(void *args)
9584
atomic_store(&thrd_pool->state, idle);
9685
} else {
9786
void *ret_value = job->func(job->args);
98-
99-
while (atomic_flag_test_and_set(&job->future->lock))
100-
;
10187
job->future->result = ret_value;
102-
103-
atomic_flag_clear(&job->future->lock);
88+
atomic_flag_clear(&job->future->flag);
10489
free(job->args);
10590
free(job);
10691
}
@@ -243,7 +228,7 @@ int main()
243228
add_job(&thrd_pool, bbp, id);
244229
}
245230
for (int i = 0; i <= PRECISION; i++) {
246-
tpool_future_get(futures[i]);
231+
tpool_future_wait(futures[i]);
247232
bbp_sum += *(double *)(futures[i]->result);
248233
tpool_future_destroy(futures[i]);
249234
}

0 commit comments

Comments
 (0)