static int swFactoryProcess_writer_start(swFactory *factory) { swFactoryProcess *this = factory->object; swThreadParam *param; int i; pthread_t pidt; swThreadStartFunc thread_main; #ifdef SW_USE_SHM_CHAN thread_main = (swThreadStartFunc) swFactoryProcess_writer_loop_ex; #else thread_main = (swThreadStartFunc)swFactoryProcess_writer_loop; #endif for (i = 0; i < this->writer_num; i++) { param = sw_malloc(sizeof(swThreadParam)); if (param == NULL ) { swError("malloc fail\n"); return SW_ERR; } #ifdef SW_USE_SHM_CHAN #if defined(SW_CHAN_USE_MMAP) || SW_CHAN_USE_MMAP==1 mm = swShareMemory_mmap_create(&this->writers[i].shm, SW_CHAN_BUFFER_SIZE, 0); #else mm = swShareMemory_sysv_create(&this->writers[i].shm, SW_CHAN_BUFFER_SIZE, SW_CHAN_SYSV_KEY); #endif if (mm == NULL ) { swError("shm_create fail\n"); return SW_ERR; } if (swChan_create(&this->writers[i].chan, mm, SW_CHAN_BUFFER_SIZE, SW_CHAN_ELEM_SIZE) < 0) { swError("swChan_create fail\n"); return SW_ERR; } #endif param->object = factory; param->pti = i; if (pthread_create(&pidt, NULL, thread_main, (void *) param) < 0) { swTrace("pthread_create fail\n"); return SW_ERR; } this->writers[i].ptid = pidt; SW_START_SLEEP; } return SW_OK; }
void* sw_shm_malloc(size_t size) { swShareMemory object; void *mem; //object对象需要保存在头部 size += sizeof(swShareMemory); mem = swShareMemory_mmap_create(&object, size, NULL); if (mem == NULL) { return NULL; } else { memcpy(mem, &object, sizeof(swShareMemory)); return mem + sizeof(swShareMemory); } }
void* sw_shm_calloc(size_t num, size_t _size) { swShareMemory object; void *mem; void *ret_mem; //object对象需要保存在头部 int size = sizeof(swShareMemory) + (num * _size); mem = swShareMemory_mmap_create(&object, size, NULL); if (mem == NULL) { return NULL; } else { memcpy(mem, &object, sizeof(swShareMemory)); ret_mem = mem + sizeof(swShareMemory); //calloc需要初始化 bzero(ret_mem, size - sizeof(swShareMemory)); return ret_mem; } }