Пример #1
0
static void* APR_THREAD_FUNC test_server_run(apr_thread_t *thd, void *data) {

	server_thread_data * server_data = (server_thread_data*)data;
	
	apr_status_t rv;
    apr_pool_t *mp;
    apr_socket_t *s;/* listening socket */

    apr_pool_create(&mp, NULL);

    rv = do_listen(&s, mp, server_data->port);
    if (rv != APR_SUCCESS) {
        char errbuf[256];
	    apr_strerror(rv, errbuf, sizeof(errbuf));
	    printf("test server error listening: %d, %s\n", rv, errbuf);
		apr_pool_destroy(mp);
		apr_thread_exit(thd, rv);
		return;
    }
	server_data->handle->test_server_running = 1;
    while (server_data->handle->test_server_running == 1) {
        apr_socket_t *ns;/* accepted socket */

		//printf("A");
        rv = apr_socket_accept(&ns, s, mp);
		if(rv == 11) {
			//printf(".");
		} else {
			//printf("B");
		    if (rv != APR_SUCCESS) {
		        char errbuf[256];
				apr_strerror(rv, errbuf, sizeof(errbuf));
				printf("test server error accepting: %d, %s\n", rv, errbuf);
				apr_pool_destroy(mp);
				apr_thread_exit(thd, rv);
				return;
		    }
		    /* it is a good idea to specify socket options for the newly accepted socket explicitly */
		    apr_socket_opt_set(ns, APR_SO_NONBLOCK, 0);
		    apr_socket_timeout_set(ns, 5000);

			rv = server_data->request_process_callback(ns, mp, server_data);
			apr_socket_close(ns);
		    if (rv != APR_SUCCESS) {
		        char errbuf[256];
				apr_strerror(rv, errbuf, sizeof(errbuf));
				printf("test server error processing: %d, %s\n", rv, errbuf);
				apr_pool_destroy(mp);
				apr_thread_exit(thd, rv);
				return;
		    }
		}
    }
	//printf("apr_pool_destroy\n");
    apr_pool_destroy(mp);
	//printf("apr_thread_exit\n");
    apr_thread_exit(thd, APR_SUCCESS);
	//printf("return\n");
	return NULL;
}
Пример #2
0
static void * APR_THREAD_FUNC resource_consuming_thread(apr_thread_t *thd,
                                                        void *data)
{
    apr_status_t rv;
    my_thread_info_t *thread_info = data;
    apr_reslist_t *rl = thread_info->reslist;
    int i;

    for (i = 0; i < CONSUMER_ITERATIONS; i++) {
        my_resource_t *res;
        rv = apr_reslist_acquire(rl, (void**)&res);
        if (rv != APR_SUCCESS) {
            fprintf(stderr, "Failed to retrieve resource from reslist\n");
            apr_thread_exit(thd, rv);
            return NULL;
        }
        printf("  [tid:%d,iter:%d] using resource id:%d\n", thread_info->tid,
               i, res->id);
        apr_sleep(thread_info->work_delay_sleep);
        rv = apr_reslist_release(rl, res);
        if (rv != APR_SUCCESS) {
            fprintf(stderr, "Failed to return resource to reslist\n");
            apr_thread_exit(thd, rv);
            return NULL;
        }
    }

    return APR_SUCCESS;
}
Пример #3
0
static void *APR_THREAD_FUNC shellTui_runnable(apr_thread_t *thd, void *data) {
	shell_tui_activator_pt act = (shell_tui_activator_pt) data;

	char in[256];
	bool needPrompt = true;
	while (act->running) {
		char * dline = NULL;
		char * line = NULL;
		if (needPrompt) {
			printf("-> ");
			needPrompt = false;
		}
		fgets(in, 256, stdin);
		needPrompt = true;
		dline = strdup(in);
		line = utils_stringTrim(dline);
		if (strlen(line) == 0) {
		    free(dline);
			continue;
		}
		if (act->shell == NULL) {
		    free(dline);
			continue;
		}
		act->shell->executeCommand(act->shell->shell, line, shellTui_write, shellTui_write);
		free(dline);
	}
	apr_thread_exit(thd, APR_SUCCESS);
	return NULL;
}
Пример #4
0
void *monitor_thread(apr_thread_t *th, void *data)
{
    tbx_ns_monitor_t *nm = (tbx_ns_monitor_t *)data;
    tbx_ns_t *ns = nm->ns;
    int i;

    log_printf(15, "monitor_thread: Monitoring port %d\n", nm->port);

    apr_thread_mutex_lock(nm->lock);
    while (nm->shutdown_request == 0) {
        apr_thread_mutex_unlock(nm->lock);

        i = ns->connection_request(ns->sock, 1);

        if (i == 1) {  //** Got a request
            log_printf(15, "monitor_thread: port=%d ns=%d Got a connection request time=" TT "\n", nm->port, tbx_ns_getid(ns), apr_time_now());

            //** Mark that I have a connection pending
            apr_thread_mutex_lock(nm->lock);
            nm->is_pending = 1;
            apr_thread_mutex_unlock(nm->lock);

            //** Wake up the calling thread
            apr_thread_mutex_lock(nm->trigger_lock);
            (*(nm->trigger_count))++;
            apr_thread_cond_signal(nm->trigger_cond);
            apr_thread_mutex_unlock(nm->trigger_lock);

            log_printf(15, "monitor_thread: port=%d ns=%d waiting for accept\n", nm->port, tbx_ns_getid(ns));

            //** Sleep until my connection is accepted
            apr_thread_mutex_lock(nm->lock);
            while ((nm->is_pending == 1) && (nm->shutdown_request == 0)) {
                apr_thread_cond_wait(nm->cond, nm->lock);
                log_printf(15, "monitor_thread: port=%d ns=%d Cond triggered=" TT " trigger_count=%d\n", nm->port, tbx_ns_getid(ns), apr_time_now(), *(nm->trigger_count));
            }
            apr_thread_mutex_unlock(nm->lock);
            log_printf(15, "monitor_thread: port=%d ns=%d Connection accepted time=" TT "\n", nm->port, tbx_ns_getid(ns), apr_time_now());

            //** Update pending count
//         apr_thread_mutex_lock(nm->trigger_lock);
//         *(nm->trigger_count)--;
//         apr_thread_mutex_unlock(nm->trigger_lock);
        }

        apr_thread_mutex_lock(nm->lock);
    }

    apr_thread_mutex_unlock(nm->lock);

    //** Lastly shutdown my socket
    tbx_ns_close(ns);

    log_printf(15, "monitor_thread: Closing port %d\n", nm->port);

    apr_thread_exit(th, 0);

    return(NULL);
}
Пример #5
0
void * APR_THREAD_FUNC lfd_worker_protocol_main(apr_thread_t * thd, void* param)
{
	void * APR_THREAD_FUNC ret = lfd_worker_protocol_main_impl(thd, param);
	//wrapper_apr_thread_exit(thd, 0);
        //TODO: check if we should or not call wrapper_apr_thread_exit()
        apr_thread_exit(thd, 0);
	return ret;
}
Пример #6
0
static void* APR_THREAD_FUNC _thread_get_tile(apr_thread_t *thread, void *data)
{
  _thread_tile* t = (_thread_tile*)data;
  mapcache_tileset_tile_get(t->ctx, t->tile);
#if !USE_THREADPOOL
  apr_thread_exit(thread, APR_SUCCESS);
#endif
  return NULL;
}
static void * APR_THREAD_FUNC process_security_thread_handler(apr_thread_t *thread, void *data)
{
    request_rec *r = (request_rec *) data;
    int result;

    thread_on = 1;

    if (process_security_set_cap(r) < 0)
        apr_thread_exit(thread, HTTP_INTERNAL_SERVER_ERROR);

    result = ap_run_handler(r);

    if (result == DECLINED)
        result = HTTP_INTERNAL_SERVER_ERROR;

    apr_thread_exit(thread, result);

    return NULL;
}
Пример #8
0
static void *APR_THREAD_FUNC thread_routine(apr_thread_t *thd, void *data)
{
    toolbox_t *box = data;

    box->func(box);

    apr_thread_exit(thd, 0);

    return NULL;
}
Пример #9
0
static void *APR_THREAD_FUNC thread_func_busyloop(apr_thread_t *thd, void *data)
{
    tbox_t *tbox = data;

    tbox->func(tbox);

    apr_thread_exit(thd, 0);

    return NULL;
}
Пример #10
0
void * APR_THREAD_FUNC thread_func_none(apr_thread_t *thd, void *data)
{
    int i;

    for (i = 0; i < NUM_ITERATIONS ; i++) {
        z++;
    }
    apr_thread_exit(thd, exit_ret_val);
    return NULL;
}
Пример #11
0
void * APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data)
{
    int i;

    for (i = 0; i < NUM_ITERATIONS; i++) {
        apr_thread_mutex_lock(thread_lock);
        x++;
        apr_thread_mutex_unlock(thread_lock);
    }
    apr_thread_exit(thd, exit_ret_val);
    return NULL;
} 
Пример #12
0
static void * APR_THREAD_FUNC eachThread(apr_thread_t *id, void *p)
{
    test_mode_e test_mode = (test_mode_e)p;

    lock_grab(test_mode);
    ++counter;
    assert(apr_thread_mutex_lock(thread_mutex) == APR_SUCCESS);
    assert(apr_thread_mutex_unlock(thread_mutex) == APR_SUCCESS);
    lock_release(test_mode);
    apr_thread_exit(id, 0);
    return NULL;
}
Пример #13
0
static void *APR_THREAD_FUNC trk_send(apr_thread_t *thd, void *handle) {
    echo_client_pt client = (echo_client_pt) handle;
    while (client->running) {
        echo_service_pt service = (echo_service_pt) serviceTracker_getService(client->tracker);
        if (service != NULL) {
            service->echo(service->server, "hi");
        }
        apr_sleep(1000000);
    }
    apr_thread_exit(thd, APR_SUCCESS);
    return NULL;
}
Пример #14
0
static void* APR_THREAD_FUNC seed_thread(apr_thread_t *thread, void *data)
#endif
{
  mapcache_tile *tile;
   mapcache_context seed_ctx = ctx;
   seed_ctx.log = seed_log;
   apr_pool_create(&seed_ctx.pool,ctx.pool);
   tile = mapcache_tileset_tile_create(ctx.pool, tileset, grid_link);
   tile->dimensions = dimensions;
   while(1) {
     struct seed_cmd cmd;
      apr_status_t ret;
      apr_pool_clear(seed_ctx.pool);
      
      ret = pop_queue(&cmd);
      if(ret != APR_SUCCESS || cmd.command == MAPCACHE_CMD_STOP) break;
      tile->x = cmd.x;
      tile->y = cmd.y;
      tile->z = cmd.z;
      if(cmd.command == MAPCACHE_CMD_SEED) {
         /* aquire a lock on the metatile ?*/
         mapcache_metatile *mt = mapcache_tileset_metatile_get(&seed_ctx, tile);
         int isLocked = mapcache_lock_or_wait_for_resource(&seed_ctx, mapcache_tileset_metatile_resource_key(&seed_ctx,mt));
         if(isLocked == MAPCACHE_TRUE) {
            /* this will query the source to create the tiles, and save them to the cache */
            mapcache_tileset_render_metatile(&seed_ctx, mt);
            mapcache_unlock_resource(&seed_ctx, mapcache_tileset_metatile_resource_key(&seed_ctx,mt));
         }
      } else if (cmd.command == MAPCACHE_CMD_TRANSFER) {
         int i;
         mapcache_metatile *mt = mapcache_tileset_metatile_get(&seed_ctx, tile);
         for (i = 0; i < mt->ntiles; i++) {
            mapcache_tile *subtile = &mt->tiles[i];
            mapcache_tileset_tile_get(&seed_ctx, subtile);
            subtile->tileset = tileset_transfer;
            tileset_transfer->cache->tile_set(&seed_ctx, subtile);
         }
      }
      else { //CMD_DELETE
         mapcache_tileset_tile_delete(&seed_ctx,tile,MAPCACHE_TRUE);
      }
      if(seed_ctx.get_error(&seed_ctx)) {
         error_detected++;
         ctx.log(&ctx,MAPCACHE_INFO,seed_ctx.get_error_message(&seed_ctx));
      }
   }
#ifdef USE_FORK
   return 0;
#else
   apr_thread_exit(thread,MAPCACHE_SUCCESS);
   return NULL;
#endif
}
Пример #15
0
void * _lt_http_server_run( apr_thread_t * thread, void * userarg )
{
    /* convert thread routine argument to our server structure */
    lt_http_server_t * server = (lt_http_server_t*) userarg;

    /* run the server */
    lt_http_server_run( server );

    apr_thread_exit( thread, 0 );

    return NULL;
}
Пример #16
0
void* APR_THREAD_FUNC poll_worker_run(
        apr_thread_t *th,
        void *arg) {
    poll_worker_t *worker = arg;
    
    LOG_TRACE("poll worker loop starts");
    
    while (!worker->fg_exit) {
        int num = 0;
        const apr_pollfd_t *ret_pfd;
	// wait 100 * 1000 micro seconds.
        apr_status_t status = apr_pollset_poll(worker->ps, 100 * 1000, 
					       &num, &ret_pfd);

        if (status == APR_SUCCESS) {
   	    LOG_TRACE("poll worker loop: found something");
            SAFE_ASSERT(num > 0);
            for (int i = 0; i < num; i++) {
                int rtne = ret_pfd[i].rtnevents;
                SAFE_ASSERT(rtne & (APR_POLLIN | APR_POLLOUT));
                poll_job_t *job = ret_pfd[i].client_data;
                SAFE_ASSERT(job != NULL);
                if (rtne & APR_POLLIN) {
	    	    LOG_TRACE("poll worker loop: found something to read");
		    SAFE_ASSERT(job->do_read != NULL);
		    (*job->do_read)(job->holder); 
                }
                if (rtne & APR_POLLOUT) {
	    	    LOG_TRACE("poll worker loop: found something to write");
		    SAFE_ASSERT(job->do_write);
                    (*job->do_write)(job->holder);
                }
            }
        } else if (status == APR_EINTR) {
            // the signal we get when process exit, 
            // wakeup, or add in and write.
            LOG_WARN("the receiver epoll exits?");
            continue;
        } else if (status == APR_TIMEUP) {
	    //LOG_DEBUG("poll time out");
            continue;
        } else {
            LOG_ERROR("unknown poll error. %s", 
                    apr_strerror(status, calloc(1, 100), 100));
            SAFE_ASSERT(0);
        }
    }
    
    LOG_TRACE("poll worker loop stops");
    SAFE_ASSERT(apr_thread_exit(th, APR_SUCCESS) == APR_SUCCESS);
    return NULL;
}
Пример #17
0
void * APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data)
{
    int i;

    for (i = 0; i < NUM_ITERATIONS ; i++) {
        apr_atomic_inc32(&y);
        apr_atomic_add32(&y, 2);
        apr_atomic_dec32(&y);
        apr_atomic_dec32(&y);
    }
    apr_thread_exit(thd, exit_ret_val);
    return NULL;
}
Пример #18
0
void LLThread::shutdown()
{
	// Warning!  If you somehow call the thread destructor from itself,
	// the thread will die in an unclean fashion!
	if (mAPRThreadp)
	{
		if (!isStopped())
		{
			// The thread isn't already stopped
			// First, set the flag that indicates that we're ready to die
			setQuitting();

			llinfos << "LLThread::~LLThread() Killing thread " << mName << " Status: " << mStatus << llendl;
			// Now wait a bit for the thread to exit
			// It's unclear whether I should even bother doing this - this destructor
			// should netver get called unless we're already stopped, really...
			S32 counter = 0;
			const S32 MAX_WAIT = 600;
			while (counter < MAX_WAIT)
			{
				if (isStopped())
				{
					break;
				}
				// Sleep for a tenth of a second
				ms_sleep(100);
				yield();
				counter++;
			}
		}

		if (!isStopped())
		{
			// This thread just wouldn't stop, even though we gave it time
			llwarns << "LLThread::~LLThread() exiting thread before clean exit!" << llendl;
			// Put a stake in its heart.
			apr_thread_exit(mAPRThreadp, -1);
			return;
		}
		mAPRThreadp = NULL;
	}

	delete mRunCondition;
	mRunCondition = 0;
	
	if (mIsLocalPool && mAPRPoolp)
	{
		apr_pool_destroy(mAPRPoolp);
		mAPRPoolp = 0;
	}
}
Пример #19
0
static void * APR_THREAD_FUNC thread_func1(apr_thread_t *thd, void *data)
{
    int i;

    apr_thread_once(control, init_func);

    for (i = 0; i < 10000; i++) {
        apr_thread_mutex_lock(thread_lock);
        x++;
        apr_thread_mutex_unlock(thread_lock);
    }
    apr_thread_exit(thd, exit_ret_val);
    return NULL;
}
Пример #20
0
static void * APR_THREAD_FUNC
selinux_worker_handler(apr_thread_t *thread, void *data)
{
    request_rec *r = (request_rec *) data;
    int result;

    /* marks as the current context is worker thread */
    am_worker = 1;

    /* set security context */
    if (selinux_set_domain(r) < 0)
        apr_thread_exit(thread, HTTP_INTERNAL_SERVER_ERROR);

    /* invoke content handler */
    result = ap_run_handler(r);

    if (result == DECLINED)
        result = HTTP_INTERNAL_SERVER_ERROR;

    apr_thread_exit(thread, result);

    return NULL;
}
Пример #21
0
static void *APR_THREAD_FUNC chkpnt_thread(apr_thread_t *thd,void *dummy)
{
	int ret;
        DB_ENV *db_env = (DB_ENV *)dummy;
	while(!shutdown_){
		if((db_env) && (ret = db_env->txn_checkpoint(db_env,CHKPNT_DSIZE,
						CHKPNT_DTIME,0)!=0)){
				return NULL;
		}

		apr_sleep(apr_time_from_sec(CHKPNT_CYCLE));
	}
	apr_thread_exit(thd,APR_SUCCESS);
	return NULL;
}
Пример #22
0
static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data)
{
    int i=0;
    long sleeprate;
    apr_queue_t *q = (apr_queue_t*)data;
    apr_status_t rv;
    int *val;
    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", &current_thread);

    sleeprate = 1000000/producer_activity;
    apr_sleep( (rand() % 4 ) * 1000000 ); /* sleep random seconds */
        
    while(1) {
        val = apr_palloc(context, sizeof(int));
        *val=i;
        if (verbose)
            fprintf(stderr,  "%s\tpush %d\n", current_thread_str, *val);
        do {
            rv = apr_queue_push(q, val);
            if (rv == APR_EINTR) 
                fprintf(stderr, "%s\tproducer intr\n", current_thread_str);
        } while (rv == APR_EINTR);

        if (rv != APR_SUCCESS) {
            if (rv == APR_EOF) {
                fprintf(stderr, "%s\tproducer: queue terminated APR_EOF\n", current_thread_str);
                rv = APR_SUCCESS;
            }
            else
                fprintf(stderr, "%s\tproducer thread exit rv %d\n", current_thread_str, rv);
            apr_thread_exit(thd, rv);
            return NULL;
        }
        i++;
        apr_sleep( sleeprate ); /* sleep this long to acheive our rate */
    }
   /* not reached */
    return NULL;
} 
Пример #23
0
void* APR_THREAD_FUNC client_thread(apr_thread_t *th, void *v) {
    client_t *client = NULL;
    client_create(&client, NULL);
    strcpy(client->comm->ip, addr_);
    client->comm->port = port_;
    client_reg(client, ADD, add_cb);
    tm_begin_ = apr_time_now();
    client_connect(client);

    cli_ = client;
//    printf("client connected.\n");

    n_issued_ += max_outst_;
    for (int i = 0; i < max_outst_; i++) {
	call_add(cli_);
    }

    apr_thread_exit(th, APR_SUCCESS);
    return NULL;
}
Пример #24
0
static void *APR_THREAD_FUNC eventPublisherSendEventThread(apr_thread_t *thd, void *handle) {
    event_publisher_pt *client = (event_publisher_pt *) handle;

    while ((*client)->running && (*client)->eventAdminAdded) {
        apr_sleep(1000000); // 1 sec.

        event_admin_service_pt *event_admin_service = &(*client)->event_admin_service;
        event_admin_pt event_admin = (*event_admin_service)->eventAdmin;
        if (event_admin_service != NULL) {
            event_pt event;
            properties_pt props = properties_create();
            properties_set(props, "This is a key", "this is a value");
            (*event_admin_service)->createEvent(event_admin, "log/error/eventpublishers/event", props, &event);
            (*event_admin_service)->postEvent(event_admin, event);
            (*event_admin_service)->sendEvent(event_admin, event);
        }
    }
	apr_thread_exit(thd, APR_SUCCESS);
	return NULL;
}
Пример #25
0
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", &current_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;
} 
Пример #26
0
void* APR_THREAD_FUNC client_thread(apr_thread_t *th, void *v) {
    poll_mgr_t *mgr = NULL;
    client_t *client = NULL;
    poll_mgr_create(&mgr, 1);  
    client_create(&client, mgr);
    strcpy(client->comm->ip, addr_);
    client->comm->port = port_;
    client_reg(client, ADD, add_cb);
    tm_begin_ = apr_time_now();
    client_connect(client);

    int k = (intptr_t) v;
    mgrs_[k] = mgr;
    clis_[k] = client;
//    printf("client connected.\n");

    n_issues_[k] += max_outst_;
    for (int i = 0; i < max_outst_; i++) {
	call_add(clis_[k], k);
    }

    apr_thread_exit(th, APR_SUCCESS);
    return NULL;
}
Пример #27
0
/*
 * The worker thread function. Take a task from the queue and perform it if
 * there is any. Otherwise, put itself into the idle thread list and waiting
 * for signal to wake up.
 * The thread terminate directly by detach and exit when it is asked to stop
 * after finishing a task. Otherwise, the thread should be in idle thread list
 * and should be joined.
 */
static void *APR_THREAD_FUNC thread_pool_func(apr_thread_t * t, void *param)
{
    apr_thread_pool_t *me = param;
    apr_thread_pool_task_t *task = NULL;
    apr_interval_time_t wait;
    struct apr_thread_list_elt *elt;

    apr_thread_mutex_lock(me->lock);

    --me->spawning_cnt;

    elt = elt_new(me, t);
    if (!elt) {
        apr_thread_mutex_unlock(me->lock);
        apr_thread_exit(t, APR_ENOMEM);
    }

    while (!me->terminated && elt->state != TH_STOP) {
        /* Test if not new element, it is awakened from idle */
        if (APR_RING_NEXT(elt, link) != elt) {
            --me->idle_cnt;
            APR_RING_REMOVE(elt, link);
        }

        APR_RING_INSERT_TAIL(me->busy_thds, elt, apr_thread_list_elt, link);
        task = pop_task(me);
        while (NULL != task && !me->terminated) {
            ++me->tasks_run;
            elt->current_owner = task->owner;
            apr_thread_mutex_unlock(me->lock);
            apr_thread_data_set(task, "apr_thread_pool_task", NULL, t);
            task->func(t, task->param);
            apr_thread_mutex_lock(me->lock);
            APR_RING_INSERT_TAIL(me->recycled_tasks, task,
                                 apr_thread_pool_task, link);
            elt->current_owner = NULL;
            if (TH_STOP == elt->state) {
                break;
            }
            task = pop_task(me);
        }
        assert(NULL == elt->current_owner);
        if (TH_STOP != elt->state)
            APR_RING_REMOVE(elt, link);

        /* Test if a busy thread been asked to stop, which is not joinable */
        if ((me->idle_cnt >= me->idle_max
             && !(me->scheduled_task_cnt && 0 >= me->idle_max)
             && !me->idle_wait)
            || me->terminated || elt->state != TH_RUN) {
            --me->thd_cnt;
            if ((TH_PROBATION == elt->state) && me->idle_wait)
                ++me->thd_timed_out;
            APR_RING_INSERT_TAIL(me->recycled_thds, elt,
                                 apr_thread_list_elt, link);
            apr_thread_mutex_unlock(me->lock);
            apr_thread_detach(t);
            apr_thread_exit(t, APR_SUCCESS);
            return NULL;        /* should not be here, safe net */
        }

        /* busy thread become idle */
        ++me->idle_cnt;
        APR_RING_INSERT_TAIL(me->idle_thds, elt, apr_thread_list_elt, link);

        /* 
         * If there is a scheduled task, always scheduled to perform that task.
         * Since there is no guarantee that current idle threads are scheduled
         * for next scheduled task.
         */
        if (me->scheduled_task_cnt)
            wait = waiting_time(me);
        else if (me->idle_cnt > me->idle_max) {
            wait = me->idle_wait;
            elt->state = TH_PROBATION;
        }
        else
            wait = -1;

        if (wait >= 0) {
            apr_thread_cond_timedwait(me->cond, me->lock, wait);
        }
        else {
            apr_thread_cond_wait(me->cond, me->lock);
        }
    }

    /* idle thread been asked to stop, will be joined */
    --me->thd_cnt;
    apr_thread_mutex_unlock(me->lock);
    apr_thread_exit(t, APR_SUCCESS);
    return NULL;                /* should not be here, safe net */
}
Пример #28
0
static void * APR_THREAD_FUNC deploymentAdmin_poll(apr_thread_t *thd, void *deploymentAdmin) {
	deployment_admin_pt admin = deploymentAdmin;

	/*first poll send framework started audit event, note this will register the target in Apache ACE*/
	deploymentAdmin_updateAuditPool(admin, DEPLOYMENT_ADMIN_AUDIT_EVENT__FRAMEWORK_STARTED);

	while (admin->running) {
		//poll ace
		array_list_pt versions = NULL;
		deploymentAdmin_readVersions(admin, &versions);

		char *last = arrayList_get(versions, arrayList_size(versions) - 1);

		if (last != NULL) {
			if (admin->current == NULL || strcmp(last, admin->current) > 0) {
				char *request = NULL;
				if (admin->current == NULL) {
					request = apr_pstrcat(admin->pool, admin->pollUrl, "/", last, NULL);
				} else {
					// We do not yet support fix packages
					//request = apr_pstrcat(admin->pool, VERSIONS, "/", last, "?current=", admin->current, NULL);
					request = apr_pstrcat(admin->pool, admin->pollUrl, "/", last, NULL);
				}

				char inputFile[256];
				inputFile[0] = '\0';
				char *test = inputFile;
				celix_status_t status = deploymentAdmin_download(request, &test);
				if (status == CELIX_SUCCESS) {
					bundle_pt bundle = NULL;
					bundleContext_getBundle(admin->context, &bundle);
					char *entry = NULL;
					bundle_getEntry(bundle, "/", &entry);

					// Handle file
					char tmpDir[256];
					char uuidStr[128];
                    apr_uuid_t tmpUuid;
                    apr_uuid_get(&tmpUuid);
                    apr_uuid_format(uuidStr, &tmpUuid);
                    sprintf(tmpDir, "%s%s", entry, uuidStr);
					apr_dir_make(tmpDir, APR_UREAD|APR_UWRITE|APR_UEXECUTE, admin->pool);

					// TODO: update to use bundle cache DataFile instead of module entries.
					unzip_extractDeploymentPackage(test, tmpDir);
					char *manifest = apr_pstrcat(admin->pool, tmpDir, "/META-INF/MANIFEST.MF", NULL);
					manifest_pt mf = NULL;
					manifest_createFromFile(manifest, &mf);
					deployment_package_pt source = NULL;
					deploymentPackage_create(admin->pool, admin->context, mf, &source);
					char *name = NULL;
					deploymentPackage_getName(source, &name);

					char *repoDir = apr_pstrcat(admin->pool, entry, "repo", NULL);
					apr_dir_make(repoDir, APR_UREAD|APR_UWRITE|APR_UEXECUTE, admin->pool);
					char *repoCache = apr_pstrcat(admin->pool, entry, "repo/", name, NULL);
					deploymentAdmin_deleteTree(repoCache, admin->pool);
					apr_status_t stat = apr_file_rename(tmpDir, repoCache, admin->pool);
					if (stat != APR_SUCCESS) {
						printf("No success\n");
					}

					deployment_package_pt target = hashMap_get(admin->packages, name);
					if (target == NULL) {
//						target = empty package
					}

					deploymentAdmin_stopDeploymentPackageBundles(admin, target);
					deploymentAdmin_updateDeploymentPackageBundles(admin, source);
					deploymentAdmin_startDeploymentPackageCustomizerBundles(admin, source, target);
					deploymentAdmin_processDeploymentPackageResources(admin, source);
					deploymentAdmin_dropDeploymentPackageResources(admin, source, target);
					deploymentAdmin_dropDeploymentPackageBundles(admin, source, target);
					deploymentAdmin_startDeploymentPackageBundles(admin, source);

					deploymentAdmin_deleteTree(repoCache, admin->pool);
					deploymentAdmin_deleteTree(tmpDir, admin->pool);
					remove(test);
					admin->current = strdup(last);
					hashMap_put(admin->packages, name, source);
				}
			}
		}
		sleep(5);
	}

	apr_thread_exit(thd, APR_SUCCESS);
	return NULL;
}
Пример #29
0
/**
 * stop the current thread
 * @param thd The thread to stop
 * @param retval The return value to pass back to any thread that cares
 */
SWITCH_DECLARE(switch_status_t) switch_thread_exit(switch_thread_t *thd, switch_status_t retval)
{
	return apr_thread_exit((apr_thread_t *) thd, retval);
}
Пример #30
0
/*--------------------------------------------------------------------------*/
static void* APR_THREAD_FUNC wd_worker(apr_thread_t *thread, void *data)
{
    ap_watchdog_t *w = (ap_watchdog_t *)data;
    apr_status_t rv;
    int locked = 0;
    int probed = 0;
    int inited = 0;
    int mpmq_s = 0;

    w->pool = apr_thread_pool_get(thread);
    w->is_running = 1;

    apr_thread_mutex_unlock(w->startup);
    if (w->mutex) {
        while (w->is_running) {
            if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpmq_s) != APR_SUCCESS) {
                w->is_running = 0;
                break;
            }
            if (mpmq_s == AP_MPMQ_STOPPING) {
                w->is_running = 0;
                break;
            }
            rv = apr_proc_mutex_trylock(w->mutex);
            if (rv == APR_SUCCESS) {
                if (probed) {
                    /* Sleep after we were locked
                     * up to 1 second. Httpd can be
                     * in the middle of shutdown, and
                     * our child didn't yet received
                     * the shutdown signal.
                     */
                    probed = 10;
                    while (w->is_running && probed > 0) {
                        apr_sleep(AP_WD_TM_INTERVAL);
                        probed--;
                        if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpmq_s) != APR_SUCCESS) {
                            w->is_running = 0;
                            break;
                        }
                        if (mpmq_s == AP_MPMQ_STOPPING) {
                            w->is_running = 0;
                            break;
                        }
                    }
                }
                locked = 1;
                break;
            }
            probed = 1;
            apr_sleep(AP_WD_TM_SLICE);
        }
    }
    if (w->is_running) {
        watchdog_list_t *wl = w->callbacks;
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, wd_server_conf->s,
                     APLOGNO(02972) "%sWatchdog (%s) running",
                     w->singleton ? "Singleton " : "", w->name);
        apr_time_clock_hires(w->pool);
        if (wl) {
            apr_pool_t *ctx = NULL;
            apr_pool_create(&ctx, w->pool);
            while (wl && w->is_running) {
                /* Execute watchdog callback */
                wl->status = (*wl->callback_fn)(AP_WATCHDOG_STATE_STARTING,
                                                (void *)wl->data, ctx);
                wl = wl->next;
            }
            apr_pool_destroy(ctx);
        }
        else {
            ap_run_watchdog_init(wd_server_conf->s, w->name, w->pool);
            inited = 1;
        }
    }

    /* Main execution loop */
    while (w->is_running) {
        apr_pool_t *ctx = NULL;
        apr_time_t curr;
        watchdog_list_t *wl = w->callbacks;

        apr_sleep(AP_WD_TM_SLICE);
        if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpmq_s) != APR_SUCCESS) {
            w->is_running = 0;
        }
        if (mpmq_s == AP_MPMQ_STOPPING) {
            w->is_running = 0;
        }
        if (!w->is_running) {
            break;
        }
        curr = apr_time_now() - AP_WD_TM_SLICE;
        while (wl && w->is_running) {
            if (wl->status == APR_SUCCESS) {
                wl->step += (apr_time_now() - curr);
                if (wl->step >= wl->interval) {
                    if (!ctx)
                        apr_pool_create(&ctx, w->pool);
                    wl->step = 0;
                    /* Execute watchdog callback */
                    wl->status = (*wl->callback_fn)(AP_WATCHDOG_STATE_RUNNING,
                                                    (void *)wl->data, ctx);
                    if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpmq_s) != APR_SUCCESS) {
                        w->is_running = 0;
                    }
                    if (mpmq_s == AP_MPMQ_STOPPING) {
                        w->is_running = 0;
                    }
                }
            }
            wl = wl->next;
        }
        if (w->is_running && w->callbacks == NULL) {
            /* This is hook mode watchdog
             * running on WatchogInterval
             */
            w->step += (apr_time_now() - curr);
            if (w->step >= wd_interval) {
                if (!ctx)
                    apr_pool_create(&ctx, w->pool);
                w->step = 0;
                /* Run watchdog step hook */
                ap_run_watchdog_step(wd_server_conf->s, w->name, ctx);
            }
        }
        if (ctx)
            apr_pool_destroy(ctx);
        if (!w->is_running) {
            break;
        }
    }
    if (inited) {
        /* Run the watchdog exit hooks.
         * If this was singleton watchdog the init hook
         * might never been called, so skip the exit hook
         * in that case as well.
         */
        ap_run_watchdog_exit(wd_server_conf->s, w->name, w->pool);
    }
    else {
        watchdog_list_t *wl = w->callbacks;
        while (wl) {
            if (wl->status == APR_SUCCESS) {
                /* Execute watchdog callback with STOPPING state */
                (*wl->callback_fn)(AP_WATCHDOG_STATE_STOPPING,
                                   (void *)wl->data, w->pool);
            }
            wl = wl->next;
        }
    }
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, wd_server_conf->s,
                 APLOGNO(02973) "%sWatchdog (%s) stopping",
                 w->singleton ? "Singleton " : "", w->name);

    if (locked)
        apr_proc_mutex_unlock(w->mutex);
    apr_thread_exit(w->thread, APR_SUCCESS);

    return NULL;
}