bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_mt.h
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 /***************************************************************************
19  * Description: Multi thread portability code for JK                       *
20  * Author:      Gal Shachor <shachor@il.ibm.com>                           *
21  * Version:     $Revision: 918711 $                                           *
22  ***************************************************************************/
23
24 #ifndef _JK_MT_H
25 #define _JK_MT_H
26
27 #include "jk_global.h"
28
29
30 #if defined(WIN32)
31 #define jk_gettid()    ((jk_uint32_t)GetCurrentThreadId())
32 #elif defined(NETWARE) && !defined(__NOVELL_LIBC__)
33 #define getpid()       ((int)GetThreadGroupID())
34 #endif
35
36 #ifdef JK_PREFORK 
37 #define _MT_CODE 0
38 #else
39 #define _MT_CODE 1
40 #endif
41
42 /*
43  * Marks execution under MT compilation
44  */
45 #if _MT_CODE
46 #ifdef WIN32
47 #include <windows.h>
48
49 typedef CRITICAL_SECTION JK_CRIT_SEC;
50 #define JK_INIT_CS(x, rc) InitializeCriticalSection(x); rc = JK_TRUE
51 #define JK_DELETE_CS(x, rc) DeleteCriticalSection(x);   rc = JK_TRUE
52 #define JK_ENTER_CS(x, rc) EnterCriticalSection(x);     rc = JK_TRUE
53 #define JK_LEAVE_CS(x, rc) LeaveCriticalSection(x);     rc = JK_TRUE
54
55 #else /* !WIN32 */
56 #define _MT_CODE_PTHREAD
57 #include <pthread.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60
61 typedef pthread_mutex_t JK_CRIT_SEC;
62 #define JK_INIT_CS(x, rc)\
63             if(pthread_mutex_init(x, NULL)) rc = JK_FALSE; else rc = JK_TRUE
64
65 #define JK_DELETE_CS(x, rc)\
66             if(pthread_mutex_destroy(x))    rc = JK_FALSE; else rc = JK_TRUE
67
68 #define JK_ENTER_CS(x, rc)\
69             if(pthread_mutex_lock(x))       rc = JK_FALSE; else rc = JK_TRUE
70
71 #define JK_LEAVE_CS(x, rc)\
72             if(pthread_mutex_unlock(x))     rc = JK_FALSE; else rc = JK_TRUE
73
74 #if defined(AS400) || defined(NETWARE)
75 #define jk_pthread_t   jk_uint32_t
76 #endif /* AS400 || NETWARE */
77 jk_pthread_t jk_gettid(void);
78 #endif /* WIN32 */
79
80 #else /* !_MT_CODE */
81
82 typedef void *JK_CRIT_SEC;
83 #define JK_INIT_CS(x, rc)   rc = JK_TRUE
84 #define JK_DELETE_CS(x, rc) rc = JK_TRUE
85 #define JK_ENTER_CS(x, rc)  rc = JK_TRUE
86 #define JK_LEAVE_CS(x, rc)  rc = JK_TRUE
87 #define jk_gettid()         0
88 #endif /* MT_CODE */
89
90 #if !defined(WIN32) && !defined(NETWARE)
91 #include <unistd.h>
92 #include <fcntl.h>
93
94
95 #define USE_FLOCK_LK 0
96 #if HAVE_FLOCK
97 #ifdef JK_USE_FLOCK
98 #define USE_FLOCK_LK 1
99 #endif
100 #endif
101
102 #if USE_FLOCK_LK
103 #include <sys/file.h>
104
105 #define JK_ENTER_LOCK(x, rc)        \
106     do {                            \
107       while ((rc = flock((x), LOCK_EX) < 0) && (errno == EINTR)); \
108       rc = rc == 0 ? JK_TRUE : JK_FALSE; \
109     } while (0)
110
111 #define JK_LEAVE_LOCK(x, rc)        \
112     do {                            \
113       while ((rc = flock((x), LOCK_UN) < 0) && (errno == EINTR)); \
114       rc = rc == 0 ? JK_TRUE : JK_FALSE; \
115     } while (0)
116
117 #else /* !USE_FLOCK_LK */
118
119 #define JK_ENTER_LOCK(x, rc)        \
120     do {                            \
121       struct flock _fl;             \
122       _fl.l_type   = F_WRLCK;       \
123       _fl.l_whence = SEEK_SET;      \
124       _fl.l_start  = 0;             \
125       _fl.l_len    = 1L;            \
126       _fl.l_pid    = 0;             \
127       while ((rc = fcntl((x), F_SETLKW, &_fl) < 0) && (errno == EINTR)); \
128       rc = rc == 0 ? JK_TRUE : JK_FALSE; \
129     } while (0)
130
131 #define JK_LEAVE_LOCK(x, rc)        \
132     do {                            \
133       struct flock _fl;             \
134       _fl.l_type   = F_UNLCK;       \
135       _fl.l_whence = SEEK_SET;      \
136       _fl.l_start  = 0;             \
137       _fl.l_len    = 1L;            \
138       _fl.l_pid    = 0;             \
139       while ((rc = fcntl((x), F_SETLKW, &_fl) < 0) && (errno == EINTR)); \
140       rc = rc == 0 ? JK_TRUE : JK_FALSE; \
141     } while (0)
142
143 #endif /* USE_FLOCK_LK */
144
145 #else  /* WIN32 || NETWARE */
146 #define JK_ENTER_LOCK(x, rc) rc = JK_TRUE
147 #define JK_LEAVE_LOCK(x, rc) rc = JK_TRUE
148 #endif
149
150 #endif /* _JK_MT_H */