Exemple #1
0
int main()
{
	apr_thread_t* push_thds[1000];
	apr_thread_t* pop_thds[1000];
	apr_threadattr_t* thd_attr;
	apr_initialize();
	apr_pool_create(&global_pool, NULL);
	apr_atomic_init(global_pool);
	frl_queue_create(&queue, global_pool, 1, FRL_LOCK_FREE);
	apr_threadattr_create(&thd_attr, global_pool);
	apr_time_t now = apr_time_now();
	for (int i = 0; i < 100; i++)
	{
		apr_thread_create(&pop_thds[i], thd_attr, threadsafe_test_pop, (void*)i, global_pool);
		apr_thread_create(&push_thds[i], thd_attr, threadsafe_test_push, (void*)i, global_pool);
	}
	apr_status_t rv;
	for (int i = 0; i < 100; i++)
	{
		printf("Stop at %d\n", i+1);
		apr_thread_join(&rv, push_thds[i]);
		apr_thread_join(&rv, pop_thds[i]);
	}
	printf("Pass with %dus.\n", apr_time_now()-now);
	apr_terminate();
}
static void test_thread_mutex(abts_case *tc, void *data)
{
    apr_thread_t *t1, *t2, *t3, *t4;
    apr_status_t s1, s2, s3, s4;

    s1 = apr_thread_mutex_create(&thread_mutex, APR_THREAD_MUTEX_DEFAULT, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s1);
    ABTS_PTR_NOTNULL(tc, thread_mutex);

    i = 0;
    x = 0;

    s1 = apr_thread_create(&t1, NULL, thread_mutex_function, NULL, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s1);
    s2 = apr_thread_create(&t2, NULL, thread_mutex_function, NULL, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s2);
    s3 = apr_thread_create(&t3, NULL, thread_mutex_function, NULL, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s3);
    s4 = apr_thread_create(&t4, NULL, thread_mutex_function, NULL, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s4);

    apr_thread_join(&s1, t1);
    apr_thread_join(&s2, t2);
    apr_thread_join(&s3, t3);
    apr_thread_join(&s4, t4);

    ABTS_INT_EQUAL(tc, MAX_ITER, x);
}
Exemple #3
0
int main(int argc,char **argv) {

    apr_initialize();
    apr_pool_t *pool;
    apr_pool_create(&pool,NULL);
    
    apr_status_t st;
    
    apr_thread_t *t1;
    apr_thread_t *t2;
    apr_thread_t *t3;
    
    apr_thread_create(&t1,NULL,thread_func,(void *)1L,pool);
    apr_thread_create(&t2,NULL,thread_func,(void *)2L,pool);
    apr_thread_create(&t3,NULL,thread_func,(void *)3L,pool);
    apr_thread_join(&st,t1);
    apr_thread_join(&st,t2); 

    apr_thread_join(&st,t3); 

    apr_pool_destroy(pool);
    apr_terminate();

    return 0;

}
static void test_thread_rwlock(abts_case *tc, void *data)
{
    apr_thread_t *t1, *t2, *t3, *t4;
    apr_status_t s1, s2, s3, s4;

    s1 = apr_thread_rwlock_create(&rwlock, p);
    if (s1 == APR_ENOTIMPL) {
        ABTS_NOT_IMPL(tc, "rwlocks not implemented");
        return;
    }
    APR_ASSERT_SUCCESS(tc, "rwlock_create", s1);
    ABTS_PTR_NOTNULL(tc, rwlock);

    i = 0;
    x = 0;

    s1 = apr_thread_create(&t1, NULL, thread_rwlock_func, NULL, p);
    APR_ASSERT_SUCCESS(tc, "create thread 1", s1);
    s2 = apr_thread_create(&t2, NULL, thread_rwlock_func, NULL, p);
    APR_ASSERT_SUCCESS(tc, "create thread 2", s2);
    s3 = apr_thread_create(&t3, NULL, thread_rwlock_func, NULL, p);
    APR_ASSERT_SUCCESS(tc, "create thread 3", s3);
    s4 = apr_thread_create(&t4, NULL, thread_rwlock_func, NULL, p);
    APR_ASSERT_SUCCESS(tc, "create thread 4", s4);

    apr_thread_join(&s1, t1);
    apr_thread_join(&s2, t2);
    apr_thread_join(&s3, t3);
    apr_thread_join(&s4, t4);

    ABTS_INT_EQUAL(tc, MAX_ITER, x);

    apr_thread_rwlock_destroy(rwlock);
}
static void test_cond(abts_case *tc, void *data)
{
    apr_thread_t *p1, *p2, *p3, *p4, *c1;
    apr_status_t s0, s1, s2, s3, s4;
    int count1, count2, count3, count4;
    int sum;
    
    APR_ASSERT_SUCCESS(tc, "create put mutex",
                       apr_thread_mutex_create(&put.mutex, 
                                               APR_THREAD_MUTEX_DEFAULT, p));
    ABTS_PTR_NOTNULL(tc, put.mutex);

    APR_ASSERT_SUCCESS(tc, "create nready mutex",
                       apr_thread_mutex_create(&nready.mutex, 
                                               APR_THREAD_MUTEX_DEFAULT, p));
    ABTS_PTR_NOTNULL(tc, nready.mutex);

    APR_ASSERT_SUCCESS(tc, "create condvar",
                       apr_thread_cond_create(&nready.cond, p));
    ABTS_PTR_NOTNULL(tc, nready.cond);

    count1 = count2 = count3 = count4 = 0;
    put.nput = put.nval = 0;
    nready.nready = 0;
    i = 0;
    x = 0;

    s0 = apr_thread_create(&p1, NULL, thread_cond_producer, &count1, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s0);
    s1 = apr_thread_create(&p2, NULL, thread_cond_producer, &count2, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s1);
    s2 = apr_thread_create(&p3, NULL, thread_cond_producer, &count3, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s2);
    s3 = apr_thread_create(&p4, NULL, thread_cond_producer, &count4, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s3);
    s4 = apr_thread_create(&c1, NULL, thread_cond_consumer, NULL, p);
    ABTS_INT_EQUAL(tc, APR_SUCCESS, s4);

    apr_thread_join(&s0, p1);
    apr_thread_join(&s1, p2);
    apr_thread_join(&s2, p3);
    apr_thread_join(&s3, p4);
    apr_thread_join(&s4, c1);

    APR_ASSERT_SUCCESS(tc, "destroy condvar", 
                       apr_thread_cond_destroy(nready.cond));

    sum = count1 + count2 + count3 + count4;
    /*
    printf("count1 = %d count2 = %d count3 = %d count4 = %d\n",
            count1, count2, count3, count4);
    */
    ABTS_INT_EQUAL(tc, MAX_COUNTER, sum);
}
Exemple #6
0
static void create_threads(CuTest *tc)
{
    apr_status_t rv;

    rv = apr_thread_create(&t1, NULL, thread_func1, NULL, p);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    rv = apr_thread_create(&t2, NULL, thread_func1, NULL, p);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    rv = apr_thread_create(&t3, NULL, thread_func1, NULL, p);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
    rv = apr_thread_create(&t4, NULL, thread_func1, NULL, p);
    CuAssertIntEquals(tc, APR_SUCCESS, rv);
}
Exemple #7
0
static void test_atomics_threaded(abts_case *tc, void *data)
{
    apr_thread_t *t1[NUM_THREADS];
    apr_thread_t *t2[NUM_THREADS];
    apr_thread_t *t3[NUM_THREADS];
    apr_status_t s1[NUM_THREADS]; 
    apr_status_t s2[NUM_THREADS];
    apr_status_t s3[NUM_THREADS];
    apr_status_t rv;
    int i;

#ifdef HAVE_PTHREAD_SETCONCURRENCY
    pthread_setconcurrency(8);
#endif

    rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
    APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);

    for (i = 0; i < NUM_THREADS; i++) {
        apr_status_t r1, r2, r3;
        r1 = apr_thread_create(&t1[i], NULL, thread_func_mutex, NULL, p);
        r2 = apr_thread_create(&t2[i], NULL, thread_func_atomic, NULL, p);
        r3 = apr_thread_create(&t3[i], NULL, thread_func_none, NULL, p);
        ABTS_ASSERT(tc, "Failed creating threads",
                 r1 == APR_SUCCESS && r2 == APR_SUCCESS && 
                 r3 == APR_SUCCESS);
    }

    for (i = 0; i < NUM_THREADS; i++) {
        apr_thread_join(&s1[i], t1[i]);
        apr_thread_join(&s2[i], t2[i]);
        apr_thread_join(&s3[i], t3[i]);
                     
        ABTS_ASSERT(tc, "Invalid return value from thread_join",
                 s1[i] == exit_ret_val && s2[i] == exit_ret_val && 
                 s3[i] == exit_ret_val);
    }

    ABTS_INT_EQUAL(tc, x, NUM_THREADS * NUM_ITERATIONS);
    ABTS_INT_EQUAL(tc, apr_atomic_read32(&y), NUM_THREADS * NUM_ITERATIONS);
    /* Comment out this test, because I have no clue what this test is
     * actually telling us.  We are checking something that may or may not
     * be true, and it isn't really testing APR at all.
    ABTS_ASSERT(tc, "We expect this to fail, because we tried to update "
                 "an integer in a non-thread-safe manner.",
             z != NUM_THREADS * NUM_ITERATIONS);
     */
}
static apr_status_t wd_startup(ap_watchdog_t *w, apr_pool_t *p)
{
    apr_status_t rc;

    /* Create thread startup mutex */
    rc = apr_thread_mutex_create(&w->startup, APR_THREAD_MUTEX_UNNESTED, p);
    if (rc != APR_SUCCESS)
        return rc;

    if (w->singleton) {
        /* Initialize singleton mutex in child */
        rc = apr_proc_mutex_child_init(&w->mutex,
                                       apr_proc_mutex_lockfile(w->mutex), p);
        if (rc != APR_SUCCESS)
            return rc;
    }

    /* This mutex fixes problems with a fast start/fast end, where the pool
     * cleanup was being invoked before the thread completely spawned.
     */
    apr_thread_mutex_lock(w->startup);
    apr_pool_pre_cleanup_register(p, w, wd_worker_cleanup);

    /* Start the newly created watchdog */
    rc = apr_thread_create(&w->thread, NULL, wd_worker, w, p);
    if (rc) {
        apr_pool_cleanup_kill(p, w, wd_worker_cleanup);
    }

    apr_thread_mutex_lock(w->startup);
    apr_thread_mutex_unlock(w->startup);
    apr_thread_mutex_destroy(w->startup);

    return rc;
}
Exemple #9
0
// Startup this module
static void module_startup()
{
   Falcon::Engine::Init();
   Falcon::memPool->stop();
   //Falcon::memPool->rampMode( RAMP_MODE_STRICT_ID );

   // create also the core; we know it's empty.
   s_core = Falcon::core_module_init();

   // tell the VM to create our request and reply object
   s_ext = Falcon::WOPI::wopi_module_init( ApacheRequest::factory , ApacheReply::factory );

   if( the_falcon_config->cacheModules )
   {
      Falcon::Engine::cacheModules( true );
   }
         
   apr_pool_create(&falconWatchdogData.threadPool, NULL);      
   apr_status_t rv = apr_thread_create( 
         &falconWatchdogData.watchdogThread, 
         NULL, 
         &watchdog, 
         &falconWatchdogData, falconWatchdogData.threadPool );
  
   fprintf( stderr, "Falcon WOPI module for Apache2 %d(%s)\n", rv,
          rv == APR_SUCCESS ? "launched watchdog" : "failed to launch watchdog");

}
Exemple #10
0
void LLThread::start()
{
	apr_thread_create(&mAPRThreadp, NULL, staticRun, (void *)this, mAPRPoolp);	

	// We won't bother joining
	apr_thread_detach(mAPRThreadp);
}
Exemple #11
0
static void broadcast_threads(abts_case *tc, void *data)
{
    toolbox_t box;
    unsigned int i;
    apr_status_t rv;
    apr_uint32_t count = 0;
    apr_thread_cond_t *cond = NULL;
    apr_thread_mutex_t *mutex = NULL;
    apr_thread_t *thread[NTHREADS];

    rv = apr_thread_cond_create(&cond, p);
    ABTS_SUCCESS(rv);
    ABTS_PTR_NOTNULL(tc, cond);

    rv = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, p);
    ABTS_SUCCESS(rv);
    ABTS_PTR_NOTNULL(tc, mutex);

    rv = apr_thread_mutex_lock(mutex);
    ABTS_SUCCESS(rv);

    box.tc = tc;
    box.data = &count;
    box.mutex = mutex;
    box.cond = cond;
    box.func = lock_and_wait;

    for (i = 0; i < NTHREADS; i++) {
        rv = apr_thread_create(&thread[i], NULL, thread_routine, &box, p);
        ABTS_SUCCESS(rv);
    }

    do {
        rv = apr_thread_mutex_unlock(mutex);
        ABTS_SUCCESS(rv);
        apr_sleep(100000);
        rv = apr_thread_mutex_lock(mutex);
        ABTS_SUCCESS(rv);
    } while (apr_atomic_read32(&count) != NTHREADS);

    rv = apr_thread_cond_broadcast(cond);
    ABTS_SUCCESS(rv);

    rv = apr_thread_mutex_unlock(mutex);
    ABTS_SUCCESS(rv);

    for (i = 0; i < NTHREADS; i++) {
        apr_status_t retval;
        rv = apr_thread_join(&retval, thread[i]);
        ABTS_SUCCESS(rv);
    }

    ABTS_INT_EQUAL(tc, 0, count);

    rv = apr_thread_cond_destroy(cond);
    ABTS_SUCCESS(rv);

    rv = apr_thread_mutex_destroy(mutex);
    ABTS_SUCCESS(rv);
}
Exemple #12
0
static void dynamic_binding(abts_case *tc, void *data)
{
    unsigned int i;
    apr_status_t rv;
    toolbox_t box[NTHREADS];
    apr_thread_t *thread[NTHREADS];
    apr_thread_mutex_t *mutex[NTHREADS];
    apr_thread_cond_t *cond = NULL;

    rv = apr_thread_cond_create(&cond, p);
    ABTS_SUCCESS(rv);
    ABTS_PTR_NOTNULL(tc, cond);

    for (i = 0; i < NTHREADS; i++) {
        rv = apr_thread_mutex_create(&mutex[i], APR_THREAD_MUTEX_DEFAULT, p);
        ABTS_SUCCESS(rv);

        rv = apr_thread_mutex_lock(mutex[i]);
        ABTS_SUCCESS(rv);

        box[i].tc = tc;
        box[i].cond = cond;
        box[i].mutex = mutex[i];
        box[i].func = lock_and_signal;

        rv = apr_thread_create(&thread[i], NULL, thread_routine, &box[i], p);
        ABTS_SUCCESS(rv);
    }

    /*
     * The dynamic binding should be preserved because we use only one waiter
     */

    for (i = 0; i < NTHREADS; i++) {
        rv = apr_thread_cond_wait(cond, mutex[i]);
        ABTS_SUCCESS(rv);
    }

    for (i = 0; i < NTHREADS; i++) {
        rv = apr_thread_cond_timedwait(cond, mutex[i], 10000);
        ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_TIMEUP(rv));

        rv = apr_thread_mutex_unlock(mutex[i]);
        ABTS_SUCCESS(rv);
    }

    for (i = 0; i < NTHREADS; i++) {
        apr_status_t retval;
        rv = apr_thread_join(&retval, thread[i]);
        ABTS_SUCCESS(rv);
    }

    rv = apr_thread_cond_destroy(cond);
    ABTS_SUCCESS(rv);

    for (i = 0; i < NTHREADS; i++) {
        rv = apr_thread_mutex_destroy(mutex[i]);
        ABTS_SUCCESS(rv);
    }
}
void slayer_server_stats_timer_thread(apr_pool_t *mpool,slayer_server_stats_t *stats) {
	apr_threadattr_t *thread_attr;
	apr_thread_t *thread;
	apr_threadattr_create(&thread_attr,mpool);
	apr_threadattr_detach_set(thread_attr,1); // detach
	apr_threadattr_stacksize_set(thread_attr,4096*10);
	apr_thread_create(&thread,thread_attr,slayer_server_stats_timer_thread_run,stats,mpool);
}
apr_status_t handle_new_client(apr_socket_t *socket, apr_pool_t *pool,
		dynalogin_session_t *h)
{
	char buf[ERRBUFLEN + 1];

	apr_status_t res;
	apr_threadattr_t *t_attr;
	apr_thread_t *t;
	apr_pool_t *subpool;
	socket_thread_data_t *thread_data;

	res = apr_pool_create(&subpool, pool);
	if(res != APR_SUCCESS)
	{
		syslog(LOG_ERR, "failed to create pool: %s",
				apr_strerror(res, buf, ERRBUFLEN));
		apr_socket_close(socket);
		return res;
	}

	res = apr_threadattr_create(&t_attr, subpool);
	if(res != APR_SUCCESS)
	{
		syslog(LOG_ERR, "failed to create threadattr: %s",
				apr_strerror(res, buf, ERRBUFLEN));
		apr_pool_destroy(subpool);
		apr_socket_close(socket);
		return res;
	}

	thread_data = apr_pcalloc(subpool, sizeof(struct socket_thread_data_t));
	if(thread_data == NULL)
	{
		syslog(LOG_ERR, "handle_new_client: apr_pcalloc failed");
		apr_pool_destroy(subpool);
		apr_socket_close(socket);
		return res;
	}

	thread_data->pool = subpool;
	thread_data->socket = socket;
	thread_data->tls_session = NULL;  // to be populated later if using TLS
	thread_data->dynalogin_session = h;

	res = apr_thread_create(&t, t_attr,
			(apr_thread_start_t)socket_thread_main, thread_data, subpool);

	if(res != APR_SUCCESS)
	{
		syslog(LOG_ERR, "failed to spawn a thread: %s",
				apr_strerror(res, buf, ERRBUFLEN));
		apr_pool_destroy(subpool);
		apr_socket_close(socket);
	}

	return res;
}
Exemple #15
0
static apr_status_t add_task(apr_thread_pool_t *me, apr_thread_start_t func,
                             void *param, apr_byte_t priority, int push,
                             void *owner)
{
    apr_thread_pool_task_t *t;
    apr_thread_pool_task_t *t_loc;
    apr_thread_t *thd;
    apr_status_t rv = APR_SUCCESS;

    apr_thread_mutex_lock(me->lock);

    t = task_new(me, func, param, priority, owner, 0);
    if (NULL == t) {
        apr_thread_mutex_unlock(me->lock);
        return APR_ENOMEM;
    }

    t_loc = add_if_empty(me, t);
    if (NULL == t_loc) {
        goto FINAL_EXIT;
    }

    if (push) {
        while (APR_RING_SENTINEL(me->tasks, apr_thread_pool_task, link) !=
               t_loc && t_loc->dispatch.priority >= t->dispatch.priority) {
            t_loc = APR_RING_NEXT(t_loc, link);
        }
    }
    APR_RING_INSERT_BEFORE(t_loc, t, link);
    if (!push) {
        if (t_loc == me->task_idx[TASK_PRIORITY_SEG(t)]) {
            me->task_idx[TASK_PRIORITY_SEG(t)] = t;
        }
    }

  FINAL_EXIT:
    me->task_cnt++;
    if (me->task_cnt > me->tasks_high)
        me->tasks_high = me->task_cnt;
    if (0 == me->thd_cnt || (0 == me->idle_cnt && me->thd_cnt < me->thd_max &&
                             me->task_cnt > me->threshold)) {
        rv = apr_thread_create(&thd, NULL, thread_pool_func, me, me->pool);
        if (APR_SUCCESS == rv) {
            ++me->thd_cnt;
            if (me->thd_cnt > me->thd_high)
                me->thd_high = me->thd_cnt;
        }
    }
    apr_thread_mutex_unlock(me->lock);

    apr_thread_mutex_lock(me->cond_lock);
    apr_thread_cond_signal(me->cond);
    apr_thread_mutex_unlock(me->cond_lock);

    return rv;
}
Exemple #16
0
/**
 * dyn_blist_timer_start - 开始运行一个线程定时器
 * @pool: 使用的池
 * @timer: 定时器结构指针
 *
 * 返回值: 成功返回APR_SUCCESS,失败返回-1
 */
apr_status_t dyn_blist_timer_start(apr_pool_t *pool, blist_timer_t *timer)
{ 
    if (pool == NULL || timer == NULL) {
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "start timer thread, args fail");
        return -1;
    }
    
    /* 创建定时器线程 */
    return apr_thread_create(&timer->thread, NULL, dyn_blist_timer_cb, (void *)timer, pool);
}
Exemple #17
0
int openenv(DB_ENV **pdb_env,const char *home,const char *data_dir,
		const char *log_dir,FILE *err_file,
		int cachesize,unsigned int flag,apr_pool_t *p)
{
	int rv = -1;
	if((rv = db_env_create(pdb_env,0)) != 0){
		return rv;
	}

	if((rv = (*pdb_env)->set_cachesize((*pdb_env),0,cachesize,0)) != 0){
		return rv;
	}

	if((rv = (*pdb_env)->set_data_dir((*pdb_env),data_dir)) != 0){
		return rv;
	}
	if((rv = (*pdb_env)->set_lg_dir((*pdb_env),log_dir)) != 0){
		return rv;
	}
	if((rv = (*pdb_env)->set_lk_detect((*pdb_env),DB_LOCK_DEFAULT)) != 0){
		return rv;
	}
	if((rv = (*pdb_env)->set_tx_max((*pdb_env),1024)) != 0){
		return rv;
	}
	if((rv = (*pdb_env)->set_lg_bsize((*pdb_env),cachesize)) != 0){
		return rv;
	}
	if((rv =(*pdb_env)->set_flags((*pdb_env),
					DB_TXN_NOSYNC,1) !=0))
	{
		return rv;
	}


	if((rv = (*pdb_env)->open((*pdb_env),home,flag,0)) != 0){

		(*pdb_env)->close((*pdb_env),0);
		return rv;
	}

        if((rv=(*pdb_env)->log_set_config((*pdb_env),DB_LOG_AUTO_REMOVE,1) != 0))
	{
		return rv;
	}

	apr_threadattr_create(&thread_attr,p);
	apr_threadattr_detach_set(thread_attr,1);
	rv = apr_thread_create(&chkpnt_threadid,thread_attr,chkpnt_thread,*pdb_env,p);
	if(rv != APR_SUCCESS){
		return -1;
	}

	return 0;
}
Exemple #18
0
MPF_DECLARE(apt_bool_t) mpf_scheduler_start(mpf_scheduler_t *scheduler)
{
	mpf_scheduler_resolution_set(scheduler);
	
	scheduler->running = TRUE;
	if(apr_thread_create(&scheduler->thread,NULL,timer_thread_proc,scheduler,scheduler->pool) != APR_SUCCESS) {
		scheduler->running = FALSE;
		return FALSE;
	}
	return TRUE;
}
Exemple #19
0
celix_status_t deploymentAdmin_create(apr_pool_t *pool, bundle_context_pt context, deployment_admin_pt *admin) {
	celix_status_t status = CELIX_SUCCESS;
	apr_pool_t *subpool;
	apr_pool_create(&subpool, pool);

	*admin = apr_palloc(subpool, sizeof(**admin));
	if (!*admin) {
		status = CELIX_ENOMEM;
	} else {
		(*admin)->pool = subpool;
		(*admin)->running = true;
		(*admin)->context = context;
		(*admin)->current = NULL;
		(*admin)->packages = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
		(*admin)->targetIdentification = NULL;
		(*admin)->pollUrl = NULL;
		(*admin)->auditlogUrl = NULL;

        bundleContext_getProperty(context, IDENTIFICATION_ID, &(*admin)->targetIdentification);
        (*admin)->auditlogId = apr_time_now();
        (*admin)->aditlogSeqNr = 0;

		if ((*admin)->targetIdentification == NULL ) {
			printf("Target name must be set using \"deployment_admin_identification\"\n");
			status = CELIX_ILLEGAL_ARGUMENT;
		} else {
			char *url = NULL;
			bundleContext_getProperty(context, DISCOVERY_URL, &url);
			if (url == NULL) {
				printf("URL must be set using \"deployment_admin_url\"\n");
				status = CELIX_ILLEGAL_ARGUMENT;
			} else {
				(*admin)->pollUrl = apr_pstrcat(subpool, url, "/deployment/", (*admin)->targetIdentification, VERSIONS, NULL);
				(*admin)->auditlogUrl = apr_pstrcat(subpool, url, "/auditlog", NULL);

//				log_store_pt store = NULL;
//				log_pt log = NULL;
//				log_sync_pt sync = NULL;
//				logStore_create(subpool, &store);
//				log_create(subpool, store, &log);
//				logSync_create(subpool, (*admin)->targetIdentification, store, &sync);
//
//				log_log(log, 20000, NULL);


				apr_thread_create(&(*admin)->poller, NULL, deploymentAdmin_poll, *admin, subpool);
			}
		}
	}

	return status;
}
Exemple #20
0
/*
*   schedule a task to run in "time" microseconds. Find the spot in the ring where
*   the time fits. Adjust the short_time so the thread wakes up when the time is reached.
*/
static apr_status_t schedule_task(apr_thread_pool_t *me,
                                  apr_thread_start_t func, void *param,
                                  void *owner, apr_interval_time_t time)
{
    apr_thread_pool_task_t *t;
    apr_thread_pool_task_t *t_loc;
    apr_thread_t *thd;
    apr_status_t rv = APR_SUCCESS;
    apr_thread_mutex_lock(me->lock);

    t = task_new(me, func, param, 0, owner, time);
    if (NULL == t) {
        apr_thread_mutex_unlock(me->lock);
        return APR_ENOMEM;
    }
    t_loc = APR_RING_FIRST(me->scheduled_tasks);
    while (NULL != t_loc) {
        /* if the time is less than the entry insert ahead of it */
        if (t->dispatch.time < t_loc->dispatch.time) {
            ++me->scheduled_task_cnt;
            APR_RING_INSERT_BEFORE(t_loc, t, link);
            break;
        }
        else {
            t_loc = APR_RING_NEXT(t_loc, link);
            if (t_loc ==
                APR_RING_SENTINEL(me->scheduled_tasks, apr_thread_pool_task,
                                  link)) {
                ++me->scheduled_task_cnt;
                APR_RING_INSERT_TAIL(me->scheduled_tasks, t,
                                     apr_thread_pool_task, link);
                break;
            }
        }
    }
    /* there should be at least one thread for scheduled tasks */
    if (0 == me->thd_cnt) {
        rv = apr_thread_create(&thd, NULL, thread_pool_func, me, me->pool);
        if (APR_SUCCESS == rv) {
            ++me->thd_cnt;
            if (me->thd_cnt > me->thd_high)
                me->thd_high = me->thd_cnt;
        }
    }
    apr_thread_mutex_unlock(me->lock);
    apr_thread_mutex_lock(me->cond_lock);
    apr_thread_cond_signal(me->cond);
    apr_thread_mutex_unlock(me->cond_lock);
    return rv;
}
APU_DECLARE(apr_status_t) apr_thread_pool_create(apr_thread_pool_t ** me,
                                                 apr_size_t init_threads,
                                                 apr_size_t max_threads,
                                                 apr_pool_t * pool)
{
    apr_thread_t *t;
    apr_status_t rv = APR_SUCCESS;
    apr_thread_pool_t *tp;

    *me = NULL;
    tp = apr_pcalloc(pool, sizeof(apr_thread_pool_t));

    /*
     * This pool will be used by different threads. As we cannot ensure that
     * our caller won't use the pool without acquiring the mutex, we must
     * create a new sub pool.
     */
    rv = apr_pool_create(&tp->pool, pool);
    if (APR_SUCCESS != rv)
        return rv;
    rv = thread_pool_construct(tp, init_threads, max_threads);
    if (APR_SUCCESS != rv)
        return rv;
    apr_pool_pre_cleanup_register(tp->pool, tp, thread_pool_cleanup);

    while (init_threads) {
        /* Grab the mutex as apr_thread_create() and thread_pool_func() will 
         * allocate from (*me)->pool. This is dangerous if there are multiple 
         * initial threads to create.
         */
        apr_thread_mutex_lock(tp->lock);
        ++tp->spawning_cnt;
        rv = apr_thread_create(&t, NULL, thread_pool_func, tp, tp->pool);
        apr_thread_mutex_unlock(tp->lock);
        if (APR_SUCCESS != rv) {
            break;
        }
        tp->thd_cnt++;
        if (tp->thd_cnt > tp->thd_high) {
            tp->thd_high = tp->thd_cnt;
        }
        --init_threads;
    }

    if (rv == APR_SUCCESS) {
        *me = tp;
    }

    return rv;
}
Exemple #22
0
static void test_atomics_threaded(abts_case *tc, void *data)
{
    apr_thread_t *t1[NUM_THREADS];
    apr_thread_t *t2[NUM_THREADS];
    apr_status_t rv;
    int i;

#ifdef HAVE_PTHREAD_SETCONCURRENCY
    pthread_setconcurrency(8);
#endif

    rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
    APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);

    for (i = 0; i < NUM_THREADS; i++) {
        apr_status_t r1, r2;
        r1 = apr_thread_create(&t1[i], NULL, thread_func_mutex, NULL, p);
        r2 = apr_thread_create(&t2[i], NULL, thread_func_atomic, NULL, p);
        ABTS_ASSERT(tc, "Failed creating threads", !r1 && !r2);
    }

    for (i = 0; i < NUM_THREADS; i++) {
        apr_status_t s1, s2;
        apr_thread_join(&s1, t1[i]);
        apr_thread_join(&s2, t2[i]);

        ABTS_ASSERT(tc, "Invalid return value from thread_join",
                    s1 == exit_ret_val && s2 == exit_ret_val);
    }

    ABTS_INT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS, mutex_locks);
    ABTS_INT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS,
                   apr_atomic_read32(&atomic_ops));

    rv = apr_thread_mutex_destroy(thread_lock);
    ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
}
Exemple #23
0
lt_http_status_t lt_http_server_start( lt_http_server_t * server )
{
    if( server == NULL ) return LT_HTTP_INVALID_ARG;

    /* prepare & start thread */
    if( APR_SUCCESS != apr_thread_create(
                    &(server->thread), NULL,
                    (apr_thread_start_t)_lt_http_server_run, server,
                     server->pool ) ) {
        my_perror( "ERROR: apr_thread_create failed with: " );
        return LT_HTTP_INVALID_ARG;
    }

    return LT_HTTP_SUCCESS;
}
Exemple #24
0
int test_thread_mutex(int num_threads)
{
    apr_thread_t *t[MAX_THREADS];
    apr_status_t s[MAX_THREADS];
    apr_time_t time_start, time_stop;
    int i;

    mutex_counter = 0;

    printf("apr_thread_mutex_t Tests\n");
    printf("%-60s", "    Initializing the apr_thread_mutex_t (UNNESTED)");
    s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_UNNESTED, pool);
    if (s[0] != APR_SUCCESS) {
        printf("Failed!\n");
        return s[0];
    }
    printf("OK\n");

    apr_thread_mutex_lock(thread_lock);
    /* set_concurrency(4)? -aaron */
    printf("    Starting %d threads    ", num_threads);
    for (i = 0; i < num_threads; ++i) {
        s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
        if (s[i] != APR_SUCCESS) {
            printf("Failed!\n");
            return s[i];
        }
    }
    printf("OK\n");

    time_start = apr_time_now();
    apr_thread_mutex_unlock(thread_lock);

    /* printf("%-60s", "    Waiting for threads to exit"); */
    for (i = 0; i < num_threads; ++i) {
        apr_thread_join(&s[i], t[i]);
    }
    /* printf("OK\n"); */

    time_stop = apr_time_now();
    printf("microseconds: %" APR_INT64_T_FMT " usec\n",
           (time_stop - time_start));
    if (mutex_counter != MAX_COUNTER * num_threads)
        printf("error: counter = %ld\n", mutex_counter);

    return APR_SUCCESS;
}
int bind_server_port(Network_t *net, NetStream_t *ns, char *address, int port, int max_pending)
{
    int err, slot;
    ns_monitor_t *nm;

    apr_thread_mutex_lock(net->ns_lock);

    slot = net->used_ports;
    nm = &(net->nm[slot]);

    log_printf(15, "bind_server_port: connection=%s:%d being stored in slot=%d\n", address, port, slot);

    err = ns->bind(ns->sock, address, port);
    if (err != 0) {
        log_printf(0, "bind_server_port: Error with bind address=%s port=%d err=%d\n", address, port, err);
        return(err);
    }

    err = ns->listen(ns->sock, max_pending);
    if (err != 0) {
        log_printf(0, "bind_server_port: Error with listen address=%s port=%d err=%d\n", address, port, err);
        return(err);
    }

    apr_pool_create(&(nm->mpool), NULL);
    apr_thread_mutex_create(&(nm->lock), APR_THREAD_MUTEX_DEFAULT, nm->mpool);
    apr_thread_cond_create(&(nm->cond), nm->mpool);

    nm->shutdown_request = 0;
    nm->is_pending = 0;
    nm->ns = ns;
    nm->address = strdup(address);
    nm->port = port;
    nm->trigger_cond = net->cond;
    nm->trigger_lock = net->ns_lock;
    nm->trigger_count = &(net->accept_pending);
    ns->id = ns_generate_id();

    apr_pool_create(&(nm->mpool), NULL);
    apr_thread_create(&(nm->thread), NULL, monitor_thread, (void *)nm, nm->mpool);

    net->used_ports++;
    apr_thread_mutex_unlock(net->ns_lock);

    return(0);
}
Exemple #26
0
static void nested_wait(abts_case *tc, void *data)
{
    toolbox_fnptr_t *fnptr = data;
    toolbox_t box;
    apr_status_t rv, retval;
    apr_thread_cond_t *cond = NULL;
    apr_thread_t *thread = NULL;
    apr_thread_mutex_t *mutex = NULL;

    rv = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, p);
    ABTS_SUCCESS(rv);
    ABTS_PTR_NOTNULL(tc, mutex);

    rv = apr_thread_cond_create(&cond, p);
    ABTS_SUCCESS(rv);
    ABTS_PTR_NOTNULL(tc, cond);

    rv = apr_thread_mutex_lock(mutex);
    ABTS_SUCCESS(rv);

    box.tc = tc;
    box.cond = cond;
    box.mutex = mutex;
    box.func = fnptr->func;

    rv = apr_thread_create(&thread, NULL, thread_routine, &box, p);
    ABTS_SUCCESS(rv);

    rv = apr_thread_mutex_unlock(mutex);
    ABTS_SUCCESS(rv);

    /* yield the processor */
    apr_sleep(500000);

    rv = apr_thread_cond_signal(cond);
    ABTS_SUCCESS(rv);

    rv = apr_thread_join(&retval, thread);
    ABTS_SUCCESS(rv);

    rv = apr_thread_mutex_trylock(mutex);
    ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EBUSY(rv));

    rv = apr_thread_mutex_trylock(mutex);
    ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EBUSY(rv));
}
Exemple #27
0
extern apr_status_t napr_threadpool_init(napr_threadpool_t **threadpool, void *ctx, unsigned long nb_thread,
					 threadpool_process_data_callback_fn_t *process_data, apr_pool_t *pool)
{
    char errbuf[128];
    apr_pool_t *local_pool;
    unsigned long l;
    apr_status_t status;

    apr_pool_create(&local_pool, pool);
    (*threadpool) = apr_palloc(local_pool, sizeof(struct napr_threadpool_t));
    (*threadpool)->pool = local_pool;

    if (APR_SUCCESS !=
	(status =
	 apr_thread_mutex_create(&((*threadpool)->threadpool_mutex), APR_THREAD_MUTEX_DEFAULT, (*threadpool)->pool))) {
	DEBUG_ERR("error calling apr_thread_mutex_create: %s", apr_strerror(status, errbuf, 128));
	return status;
    }
    if (APR_SUCCESS != (status = apr_thread_cond_create(&((*threadpool)->threadpool_update), (*threadpool)->pool))) {
	DEBUG_ERR("error calling apr_thread_cond_create: %s", apr_strerror(status, errbuf, 128));
	return status;
    }
    (*threadpool)->thread = apr_palloc((*threadpool)->pool, nb_thread * sizeof(apr_thread_mutex_t *));
    (*threadpool)->ctx = ctx;
    (*threadpool)->nb_thread = nb_thread;
    (*threadpool)->nb_waiting = 0UL;
    (*threadpool)->list = napr_list_make((*threadpool)->pool);
    (*threadpool)->process_data = process_data;
    (*threadpool)->run &= 0x0;
    (*threadpool)->ended &= 0x0;

    for (l = 0; l < nb_thread; l++) {
	if (APR_SUCCESS !=
	    (status =
	     apr_thread_create(&((*threadpool)->thread[l]), NULL, napr_threadpool_loop, (*threadpool),
			       (*threadpool)->pool))) {
	    DEBUG_ERR("error calling apr_thread_create: %s", apr_strerror(status, errbuf, 128));
	    return status;
	}
    }

    return APR_SUCCESS;
}
Exemple #28
0
test_server_handle * test_start_looped_server(char *response_string, int port, apr_pool_t *mp) {
	server_thread_data *data = apr_palloc(mp, sizeof(server_thread_data));
	data->handle = apr_palloc(mp, sizeof(test_server_handle));
	//apr_thread_mutex_create(&data->handle->mutex, APR_THREAD_MUTEX_UNNESTED, mp);
    //apr_thread_cond_create(&data->handle->cond, mp);

	
	data->port = port;
	data->request_process_callback = looped_response_test;
	data->callback_data = response_string;
	apr_threadattr_t *thd_attr;
	apr_threadattr_create(&thd_attr, mp);
	data->handle->test_server_running = 0;
	apr_status_t rv = apr_thread_create(&data->handle->test_server_thread, thd_attr, test_server_run, (void*)data, mp);
	assert(rv == APR_SUCCESS);
	while(data->handle->test_server_running ==0) {
		apr_sleep(100);
	}
	return data->handle;
}
Exemple #29
0
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
    celix_status_t status;

	shell_tui_activator_pt act = (shell_tui_activator_pt) userData;
	service_listener_pt listener = (service_listener_pt) malloc(sizeof(*listener));

	act->context = context;
	act->running = true;

	act->listener = listener;
	act->listener->handle = act;
	act->listener->serviceChanged = (void *) shellTui_serviceChanged;
	status = bundleContext_addServiceListener(context, act->listener, "(objectClass=shellService)");

	if (status == CELIX_SUCCESS) {
        shellTui_initializeService(act);
		apr_thread_create(&act->runnable, NULL, shellTui_runnable, act, act->pool);
	}

	return status;
}
Exemple #30
0
void LLThread::start()
{
    llassert(isStopped());

    // Set thread state to running
    mStatus = RUNNING;

    apr_status_t status =
        apr_thread_create(&mAPRThreadp, NULL, staticRun, (void *)this, tldata().mRootPool());

    if(status == APR_SUCCESS)
    {
        // We won't bother joining
        apr_thread_detach(mAPRThreadp);
    }
    else
    {
        mStatus = STOPPED;
        llwarns << "failed to start thread " << mName << llendl;
        ll_apr_warn_status(status);
    }
}