File: _kicore_connection_timeout.h

package info (click to toggle)
python-kinterbasdb 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,432 kB
  • ctags: 1,830
  • sloc: ansic: 16,803; python: 3,514; makefile: 63; sh: 22
file content (423 lines) | stat: -rw-r--r-- 15,608 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* KInterbasDB Python Package - Header File for Connection Timeout
 *
 * Version 3.3
 *
 * The following contributors hold Copyright (C) over their respective
 * portions of code (see license.txt for details):
 *
 * [Original Author (maintained through version 2.0-0.3.1):]
 *   1998-2001 [alex]  Alexander Kuznetsov   <alexan@users.sourceforge.net>
 * [Maintainers (after version 2.0-0.3.1):]
 *   2001-2002 [maz]   Marek Isalski         <kinterbasdb@maz.nu>
 *   2002-2007 [dsr]   David Rushby          <woodsplitter@rocketmail.com>
 * [Contributors:]
 *   2001      [eac]   Evgeny A. Cherkashin  <eugeneai@icc.ru>
 *   2001-2002 [janez] Janez Jere            <janez.jere@void.si>
 */

#ifndef _KICORE_CONNECTION_TIMEOUT_H
#define _KICORE_CONNECTION_TIMEOUT_H

#ifdef ENABLE_CONNECTION_TIMEOUT

#include "_kinterbasdb.h"
#include "_kisupport.h"

/* Type definitions: */
typedef enum {
  CONOP_IDLE,
    /* There is no operation active on the Connection at present, and it has
     * not timed out. */
  CONOP_ACTIVE,
    /* There is an operation active, and the Connection has not timed out. */
  CONOP_TIMED_OUT_TRANSPARENTLY,
    /* The Connection was closed by the ConnectionTimeoutThread, and can be
     * transparently resurrected because no transaction was in progress when
     * the timeout occurred. */
  CONOP_TIMED_OUT_NONTRANSPARENTLY,
    /* The Connection was closed by the ConnectionTimeoutThread, and cannot be
     * transparently resurrected because a transaction was in progress when the
     * timeout occurred.  The next attempt to use the Connection (directly, by
     * calling a method of the Connection, or indirectly, by calling a method
     * of a dependent object such as a Cursor) will raise a
     * kinterbasdb.ConnectionTimedOut exception. */
  CONOP_PERMANENTLY_CLOSED
} ConnectionOpState;

/* The user-supplied before_callback returns one of these codes to indicate how
 * the ConnectionTimeoutThread should proceed: */
typedef enum {
  CT_VETO,
    /* Don't time the connection out, and don't check it again until at least a
     * full ->timeout_period has elapsed. */
  CT_ROLLBACK,
    /* Roll back the connection's unresolved transaction (if any) before timing
     * the connection out. */
  CT_COMMIT,
    /* Commit the connection's unresolved transaction (if any) before timing
     * the connection out. */
  CT_NONTRANSPARENT
    /* Roll back the connection's unresolved transaction (if any) and time the
     * connection out nontransparently (so that future attempts to use it will
     * raise a kinterbasdb.ConnectionTimedOut exception). */
} CTCallbackVerdict;
const CTCallbackVerdict CT_DEFAULT = CT_NONTRANSPARENT;

typedef struct _ConnectionTimeoutParams {
  /* Each Connection for which timeout is enabled has a ConnectionTimeoutParams
   * instance associated with it. */

  PyThread_type_lock lock;
  PlatformThreadIdType owner;

  volatile ConnectionOpState state;
  /* state and timeout_period are placed together so that on a 32-bit platform,
   * they won't need padding (reduces size of struct from 48 to 40 bytes on
   * Win32). */
  volatile long timeout_period;

  volatile LONG_LONG connected_at;
  volatile LONG_LONG last_active;
  /* soonest_might_time_out = last_active + timeout_period: */
  volatile LONG_LONG soonest_might_time_out;

  /* User-supplied callbacks: */
  volatile PyObject *py_callback_before;
  volatile PyObject *py_callback_after;
} ConnectionTimeoutParams;

/* The global ConnectionTimeoutManager uses a linked list of these to keep
 * track of all connections on which timeout is enabled: */
typedef struct _ConnectionTracker {
  volatile CConnection *contained;
  volatile struct _ConnectionTracker *next;
} ConnectionTracker;

/* Cast away volatile qualifier: */
#define DV_CT(ct_ptr) ((ConnectionTracker *) (ct))

struct ConnectionTimeoutManager {
  /* There's only one instance of ConnectionTimeoutManager (named global_ctm)
   * in the entire process. */

  PlatformMutexType lock;

  /* There are too many differences between Windows Events and pthread
   * condition variables for us to handle them with a uniform abstraction. */
  #ifdef PLATFORM_WINDOWS
    HANDLE
  #else
    pthread_cond_t
  #endif
  reconsider_wait_interval;

  volatile Py_ssize_t n_cons; /* Number of nodes in linked list cons. */
  volatile ConnectionTracker *cons;
  /* The soonest point in time (expressed in milliseconds since the epoch) that
   * one of the connections tracked in cons might need to be timed out: */
  volatile LONG_LONG soonest_next_connection_might_timeout;

  /* timeout_thread_py is a reference to a Python object, but a typical client
   * of global_ctm does not hold the GIL.  The only operation performed on
   * timeout_thread_py without the GIL held is to nullify it, so no problem. */
  PyObject *timeout_thread_py;
  PlatformThreadRefType timeout_thread;
  PlatformThreadIdType timeout_thread_id;
  volatile boolean ctt_should_stop;
} global_ctm;

/* Misc. constraints: */
#define RUNNING_IN_CONNECTION_TIMEOUT_THREAD \
  (Thread_ids_equal(Thread_current_id(), global_ctm.timeout_thread_id))

#define NOT_RUNNING_IN_CONNECTION_TIMEOUT_THREAD \
  (!RUNNING_IN_CONNECTION_TIMEOUT_THREAD)

#define S_IN_14_DAYS     1209600
#define MS_IN_14_DAYS 1209600000
#define TIMEOUT_PERIOD_IS_IN_RANGE(period) \
  ((period) > 0 && (period) <= MS_IN_14_DAYS)

/* ConnectionTimeoutManager method prototypes: */
static int CTM_initialize(void);
static int CTM_add(volatile CConnection *con, ConnectionTimeoutParams *tp);
static int CTM_remove(volatile CConnection *con);

#define CTM_LOCK \
  debug_print3("CTM-> ?ACQUIRE: %ld file %s line %d\n", \
      PyThread_get_thread_ident(), __FILE__, __LINE__ \
    ); \
  Mutex_lock(&global_ctm.lock); \
  debug_print3("CTM-> !!ACQUIRED: %ld file %s line %d\n", \
      PyThread_get_thread_ident(), __FILE__, __LINE__ \
    );

#define CTM_UNLOCK \
  debug_print3("CTM-> ?RELEASE: %ld file %s line %d\n", \
      PyThread_get_thread_ident(), __FILE__, __LINE__ \
    ); \
  Mutex_unlock(&global_ctm.lock); \
  debug_print3("CTM-> !!RELEASED: %ld file %s line %d\n", \
      PyThread_get_thread_ident(), __FILE__, __LINE__ \
    );

/* ConnectionTimeoutParams method prototypes: */
static ConnectionTimeoutParams *ConnectionTimeoutParams_create(
    long period,
    PyObject *py_callback_before, PyObject *py_callback_after
  );
static int _ConnectionTimeoutParams_destroy_(
    ConnectionTimeoutParams **tp_, boolean should_destroy_lock
  );
static int ConnectionTimeoutParams_destroy(ConnectionTimeoutParams **tp_);
static ConnectionOpState ConnectionTimeoutParams_trans_while_already_locked(
    ConnectionTimeoutParams *tp,
    ConnectionOpState expected_old_state, ConnectionOpState requested_new_state
  );
static ConnectionOpState ConnectionTimeoutParams_trans(
    ConnectionTimeoutParams *tp,
    ConnectionOpState expected_old_state, ConnectionOpState requested_new_state
  );
static void _ConnectionTimeoutParams_touch(ConnectionTimeoutParams *tp);

/* Connection activation and deactivation: */
#define TP_RECORD_OWNERSHIP(tp) \
  (tp)->owner = Thread_current_id()
#define TP_RELEASE_OWNERSHIP(tp) \
  (tp)->owner = THREAD_ID_NONE

static boolean CURRENT_THREAD_OWNS_TP(ConnectionTimeoutParams *tp) {
  assert (tp != NULL);
  return Thread_ids_equal(Thread_current_id(), tp->owner);
}

static boolean CURRENT_THREAD_OWNS_CON_TP(CConnection *con) {
  assert (con != NULL);
  if (!Connection_timeout_enabled(con)) {
    return TRUE;
  } else {
    ConnectionTimeoutParams *tp = con->timeout;
    assert (tp != NULL);
    return CURRENT_THREAD_OWNS_TP(tp);
  }
}

/* TP_LOCK must not be called when the GIL is held; use
 * ACQUIRE_TP_WITH_GIL_HELD for that.  Violating this rule could result in a
 * deadlock between the violating thread and the ConnectionTimeoutThread. */
#define TP_LOCK(tp) \
  debug_print4("TP(%p)-> ?ACQUIRE: %ld file %s line %d\n", \
      tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
    ); \
  PyThread_acquire_lock((tp)->lock, WAIT_LOCK); \
  TP_RECORD_OWNERSHIP(tp); \
  debug_print4("TP(%p)-> !!ACQUIRED: %ld file %s line %d\n", \
      tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
    );

static boolean TP_TRYLOCK(ConnectionTimeoutParams *tp);

#define TP_UNLOCK(tp) \
  debug_print4("TP(%p)-> ?RELEASE: %ld file %s line %d\n", \
      tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
    ); \
  TP_RELEASE_OWNERSHIP(tp); \
  PyThread_release_lock((tp)->lock); \
  debug_print4("TP(%p)-> !!RELEASED: %ld file %s line %d\n", \
      tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
    );

/* ACQUIRE_TP_WITH_GIL_HELD:
 * (This macro is meant to be used only by the thread that performs routine
 * operations on a connection (ROT), not by the ConnectionTimeoutThread (CTT).)
 *
 * There can be at most two threads vying for control of a connection at once:
 *   - the ROT
 *   - the CTT
 *
 * ACQUIRE_TP_WITH_GIL_HELD, invoked by the ROT, first tries to acquire the
 * lock over the ConnectionTimeoutParams object tp.  If that attempt fails, the
 * ROT knows that the CTT was holding the lock.  The CTT sometimes tries to
 * acquire the GIL while it is holding a ConnectionTimeoutParams lock, so in
 * order to avoid deadlock when the ROT detects that the CTT is holding
 * tp->lock, the ROT must release the GIL and wait to acquire tp->lock before
 * reacquiring the GIL. */
#define ACQUIRE_TP_WITH_GIL_HELD(tp) \
  debug_print4("TP(%p)-> ?TRY-ACQUIRE: %ld file %s line %d\n", \
      tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
    ); \
  if (TP_TRYLOCK(tp)) { \
    debug_print4("TP(%p)-> ?TRY-ACQUIRED: %ld file %s line %d\n", \
        tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
      ); \
  } else { \
    debug_print4("TP(%p)-> ?TRY-ACQUIRE-FAILED: %ld file %s line %d\n", \
        tp, PyThread_get_thread_ident(), __FILE__, __LINE__ \
      ); \
    { \
      PyThreadState *tstate = PyThreadState_Get(); \
      LEAVE_GIL_USING_THREADSTATE(tstate); \
      TP_LOCK(tp); \
      ENTER_GIL_USING_THREADSTATE(tstate); \
    } \
  }

#define ACQUIRE_CON_TP_WITH_GIL_HELD(con) \
  if (Connection_timeout_enabled(con)) { \
    ACQUIRE_TP_WITH_GIL_HELD((con)->timeout); \
  }

/* RELEASE_CON_TP is the inverse of ACQUIRE_CON_TP_WITH_GIL_HELD. */
#define RELEASE_CON_TP(con) \
  if (Connection_timeout_enabled(con)) { \
    TP_UNLOCK((con)->timeout); \
  }

static int Connection_activate(CConnection *con,
    const boolean con_tp_already_locked,
    const boolean allow_transparent_resumption
  );

/* Connection Activation and passivation macros: */
#define _CON_ACTIVATE( \
  con, failure_action, con_tp_already_locked, allow_transparent_resumption \
 ) \
  /* The GIL must be held when this macro is used. */ \
  assert (con != NULL); \
  if (Connection_activate(con, con_tp_already_locked, \
        allow_transparent_resumption \
      ) != 0 \
     ) \
  { \
    assert (PyErr_Occurred()); \
    failure_action; \
  }

#define CON_ACTIVATE(con, failure_action) \
  _CON_ACTIVATE(con, failure_action, FALSE, TRUE)

#define CON_ACTIVATE__FORBID_TRANSPARENT_RESUMPTION(con, failure_action) \
  _CON_ACTIVATE(con, failure_action, FALSE, FALSE)

#define CON_ACTIVATE__ALREADY_LOCKED(con, failure_action) \
  _CON_ACTIVATE(con, failure_action, TRUE, TRUE)

#define CON_ACTIVATE__FORBID_TRANSPARENT_RESUMPTION__ALREADY_LOCKED( \
  con, failure_action \
 ) \
  _CON_ACTIVATE(con, failure_action, TRUE, FALSE)


#define _CON_PASSIVATE(con, state_trans_func) \
  if (Connection_timeout_enabled(con)) { \
    LONG_LONG orig_last_active; \
    ConnectionOpState achieved_state; \
    \
    assert ((con)->timeout->state == CONOP_ACTIVE); \
    orig_last_active = (con)->timeout->last_active; \
    /* This should never fail: */ \
    achieved_state = state_trans_func( \
        (con)->timeout, CONOP_ACTIVE, CONOP_IDLE \
      ); \
    assert (achieved_state == CONOP_IDLE); \
    /* The last_active timestamp should now be later than it was (but it \
     * might be mathematically the same if the timer resolution is
     * course). */ \
    assert ((con)->timeout->last_active - orig_last_active >= 0); \
  }

#define CON_PASSIVATE(con) \
  _CON_PASSIVATE(con, ConnectionTimeoutParams_trans)

#define CON_PASSIVATE__ALREADY_LOCKED(con) \
  _CON_PASSIVATE(con, ConnectionTimeoutParams_trans_while_already_locked)

#define CON_MUST_ALREADY_BE_ACTIVE(con) \
  /* This macro performs no locking because it's only intended to be used in \
   * places where the connection is expected to *retain a state of \
   * CONOP_ACTIVE set earlier while a lock was held* (the \
   * ConnectionTimeoutThread never times out a connection that has state \
   * CONOP_ACTIVE, so the fact that the lock has been released since is no \
   * problem). */ \
  assert ((con) != NULL); \
  assert ( \
         !Connection_timeout_enabled(con) \
      || (con)->timeout->state == CONOP_ACTIVE \
    );

#define CON_MUST_NOT_BE_ACTIVE(con) \
  assert (con != NULL); \
  assert ( \
         !Connection_timeout_enabled(con) \
      || (con)->timeout->state != CONOP_ACTIVE \
    );

#else /* not defined(ENABLE_CONNECTION_TIMEOUT) */

  #define RUNNING_IN_CONNECTION_TIMEOUT_THREAD       FALSE
  #define NOT_RUNNING_IN_CONNECTION_TIMEOUT_THREAD   TRUE

  /* Connection Activation and Passivation macros: */
  #define _CON_ACTIVATE( \
      con, failure_action, \
      con_tp_already_locked, allow_transparent_resumption \
    ) \
      assert (con != NULL); \
      if (con == null_connection || (con)->state != CON_STATE_OPEN) { \
        raise_exception(ProgrammingError, "Invalid connection state.  The" \
            " connection must be open to perform this operation." \
          ); \
        failure_action; \
      }

  #define CON_ACTIVATE(con, failure_action) \
    _CON_ACTIVATE(con, failure_action, FALSE, FALSE)

  #define CON_ACTIVATE__FORBID_TRANSPARENT_RESUMPTION(con, failure_action) \
    CON_ACTIVATE(con, failure_action)

  #define CON_PASSIVATE(con)

  #define CON_MUST_ALREADY_BE_ACTIVE(con)
  #define CON_MUST_NOT_BE_ACTIVE(con)

#endif /* defined(ENABLE_CONNECTION_TIMEOUT) */

/* Cursor activation and deactivation: */
#define CUR_REQUIRE_OPEN_WITH_FAILURE(cursor, failure_action) \
  if (_Cursor_require_open(cursor, NULL) != 0) { failure_action; }

#define CUR_REQUIRE_OPEN(cursor) \
  CUR_REQUIRE_OPEN_WITH_FAILURE(cursor, return NULL)

#define CUR_REQUIRE_OPEN2(cursor, failure_message) \
  if (_Cursor_require_open(cursor, failure_message) != 0) { return NULL; }

#define _CUR_ACTIVATE(cursor, failure_action, allow_transparent_resumption) \
  assert (cursor != NULL); \
  if ((cursor)->trans != NULL) { \
    /* If cursor has a connection at all, _CON_ACTIVATE should be executed \
     * before CUR_REQUIRE_OPEN_WITH_FAILURE, so that a ConnectionTimedOut \
     * exception will be thrown (instead of ProgrammingError) where \
     * appropriate: */ \
    CConnection *con = Transaction_get_con((cursor)->trans); \
    if (con != NULL) { \
      _CON_ACTIVATE(con, failure_action, FALSE, allow_transparent_resumption); \
    } \
  } \
  CUR_REQUIRE_OPEN_WITH_FAILURE(cursor, failure_action); \

#define CUR_ACTIVATE(cursor, failure_action) \
  _CUR_ACTIVATE(cursor, failure_action, TRUE)

#define CUR_ACTIVATE__FORBID_TRANSPARENT_RESUMPTION(cursor, failure_action) \
  _CUR_ACTIVATE(cursor, failure_action, FALSE)


#define CUR_PASSIVATE(cursor) \
  assert (cursor != NULL); \
  assert ((cursor)->trans != NULL); \
  assert (Transaction_get_con((cursor)->trans) != NULL); \
  CON_PASSIVATE(Transaction_get_con((cursor)->trans))

#endif /* not def _KICORE_CONNECTION_TIMEOUT_H */