clamd/thrmgr.h
c238ac42
 /*
206dbaef
  *  Copyright (C) 2013-2020 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
52cddcbc
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
086eab5c
  *
  *  Authors: Tomasz Kojm, Török Edvin
c238ac42
  *
  *  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 2 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, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
c238ac42
  */
 
 #ifndef __THRMGR_H__
 #define __THRMGR_H__
 
 #include <pthread.h>
67118e92
 
e0bb54d7
 #ifndef _WIN32
52e8d3c6
 #include <sys/time.h>
67118e92
 #endif
52e8d3c6
 
 typedef struct work_item_tag {
72fd33c8
     struct work_item_tag *next;
     void *data;
     struct timeval time_queued;
52e8d3c6
 } work_item_t;
72fd33c8
 
52e8d3c6
 typedef struct work_queue_tag {
72fd33c8
     work_item_t *head;
     work_item_t *tail;
     int item_count;
     int popped;
52e8d3c6
 } work_queue_t;
 
 typedef enum {
72fd33c8
     POOL_INVALID,
     POOL_VALID,
     POOL_EXIT
52e8d3c6
 } pool_state_t;
 
aa22174b
 struct task_desc {
72fd33c8
     const char *filename;
     const char *command;
     struct timeval tv;
     struct task_desc *prv;
     struct task_desc *nxt;
     const struct cl_engine *engine;
aa22174b
 };
 
52e8d3c6
 typedef struct threadpool_tag {
72fd33c8
     pthread_mutex_t pool_mutex;
     pthread_cond_t pool_cond;
     pthread_attr_t pool_attr;
 
     pthread_cond_t idle_cond;
     pthread_cond_t queueable_single_cond;
     pthread_cond_t queueable_bulk_cond;
 
     pool_state_t state;
     int thr_max;
     int queue_max;
     int thr_alive;
     int thr_idle;
     int thr_multiscan;
     int idle_timeout;
     struct task_desc *tasks;
 
     void (*handler)(void *);
 
     work_queue_t *bulk_queue;
     work_queue_t *single_queue;
52e8d3c6
 } threadpool_t;
 
949c6fe5
 typedef struct jobgroup {
     pthread_mutex_t mutex;
     pthread_cond_t only;
72fd33c8
     unsigned jobs;
     unsigned exit_ok;
     unsigned exit_error;
     unsigned exit_total;
     int force_exit;
949c6fe5
 } jobgroup_t;
 
 enum thrmgr_exit {
     EXIT_OK,
     EXIT_ERROR,
     EXIT_OTHER
 };
 
 threadpool_t *thrmgr_new(int max_threads, int idle_timeout, int max_queue, void (*handler)(void *));
52e8d3c6
 void thrmgr_destroy(threadpool_t *threadpool);
dac084fb
 void thrmgr_wait_for_threads(threadpool_t *threadpool);
52e8d3c6
 int thrmgr_dispatch(threadpool_t *threadpool, void *user_data);
1514794c
 int thrmgr_group_dispatch(threadpool_t *threadpool, jobgroup_t *group, void *user_data, int bulk);
949c6fe5
 void thrmgr_group_waitforall(jobgroup_t *group, unsigned *ok, unsigned *error, unsigned *total);
 int thrmgr_group_finished(jobgroup_t *group, enum thrmgr_exit exitc);
 int thrmgr_group_need_terminate(jobgroup_t *group);
0378a9ab
 void thrmgr_group_terminate(jobgroup_t *group);
949c6fe5
 jobgroup_t *thrmgr_group_new(void);
f2381892
 int thrmgr_printstats(int outfd, char term);
72fd33c8
 void thrmgr_setactivetask(const char *filename, const char *command);
deb30312
 void thrmgr_setactiveengine(const struct cl_engine *engine);
c238ac42
 
 #endif