~drizzle-trunk/drizzle/development

1548.4.113 by Zimin
remove table lock, add FilesystemLock to this storage engine
1
/*
2
  Copyright (C) 2010 Zimin
3
4
  This program is free software; you can redistribute it and/or
5
  modify it under the terms of the GNU General Public License
6
  as published by the Free Software Foundation; either version 2
7
  of the License, or (at your option) any later version.
8
9
  This program is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with this program; if not, write to the Free Software
16
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
*/
18
1800.3.1 by Vijay Samuel
Merge change of <config.h> to "config.h"
19
#include "config.h"
1548.4.113 by Zimin
remove table lock, add FilesystemLock to this storage engine
20
#include <pthread.h>
21
#include <assert.h>
22
#include "filesystemlock.h"
23
24
FilesystemLock::FilesystemLock()
25
  : scanner_count(0),
26
    updater_count(0)
27
{
28
  pthread_cond_init(&condition, NULL);
29
  pthread_mutex_init(&mutex, NULL);
30
}
31
32
FilesystemLock::~FilesystemLock()
33
{
34
  pthread_cond_destroy(&condition);
35
  pthread_mutex_destroy(&mutex);
36
}
37
38
void FilesystemLock::scan_begin()
39
{
40
  pthread_mutex_lock(&mutex);
41
  while (true)
42
  {
43
    if (updater_count == 0)
44
    {
45
      scanner_count++;
46
      pthread_mutex_unlock(&mutex);
47
      return;
48
    }
49
    pthread_cond_wait(&condition, &mutex);
50
  }
51
}
52
53
void FilesystemLock::scan_end() {
54
  pthread_mutex_lock(&mutex);
55
  scanner_count--;
56
  assert(scanner_count >= 0);
57
  if (scanner_count == 0)
58
    pthread_cond_broadcast(&condition);
59
  pthread_mutex_unlock(&mutex);
60
}
61
62
void FilesystemLock::scan_update_begin()
63
{
64
  pthread_mutex_lock(&mutex);
65
  while (true)
66
  {
67
    if (scanner_count == 0 && updater_count == 0)
68
    {
69
      scanner_count++;
70
      updater_count++;
71
      pthread_mutex_unlock(&mutex);
72
      return;
73
    }
74
    pthread_cond_wait(&condition, &mutex);
75
  }
76
}
77
78
void FilesystemLock::scan_update_end()
79
{
80
  pthread_mutex_lock(&mutex);
81
  scanner_count--;
82
  updater_count--;
83
  assert(scanner_count >= 0 && updater_count >= 0);
84
85
  pthread_cond_broadcast(&condition);
86
  pthread_mutex_unlock(&mutex);
87
}