void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data) { apr_status_t rv; struct redis_command *command = NULL; info_print("enter: consumer thread\n"); while (1) { rv = apr_queue_pop(queue, (void **)&command); if (rv == APR_EINTR) continue; if (rv == APR_EOF) { info_print("queue has terminated, consumer thread exit\n"); break; } if (rv != APR_SUCCESS ) { apr_sleep(1000* 1000); //sleep 1 second. continue; } if(command) { int res = _do_redis_command((const char **)command->argv,(const size_t *)command->argvlen, command->arg_count); free_command(command); command = NULL; } } info_print("exit:consumer thread\n"); return NULL; }
static apt_bool_t apt_consumer_task_run(apt_task_t *task) { apr_status_t rv; void *msg; apt_bool_t *running; apt_consumer_task_t *consumer_task; consumer_task = apt_task_object_get(task); if(!consumer_task) { return FALSE; } running = apt_task_running_flag_get(task); if(!running) { return FALSE; } while(*running) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Wait for Messages [%s]",apt_task_name_get(task)); rv = apr_queue_pop(consumer_task->msg_queue,&msg); if(rv == APR_SUCCESS) { if(msg) { apt_task_msg_t *task_msg = msg; apt_task_msg_process(consumer_task->base,task_msg); } } } return TRUE; }
/* * Main writer thread. Takes aside messages from the queue and saves them */ static void *writer_main(apr_thread_t *apr_thread, void *data) { ap_log_error(APLOG_MARK, APLOG_INFO, 0, tee_server, "tee: Starting new writer thread (pid/tid %d/%d)", getpid(), gettid()); while (!finishing) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, tee_server, "tee: Blocking on queue (pid %d)", getpid()); tee_update_status(WAITING); tee_saved_request *sr; apr_status_t rc = apr_queue_pop(tee_global_queue, (void **) &sr); if (rc == APR_EINTR) { // This happens when process is ending continue; } tee_update_status(WRITING); ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, tee_server, "tee: Request popped (pid %d)", getpid()); tee_update_queue_len(apr_queue_size(tee_global_queue)); if (process_request(sr)) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, sr->server, "tee: File written in %.3f ms (pid %d)", (sr->time_written - sr->time_dequeued) / 1000.0, getpid()); } tee_update_saved_file_stats(sr); apr_pool_destroy(sr->pool); } tee_update_status(NULL_STATUS); ap_log_error(APLOG_MARK, APLOG_INFO, 0, tee_server, "tee: Finishing writer thread (pid/tid %d/%d)", getpid(), gettid()); return NULL; }
apr_status_t procmgr_send_spawn_cmd(fcgid_command * command, request_rec * r) { if (g_thread && g_msgqueue && !g_must_exit && g_reqlock && g_notifyqueue) { apr_status_t rv; /* Prepare the message send to another thread destroy the message if I can't push to message */ fcgid_command *postcmd = (fcgid_command *) malloc(sizeof(fcgid_command)); if (!postcmd) return APR_ENOMEM; memcpy(postcmd, command, sizeof(*command)); /* Get request lock first */ if ((rv = apr_thread_mutex_lock(g_reqlock)) != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, "mod_fcgid: can't get request lock"); return rv; } /* Try push the message */ if ((rv = apr_queue_push(g_msgqueue, postcmd)) != APR_SUCCESS) { apr_thread_mutex_unlock(g_reqlock); free(postcmd); ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, "mod_fcgid: can't push request message"); return rv; } else { /* Wait the respond from process manager */ char *notifybyte = NULL; if ((rv = apr_queue_pop(g_notifyqueue, (void **)¬ifybyte)) != APR_SUCCESS) { apr_thread_mutex_unlock(g_reqlock); ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, "mod_fcgid: can't pop notify message"); return rv; } } /* Release the lock now */ if ((rv = apr_thread_mutex_unlock(g_reqlock)) != APR_SUCCESS) { /* It's a fatal error */ ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, "mod_fcgid: can't release request lock"); exit(1); } } return APR_SUCCESS; }
void * LLThreadSafeQueueImplementation::popBack(void) { void * element; apr_status_t status = apr_queue_pop(mQueue, &element); if(status == APR_EINTR) { throw LLThreadSafeQueueInterrupt(); } else if(status != APR_SUCCESS) { throw LLThreadSafeQueueError("pop failed"); } else { return element; } }
/* pop teno message from the queue. */ T_VOID* teno_mq_pop_queue ( T_MSG_QUEUE *ps_queue ) { T_VOID *p_data = T_NULL; T_APR_RET ret = 0; ret = apr_queue_pop(ps_queue->ps_queue, &p_data); if (!ret) { return p_data; } return T_NULL; /* service_proc() free p_data */ }
int pop_queue(struct seed_cmd *cmd) { #ifdef USE_FORK struct msg_cmd mcmd; if (msgrcv(msqid, &mcmd, sizeof(struct seed_cmd), 1, 0) == -1) { printf("failed to pop tile\n"); return APR_EGENERAL; } *cmd = mcmd.cmd; return APR_SUCCESS; #else int ret; struct seed_cmd *pcmd; ret = apr_queue_pop(work_queue, (void**)&pcmd); if(ret == APR_SUCCESS) { *cmd = *pcmd; free(pcmd); } return ret; #endif }
static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data) { long sleeprate; apr_queue_t *q = (apr_queue_t*)data; apr_status_t rv; int val; void *v; char current_thread_str[30]; apr_os_thread_t current_thread = apr_os_thread_current(); apr_snprintf(current_thread_str, sizeof current_thread_str, "%pT", ¤t_thread); sleeprate = 1000000/consumer_activity; apr_sleep( (rand() % 4 ) * 1000000 ); /* sleep random seconds */ while (1) { do { rv = apr_queue_pop(q, &v); if (rv == APR_EINTR) { fprintf(stderr, "%s\tconsumer intr\n", current_thread_str); } } while (rv == APR_EINTR) ; if (rv != APR_SUCCESS) { if (rv == APR_EOF) { fprintf(stderr, "%s\tconsumer:queue terminated APR_EOF\n", current_thread_str); rv=APR_SUCCESS; } else fprintf(stderr, "%s\tconsumer thread exit rv %d\n", current_thread_str, rv); apr_thread_exit(thd, rv); return NULL; } val = *(int*)v; if (verbose) fprintf(stderr, "%s\tpop %d\n", current_thread_str, val); apr_sleep( sleeprate ); /* sleep this long to acheive our rate */ } /* not reached */ return NULL; }
apr_status_t procmgr_fetch_cmd(fcgid_command * command, server_rec * main_server) { fcgid_command *peakcmd = NULL; if (!g_must_exit && g_msgqueue) { if (apr_queue_pop(g_msgqueue, (void **)&peakcmd) == APR_SUCCESS) { if (!peakcmd) return APR_TIMEUP; /* This a wake up message */ else { /* Copy the command, and then free the memory */ memcpy(command, peakcmd, sizeof(*peakcmd)); free(peakcmd); return APR_SUCCESS; } } } return APR_TIMEUP; }
static apt_bool_t apt_consumer_task_run(apt_task_t *task) { apr_status_t rv; void *msg; apt_bool_t running = TRUE; apt_consumer_task_t *consumer_task; consumer_task = apt_task_object_get(task); if(!consumer_task) { return FALSE; } while(running) { rv = apr_queue_pop(consumer_task->msg_queue,&msg); if(rv == APR_SUCCESS) { if(msg) { apt_task_msg_t *task_msg = msg; if(apt_task_msg_process(consumer_task->base,task_msg) == FALSE) { running = FALSE; } } } } return TRUE; }
SWITCH_DECLARE(switch_status_t) switch_queue_pop(switch_queue_t *queue, void **data) { return apr_queue_pop(queue, data); }
void * mysql_connector(void *args) { char *neaf; char *uid; char *birth_year; char *birth_month; char *birth_day; char *constellation; char *blood_types; char *sex; char *home_nation; char *home_pro; char *home_city; char *now_nation; char *now_pro; char *now_city; char *header; char *edu; char *school; char *department; char *class_; char *year; char *begin_year; char *begin_month; char *end_year; char *end_month; char *company; char *post; char *update_proto; char *delete_proto; char *insert_proto; int mysql_query_rc; int flag; void *pop_string; char *raw_string; char *tmp; unsigned long long affect; int raw_len; MYSQL mysql; my_bool reconnect = 1; mysql_init(&mysql); mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect); /* Connect to mysql server */ if(!mysql_real_connect( &mysql, server.mysqlIP, server.mysqlUser, server.mysqlPasswd, server.db, MYSQL_PORT, NULL, 0)) { log4c_category_log( log_handler, LOG4C_PRIORITY_ERROR, "MySQL_conn: MySQL connecotr error"); return ((void *)-1); } log4c_category_log( log_handler, LOG4C_PRIORITY_INFO, "MySQL_conn: MySQL connecotr start up"); /* Turn on auto commint */ mysql_autocommit(&mysql, 1); if(!mysql_ping(&mysql)) { mysql_query(&mysql, "SET NAMES UTF8"); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn: Mysql Server set utf-8"); log4c_category_log( log_handler, LOG4C_PRIORITY_INFO, "MySQL_conn: %s", "SET NAMES UTF8"); } else { log4c_category_log( log_handler, LOG4C_PRIORITY_FATAL, "MySQL_conn: lost connect to Mysql Server"); return ((void *)0); } int malloc_size; while(1) { update_proto = NULL; delete_proto = NULL; insert_proto = NULL; apr_queue_pop(queue, &pop_string); raw_string = pop_string; raw_len = strlen(raw_string); if(NULL == raw_string) { log4c_category_log( log_handler, LOG4C_PRIORITY_NOTICE, "MySQL_conn: POP from queue is NULL"); continue; } log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn: POP from queue: %s", raw_string); /* Get flag */ tmp = strsep(&raw_string, ":"); flag= atoi(tmp); /* Keep elegant */ /* If Basic Info */ if(1 == flag) { log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: post basic"); /* Split raw string */ uid = strsep(&raw_string, ","); birth_year = strsep(&raw_string, ","); birth_month = strsep(&raw_string, ","); birth_day = strsep(&raw_string, ","); constellation = strsep(&raw_string, ","); blood_types = strsep(&raw_string, ","); sex = strsep(&raw_string, ","); home_nation = strsep(&raw_string, ","); home_pro = strsep(&raw_string, ","); home_city = strsep(&raw_string, ","); now_nation = strsep(&raw_string, ","); now_pro = strsep(&raw_string, ","); now_city = strsep(&raw_string, ","); /* Magic number 350 is SQL proto length */ malloc_size = raw_len + 350; update_proto = xmalloc(malloc_size); snprintf(update_proto, (malloc_size), "update base_user_info set\ birth_year=%s, birth_month=%s, birth_day=%s,\ constellation=%s, blood_types=%s, sex=%s,\ home_nation='%s', home_pro='%s', home_city='%s',\ now_nation='%s', now_pro='%s', now_city='%s'\ where uid=%s", birth_year, birth_month, birth_day, constellation, blood_types, sex, home_nation, home_pro, home_city, now_nation, now_pro, now_city, uid); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: updata proto: %s", update_proto); /* query mysql */ if(!mysql_ping(&mysql)) { mysql_query(&mysql, "SET NAMES UTF8"); mysql_query_rc = mysql_query(&mysql, update_proto); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: Mysql Server update return: %d", mysql_query_rc); log4c_category_log( log_handler, LOG4C_PRIORITY_INFO, "MySQL_conn_basic: %s", update_proto); } else { /* Dump to file */ log4c_category_log( log_handler, LOG4C_PRIORITY_ERROR, "MySQL_conn_basic: lost connect to Mysql Server"); fprintf(server.dump_file_handler, "%s\n", update_proto); fflush(server.dump_file_handler); } if(mysql_field_count(&mysql) == 0) { affect = (unsigned long long )mysql_affected_rows(&mysql); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: update affect:%d", affect); } /* new user */ if(0 == affect) { malloc_size = raw_len + 358; insert_proto = xmalloc(malloc_size); snprintf(insert_proto, malloc_size, "insert into base_user_info set\ birth_year=%s, birth_month=%s, birth_day=%s,\ constellation=%s, blood_types=%s, sex=%s,\ home_nation='%s', home_pro='%s', home_city='%s',\ now_nation='%s', now_pro='%s', now_city='%s',\ uid=%s", birth_year, birth_month, birth_day, constellation, blood_types, sex, home_nation, home_pro, home_city, now_nation, now_pro, now_city, uid); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: insert proto: %s", insert_proto); if(!mysql_ping(&mysql)) { mysql_query(&mysql, "SET NAMES UTF8"); mysql_query_rc = mysql_query(&mysql, insert_proto); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: Mysql Server insert return: %d", mysql_query_rc); log4c_category_log( log_handler, LOG4C_PRIORITY_INFO, "MySQL_conn_basic: %s", insert_proto); } else { log4c_category_log( log_handler, LOG4C_PRIORITY_ERROR, "MySQL_conn_basic: lost connect to Mysql Server"); /* Dump to file */ fprintf(server.dump_file_handler, "%s\n", insert_proto); fflush(server.dump_file_handler); } affect = (unsigned long long )mysql_affected_rows(&mysql); log4c_category_log( log_handler, LOG4C_PRIORITY_TRACE, "MySQL_conn_basic: insert affect:%d", affect); } xfree(insert_proto); xfree(update_proto); }
static apt_bool_t apt_consumer_task_run(apt_task_t *task) { apr_status_t rv; void *msg; apt_bool_t *running; apt_consumer_task_t *consumer_task; #if APR_HAS_QUEUE_TIMEOUT apr_interval_time_t timeout; apr_uint32_t queue_timeout; apr_time_t time_now, time_last = 0; #endif const char *task_name; consumer_task = apt_task_object_get(task); if(!consumer_task) { return FALSE; } task_name = apt_task_name_get(consumer_task->base), running = apt_task_running_flag_get(task); if(!running) { return FALSE; } while(*running) { #if APR_HAS_QUEUE_TIMEOUT if(apt_timer_queue_timeout_get(consumer_task->timer_queue,&queue_timeout) == TRUE) { timeout = (apr_interval_time_t)queue_timeout * 1000; time_last = apr_time_now(); apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Wait for Messages [%s] timeout [%u]", task_name, queue_timeout); rv = apr_queue_timedpop(consumer_task->msg_queue,timeout,&msg); } else { timeout = -1; apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Wait for Messages [%s]",task_name); rv = apr_queue_pop(consumer_task->msg_queue,&msg); } #else apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Wait for Messages [%s]",task_name); rv = apr_queue_pop(consumer_task->msg_queue,&msg); #endif if(rv == APR_SUCCESS) { if(msg) { apt_task_msg_t *task_msg = msg; apt_task_msg_process(consumer_task->base,task_msg); } } else if(rv != APR_TIMEUP) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Pop Message [%s] status: %d",task_name,rv); } #if APR_HAS_QUEUE_TIMEOUT if(timeout != -1) { time_now = apr_time_now(); if(time_now > time_last) { apt_timer_queue_advance(consumer_task->timer_queue,(apr_uint32_t)((time_now - time_last)/1000)); } } #endif } return TRUE; }