~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/blitzdb/blitzlock.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 - 2010 Toru Maesaka
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
#include "ha_blitz.h"
 
22
 
 
23
using namespace drizzled;
 
24
 
 
25
BlitzLock::BlitzLock() : scanner_count(0), updater_count(0) {
 
26
  pthread_cond_init(&condition, NULL);
 
27
  pthread_mutex_init(&mutex, NULL);
 
28
 
 
29
  for (int i = 0; i < BLITZ_LOCK_SLOTS; i++)
 
30
    pthread_mutex_init(&slots[i], NULL);
 
31
}
 
32
 
 
33
BlitzLock::~BlitzLock() {
 
34
  pthread_cond_destroy(&condition);
 
35
  pthread_mutex_destroy(&mutex);
 
36
 
 
37
  for (int i = 0; i < BLITZ_LOCK_SLOTS; i++)
 
38
    pthread_mutex_destroy(&slots[i]);
 
39
}
 
40
 
 
41
uint32_t BlitzLock::slot_id(const void *data, size_t len) {
 
42
  uint64_t hash = 14695981039346656037ULL;
 
43
  const unsigned char *rp = (unsigned char *)data;
 
44
  const unsigned char *ep = rp + len;
 
45
 
 
46
  while (rp < ep)
 
47
    hash = (hash ^ *(rp++)) * 109951162811ULL;
 
48
 
 
49
  return (uint32_t)(hash % BLITZ_LOCK_SLOTS);
 
50
}
 
51
 
 
52
int BlitzLock::slotted_lock(const uint32_t id) {
 
53
  return pthread_mutex_lock(&slots[id]);
 
54
}
 
55
 
 
56
int BlitzLock::slotted_unlock(const uint32_t id) {
 
57
  return pthread_mutex_unlock(&slots[id]);
 
58
}
 
59
 
 
60
void BlitzLock::update_begin() {
 
61
  pthread_mutex_lock(&mutex);
 
62
  while (true) {
 
63
    if (scanner_count < 1) {
 
64
      updater_count++;
 
65
      pthread_mutex_unlock(&mutex);
 
66
      return;
 
67
    }
 
68
    pthread_cond_wait(&condition, &mutex);
 
69
  }
 
70
  pthread_mutex_unlock(&mutex);
 
71
}
 
72
 
 
73
void BlitzLock::update_end() {
 
74
  pthread_mutex_lock(&mutex);
 
75
  updater_count--;
 
76
  assert(updater_count >= 0);
 
77
  if (updater_count == 0)
 
78
    pthread_cond_broadcast(&condition);
 
79
  pthread_mutex_unlock(&mutex);
 
80
}
 
81
 
 
82
void BlitzLock::scan_begin() {
 
83
  pthread_mutex_lock(&mutex);
 
84
  while (true) {
 
85
    if (updater_count == 0) {
 
86
      scanner_count++;
 
87
      pthread_mutex_unlock(&mutex);
 
88
      return;
 
89
    }
 
90
    pthread_cond_wait(&condition, &mutex);
 
91
  }
 
92
  pthread_mutex_unlock(&mutex);
 
93
}
 
94
 
 
95
void BlitzLock::scan_end() {
 
96
  pthread_mutex_lock(&mutex);
 
97
  scanner_count--;
 
98
  assert(scanner_count >= 0);
 
99
  if (scanner_count == 0)
 
100
    pthread_cond_broadcast(&condition);
 
101
  pthread_mutex_unlock(&mutex);
 
102
}
 
103
 
 
104
void BlitzLock::scan_update_begin() {
 
105
  pthread_mutex_lock(&mutex);
 
106
  while (true) {
 
107
    if (scanner_count == 0 && updater_count == 0) {
 
108
      scanner_count++;
 
109
      updater_count++;
 
110
      pthread_mutex_unlock(&mutex);
 
111
      return;
 
112
    }
 
113
    pthread_cond_wait(&condition, &mutex);
 
114
  }
 
115
  pthread_mutex_unlock(&mutex);
 
116
}
 
117
 
 
118
void BlitzLock::scan_update_end() {
 
119
  pthread_mutex_lock(&mutex);
 
120
  scanner_count--;
 
121
  updater_count--;
 
122
  assert(scanner_count >= 0 && updater_count >= 0);
 
123
 
 
124
  /* All other threads are guaranteed to be
 
125
     waiting so broadcast regardless. */
 
126
  pthread_cond_broadcast(&condition);
 
127
  pthread_mutex_unlock(&mutex);
 
128
}
 
129
 
 
130
int ha_blitz::blitz_optimal_lock() {
 
131
  if (sql_command_type == SQLCOM_ALTER_TABLE ||
 
132
      sql_command_type == SQLCOM_UPDATE ||
 
133
      sql_command_type == SQLCOM_DELETE ||
 
134
      sql_command_type == SQLCOM_REPLACE ||
 
135
      sql_command_type == SQLCOM_REPLACE_SELECT) {
 
136
    share->blitz_lock.scan_update_begin();
 
137
  } else {
 
138
    share->blitz_lock.scan_begin();
 
139
  }
 
140
  thread_locked = true;
 
141
  return 0;
 
142
}
 
143
 
 
144
int ha_blitz::blitz_optimal_unlock() {
 
145
  if (sql_command_type == SQLCOM_ALTER_TABLE ||
 
146
      sql_command_type == SQLCOM_UPDATE ||
 
147
      sql_command_type == SQLCOM_DELETE ||
 
148
      sql_command_type == SQLCOM_REPLACE ||
 
149
      sql_command_type == SQLCOM_REPLACE_SELECT) {
 
150
    share->blitz_lock.scan_update_end();
 
151
  } else {
 
152
    share->blitz_lock.scan_end();
 
153
  }
 
154
  thread_locked = false;
 
155
  return 0;
 
156
}