/* {{{ http_request_callback_ctx http_request_callback_data(void *) */ http_request_callback_ctx *_http_request_callback_data_ex(void *data, zend_bool cpy TSRMLS_DC) { http_request_callback_ctx *ctx = emalloc(sizeof(http_request_callback_ctx)); TSRMLS_SET_CTX(ctx->tsrm_ctx); ctx->data = data; if (cpy) { return http_request_data_copy(COPY_CONTEXT, ctx); } else { return ctx; } }
/* {{{ PHP_GINIT_FUNCTION */ static PHP_GINIT_FUNCTION(mongo) { /* On windows, the max length is 256. Linux doesn't have a limit, but it * will fill in the first 256 chars of hostname even if the actual * hostname is longer. If you can't get a unique character in the first * 256 chars of your hostname, you're doing it wrong. */ int len, win_max = 256; char *hostname, host_start[256]; register ulong hash; mongo_globals->default_host = "localhost"; mongo_globals->default_port = 27017; mongo_globals->request_id = 3; mongo_globals->chunk_size = DEFAULT_CHUNK_SIZE; mongo_globals->cmd_char = "$"; mongo_globals->inc = 0; mongo_globals->response_num = 0; mongo_globals->errmsg = 0; mongo_globals->pool_size = -1; hostname = host_start; /* from the gnu manual: * gethostname stores the beginning of the host name in name even if the * host name won't entirely fit. For some purposes, a truncated host name * is good enough. If it is, you can ignore the error code. * So we'll ignore the error code. * Returns 0-terminated hostname. */ gethostname(hostname, win_max); len = strlen(hostname); hash = 5381; /* from zend_hash.h */ /* variant with the hash unrolled eight times */ for (; len >= 8; len -= 8) { hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; hash = ((hash << 5) + hash) + *hostname++; } switch (len) { case 7: hash = ((hash << 5) + hash) + *hostname++; /* fallthrough... */ case 6: hash = ((hash << 5) + hash) + *hostname++; /* fallthrough... */ case 5: hash = ((hash << 5) + hash) + *hostname++; /* fallthrough... */ case 4: hash = ((hash << 5) + hash) + *hostname++; /* fallthrough... */ case 3: hash = ((hash << 5) + hash) + *hostname++; /* fallthrough... */ case 2: hash = ((hash << 5) + hash) + *hostname++; /* fallthrough... */ case 1: hash = ((hash << 5) + hash) + *hostname++; break; case 0: break; } mongo_globals->machine = hash; mongo_globals->ts_inc = 0; #if PHP_VERSION_ID >= 50300 mongo_globals->log_callback_info = empty_fcall_info; mongo_globals->log_callback_info_cache = empty_fcall_info_cache; #endif mongo_globals->manager = mongo_init(); TSRMLS_SET_CTX(mongo_globals->manager->log_context); mongo_globals->manager->log_function = php_mcon_log_wrapper; #if MONGO_PHP_STREAMS mongo_globals->manager->connect = php_mongo_io_stream_connect; mongo_globals->manager->recv_header = php_mongo_io_stream_read; mongo_globals->manager->recv_data = php_mongo_io_stream_read; mongo_globals->manager->send = php_mongo_io_stream_send; mongo_globals->manager->close = php_mongo_io_stream_close; mongo_globals->manager->forget = php_mongo_io_stream_forget; #endif }
PHP_METHOD(swoole_lock, __construct) { long type = SW_MUTEX; char *filelock; int filelock_len = 0; int ret; #ifdef ZTS if(sw_thread_ctx == NULL) { TSRMLS_SET_CTX(sw_thread_ctx); } #endif if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &type, &filelock, &filelock_len) == FAILURE) { RETURN_FALSE; } swLock *lock = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swLock)); if (lock == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "alloc failed."); RETURN_FALSE; } switch(type) { case SW_RWLOCK: ret = swRWLock_create(lock, 1); break; case SW_FILELOCK: if (filelock_len <= 0) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "filelock require lock file name."); RETURN_FALSE; } int fd; if ((fd = open(filelock, O_RDWR | O_CREAT, 0666)) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "open file[%s] failed. Error: %s [%d]", filelock, strerror(errno), errno); RETURN_FALSE; } ret = swFileLock_create(lock, fd); break; case SW_SEM: ret = swSem_create(lock, IPC_PRIVATE, 1); break; #ifdef HAVE_SPINLOCK case SW_SPINLOCK: ret = swSpinLock_create(lock, 1); break; #endif case SW_MUTEX: default: ret = swMutex_create(lock, 1); break; } if (ret < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "create lock failed"); RETURN_FALSE; } zval *zres; MAKE_STD_ZVAL(zres); ZEND_REGISTER_RESOURCE(zres, lock, le_swoole_lock); zend_update_property(swoole_lock_class_entry_ptr, getThis(), ZEND_STRL("_lock"), zres TSRMLS_CC); zval_ptr_dtor(&zres); RETURN_TRUE; }