예제 #1
0
int main(int argc,char **argv) {
    apr_initialize();
    apr_pool_t *pool;
    apr_pool_create(&pool,NULL);
    apr_status_t st;
    
    apr_thread_pool_t *tpl;
    st=apr_thread_pool_create(&tpl,8,128,pool);

    apr_size_t n;
    for(long i=0;i<30;i++) {
        apr_thread_pool_push(tpl,thread_func,(void *)i,APR_THREAD_TASK_PRIORITY_NORMAL,NULL);
        apr_sleep(50000);
    }
    for(long i=0;i<10;i++) {
        n=apr_thread_pool_idle_count(tpl);
        printf("idle thread: %d\n",n);
        apr_sleep(1000000);
    }
    for(long i=100;i<130;i++) {
        apr_thread_pool_push(tpl,thread_func,(void *)i,APR_THREAD_TASK_PRIORITY_NORMAL,NULL);
        apr_sleep(50000);
    }
    for(long i=0;i<10;i++) {
        n=apr_thread_pool_idle_count(tpl);
        printf("idle thread: %d\n",n);
        apr_sleep(1000000);
    }

    apr_thread_pool_destroy(tpl);
    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}
예제 #2
0
static apr_status_t pconfig_cleanup(void *data)
{
    int advertise_run = ma_advertise_run;

    is_mp_running     = 0;
    ma_advertise_run  = 0;
    if (advertise_run) {
        ma_advertise_stat = HTTP_FORBIDDEN;
        ma_advertise_server(ma_server_rec, MA_ADVERTISE_STATUS);
    }

    if (is_mp_created) {
        apr_sleep(1000);
        /* Wait for the parent maintenance thread to finish */
        while (is_mp_created) {
            apr_sleep(MA_TM_RESOLUTION);
        }
    }
    if (advertise_run) {
        ma_advertise_stat = HTTP_FORBIDDEN;
        ma_advertise_server(ma_server_rec, MA_ADVERTISE_STATUS);
    }
    if (magd) {
        /* Remove the process_cleanup.
         * We need to reattach again because the
         * module can be reloaded on different address
         */
        apr_pool_cleanup_kill(magd->ppool, magd, process_cleanup);
    }
    return APR_SUCCESS;
}
예제 #3
0
static apr_status_t process_cleanup(void *data)
{
    int advertise_run = ma_advertise_run;

    is_mp_running     = 0;
    ma_advertise_run  = 0;
    if (advertise_run) {
        ma_advertise_stat = HTTP_FORBIDDEN;
        ma_advertise_server(ma_server_rec, MA_ADVERTISE_STATUS);
    }
    if (is_mp_created) {
        apr_sleep(1000);
        /* Wait for the parent maintenance thread to finish */
        while (is_mp_created) {
            apr_sleep(MA_TM_RESOLUTION);
        }
    }
    if (advertise_run) {
        ma_advertise_stat = HTTP_GONE;
        ma_advertise_server(ma_server_rec, MA_ADVERTISE_STATUS);
        ma_group_leave();
    }
    /* We don't need the post_config cleanup to run,
     */
    apr_pool_cleanup_kill(magd->cpool, magd, pconfig_cleanup);
    magd = NULL;

    return APR_SUCCESS;
}
예제 #4
0
파일: htcacheclean.c 프로젝트: aptana/Jaxer
/*
 * delete cache file set
 */
static void delete_entry(char *path, char *basename, apr_pool_t *pool)
{
    char *nextpath;
    apr_pool_t *p;

    if (dryrun) {
        return;
    }

    /* temp pool, otherwise lots of memory could be allocated */
    apr_pool_create(&p, pool);

    nextpath = apr_pstrcat(p, path, "/", basename, CACHE_HEADER_SUFFIX, NULL);
    apr_file_remove(nextpath, p);

    nextpath = apr_pstrcat(p, path, "/", basename, CACHE_DATA_SUFFIX, NULL);
    apr_file_remove(nextpath, p);

    apr_pool_destroy(p);

    if (benice) {
        delcount += 2;
        if (delcount >= DELETE_NICE) {
            apr_sleep(NICE_DELAY);
            delcount = 0;
        }
    }
}
예제 #5
0
LLCurlRequest::~LLCurlRequest()
{
	llassert_always(mThreadID == LLThread::currentID());

	//stop all Multi handle background threads
	for (curlmulti_set_t::iterator iter = mMultiSet.begin(); iter != mMultiSet.end(); ++iter)
	{
		LLCurl::Multi* multi = *iter;
		if (multi->mThreaded)
			multi->mSignal->lock();
		multi->mQuitting = true;
		if (multi->mThreaded)
		{
			while (!multi->isStopped())
			{
				multi->mSignal->signal();
				multi->mSignal->unlock();
				apr_sleep(1000);
				multi->mSignal->lock();
			}
		}
		if (multi->mThreaded)
			multi->mSignal->unlock();
	}
	for_each(mMultiSet.begin(), mMultiSet.end(), DeletePointer());
}
예제 #6
0
파일: lock.c 프로젝트: jmckenna/mapcache
int mapcache_lock_or_wait_for_resource(mapcache_context *ctx, mapcache_locker *locker, char *resource, void **lock)
{
  mapcache_lock_result rv = locker->aquire_lock(ctx, locker, resource, lock);
  if(GC_HAS_ERROR(ctx)) {
    return MAPCACHE_FAILURE;
  }
  if(rv == MAPCACHE_LOCK_AQUIRED)
    return MAPCACHE_TRUE;
  else {
    apr_time_t start_wait = apr_time_now();
    rv = MAPCACHE_LOCK_LOCKED;
    
    while(rv != MAPCACHE_LOCK_NOENT) {
      unsigned int waited = apr_time_as_msec(apr_time_now()-start_wait);
      if(waited > locker->timeout*1000) {
        mapcache_unlock_resource(ctx,locker,resource, *lock);
        ctx->log(ctx,MAPCACHE_ERROR,"deleting a possibly stale lock after waiting on it for %g seconds",waited/1000.0);
        return MAPCACHE_FALSE;
      }
      apr_sleep(locker->retry_interval * 1000000);
      rv = locker->ping_lock(ctx,locker,resource, *lock);
    }
    return MAPCACHE_FALSE;
  }
}
예제 #7
0
파일: mpm_common.c 프로젝트: apresley/Jaxer
void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
                        apr_pool_t *p)
{
    apr_status_t rv;

    ++wait_or_timeout_counter;
    if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
        wait_or_timeout_counter = 0;
        ap_run_monitor(p);
    }

    rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
    if (APR_STATUS_IS_EINTR(rv)) {
        ret->pid = -1;
        return;
    }

    if (APR_STATUS_IS_CHILD_DONE(rv)) {
        return;
    }

#ifdef NEED_WAITPID
    if ((ret = reap_children(exitcode, status)) > 0) {
        return;
    }
#endif

    apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
    ret->pid = -1;
    return;
}
예제 #8
0
파일: lock.c 프로젝트: MiniHero/mapcache
int mapcache_lock_or_wait_for_resource(mapcache_context *ctx, char *resource)
{
  char *lockname = lock_filename_for_resource(ctx,resource);
  apr_file_t *lockfile;
  apr_status_t rv;
  /* create the lockfile */
  rv = apr_file_open(&lockfile,lockname,APR_WRITE|APR_CREATE|APR_EXCL|APR_XTHREAD,APR_OS_DEFAULT,ctx->pool);

  /* if the file already exists, wait for it to disappear */
  /* TODO: check the lock isn't stale (i.e. too old) */
  if( rv != APR_SUCCESS ) {
    apr_finfo_t info;
    rv = apr_stat(&info,lockname,0,ctx->pool);
#ifdef DEBUG
    if(!APR_STATUS_IS_ENOENT(rv)) {
      ctx->log(ctx, MAPCACHE_DEBUG, "waiting on resource lock %s", resource);
    }
#endif
    while(!APR_STATUS_IS_ENOENT(rv)) {
      /* sleep for the configured number of micro-seconds (default is 1/100th of a second) */
      apr_sleep(ctx->config->lock_retry_interval);
      rv = apr_stat(&info,lockname,0,ctx->pool);
    }
    return MAPCACHE_FALSE;
  } else {
    /* we acquired the lock */
    apr_file_close(lockfile);
    return MAPCACHE_TRUE;
  }
}
예제 #9
0
void _reap_hportal(host_portal_t *hp, int quick)
{
    host_connection_t *hc;
    apr_status_t value;
    int count;

    tbx_stack_move_to_top(hp->closed_que);
    while ((hc = (host_connection_t *)tbx_stack_get_current_data(hp->closed_que)) != NULL) {
        apr_thread_join(&value, hc->recv_thread);
        log_printf(5, "hp=%s ns=%d\n", hp->skey, tbx_ns_getid(hc->ns));
        for (count=0; ((quick == 0) || (count < 2)); count++) {
            lock_hc(hc);  //** Make sure that no one is running close_hc() while we're trying to close it
            if (hc->closing != 1) {  //** Ok to to remove it
                unlock_hc(hc);

                tbx_stack_delete_current(hp->closed_que, 0, 0);
                destroy_host_connection(hc);

                break;
            } else {  //** Got somone trying ot close it so wait a little bit
                unlock_hc(hc);
                apr_sleep(apr_time_from_msec(10));
            }
        }

        tbx_stack_move_down(hp->closed_que);
        if (tbx_stack_get_current_data(hp->closed_que) == NULL) tbx_stack_move_to_top(hp->closed_que);  //** Restart it needed
    }
}
예제 #10
0
static void wait_on_busy_threads(apr_thread_pool_t *me, void *owner)
{
#ifndef NDEBUG
    apr_os_thread_t *os_thread;
#endif
    struct apr_thread_list_elt *elt;
    apr_thread_mutex_lock(me->lock);
    elt = APR_RING_FIRST(me->busy_thds);
    while (elt != APR_RING_SENTINEL(me->busy_thds, apr_thread_list_elt, link)) {
        if (elt->current_owner != owner) {
            elt = APR_RING_NEXT(elt, link);
            continue;
        }
#ifndef NDEBUG
        /* make sure the thread is not the one calling tasks_cancel */
        apr_os_thread_get(&os_thread, elt->thd);
#ifdef WIN32
        /* hack for apr win32 bug */
        assert(!apr_os_thread_equal(apr_os_thread_current(), os_thread));
#else
        assert(!apr_os_thread_equal(apr_os_thread_current(), *os_thread));
#endif
#endif
        while (elt->current_owner == owner) {
            apr_thread_mutex_unlock(me->lock);
            apr_sleep(200 * 1000);
            apr_thread_mutex_lock(me->lock);
        }
        elt = APR_RING_FIRST(me->busy_thds);
    }
    apr_thread_mutex_unlock(me->lock);
    return;
}
예제 #11
0
파일: testcond.c 프로젝트: ohmann/checkapi
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);
}
예제 #12
0
파일: sendrecv.c 프로젝트: ohmann/checkapi
APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
                                          apr_size_t *len)
{
    apr_ssize_t rv;

    do {
        rv = send(sock->socketdes, buf, (*len), 0);
    } while (rv == -1 && errno == EINTR);

    if (rv == -1 && errno == EWOULDBLOCK && sock->timeout > 0) {
        apr_int32_t snooze_val = SEND_WAIT;
        apr_int32_t zzz = 0;

        do {
            rv = send(sock->socketdes, buf, (*len), 0);
            if (rv == -1 && errno == EWOULDBLOCK){
                apr_sleep (snooze_val);
                zzz += snooze_val;
                snooze_val += SEND_WAIT;
                /* have we passed our timeout value */
                if (zzz > (sock->timeout * APR_USEC_PER_SEC))
                    break;
            }
        } while (rv == -1 && (errno == EINTR || errno == EWOULDBLOCK));
    }
    if (rv == -1) {
        *len = 0;
        return errno;
    }
    (*len) = rv;

    return APR_SUCCESS;
}
예제 #13
0
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;
}
예제 #14
0
파일: ltapi.c 프로젝트: acassis/lintouch
int _ltapi_sleep( lua_State * L )
{
    apr_interval_time_t total = 0, partial = 0;
    unsigned steps = 1, i;

    LTAPI_GET_PLUGIN;

    total = partial = (apr_interval_time_t) lua_tonumber( L, 1 );

    /* when sleeping more than one second, check exit flag every second */
    if( total > 1 * 1000 * 1000 ) {
        partial = 1 * 1000 * 1000;
        steps = total / partial;
    }

    for( i = 0; i < steps; i ++ ) {
        apr_sleep( partial );
        if( ! lt_server_plugin_should_run( plugin ) ) {
            break;
        }
    }

    /* push the result to the stack */
    lua_pushboolean( L, lt_server_plugin_should_run( plugin ) );

    return 1;
}
예제 #15
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;
}
static apr_status_t pass_data_to_filter(ap_filter_t *f, const char *data,
                                        apr_size_t len, apr_bucket_brigade *bb)
{
    ef_ctx_t *ctx = f->ctx;
    ef_dir_t *dc = ctx->dc;
    apr_status_t rv;
    apr_size_t bytes_written = 0;
    apr_size_t tmplen;

    do {
        tmplen = len - bytes_written;
        rv = apr_file_write(ctx->proc->in,
                       (const char *)data + bytes_written,
                       &tmplen);
        bytes_written += tmplen;
        if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, f->r,
                          "apr_file_write(child input), len %" APR_SIZE_T_FMT,
                          tmplen);
            return rv;
        }
        if (APR_STATUS_IS_EAGAIN(rv)) {
            /* XXX handle blocking conditions here...  if we block, we need
             * to read data from the child process and pass it down to the
             * next filter!
             */
            rv = drain_available_output(f, bb);
            if (APR_STATUS_IS_EAGAIN(rv)) {
#if APR_FILES_AS_SOCKETS
                int num_events;
                const apr_pollfd_t *pdesc;

                rv = apr_pollset_poll(ctx->pollset, f->r->server->timeout,
                                      &num_events, &pdesc);
                if (rv || dc->debug >= DBGLVL_GORY) {
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG,
                                  rv, f->r, "apr_pollset_poll()");
                }
                if (rv != APR_SUCCESS && !APR_STATUS_IS_EINTR(rv)) {
                    /* some error such as APR_TIMEUP */
                    return rv;
                }
#else /* APR_FILES_AS_SOCKETS */
                /* Yuck... I'd really like to wait until I can read
                 * or write, but instead I have to sleep and try again
                 */
                apr_sleep(100000); /* 100 milliseconds */
                if (dc->debug >= DBGLVL_GORY) {
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG,
                                  0, f->r, "apr_sleep()");
                }
#endif /* APR_FILES_AS_SOCKETS */
            }
            else if (rv != APR_SUCCESS) {
                return rv;
            }
        }
    } while (bytes_written < len);
    return rv;
}
예제 #17
0
static void test_atreadeof(abts_case *tc, void *data)
{
    apr_status_t rv;
    apr_socket_t *sock;
    apr_socket_t *sock2;
    apr_proc_t proc;
    apr_size_t length = STRLEN;
    char datastr[STRLEN];
    int atreadeof = -1;

    sock = setup_socket(tc);
    if (!sock) return;

    launch_child(tc, &proc, "write", p);

    rv = apr_socket_accept(&sock2, sock, p);
    APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);

    /* Check that the remote socket is still open */
    rv = apr_socket_atreadeof(sock2, &atreadeof);
    APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #1", rv);
    ABTS_INT_EQUAL(tc, 0, atreadeof);

    memset(datastr, 0, STRLEN);
    apr_socket_recv(sock2, datastr, &length);

    /* Make sure that the server received the data we sent */
    ABTS_STR_EQUAL(tc, DATASTR, datastr);
    ABTS_SIZE_EQUAL(tc, strlen(datastr), wait_child(tc, &proc));

    /* The child is dead, so should be the remote socket */
    rv = apr_socket_atreadeof(sock2, &atreadeof);
    APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #2", rv);
    ABTS_INT_EQUAL(tc, 1, atreadeof);

    rv = apr_socket_close(sock2);
    APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);

    launch_child(tc, &proc, "close", p);

    rv = apr_socket_accept(&sock2, sock, p);
    APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv);

    /* The child closed the socket as soon as it could... */
    rv = apr_socket_atreadeof(sock2, &atreadeof);
    APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #3", rv);
    if (!atreadeof) { /* ... but perhaps not yet; wait a moment */
        apr_sleep(apr_time_from_msec(5));
        rv = apr_socket_atreadeof(sock2, &atreadeof);
        APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #4", rv);
    }
    ABTS_INT_EQUAL(tc, 1, atreadeof);
    wait_child(tc, &proc);

    rv = apr_socket_close(sock2);
    APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv);

    rv = apr_socket_close(sock);
    APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
}
예제 #18
0
AP_DECLARE(void) ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode,
                                    apr_proc_t *ret, apr_pool_t *p,
                                    server_rec *s)
{
    apr_status_t rv;

    ++wait_or_timeout_counter;
    if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
        wait_or_timeout_counter = 0;
        ap_run_monitor(p, s);
    }

    rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
    ap_update_global_status();

    if (APR_STATUS_IS_EINTR(rv)) {
        ret->pid = -1;
        return;
    }

    if (APR_STATUS_IS_CHILD_DONE(rv)) {
        return;
    }

    apr_sleep(apr_time_from_sec(1));
    ret->pid = -1;
}
예제 #19
0
void *slayer_server_stats_timer_thread_run(apr_thread_t *mthread, void *x) {
	slayer_server_stats_t *stats = (slayer_server_stats_t*) x;
	while (1) {
		apr_sleep(1000*1000 * stats->tslice);
		slayer_server_stats_tick(stats);
	}
	return NULL;
}
예제 #20
0
void *thread_func(apr_thread_t *t,void *v) {
    int i;
    for(i=0;i<10;i++) {
        apr_sleep(500000);
        printf("thread %ld => %d\n",(long)v,i);
    }
    return NULL;
}
예제 #21
0
void
rawx_stats_rrd_lock(struct rawx_stats_rrd_s *rsr)
{
    do {
        if (0 == apr_atomic_cas32(&(rsr->lock), 1, 0))
            return;
        apr_sleep(100);
    } while (1);
}
예제 #22
0
void *thread_func(apr_thread_t *thd,void *arg) {

    long i=(long)arg;
    for(int j=0;j<20;j++) {
        printf("task %d => %d\n",i,j);
        apr_sleep(300000);
    }
    return NULL;
}
예제 #23
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;
} 
예제 #24
0
static int dbd_sqlite3_query(apr_dbd_t *sql, int *nrows, const char *query)
{
    sqlite3_stmt *stmt = NULL;
    const char *tail = NULL;
    int ret = -1, length = 0;

    if (sql->trans && sql->trans->errnum) {
        return sql->trans->errnum;
    }

    length = strlen(query);
#if APR_HAS_THREADS
    apr_thread_mutex_lock(sql->mutex);
#endif

    do {
        int retry_count = 0;

        ret = sqlite3_prepare(sql->conn, query, length, &stmt, &tail);
        if (ret != SQLITE_OK) {
            sqlite3_finalize(stmt);
            break;
        }

        while(retry_count++ <= MAX_RETRY_COUNT) {
            ret = sqlite3_step(stmt);
            if (ret != SQLITE_BUSY)
                break;

#if APR_HAS_THREADS
            apr_thread_mutex_unlock(sql->mutex);
#endif
            apr_sleep(MAX_RETRY_SLEEP);
#if APR_HAS_THREADS
            apr_thread_mutex_lock(sql->mutex);
#endif
        }

        *nrows = sqlite3_changes(sql->conn);
        sqlite3_finalize(stmt);
        length -= (tail - query);
        query = tail;
    } while (length > 0);

    if (dbd_sqlite3_is_success(ret)) {
        ret = 0;
    }
#if APR_HAS_THREADS
    apr_thread_mutex_unlock(sql->mutex);
#endif
    if (sql->trans) {
        sql->trans->errnum = ret;
    }
    return ret;
}
예제 #25
0
int main(int argc,char **argv) {
    apr_initialize();
    apr_pool_t *pool;
    apr_pool_create(&pool,NULL);
    apr_status_t st;

    apr_signal_init(pool);
    apr_signal(SIGINT,signal_func);
    apr_signal_block(SIGINT);
    printf("signal SIGINT blocked\n");
    apr_sleep(5000000);
    apr_signal_unblock(SIGINT);
    printf("signal SIGINT unblocked\n");
    apr_sleep(50000000);
    

    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}
예제 #26
0
static apr_status_t my_destructor(void *resource, void *params,
                                  apr_pool_t *pool)
{
    my_resource_t *res = resource;
    my_parameters_t *my_params = params;
    res->id = my_params->d_count++;

    apr_sleep(my_params->sleep_upon_destruct);

    return APR_SUCCESS;
}
예제 #27
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;
} 
예제 #28
0
static int dbd_sqlite3_pquery(apr_pool_t *pool, apr_dbd_t *sql,
                              int *nrows, apr_dbd_prepared_t *statement,
                              int nargs, const char **values)
{
    sqlite3_stmt *stmt = statement->stmt;
    int ret = -1, retry_count = 0, i;

    if (sql->trans && sql->trans->errnum) {
        return sql->trans->errnum;
    }

#if APR_HAS_THREADS
    apr_thread_mutex_lock(sql->mutex);
#endif

    ret = sqlite3_reset(stmt);
    if (ret == SQLITE_OK) {
        for (i=0; i < nargs; i++) {
            sqlite3_bind_text(stmt, i + 1, values[i], strlen(values[i]),
                              SQLITE_STATIC);
        }

        while(retry_count++ <= MAX_RETRY_COUNT) {
            ret = sqlite3_step(stmt);
            if (ret != SQLITE_BUSY)
                break;

#if APR_HAS_THREADS
            apr_thread_mutex_unlock(sql->mutex);
#endif
            apr_sleep(MAX_RETRY_SLEEP);
#if APR_HAS_THREADS
            apr_thread_mutex_lock(sql->mutex);
#endif
        }

        *nrows = sqlite3_changes(sql->conn);

        sqlite3_reset(stmt);
    }

    if (dbd_sqlite3_is_success(ret)) {
        ret = 0;
    }
#if APR_HAS_THREADS
    apr_thread_mutex_unlock(sql->mutex);
#endif
    if (sql->trans) {
        sql->trans->errnum = ret;
    }

    return ret;
}
예제 #29
0
파일: echo_client.c 프로젝트: jawi/celix
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;
}
예제 #30
0
파일: client.cpp 프로젝트: lamontnelson/rrr
void client_connect(client_t *cli) {
    LOG_DEBUG("connecting to server %s %d", cli->comm->ip, cli->comm->port);
    
    apr_status_t status = APR_SUCCESS;
    status = apr_sockaddr_info_get(&cli->comm->sa, cli->comm->ip, APR_INET, 
				   cli->comm->port, 0, cli->comm->mp);
    SAFE_ASSERT(status == APR_SUCCESS);

    status = apr_socket_create(&cli->comm->s, cli->comm->sa->family, 
			       SOCK_STREAM, APR_PROTO_TCP, cli->comm->mp);
    SAFE_ASSERT(status == APR_SUCCESS);

    status = apr_socket_opt_set(cli->comm->s, APR_TCP_NODELAY, 1);
    SAFE_ASSERT(status == APR_SUCCESS);

    while (1) {
	// repeatedly retry
        LOG_TRACE("TCP CLIENT TRYING TO CONNECT.");
        status = apr_socket_connect(cli->comm->s, cli->comm->sa);
        if (status == APR_SUCCESS /*|| status == APR_EINPROGRESS */) {
            break;
        } else if (status == APR_ECONNREFUSED) {
	    LOG_DEBUG("client connect refused, maybe the server is not ready yet");
	} else if (status == APR_EINVAL) {
	    LOG_ERROR("client connect error, invalid argument. ip: %s, port: %d", 
		      cli->comm->ip, cli->comm->port);
	    SAFE_ASSERT(0);
	} else {
            LOG_ERROR("client connect error:%s", apr_strerror(status, (char*)malloc(100), 100));
	    SAFE_ASSERT(0);
        }
	apr_sleep(50 * 1000);
    }
    LOG_INFO("connected socket on remote addr %s, port %d", cli->comm->ip, cli->comm->port);
    status = apr_socket_opt_set(cli->comm->s, APR_SO_NONBLOCK, 1);
    SAFE_ASSERT(status == APR_SUCCESS);
    
    // add to epoll
//    context_t *ctx = c->ctx;
//    while (ctx->ps == NULL) {
//        // not inited yet, just wait.
//    }
//
    apr_pollfd_t pfd = {cli->comm->mp, APR_POLL_SOCKET, APR_POLLIN, 0, {NULL}, NULL};
    pfd.desc.s = cli->comm->s;
    pfd.client_data = cli->pjob;
    cli->pjob->pfd = pfd;
    poll_mgr_add_job(cli->pjob->mgr, cli->pjob);
    
   // status = apr_pollset_add(pollset_, &ctx->pfd);
    //  status = apr_pollset_add(ctx->ps, &ctx->pfd);
    //    SAFE_ASSERT(status == APR_SUCCESS);
}