~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/xid.cc

  • Committer: Brian Aker
  • Date: 2009-03-17 06:05:42 UTC
  • mfrom: (934.2.17 small-cleanups)
  • Revision ID: brian@tangent.org-20090317060542-a7ggj7l686cvxw31
Merge Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include <drizzled/global.h>
21
 
#include <stdint.h>
22
21
#include <string.h>
 
22
 
 
23
#include <mysys/hash.h>
23
24
#include <drizzled/xid.h>
24
25
 
25
26
XID::XID()
106
107
{
107
108
  return sizeof(gtrid_length)+sizeof(bqual_length)+gtrid_length+bqual_length;
108
109
}
 
110
 
 
111
/***************************************************************************
 
112
  Handling of XA id cacheing
 
113
***************************************************************************/
 
114
pthread_mutex_t LOCK_xid_cache;
 
115
HASH xid_cache;
 
116
 
 
117
extern "C" unsigned char *xid_get_hash_key(const unsigned char *, size_t *, bool);
 
118
extern "C" void xid_free_hash(void *);
 
119
 
 
120
unsigned char *xid_get_hash_key(const unsigned char *ptr, size_t *length,
 
121
                        bool )
 
122
{
 
123
  *length=((XID_STATE*)ptr)->xid.key_length();
 
124
  return ((XID_STATE*)ptr)->xid.key();
 
125
}
 
126
 
 
127
void xid_free_hash(void *ptr)
 
128
{
 
129
  if (!((XID_STATE*)ptr)->in_session)
 
130
    free((unsigned char*)ptr);
 
131
}
 
132
 
 
133
bool xid_cache_init()
 
134
{
 
135
  pthread_mutex_init(&LOCK_xid_cache, MY_MUTEX_INIT_FAST);
 
136
  return hash_init(&xid_cache, &my_charset_bin, 100, 0, 0,
 
137
                   xid_get_hash_key, xid_free_hash, 0) != 0;
 
138
}
 
139
 
 
140
void xid_cache_free()
 
141
{
 
142
  if (hash_inited(&xid_cache))
 
143
  {
 
144
    hash_free(&xid_cache);
 
145
    pthread_mutex_destroy(&LOCK_xid_cache);
 
146
  }
 
147
}
 
148
 
 
149
XID_STATE *xid_cache_search(XID *xid)
 
150
{
 
151
  pthread_mutex_lock(&LOCK_xid_cache);
 
152
  XID_STATE *res=(XID_STATE *)hash_search(&xid_cache, xid->key(), xid->key_length());
 
153
  pthread_mutex_unlock(&LOCK_xid_cache);
 
154
  return res;
 
155
}
 
156
 
 
157
bool xid_cache_insert(XID *xid, enum xa_states xa_state)
 
158
{
 
159
  XID_STATE *xs;
 
160
  bool res;
 
161
  pthread_mutex_lock(&LOCK_xid_cache);
 
162
  if (hash_search(&xid_cache, xid->key(), xid->key_length()))
 
163
    res=0;
 
164
  else if (!(xs=(XID_STATE *)malloc(sizeof(*xs))))
 
165
    res=1;
 
166
  else
 
167
  {
 
168
    xs->xa_state=xa_state;
 
169
    xs->xid.set(xid);
 
170
    xs->in_session=0;
 
171
    res=my_hash_insert(&xid_cache, (unsigned char*)xs);
 
172
  }
 
173
  pthread_mutex_unlock(&LOCK_xid_cache);
 
174
  return res;
 
175
}
 
176
 
 
177
bool xid_cache_insert(XID_STATE *xid_state)
 
178
{
 
179
  pthread_mutex_lock(&LOCK_xid_cache);
 
180
  assert(hash_search(&xid_cache, xid_state->xid.key(),
 
181
                          xid_state->xid.key_length())==0);
 
182
  bool res=my_hash_insert(&xid_cache, (unsigned char*)xid_state);
 
183
  pthread_mutex_unlock(&LOCK_xid_cache);
 
184
  return res;
 
185
}
 
186
 
 
187
void xid_cache_delete(XID_STATE *xid_state)
 
188
{
 
189
  pthread_mutex_lock(&LOCK_xid_cache);
 
190
  hash_delete(&xid_cache, (unsigned char *)xid_state);
 
191
  pthread_mutex_unlock(&LOCK_xid_cache);
 
192
}