ze-filter  (ze-filter-0.8.0-develop-180218)
zeCycTasks.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : Fri Apr 28 11:02:56 CEST 2006
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 
25 #include <ze-sys.h>
26 #include <ze-filter.h>
27 #include <zeCycTasks.h>
28 
29 /* ****************************************************************************
30  * *
31  * *
32  **************************************************************************** */
33 #define DIMTASKS 256
34 
35 typedef struct
36 {
38  time_t dt;
39 
40  CYCLIC_F function;
41  void *arg;
42 
43  time_t last;
44  int count;
45  time_t work;
46 
48 } CTASK_T;
49 
50 static CTASK_T tasks[DIMTASKS];
51 static bool tsk_ok = FALSE;
52 
53 static time_t dt_loop = 10;
54 static time_t t_start = 0;
55 
56 static void *CycTasks(void *arg);
57 
58 /* ****************************************************************************
59  * *
60  * *
61  **************************************************************************** */
62 bool
64  time_t dt;
65 {
66  pthread_t tid = (pthread_t) 0;
67 
68  if (!tsk_ok)
69  {
70  int r;
71 
72  t_start = time(NULL);
73  dt_loop = dt;
74  memset(tasks, 0, sizeof (tasks));
75 
76  if ((r = pthread_create(&tid, NULL, CycTasks, (void *) NULL)) != 0)
77  ZE_LogSysError("Couldn't launch CycTasks");
78 
79  tsk_ok = TRUE;
80  }
81 
82  return TRUE;
83 }
84 
85 /* ****************************************************************************
86  * *
87  * *
88  **************************************************************************** */
89 
90 bool
91 CycTasks_Register(task, arg, dt)
92  CYCLIC_F task;
93  void *arg;
94  time_t dt;
95 {
96  int i;
97 
98  ASSERT(task != NULL);
99  ASSERT(dt > 0);
100 
101  for (i = 0; i < DIMTASKS; i++)
102  {
103  if (tasks[i].signature == SIGNATURE)
104  continue;
105 
106  memset(&tasks[i], 0, sizeof(tasks[i]));
107 
108  if (tasks[i].function != NULL)
109  continue;
110 
111  tasks[i].signature = SIGNATURE;
112  tasks[i].function = task;
113  tasks[i].arg = arg;
114  tasks[i].dt = dt;
115  tasks[i].last = time(NULL);
116 
117  return TRUE;
118  }
119 
120  ZE_MessageWarning(5, "No more room available for new cyclic tasks 8-( !");
121  return FALSE;
122 }
123 
124 /* ****************************************************************************
125  * *
126  * *
127  **************************************************************************** */
128 void
130 {
131  int i;
132 
133  for (i = 0; i < DIMTASKS; i++)
134  {
135  int n, m;
136 
137  if (tasks[i].signature != SIGNATURE)
138  continue;
139 
140  n = m = 0;
141  n = tasks[i].count;
142  if (tasks[i].count > 0)
143  m = tasks[i].work / tasks[i].count;
144  ZE_MessageInfo(10, "Cyclic task %3d : n=%5d m=%6d", i, n, m);
145  }
146 }
147 
148 /* ****************************************************************************
149  * *
150  * *
151  **************************************************************************** */
152 static void *
153 CycTasks(arg)
154  void *arg;
155 {
156  pthread_t tid;
157 
158  time_t now, last;
159 
160  now = last = time(NULL);
161 
162  tid = pthread_self();
163  (void) pthread_detach(tid);
164 
165  for (;;)
166  {
167  int i;
168 
169  ZE_MessageInfo(12, "*** CycTasks running...");
170 
171  sleep(dt_loop);
172  now = time(NULL);
173 
174  if (last + dt_loop > now)
175  continue;
176 
177  last = now;
178 
179  for (i = 0; i < DIMTASKS; i++)
180  {
181  uint64_t tms;
182 
183  if (tasks[i].signature != SIGNATURE)
184  continue;
185 
186  if (tasks[i].last + tasks[i].dt > now)
187  continue;
188 
189  if (tasks[i].function == NULL)
190  continue;
191 
192  tasks[i].last = now;
193 
194  /* XXX do something with result ??? */
195  tms = zeTime_ms();
196  (void) tasks[i].function(tasks[i].arg);
197  tasks[i].work += (zeTime_ms() - tms);
198  tasks[i].count++;
199  }
200  }
201  return NULL;
202 }
203 
204 
205 /* ****************************************************************************
206  * *
207  * *
208  **************************************************************************** */
209 #if 0
210 int
211 test_task(void *arg)
212 {
213  ZE_MessageInfo(10, "Hi ! %s : %ld", STRNULL(arg, "(null)"), time(NULL) - t_start);
214 
215  return 0;
216 }
217 #endif
#define DIMTASKS
Definition: zeCycTasks.c:33
#define ASSERT(a)
Definition: macros.h:27
int(* CYCLIC_F)(void *)
Definition: zeCycTasks.h:27
void * arg
Definition: zeCycTasks.c:41
time_t dt
Definition: zeCycTasks.c:38
int count
Definition: zeCycTasks.c:44
uint64_t zeTime_ms()
Definition: zeTime.c:34
#define STRNULL(x, r)
Definition: macros.h:81
#define FALSE
Definition: macros.h:160
CYCLIC_F function
Definition: zeCycTasks.c:40
bool CycTasks_Register(CYCLIC_F task, void *arg, time_t dt)
Definition: zeCycTasks.c:91
time_t last
Definition: zeCycTasks.c:43
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
#define ZE_MessageWarning(level,...)
Definition: zeSyslog.h:92
uint32_t signature
Definition: zeCycTasks.c:37
kstats_T st
Definition: zeCycTasks.c:47
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
time_t last
Definition: ze-connopen.c:60
time_t work
Definition: zeCycTasks.c:45
void CycTasks_Stats()
Definition: zeCycTasks.c:129
#define SIGNATURE
Definition: ze-libjc.h:75
long uint32_t
Definition: ze-sys.h:489
bool CycTasks_Init(time_t dt)
Definition: zeCycTasks.c:63