From 9b99cf94012cac8772a7a7a39554a9daecc53e19 Mon Sep 17 00:00:00 2001 From: Tom Cort Date: Mon, 26 Nov 2007 23:54:41 -0500 Subject: [PATCH] Add a thread monitor. This module contains functions to manipulate a thread counter in an atomic way. It also includes a wait function to ensure that all threads exit before the program exits. --- monitor.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ monitor.h | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 monitor.c create mode 100644 monitor.h diff --git a/monitor.c b/monitor.c new file mode 100644 index 0000000..7719761 --- /dev/null +++ b/monitor.c @@ -0,0 +1,90 @@ +/* + * inoclam - Inotify+ClamAV virus scanner + * Copyright (C) 2007 Vermont Department of Taxes + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Contributor(s): + * Tom Cort + */ + +#define _XOPEN_SOURCE 500 + +#include +#include +#include +#include + +#include "monitor.h" + +/** + * Counter for the number of running threads. + */ +int cnt; + +/** + * A lock used to serialize access to cnt. + */ +pthread_mutex_t mon_lock; + +/** + * initialize monitor variables + */ +void monitor_init() +{ + pthread_mutex_init(&mon_lock, 0); + cnt = 0; +} + +/** + * increments the thread count + */ +void monitor_inc() +{ + pthread_mutex_lock(&mon_lock); + cnt++; + pthread_mutex_unlock(&mon_lock); +} + +/** + * decrements the thread count + */ +void monitor_dec() +{ + pthread_mutex_lock(&mon_lock); + cnt--; + pthread_mutex_unlock(&mon_lock); +} + +/** + * waits until no threads are running + * blocks new threads from being created + */ +void monitor_wait() +{ + raise(SIGUSR1); + + while (1) { + pthread_mutex_lock(&mon_lock); + + if (!cnt) { + /* Do NOT release lock; we don't want any more threads starting */ + return; + } else { + pthread_mutex_unlock(&mon_lock); + sched_yield(); + sleep(3); + } + } +} diff --git a/monitor.h b/monitor.h new file mode 100644 index 0000000..00c2996 --- /dev/null +++ b/monitor.h @@ -0,0 +1,46 @@ +/* + * inoclam - Inotify+ClamAV virus scanner + * Copyright (C) 2007 Vermont Department of Taxes + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Contributor(s): + * Tom Cort + */ + +#ifndef __MONITOR_H +#define __MONITOR_H + +/** + * initialize monitor variables + */ +void monitor_init(); + +/** + * increments the thread count + */ +void monitor_inc(); + +/** + * decrements the thread count + */ +void monitor_dec(); + +/** + * waits until no threads are running + * blocks new threads from being created + */ +void monitor_wait(); + +#endif -- 2.11.4.GIT