void *my_thread(void *message) { printf("tid : %u \n",pthread_self()); pthread_detach(pthread_self()); while(1) { size_t fd = pool_remove(&sp); int recv_len = 0; char buff[4096] ; memset(buff,0,4096); while(1) { if((recv_len = recv(fd, buff, sizeof(buff), 0)) > 0) { printf("recieve data from client : %s\n", buff); } else if(recv_len == -1) { if (errno == EWOULDBLOCK || errno == EAGAIN) printf("under O_NONBLOCK mode data didn't prepare \n"); } else { printf("googbye client ~~~ \n"); close(fd); break; } } } }
/* * Shut down database module * No function should be called after this */ void db_mysql_close(db_con_t* _h) { struct pool_con* con; if (!_h) { LM_ERR("invalid parameter value\n"); return; } con = (struct pool_con*)_h->tail; if (pool_remove(con) == 1) { db_mysql_free_connection((struct my_con*)con); } pkg_free(_h); }
/* * Shut down database module * No function should be called after this */ void db_close(db_con_t* _h) { struct pool_con* con; if (!_h) { LOG(L_ERR, "db_close: Invalid parameter value\n"); return; } con = (struct pool_con*)_h->tail; if (pool_remove(con) != 0) { free_connection((struct my_con*)con); } pkg_free(_h); }
void pool_free(pool_t *pool, void *ptr) { pool_node_t *p; if( !ptr ) return; p = pool_find(pool, ptr); if( p ) { p->refcount--; if(p->refcount <= 0 ) { if( p->free ) { (*p->free)(p->ptr, p->arg); } else { free(p->ptr); } pool_remove(pool, ptr); } } }
static int cancel_task(struct task_dispatcher_t* td, struct task_t* task, tasks_comparator cmp) { int result = -1; int should_fire_event = 0; pthread_mutex_lock(&td->lock); struct task_t* queued_task = pool_get_next(td->tasks, NULL); struct task_t* prev = NULL; while (queued_task != NULL) { if (cmp(queued_task, task) == 0) { LOGI("Removing task %p", queued_task); queued_task = pool_remove(td->tasks, queued_task); result = 0; if (should_fire_event == 0 && prev == NULL) { should_fire_event = 1; } } else { prev = queued_task; queued_task = pool_get_next(td->tasks, queued_task); } } if (should_fire_event) { pthread_cond_broadcast(&td->event); } pthread_mutex_unlock(&td->lock); return result; }
static void* working_thread(void* ctx) { struct task_dispatcher_t* td = (struct task_dispatcher_t*)ctx; LOGI("Task dispatcher working thread started"); pthread_mutex_lock(&td->event_lock); while (1) { if (pool_is_empty(td->tasks)) { LOGD("Waiting for event"); pthread_cond_wait(&td->event, &td->event_lock); } else { struct task_t* task = pool_get_next(td->tasks, NULL); struct timespec activate_time = {0}; timestamp_to_timespec(&task->activate_time, &activate_time); timestamp_t current = {0}; timestamp_set(¤t); LOGD("Waiting for task [%ld:%d -> %ld:%d]", current.value.tv_sec, current.value.tv_usec, task->activate_time.value.tv_sec, task->activate_time.value.tv_usec); pthread_cond_timedwait(&td->event, &td->event_lock, &activate_time); } if (!td->running) { LOGI("Stopping working thread"); break; } if (!pool_is_empty(td->tasks)) { struct task_t* task = pool_get_next(td->tasks, NULL); timestamp_t current = {0}; timestamp_set(¤t); if (timestamp_diff(¤t, &task->activate_time) >= 0) { pool_remove(td->tasks, task); LOGD("Removed task %p [queue empty %d]", task, pool_is_empty(td->tasks)); if (task->interval > 0) { task_dispatcher_enqueue_task(td, task->callback, task->ctx, task->interval, task->interval); } pthread_mutex_unlock(&td->event_lock); LOGD("Running task"); task->callback(td, task, &task->enqueue_time, ¤t, task->ctx); pthread_mutex_lock(&td->event_lock); } } } pthread_mutex_unlock(&td->event_lock); LOGI("Task dispatcher working thread stopped"); return NULL; }
void* pool_realloc(pool_t *pool, void *ptr, size_t len) { pool_remove(pool, ptr); ptr = realloc(ptr, len); return pool_add(pool, ptr, 0, 0); }