PHP_METHOD(swoole_lock, __construct) { long type = SW_MUTEX; char *filelock; int filelock_len = 0; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &type, &filelock, &filelock_len) == FAILURE) { RETURN_FALSE; } swLock *lock = emalloc(sizeof(swLock)); if (lock == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "alloc failed."); RETURN_FALSE; } switch(type) { #ifdef HAVE_RWLOCK case SW_RWLOCK: ret = swRWLock_create(lock, 1); break; #endif 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); 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; } swoole_set_object(getThis(), lock); RETURN_TRUE; }
swTable* swTable_new(uint32_t rows_size) { swTable *table = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swTable)); if (table == NULL) { return NULL; } if (swMutex_create(&table->lock, 1) < 0) { swWarn("mutex create failed."); return NULL; } table->iterator = sw_malloc(sizeof(swTable_iterator)); if (!table->iterator) { swWarn("malloc failed."); return NULL; } table->columns = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, (swHashMap_dtor)swTableColumn_free); if (!table->columns) { return NULL; } table->size = rows_size; bzero(table->iterator, sizeof(swTable_iterator)); table->memory = NULL; return table; }
swAllocator* swMemoryGlobal_create(int pagesize, char shared) { swMemoryGlobal gm, *gm_ptr; assert(pagesize >= SW_PAGE_SIZE); bzero(&gm, sizeof(swMemoryGlobal)); gm.shared = shared; gm.pagesize = pagesize; void *first_page = swMemoryGlobal_new_page(&gm); if (first_page == NULL) { return NULL; } //分配内存需要加锁 if(swMutex_create(&gm.lock, 1) < 0) { return NULL; } //root gm.root_page = first_page; gm.cur_page = first_page; gm_ptr = (swMemoryGlobal *) gm.mem; gm.offset += sizeof(swMemoryGlobal); swAllocator *allocator = (swAllocator *) (gm.mem + gm.offset); gm.offset += sizeof(swAllocator); allocator->object = gm_ptr; allocator->alloc = swMemoryGlobal_alloc; allocator->destroy = swMemoryGlobal_destroy; allocator->free = swMemoryGlobal_free; memcpy(gm_ptr, &gm, sizeof(gm)); return allocator; }
swChannel* swChannel_create(int size, int maxlen, int flag) { assert(size > SW_CHANNEL_MIN_MEM + maxlen); int ret; void *mem; //是否使用共享内存 if (flag & SW_CHAN_SHM) { mem = sw_shm_malloc(size); } else { mem = sw_malloc(size); } if (mem == NULL) { swWarn("swChannel_create: malloc fail"); return NULL; } swChannel *object = mem; mem += sizeof(swChannel); bzero(object, sizeof(swChannel)); //允许溢出 object->size = size - maxlen; object->mem = mem; object->maxlen = maxlen; object->flag = flag; //是否启用锁 if (flag & SW_CHAN_LOCK) { //初始化锁 if (swMutex_create(&object->lock, 1) < 0) { swWarn("mutex init fail\n"); return NULL; } } //初始化通知系统 if (flag & SW_CHAN_NOTIFY) { #ifdef HAVE_EVENTFD ret = swPipeEventfd_create(&object->notify_fd, 1, 1); #else ret = swPipeBase_create(&object->notify_fd, 1); #endif if (ret < 0) { swWarn("notify_fd init fail\n"); return NULL; } } return object; }
void swoole_init(void) { struct rlimit rlmt; if (SwooleG.running == 0) { bzero(&SwooleG, sizeof(SwooleG)); bzero(sw_error, SW_ERROR_MSG_SIZE); //初始化全局变量 SwooleG.running = 1; sw_errno = 0; SwooleG.cpu_num = sysconf(_SC_NPROCESSORS_ONLN); SwooleG.pagesize = getpagesize(); if (getrlimit(RLIMIT_NOFILE, &rlmt) < 0) { swWarn("getrlimit() failed. Error: %s[%d]", strerror(errno), errno); } else { SwooleG.max_sockets = (uint32_t) rlmt.rlim_cur; } //random seed srandom(time(NULL)); //init global lock swMutex_create(&SwooleG.lock, 0); //init signalfd #ifdef HAVE_SIGNALFD swSignalfd_init(); SwooleG.use_signalfd = 1; #endif //timerfd #ifdef HAVE_TIMERFD SwooleG.use_timerfd = 1; #endif SwooleG.use_timer_pipe = 1; //将日志设置为标准输出 SwooleG.log_fd = STDOUT_FILENO; //初始化全局内存 SwooleG.memory_pool = swMemoryGlobal_new(SW_GLOBAL_MEMORY_PAGESIZE, 1); if (SwooleG.memory_pool == NULL) { swError("[Master] Fatal Error: create global memory fail. Error: %s[%d]", strerror(errno), errno); } SwooleGS = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swServerGS)); if (SwooleGS == NULL) { swError("[Master] Fatal Error: alloc memory for SwooleGS fail. Error: %s[%d]", strerror(errno), errno); } } }
int swCond_create(swCond *cond) { if (pthread_cond_init(&cond->cond, NULL) < 0) { swWarn("pthread_cond_init fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } if (swMutex_create(&cond->lock, 0) < 0) { return SW_ERR; } return SW_OK; }
int swWorker_create(swWorker *worker) { /** * Create shared memory storage */ worker->send_shm = sw_shm_malloc(SwooleG.serv->buffer_output_size); if (worker->send_shm == NULL) { swWarn("malloc for worker->store failed."); return SW_ERR; } swMutex_create(&worker->lock, 1); return SW_OK; }
int swAio_init(void) { if (SwooleAIO.init) { swWarn("AIO has already been initialized"); return SW_ERR; } if (!SwooleG.main_reactor) { swWarn("No eventloop, cannot initialized"); return SW_ERR; } if (swPipeBase_create(&_aio_pipe, 0) < 0) { return SW_ERR; } if (swMutex_create(&SwooleAIO.lock, 0) < 0) { swWarn("create mutex lock error"); return SW_ERR; } if (SwooleAIO.thread_num <= 0) { SwooleAIO.thread_num = SW_AIO_THREAD_NUM_DEFAULT; } if (swThreadPool_create(&pool, SwooleAIO.thread_num) < 0) { return SW_ERR; } pool.onTask = swAio_onTask; _pipe_read = _aio_pipe.getFd(&_aio_pipe, 0); _pipe_write = _aio_pipe.getFd(&_aio_pipe, 1); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_AIO, swAio_onCompleted); SwooleG.main_reactor->add(SwooleG.main_reactor, _pipe_read, SW_FD_AIO); if (swThreadPool_run(&pool) < 0) { return SW_ERR; } SwooleAIO.init = 1; return SW_OK; }
int swFactoryThread_create(swFactory *factory, int worker_num) { swFactoryThread *object; swServer *serv = factory->ptr; object = sw_calloc(worker_num, sizeof(swFactoryThread)); if (object == NULL) { swWarn("malloc[0] failed"); return SW_ERR; } if (swThreadPool_create(&object->workers, worker_num) < 0) { return SW_ERR; } int i; swReactorThread *thread; for (i = 0; i < serv->reactor_num; i++) { thread = swServer_get_thread(serv, i); swMutex_create(&thread->lock, 0); } object->worker_num = worker_num; factory->object = object; factory->dispatch = swFactoryThread_dispatch; factory->finish = swFactoryThread_finish; factory->end = swFactory_end; factory->start = swFactoryThread_start; factory->shutdown = swFactoryThread_shutdown; factory->notify = swFactory_notify; factory->onTask = NULL; factory->onFinish = NULL; object->workers.onStart = swFactoryThread_onStart; object->workers.onStop = swFactoryThread_onStop; object->workers.onTask = swFactoryThread_onTask; object->workers.ptr1 = factory->ptr; object->workers.ptr2 = factory; return SW_OK; }
swTable* swTable_new(uint32_t rows_size) { if (rows_size >= 0x80000000) { rows_size = 0x80000000; } else { uint32_t i = 10; while ((1U << i) < rows_size) { i++; } rows_size = 1 << i; } swTable *table = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swTable)); if (table == NULL) { return NULL; } if (swMutex_create(&table->lock, 1) < 0) { swWarn("mutex create failed."); return NULL; } table->iterator = sw_malloc(sizeof(swTable_iterator)); if (!table->iterator) { swWarn("malloc failed."); return NULL; } table->columns = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, (swHashMap_dtor)swTableColumn_free); if (!table->columns) { return NULL; } table->size = rows_size; table->mask = rows_size - 1; bzero(table->iterator, sizeof(swTable_iterator)); table->memory = NULL; return table; }
int swAioBase_init(int max_aio_events) { if (swPipeBase_create(&swoole_aio_pipe, 0) < 0) { return SW_ERR; } if (swMutex_create(&SwooleAIO.lock, 0) < 0) { swWarn("create mutex lock error."); return SW_ERR; } if (SwooleAIO.thread_num <= 0) { SwooleAIO.thread_num = SW_AIO_THREAD_NUM_DEFAULT; } if (swThreadPool_create(&swAioBase_thread_pool, SwooleAIO.thread_num) < 0) { return SW_ERR; } swAioBase_thread_pool.onTask = swAioBase_thread_onTask; swAioBase_pipe_read = swoole_aio_pipe.getFd(&swoole_aio_pipe, 0); swAioBase_pipe_write = swoole_aio_pipe.getFd(&swoole_aio_pipe, 1); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_AIO, swAioBase_onFinish); SwooleG.main_reactor->add(SwooleG.main_reactor, swAioBase_pipe_read, SW_FD_AIO); if (swThreadPool_run(&swAioBase_thread_pool) < 0) { return SW_ERR; } SwooleAIO.callback = swAio_callback_test; SwooleAIO.destroy = swAioBase_destroy; SwooleAIO.read = swAioBase_read; SwooleAIO.write = swAioBase_write; return SW_OK; }
void swoole_init(void) { if (SwooleG.running == 0) { bzero(&SwooleG, sizeof(SwooleG)); bzero(sw_error, SW_ERROR_MSG_SIZE); //初始化全局变量 SwooleG.running = 1; sw_errno = 0; //init global lock swMutex_create(&SwooleG.lock, 0); //init signalfd #ifdef HAVE_SIGNALFD swSignalfd_init(); SwooleG.use_signalfd = 1; #endif //timerfd #ifdef HAVE_TIMERFD SwooleG.use_timerfd = 1; #endif //将日志设置为标准输出 SwooleG.log_fd = STDOUT_FILENO; //初始化全局内存 SwooleG.memory_pool = swMemoryGlobal_new(SW_GLOBAL_MEMORY_PAGESIZE, 1); if(SwooleG.memory_pool == NULL) { swError("[Master] Fatal Error: create global memory fail. Error: %s[%d]", strerror(errno), errno); } SwooleGS = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swServerGS)); if(SwooleGS == NULL) { swError("[Master] Fatal Error: alloc memory for SwooleGS fail. Error: %s[%d]", strerror(errno), errno); } } }
/** * @elem_size 每个item内存块的长度 * @elem_max item最多个数 */ int swChan_create(swChan **chan_addr, void *mem, int mem_size, int elem_max, int elem_size) { int slab_size, ret; bzero(mem, sizeof(swChan)); //初始化内存块 if (mem_size <= 0 || mem == NULL || mem_size < SW_CHAN_MIN_MEM) { swWarn("error: mem_size <= %d or mem == NULL\n", SW_CHAN_MIN_MEM); return SW_ERR; } *chan_addr = mem; swChan *chan = *chan_addr; mem += sizeof(swChan); //去掉swChan结构占用的部分 if (swMutex_create(&chan->lock, 1) < 0) { swWarn("create mutex fail.\n"); return SW_ERR; } if (elem_max == 0) { elem_max = 65535; } #ifdef HAVE_EVENTFD ret = swPipeEventfd_create(&chan->notify_fd, 1, 1); #else ret = swPipeBase_create(&chan->notify_fd, 1); #endif if(ret < 0) { swWarn("create eventfd fail.\n"); return SW_ERR; } slab_size = sizeof(swChanElem)*elem_max; chan->elem_max = elem_max; chan->mem_size = mem_size - slab_size - sizeof(swChan);//允许溢出 chan->elems = (swChanElem *) mem; chan->mem = mem + slab_size; swMemPool_create(&chan->pool, chan->mem, chan->mem_size, elem_size); return SW_OK; }
void swoole_init(void) { struct rlimit rlmt; if (SwooleG.running) { return; } bzero(&SwooleG, sizeof(SwooleG)); bzero(&SwooleWG, sizeof(SwooleWG)); bzero(sw_error, SW_ERROR_MSG_SIZE); SwooleG.running = 1; sw_errno = 0; SwooleG.log_fd = STDOUT_FILENO; SwooleG.cpu_num = sysconf(_SC_NPROCESSORS_ONLN); SwooleG.pagesize = getpagesize(); SwooleG.pid = getpid(); SwooleG.socket_buffer_size = SW_SOCKET_BUFFER_SIZE; //get system uname uname(&SwooleG.uname); //random seed srandom(time(NULL)); //init global shared memory SwooleG.memory_pool = swMemoryGlobal_new(SW_GLOBAL_MEMORY_PAGESIZE, 1); if (SwooleG.memory_pool == NULL) { printf("[Master] Fatal Error: create global memory failed."); exit(1); } SwooleGS = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swServerGS)); if (SwooleGS == NULL) { printf("[Master] Fatal Error: alloc memory for SwooleGS failed."); exit(2); } //init global lock swMutex_create(&SwooleGS->lock, 1); swMutex_create(&SwooleGS->lock_2, 1); if (getrlimit(RLIMIT_NOFILE, &rlmt) < 0) { swWarn("getrlimit() failed. Error: %s[%d]", strerror(errno), errno); SwooleG.max_sockets = 1024; } else { SwooleG.max_sockets = (uint32_t) rlmt.rlim_cur; } SwooleG.module_stack = swString_new(8192); if (SwooleG.module_stack == NULL) { exit(3); } if (!SwooleG.task_tmpdir) { SwooleG.task_tmpdir = strndup(SW_TASK_TMP_FILE, sizeof(SW_TASK_TMP_FILE)); SwooleG.task_tmpdir_len = sizeof(SW_TASK_TMP_FILE); } char *tmp_dir = swoole_dirname(SwooleG.task_tmpdir); //create tmp dir if (access(tmp_dir, R_OK) < 0 && swoole_mkdir_recursive(tmp_dir) < 0) { swWarn("create task tmp dir(%s) failed.", tmp_dir); } if (tmp_dir) { sw_strdup_free(tmp_dir); } //init signalfd #ifdef HAVE_SIGNALFD swSignalfd_init(); SwooleG.use_signalfd = 1; #endif //timerfd #ifdef HAVE_TIMERFD SwooleG.use_timerfd = 1; #endif SwooleG.use_timer_pipe = 1; SwooleStats = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swServerStats)); if (SwooleStats == NULL) { swError("[Master] Fatal Error: alloc memory for SwooleStats failed."); } swoole_update_time(); }
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; }
void swoole_init(void) { struct rlimit rlmt; if (SwooleG.running) { return; } bzero(&SwooleG, sizeof(SwooleG)); bzero(&SwooleWG, sizeof(SwooleWG)); bzero(sw_error, SW_ERROR_MSG_SIZE); SwooleG.running = 1; sw_errno = 0; SwooleG.log_fd = STDOUT_FILENO; SwooleG.cpu_num = sysconf(_SC_NPROCESSORS_ONLN); SwooleG.pagesize = getpagesize(); SwooleG.pid = getpid(); //get system uname uname(&SwooleG.uname); //random seed srandom(time(NULL)); //init global shared memory SwooleG.memory_pool = swMemoryGlobal_new(SW_GLOBAL_MEMORY_PAGESIZE, 1); if (SwooleG.memory_pool == NULL) { printf("[Master] Fatal Error: create global memory failed."); exit(1); } SwooleGS = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swServerGS)); if (SwooleGS == NULL) { printf("[Master] Fatal Error: alloc memory for SwooleGS failed."); exit(2); } //init global lock swMutex_create(&SwooleGS->lock, 1); swMutex_create(&SwooleGS->lock_2, 1); if (getrlimit(RLIMIT_NOFILE, &rlmt) < 0) { swWarn("getrlimit() failed. Error: %s[%d]", strerror(errno), errno); SwooleG.max_sockets = 1024; } else { SwooleG.max_sockets = (uint32_t) rlmt.rlim_cur; } //init signalfd #ifdef HAVE_SIGNALFD swSignalfd_init(); SwooleG.use_signalfd = 1; #endif //timerfd #ifdef HAVE_TIMERFD SwooleG.use_timerfd = 1; #endif SwooleG.use_timer_pipe = 1; SwooleStats = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swServerStats)); if (SwooleStats == NULL) { swError("[Master] Fatal Error: alloc memory for SwooleStats failed."); } swoole_update_time(); }