1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
3 | #ident "$Id$" |
4 | /*====== |
5 | This file is part of PerconaFT. |
6 | |
7 | |
8 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
9 | |
10 | PerconaFT is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License, version 2, |
12 | as published by the Free Software Foundation. |
13 | |
14 | PerconaFT is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
21 | |
22 | ---------------------------------------- |
23 | |
24 | PerconaFT is free software: you can redistribute it and/or modify |
25 | it under the terms of the GNU Affero General Public License, version 3, |
26 | as published by the Free Software Foundation. |
27 | |
28 | PerconaFT is distributed in the hope that it will be useful, |
29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31 | GNU Affero General Public License for more details. |
32 | |
33 | You should have received a copy of the GNU Affero General Public License |
34 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
35 | ======= */ |
36 | |
37 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
38 | |
39 | #pragma once |
40 | |
41 | #include <toku_pthread.h> |
42 | #include <toku_time.h> |
43 | |
44 | // Specification: |
45 | // A minicron is a miniature cron job for executing a job periodically inside a pthread. |
46 | // To create a minicron, |
47 | // 1) allocate a "struct minicron" somewhere. |
48 | // Rationale: This struct can be stored inside another struct (such as the cachetable), avoiding a malloc/free pair. |
49 | // 2) call toku_minicron_setup, specifying a period (in milliseconds), a function, and some arguments. |
50 | // If the period is positive then the function is called periodically (with the period specified) |
51 | // Note: The period is measured from when the previous call to f finishes to when the new call starts. |
52 | // Thus, if the period is 5 minutes, and it takes 8 minutes to run f, then the actual periodicity is 13 minutes. |
53 | // Rationale: If f always takes longer than f to run, then it will get "behind". This module makes getting behind explicit. |
54 | // 3) When finished, call toku_minicron_shutdown. |
55 | // 4) If you want to change the period, then call toku_minicron_change_period. The time since f finished is applied to the new period |
56 | // and the call is rescheduled. (If the time since f finished is more than the new period, then f is called immediately). |
57 | |
58 | struct minicron { |
59 | toku_pthread_t thread; |
60 | toku_timespec_t time_of_last_call_to_f; |
61 | toku_mutex_t mutex; |
62 | toku_cond_t condvar; |
63 | int (*f)(void*); |
64 | void *arg; |
65 | uint32_t period_in_ms; |
66 | bool do_shutdown; |
67 | }; |
68 | |
69 | int toku_minicron_setup (struct minicron *s, uint32_t period_in_ms, int(*f)(void *), void *arg); |
70 | void toku_minicron_change_period(struct minicron *p, uint32_t new_period); |
71 | uint32_t toku_minicron_get_period_in_seconds_unlocked(struct minicron *p); |
72 | uint32_t toku_minicron_get_period_in_ms_unlocked(struct minicron *p); |
73 | int toku_minicron_shutdown(struct minicron *p); |
74 | bool toku_minicron_has_been_shutdown(struct minicron *p); |
75 | |