~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/src/cslib/CSGlobal.h

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 SNAP Innovation GmbH
 
2
 *
 
3
 * BLOB Streaming for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Paul McCullagh (H&G2JCtL)
 
20
 *
 
21
 * 2007-05-20
 
22
 *
 
23
 * CORE SYSTEM:
 
24
 * The definitions are used by all code, and is NOT required
 
25
 * by any header file!
 
26
 *
 
27
 */
 
28
 
 
29
#ifndef __CSGLOBAL_H__
 
30
#define __CSGLOBAL_H__
 
31
 
 
32
#include "CSDefs.h"
 
33
#include "CSThread.h"
 
34
 
 
35
/* Those compilers that support the function
 
36
 * macro (in particular the "pretty" function
 
37
 * macro must be defined here.
 
38
 */
 
39
#ifdef __MWERKS__
 
40
#define __FUNC__                        __PRETTY_FUNCTION__
 
41
#else
 
42
#define __FUNC__                        __FUNCTION__
 
43
#endif
 
44
 
 
45
/* This is the call context: */
 
46
#define CS_CONTEXT                      __FUNC__, __FILE__, __LINE__
 
47
 
 
48
#ifdef DBUG_ON
 
49
int cs_assert(const char *func, const char *file, int line, const char *message);
 
50
int cs_hope(const char *func, const char *file, int line, const char *message);
 
51
 
 
52
#define ASSERT(x)                       ((x) ? 1 : cs_assert(CS_CONTEXT, #x))
 
53
#define HOPE(x)                         ((x) ? 1 : cs_hope(CS_CONTEXT, #x))
 
54
#else
 
55
#define ASSERT(x)
 
56
#define ASSERT(x)
 
57
#endif
 
58
 
 
59
#define new_(v, t)                      do { v = new t; if (!v) CSException::throwOSError(CS_CONTEXT, ENOMEM); } while (0)
 
60
 
 
61
/*
 
62
 * -----------------------------------------------------------------------
 
63
 * Call stack
 
64
 */
 
65
 
 
66
/*
 
67
 * This macro must be placed at the start of every function.
 
68
 * It records the current context so that we can
 
69
 * dump a type of stack trace later if necessary.
 
70
 *
 
71
 * It also sets up the current thread pointer 'self'.
 
72
 */
 
73
#define inner_()                        int                     cs_frame = self->callTop++; \
 
74
                                                        do { \
 
75
                                                                if (cs_frame< CS_CALL_STACK_SIZE) { \
 
76
                                                                        self->callStack[cs_frame].cs_func = __FUNC__; \
 
77
                                                                        self->callStack[cs_frame].cs_file = __FILE__; \
 
78
                                                                        self->callStack[cs_frame].cs_line = __LINE__; \
 
79
                                                                } \
 
80
                                                        } while (0)
 
81
 
 
82
#define outer_()                        self->callTop = cs_frame;
 
83
 
 
84
#define enter_()                        CSThread        *self = CSThread::getSelf(); \
 
85
                                                        inner_()
 
86
 
 
87
/*
 
88
 * On exit to a function, either exit_() or
 
89
 * return_() must be called.
 
90
 */
 
91
#define exit_()                         do { \
 
92
                                                                outer_(); \
 
93
                                                                return; \
 
94
                                                        } while (0)
 
95
                                        
 
96
 
 
97
#define return_(x)                      do { \
 
98
                                                                outer_(); \
 
99
                                                                return(x); \
 
100
                                                        } while (0)
 
101
 
 
102
 
 
103
/*
 
104
 * -----------------------------------------------------------------------
 
105
 * Throwing and catching (the jump stack)
 
106
 */
 
107
 
 
108
int prof_setjmp(void);
 
109
 
 
110
#define TX_CHK_JMP()            if ((self)->jumpDepth < 0 || (self)->jumpDepth >= CS_JUMP_STACK_SIZE) CSException::throwCoreError(__FUNC__, __FILE__, __LINE__, CS_ERR_JUMP_OVERFLOW)
 
111
#ifdef PROFILE
 
112
#define profile_setjmp          prof_setjmp()
 
113
#else
 
114
#define profile_setjmp                  
 
115
#endif
 
116
 
 
117
#define throw_()                        (self)->throwException()
 
118
#define try_(n)                         int throw_##n; throw_##n = 0; TX_CHK_JMP(); \
 
119
                                                        (self)->jumpEnv[(self)->jumpDepth].jb_res_top = (self)->relTop; \
 
120
                                                        (self)->jumpEnv[(self)->jumpDepth].jb_call_top = (self)->callTop; \
 
121
                                                        (self)->jumpDepth++; profile_setjmp; \
 
122
                                                        if (setjmp((self)->jumpEnv[(self)->jumpDepth-1].jb_buffer)) goto catch_##n;
 
123
#define catch_(n)                       (self)->jumpDepth--; goto cont_##n; catch_##n: (self)->jumpDepth--; self->caught();
 
124
#define finally_(n)                     (self)->jumpDepth--; goto final_##n; catch_##n: throw_##n = 1; (self)->jumpDepth--; self->caught(); final_##n:
 
125
#define cont_(n)                        if (throw_##n) throw_(); cont_##n:
 
126
 
 
127
/*
 
128
 * -----------------------------------------------------------------------
 
129
 * The release stack
 
130
 */
 
131
 
 
132
#define push_(r)                        do { \
 
133
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
 
134
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
 
135
                                                                (self)->relTop->r_type = CS_RELEASE_OBJECT; \
 
136
                                                                (self)->relTop->x.r_object = (r); \
 
137
                                                                (self)->relTop++; \
 
138
                                                        } while (0)
 
139
 
 
140
#define pop_(r)                         do { \
 
141
                                                                ASSERT((self)->relTop > (self)->relStack); \
 
142
                                                                if (((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT) \
 
143
                                                                        ASSERT(((self)->relTop - 1)->x.r_object == ((CSObject *) r)); \
 
144
                                                                else if (((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX) \
 
145
                                                                        ASSERT(((self)->relTop - 1)->x.r_mutex == ((CSMutex *) r)); \
 
146
                                                                else \
 
147
                                                                        ASSERT(((self)->relTop - 1)->x.r_pooled == ((CSPooled *) r)); \
 
148
                                                                (self)->relTop--; \
 
149
                                                        } while (0)
 
150
 
 
151
#define retain_(r)                      do { \
 
152
                                                                (r)->retain(); \
 
153
                                                                push_(r); \
 
154
                                                        } while (0)
 
155
 
 
156
#define release_(r)                     do {  \
 
157
                                                                register CSObject *rp; \
 
158
                                                                ASSERT((self)->relTop > (self)->relStack); \
 
159
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_OBJECT); \
 
160
                                                                rp = ((self)->relTop - 1)->x.r_object; \
 
161
                                                                ASSERT(rp == (r)); \
 
162
                                                                (self)->relTop--; \
 
163
                                                                rp->release(); \
 
164
                                                        } while (0)
 
165
 
 
166
#define lock_(r)                        do { \
 
167
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
 
168
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
 
169
                                                                (r)->lock(); \
 
170
                                                                (self)->relTop->r_type = CS_RELEASE_MUTEX; \
 
171
                                                                (self)->relTop->x.r_mutex = (r); \
 
172
                                                                (self)->relTop++; \
 
173
                                                        } while (0)
 
174
 
 
175
#define unlock_(r)                      do {  \
 
176
                                                                register CSMutex *rp; \
 
177
                                                                ASSERT((self)->relTop > (self)->relStack); \
 
178
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_MUTEX); \
 
179
                                                                rp = ((self)->relTop - 1)->x.r_mutex; \
 
180
                                                                ASSERT(rp == (r)); \
 
181
                                                                (self)->relTop--; \
 
182
                                                                rp->unlock(); \
 
183
                                                        } while (0)
 
184
 
 
185
#define frompool_(r)            do { \
 
186
                                                                if ((self)->relTop >= (self)->relStack + CS_RELEASE_STACK_SIZE) \
 
187
                                                                        CSException::throwCoreError(CS_CONTEXT, CS_ERR_RELEASE_OVERFLOW); \
 
188
                                                                (self)->relTop->r_type = CS_RELEASE_POOLED; \
 
189
                                                                (self)->relTop->x.r_pooled = (r); \
 
190
                                                                (self)->relTop++; \
 
191
                                                        } while (0)
 
192
 
 
193
#define backtopool_(r)          do {  \
 
194
                                                                register CSPooled *rp; \
 
195
                                                                ASSERT((self)->relTop > (self)->relStack); \
 
196
                                                                ASSERT(((self)->relTop - 1)->r_type == CS_RELEASE_POOLED); \
 
197
                                                                rp = ((self)->relTop - 1)->x.r_pooled; \
 
198
                                                                ASSERT(rp == (r)); \
 
199
                                                                (self)->relTop--; \
 
200
                                                                rp->returnToPool(); \
 
201
                                                        } while (0)
 
202
 
 
203
#endif