00001
00002
00003
00004
00005
00006
00007
00008
00014 #ifndef __GEARMAN_SERVER_COMMON_H__
00015 #define __GEARMAN_SERVER_COMMON_H__
00016
00017 #include "config.h"
00018
00019 #include "server.h"
00020
00021 #ifdef HAVE_ASSERT_H
00022 #include <assert.h>
00023 #endif
00024 #ifdef HAVE_ERRNO_H
00025 #include <errno.h>
00026 #endif
00027 #ifdef HAVE_FCNTL_H
00028 #include <fcntl.h>
00029 #endif
00030 #ifdef HAVE_GETOPT_H
00031 #include <getopt.h>
00032 #endif
00033 #ifdef HAVE_PTHREAD
00034 #include <pthread.h>
00035 #endif
00036 #ifdef HAVE_STDARG_H
00037 #include <stdarg.h>
00038 #endif
00039 #ifdef HAVE_STDDEF_H
00040 #include <stddef.h>
00041 #endif
00042 #ifdef HAVE_STDIO_H
00043 #include <stdio.h>
00044 #endif
00045 #ifdef HAVE_STDLIB_H
00046 #include <stdlib.h>
00047 #endif
00048 #ifdef HAVE_STRING_H
00049 #include <string.h>
00050 #endif
00051 #ifdef HAVE_STRINGS_H
00052 #include <strings.h>
00053 #endif
00054 #ifdef HAVE_SYS_UTSNAME_H
00055 #include <sys/utsname.h>
00056 #endif
00057 #ifdef HAVE_NETINET_TCP_H
00058 #include <netinet/tcp.h>
00059 #endif
00060 #ifdef HAVE_UNISTD_H
00061 #include <unistd.h>
00062 #endif
00063 #ifdef HAVE_UUID_UUID_H
00064 #include <uuid/uuid.h>
00065 #endif
00066
00067 #ifdef TIME_WITH_SYS_TIME
00068 # include <sys/time.h>
00069 # include <time.h>
00070 #else
00071 # ifdef HAVE_SYS_TIME_H
00072 # include <sys/time.h>
00073 # else
00074 # include <time.h>
00075 # endif
00076 #endif
00077
00078 #ifdef __cplusplus
00079 extern "C" {
00080 #endif
00081
00082 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
00083 #define likely(__x) if((__x))
00084 #define unlikely(__x) if((__x))
00085 #else
00086 #define likely(__x) if(__builtin_expect((__x), 1))
00087 #define unlikely(__x) if(__builtin_expect((__x), 0))
00088 #endif
00089
00095 #define GEARMAN_LOG(__gearman, __verbose, ...) { \
00096 if ((__gearman)->log_fn != NULL) \
00097 { \
00098 char _log_buffer[GEARMAN_MAX_ERROR_SIZE]; \
00099 snprintf(_log_buffer, GEARMAN_MAX_ERROR_SIZE, __VA_ARGS__); \
00100 (*((__gearman)->log_fn))(_log_buffer, __verbose, \
00101 (void *)(__gearman)->log_context); \
00102 } \
00103 }
00104
00109 #define GEARMAN_FATAL(__gearman, ...) \
00110 GEARMAN_LOG(__gearman, GEARMAN_VERBOSE_FATAL, __VA_ARGS__)
00111 #define GEARMAN_SERVER_FATAL(__server, ...) \
00112 GEARMAN_FATAL((__server)->gearman, __VA_ARGS__)
00113
00118 #define GEARMAN_ERROR(__gearman, ...) { \
00119 unlikely ((__gearman)->verbose >= GEARMAN_VERBOSE_ERROR) \
00120 GEARMAN_LOG(__gearman, GEARMAN_VERBOSE_ERROR, __VA_ARGS__) \
00121 }
00122 #define GEARMAN_SERVER_ERROR(__server, ...) \
00123 GEARMAN_ERROR((__server)->gearman, __VA_ARGS__)
00124
00129 #define GEARMAN_INFO(__gearman, ...) { \
00130 unlikely ((__gearman)->verbose >= GEARMAN_VERBOSE_INFO) \
00131 GEARMAN_LOG(__gearman, GEARMAN_VERBOSE_INFO, __VA_ARGS__) \
00132 }
00133 #define GEARMAN_SERVER_INFO(__server, ...) \
00134 GEARMAN_INFO((__server)->gearman, __VA_ARGS__)
00135
00140 #define GEARMAN_DEBUG(__gearman, ...) { \
00141 unlikely ((__gearman)->verbose >= GEARMAN_VERBOSE_DEBUG) \
00142 GEARMAN_LOG(__gearman, GEARMAN_VERBOSE_DEBUG, __VA_ARGS__) \
00143 }
00144 #define GEARMAN_SERVER_DEBUG(__server, ...) \
00145 GEARMAN_DEBUG((__server)->gearman, __VA_ARGS__)
00146
00151 #define GEARMAN_CRAZY(__gearman, ...) { \
00152 unlikely ((__gearman)->verbose >= GEARMAN_VERBOSE_CRAZY) \
00153 GEARMAN_LOG(__gearman, GEARMAN_VERBOSE_CRAZY, __VA_ARGS__) \
00154 }
00155 #define GEARMAN_SERVER_CRAZY(__server, ...) \
00156 GEARMAN_CRAZY((__server)->gearman, __VA_ARGS__)
00157
00162 #define GEARMAN_ERROR_SET(__gearman, __function, ...) { \
00163 if ((__gearman)->log_fn == NULL) \
00164 { \
00165 snprintf((__gearman)->last_error, GEARMAN_MAX_ERROR_SIZE, \
00166 __function ":" __VA_ARGS__); \
00167 } \
00168 else \
00169 GEARMAN_FATAL(__gearman, __function ":" __VA_ARGS__) \
00170 }
00171 #define GEARMAN_SERVER_ERROR_SET(__server, ...) \
00172 GEARMAN_ERROR_SET((__server)->gearman, __VA_ARGS__)
00173
00178 #define GEARMAN_CONF_ERROR_SET(__conf, __function, ...) { \
00179 snprintf((__conf)->last_error, GEARMAN_MAX_ERROR_SIZE, \
00180 __function ":" __VA_ARGS__); \
00181 }
00182
00187 #define GEARMAN_SERVER_THREAD_LOCK(__thread) { \
00188 if ((__thread)->server->thread_count > 1) \
00189 (void) pthread_mutex_lock(&((__thread)->lock)); \
00190 }
00191
00196 #define GEARMAN_SERVER_THREAD_UNLOCK(__thread) { \
00197 if ((__thread)->server->thread_count > 1) \
00198 (void) pthread_mutex_unlock(&((__thread)->lock)); \
00199 }
00200
00205 #define GEARMAN_LIST_ADD(__list, __obj, __prefix) { \
00206 if (__list ## _list != NULL) \
00207 __list ## _list->__prefix ## prev= __obj; \
00208 __obj->__prefix ## next= __list ## _list; \
00209 __obj->__prefix ## prev= NULL; \
00210 __list ## _list= __obj; \
00211 __list ## _count++; \
00212 }
00213
00218 #define GEARMAN_LIST_DEL(__list, __obj, __prefix) { \
00219 if (__list ## _list == __obj) \
00220 __list ## _list= __obj->__prefix ## next; \
00221 if (__obj->__prefix ## prev != NULL) \
00222 __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
00223 if (__obj->__prefix ## next != NULL) \
00224 __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
00225 __list ## _count--; \
00226 }
00227
00232 #define GEARMAN_FIFO_ADD(__list, __obj, __prefix) { \
00233 if (__list ## _end == NULL) \
00234 __list ## _list= __obj; \
00235 else \
00236 __list ## _end->__prefix ## next= __obj; \
00237 __list ## _end= __obj; \
00238 __list ## _count++; \
00239 }
00240
00245 #define GEARMAN_FIFO_DEL(__list, __obj, __prefix) { \
00246 __list ## _list= __obj->__prefix ## next; \
00247 if (__list ## _list == NULL) \
00248 __list ## _end= NULL; \
00249 __list ## _count--; \
00250 }
00251
00256 #define GEARMAN_HASH_ADD(__hash, __key, __obj, __prefix) { \
00257 if (__hash ## _hash[__key] != NULL) \
00258 __hash ## _hash[__key]->__prefix ## prev= __obj; \
00259 __obj->__prefix ## next= __hash ## _hash[__key]; \
00260 __obj->__prefix ## prev= NULL; \
00261 __hash ## _hash[__key]= __obj; \
00262 __hash ## _count++; \
00263 }
00264
00269 #define GEARMAN_HASH_DEL(__hash, __key, __obj, __prefix) { \
00270 if (__hash ## _hash[__key] == __obj) \
00271 __hash ## _hash[__key]= __obj->__prefix ## next; \
00272 if (__obj->__prefix ## prev != NULL) \
00273 __obj->__prefix ## prev->__prefix ## next= __obj->__prefix ## next; \
00274 if (__obj->__prefix ## next != NULL) \
00275 __obj->__prefix ## next->__prefix ## prev= __obj->__prefix ## prev; \
00276 __hash ## _count--; \
00277 }
00278
00279
00280
00281 #ifndef HAVE_EVENT_BASE_NEW
00282 #define event_base_new event_init
00283 #endif
00284
00285 #ifndef HAVE_EVENT_BASE_FREE
00286 #define event_base_free (void)
00287 #endif
00288
00289 #ifndef HAVE_EVENT_BASE_GET_METHOD
00290 #define event_base_get_method(__base) event_get_method()
00291 #endif
00292
00293 #ifdef __cplusplus
00294 }
00295 #endif
00296
00297 #endif