Skip to content

Commit bd9ce93

Browse files
committed
Check if job is finished by the result of future
1. Check if `future->result` is NULL. - If `future->result` is NULL, the job is still in progress. - If `future->result` is not NULL, the job has been completed by the worker.
1 parent f37c425 commit bd9ce93

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

examples/rmw_example.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define N_THREADS 64
1515

1616
struct tpool_future {
17-
atomic_int flag;
1817
void *result;
1918
atomic_flag lock;
2019
};
@@ -32,7 +31,7 @@ typedef struct idle_job {
3231
job_t job;
3332
} idle_job_t;
3433

35-
enum state { __FUTURE_START, __FUTURE_FINISHED, idle, running, cancelled };
34+
enum state { idle, running, cancelled };
3635

3736
typedef struct tpool {
3837
atomic_flag initialezed;
@@ -48,7 +47,6 @@ static struct tpool_future *tpool_future_create(void)
4847
{
4948
struct tpool_future *future = malloc(sizeof(struct tpool_future));
5049
if (future) {
51-
future->flag = __FUTURE_START;
5250
future->result = NULL;
5351
atomic_flag_clear(&future->lock);
5452
}
@@ -57,7 +55,7 @@ static struct tpool_future *tpool_future_create(void)
5755

5856
void tpool_future_get(struct tpool_future *future)
5957
{
60-
while (future->flag != __FUTURE_FINISHED)
58+
while (future->result == NULL)
6159
;
6260
while (atomic_flag_test_and_set(&future->lock))
6361
;
@@ -69,7 +67,7 @@ int tpool_future_destroy(struct tpool_future *future)
6967
if (future) {
7068
while (atomic_flag_test_and_set(&future->lock))
7169
;
72-
if (future->flag & __FUTURE_FINISHED) {
70+
if (future->result != NULL) {
7371
free(future);
7472
} else {
7573
atomic_flag_clear(&future->lock);
@@ -100,7 +98,6 @@ static int worker(void *args)
10098

10199
while (atomic_flag_test_and_set(&job->future->lock))
102100
;
103-
job->future->flag |= __FUTURE_FINISHED;
104101
job->future->result = ret_value;
105102

106103
atomic_flag_clear(&job->future->lock);

0 commit comments

Comments
 (0)