Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronization functions are now thread pool specific #43

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions thpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#endif

static volatile int threads_keepalive;
static volatile int threads_on_hold;



Expand Down Expand Up @@ -76,6 +75,7 @@ typedef struct thpool_{
pthread_mutex_t thcount_lock; /* used for thread count etc */
pthread_cond_t threads_all_idle; /* signal to thpool_wait */
jobqueue* jobqueue_p; /* pointer to the job queue */
int paused; /* thread pool is paused */
} thpool_;


Expand Down Expand Up @@ -112,7 +112,6 @@ static void bsem_wait(struct bsem *bsem_p);
/* Initialise thread pool */
struct thpool_* thpool_init(int num_threads){

threads_on_hold = 0;
threads_keepalive = 1;

if (num_threads < 0){
Expand Down Expand Up @@ -240,16 +239,24 @@ void thpool_destroy(thpool_* thpool_p){

/* Pause all threads in threadpool */
void thpool_pause(thpool_* thpool_p) {
int n;
/* construct signal handler values for this thread pool */
union sigval value;
value.sival_ptr = (void *)thpool_p;

/* mark threadpool as paused */
thpool_p->paused = 1;

/* send sigqueue with specific thpool */
int n;
for (n=0; n < thpool_p->num_threads_alive; n++){
pthread_kill(thpool_p->threads[n]->pthread, SIGUSR1);
pthread_sigqueue(thpool_p->threads[n]->pthread, SIGUSR1, value);
}
}


/* Resume all threads in threadpool */
void thpool_resume(thpool_* thpool_p) {
threads_on_hold = 0;
thpool_p->paused = 0;
}


Expand Down Expand Up @@ -282,12 +289,11 @@ static int thread_init (thpool_* thpool_p, struct thread** thread_p, int id){
}


/* Sets the calling thread on hold */
static void thread_hold () {
threads_on_hold = 1;
while (threads_on_hold){
sleep(1);
}
/* Holds thread until parent threadpool resumes */
static void thread_hold (int signo, siginfo_t *info, void *extra) {
while (((struct thpool_*)(info->si_ptr))->paused){
sleep(1);
}
}


Expand Down Expand Up @@ -320,8 +326,8 @@ static void* thread_do(struct thread* thread_p){
/* Register signal handler */
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = thread_hold;
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = thread_hold;
if (sigaction(SIGUSR1, &act, NULL) == -1) {
fprintf(stderr, "thread_do(): cannot handle SIGUSR1");
}
Expand Down