Skip to content

Commit

Permalink
iiod: thread-pool: Add function thread_pool_is_stopped
Browse files Browse the repository at this point in the history
This function can be used to know whether or not thread_pool_stop() was
called on a thread pool.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Oct 1, 2021
1 parent aad53e3 commit d97a476
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions iiod/thread-pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct thread_pool {
pthread_cond_t thread_count_cond;
unsigned int thread_count;
int stop_fd;
bool stop;
};

struct thread_body_data {
Expand Down Expand Up @@ -130,6 +131,8 @@ struct thread_pool * thread_pool_new(void)
return NULL;
}

pool->stop = false;

pthread_mutex_init(&pool->thread_count_lock, NULL);
pthread_cond_init(&pool->thread_count_cond, NULL);
pool->thread_count = 0;
Expand All @@ -147,6 +150,8 @@ void thread_pool_stop(struct thread_pool *pool)
uint64_t e = 1;
int ret;

pool->stop = true;

do {
ret = write(pool->stop_fd, &e, sizeof(e));
} while (ret == -1 && errno == EINTR);
Expand All @@ -170,6 +175,11 @@ void thread_pool_stop_and_wait(struct thread_pool *pool)
} while (ret != -1 || errno == EINTR);
}

bool thread_pool_is_stopped(const struct thread_pool *pool)
{
return pool->stop;
}

void thread_pool_destroy(struct thread_pool *pool)
{
pthread_mutex_destroy(&pool->thread_count_lock);
Expand Down
3 changes: 3 additions & 0 deletions iiod/thread-pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__

#include <stdbool.h>

struct thread_pool;

struct thread_pool * thread_pool_new(void);

int thread_pool_get_poll_fd(const struct thread_pool *pool);
void thread_pool_stop(struct thread_pool *pool);
void thread_pool_stop_and_wait(struct thread_pool *pool);
bool thread_pool_is_stopped(const struct thread_pool *pool);

void thread_pool_destroy(struct thread_pool *pool);

Expand Down

0 comments on commit d97a476

Please sign in to comment.