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/*======
5This file is part of PerconaFT.
6
7
8Copyright (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#include <portability/toku_config.h>
40#include <memory.h>
41#include <toku_pthread.h>
42
43#include "cachetable/background_job_manager.h"
44
45toku_instr_key *bjm_jobs_lock_mutex_key;
46toku_instr_key *bjm_jobs_wait_key;
47
48struct background_job_manager_struct {
49 bool accepting_jobs;
50 uint32_t num_jobs;
51 toku_cond_t jobs_wait;
52 toku_mutex_t jobs_lock;
53};
54
55void bjm_init(BACKGROUND_JOB_MANAGER *pbjm) {
56 BACKGROUND_JOB_MANAGER XCALLOC(bjm);
57 toku_mutex_init(*bjm_jobs_lock_mutex_key, &bjm->jobs_lock, nullptr);
58 toku_cond_init(*bjm_jobs_wait_key, &bjm->jobs_wait, nullptr);
59 bjm->accepting_jobs = true;
60 bjm->num_jobs = 0;
61 *pbjm = bjm;
62}
63
64void bjm_destroy(BACKGROUND_JOB_MANAGER bjm) {
65 assert(bjm->num_jobs == 0);
66 toku_cond_destroy(&bjm->jobs_wait);
67 toku_mutex_destroy(&bjm->jobs_lock);
68 toku_free(bjm);
69}
70
71void bjm_reset(BACKGROUND_JOB_MANAGER bjm) {
72 toku_mutex_lock(&bjm->jobs_lock);
73 assert(bjm->num_jobs == 0);
74 bjm->accepting_jobs = true;
75 toku_mutex_unlock(&bjm->jobs_lock);
76}
77
78int bjm_add_background_job(BACKGROUND_JOB_MANAGER bjm) {
79 int ret_val;
80 toku_mutex_lock(&bjm->jobs_lock);
81 if (bjm->accepting_jobs) {
82 bjm->num_jobs++;
83 ret_val = 0;
84 }
85 else {
86 ret_val = -1;
87 }
88 toku_mutex_unlock(&bjm->jobs_lock);
89 return ret_val;
90}
91void bjm_remove_background_job(BACKGROUND_JOB_MANAGER bjm){
92 toku_mutex_lock(&bjm->jobs_lock);
93 assert(bjm->num_jobs > 0);
94 bjm->num_jobs--;
95 if (bjm->num_jobs == 0 && !bjm->accepting_jobs) {
96 toku_cond_broadcast(&bjm->jobs_wait);
97 }
98 toku_mutex_unlock(&bjm->jobs_lock);
99}
100
101void bjm_wait_for_jobs_to_finish(BACKGROUND_JOB_MANAGER bjm) {
102 toku_mutex_lock(&bjm->jobs_lock);
103 bjm->accepting_jobs = false;
104 while (bjm->num_jobs > 0) {
105 toku_cond_wait(&bjm->jobs_wait, &bjm->jobs_lock);
106 }
107 toku_mutex_unlock(&bjm->jobs_lock);
108}
109
110