~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to sql/sql_manager.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000, 2002, 2005 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* 
 
17
 * sql_manager.cc
 
18
 * This thread manages various maintenance tasks.
 
19
 *
 
20
 *   o Flushing the tables every flush_time seconds.
 
21
 *   o Berkeley DB: removing unneeded log files.
 
22
 */
 
23
 
 
24
#include "mysql_priv.h"
 
25
 
 
26
ulong volatile manager_status;
 
27
bool volatile manager_thread_in_use;
 
28
 
 
29
pthread_t manager_thread;
 
30
pthread_mutex_t LOCK_manager;
 
31
pthread_cond_t COND_manager;
 
32
 
 
33
struct handler_cb {
 
34
   struct handler_cb *next;
 
35
   void (*action)(void);
 
36
};
 
37
 
 
38
static struct handler_cb * volatile cb_list;
 
39
 
 
40
bool mysql_manager_submit(void (*action)())
 
41
{
 
42
  bool result= FALSE;
 
43
  struct handler_cb * volatile *cb;
 
44
  pthread_mutex_lock(&LOCK_manager);
 
45
  cb= &cb_list;
 
46
  while (*cb && (*cb)->action != action)
 
47
    cb= &(*cb)->next;
 
48
  if (!*cb)
 
49
  {
 
50
    *cb= (struct handler_cb *)my_malloc(sizeof(struct handler_cb), MYF(MY_WME));
 
51
    if (!*cb)
 
52
      result= TRUE;
 
53
    else
 
54
    {
 
55
      (*cb)->next= NULL;
 
56
      (*cb)->action= action;
 
57
    }
 
58
  }
 
59
  pthread_mutex_unlock(&LOCK_manager);
 
60
  return result;
 
61
}
 
62
 
 
63
pthread_handler_t handle_manager(void *arg __attribute__((unused)))
 
64
{
 
65
  int error = 0;
 
66
  ulong status;
 
67
  struct timespec abstime;
 
68
  bool reset_flush_time = TRUE;
 
69
  struct handler_cb *cb= NULL;
 
70
  my_thread_init();
 
71
  DBUG_ENTER("handle_manager");
 
72
 
 
73
  pthread_detach_this_thread();
 
74
  manager_thread = pthread_self();
 
75
  manager_status = 0;
 
76
  manager_thread_in_use = 1;
 
77
 
 
78
  for (;;)
 
79
  {
 
80
    pthread_mutex_lock(&LOCK_manager);
 
81
    /* XXX: This will need to be made more general to handle different
 
82
     * polling needs. */
 
83
    if (flush_time)
 
84
    {
 
85
      if (reset_flush_time)
 
86
      {
 
87
        set_timespec(abstime, flush_time);
 
88
        reset_flush_time = FALSE;
 
89
      }
 
90
      while (!manager_status && (!error || error == EINTR) && !abort_loop)
 
91
        error= pthread_cond_timedwait(&COND_manager, &LOCK_manager, &abstime);
 
92
    }
 
93
    else
 
94
    {
 
95
      while (!manager_status && (!error || error == EINTR) && !abort_loop)
 
96
        error= pthread_cond_wait(&COND_manager, &LOCK_manager);
 
97
    }
 
98
    status = manager_status;
 
99
    manager_status = 0;
 
100
    if (cb == NULL)
 
101
    {
 
102
      cb= cb_list;
 
103
      cb_list= NULL;
 
104
    }
 
105
    pthread_mutex_unlock(&LOCK_manager);
 
106
 
 
107
    if (abort_loop)
 
108
      break;
 
109
 
 
110
    if (error == ETIMEDOUT || error == ETIME)
 
111
    {
 
112
      flush_tables();
 
113
      error = 0;
 
114
      reset_flush_time = TRUE;
 
115
    }
 
116
 
 
117
    while (cb)
 
118
    {
 
119
      struct handler_cb *next= cb->next;
 
120
      cb->action();
 
121
      my_free((uchar*)cb, MYF(0));
 
122
      cb= next;
 
123
    }
 
124
 
 
125
    if (status)
 
126
      DBUG_PRINT("error", ("manager did not handle something: %lx", status));
 
127
  }
 
128
  manager_thread_in_use = 0;
 
129
  my_thread_end();
 
130
  DBUG_RETURN(NULL);
 
131
}