コード例 #1
0
ファイル: rspamd_http_test.c プロジェクト: bryongloden/rspamd
static void
rspamd_http_server_func (gint fd, const gchar *path, rspamd_inet_addr_t *addr,
		struct rspamd_cryptobox_keypair *kp, struct rspamd_keypair_cache *c)
{
	struct rspamd_http_connection_router *rt;
	struct event_base *ev_base = event_init ();
	struct event accept_ev, term_ev;

	rt = rspamd_http_router_new (rspamd_server_error, rspamd_server_finish,
			NULL, ev_base, path, c);
	g_assert (rt != NULL);

	rspamd_http_router_set_key (rt, kp);
	event_set (&accept_ev, fd, EV_READ | EV_PERSIST, rspamd_server_accept, rt);
	event_base_set (ev_base, &accept_ev);
	event_add (&accept_ev, NULL);

	evsignal_set (&term_ev, SIGTERM, rspamd_http_term_handler, ev_base);
	event_base_set (ev_base, &term_ev);
	event_add (&term_ev, NULL);

	event_base_loop (ev_base, 0);
}
コード例 #2
0
void LibEventServer::dispatchWithTimeout(int timeoutSeconds) {
  struct timeval timeout;
  timeout.tv_sec = timeoutSeconds;
  timeout.tv_usec = 0;

  event eventTimeout;
  event_set(&eventTimeout, -1, 0, on_timer, m_eventBase);
  event_base_set(m_eventBase, &eventTimeout);
  event_add(&eventTimeout, &timeout);

  event_base_loop(m_eventBase, EVLOOP_ONCE);

  event_del(&eventTimeout);
}
コード例 #3
0
ファイル: symbols_cache.c プロジェクト: Sp1l/rspamd
void
rspamd_symbols_cache_start_refresh (struct symbols_cache * cache,
		struct event_base *ev_base)
{
	struct timeval tv;
	gdouble tm;

	tm = rspamd_time_jitter (cache->reload_time, 0);
	g_assert (cache != NULL);
	evtimer_set (&cache->resort_ev, rspamd_symbols_cache_resort_cb, cache);
	event_base_set (ev_base, &cache->resort_ev);
	double_to_tv (tm, &tv);
	event_add (&cache->resort_ev, &tv);
}
コード例 #4
0
static int new_connection(struct connection *t)
{
    int sock;
    struct sockaddr_in dest_addr;
    int flags = 1;
    struct connection *c = (struct connection *)malloc(sizeof(struct connection));
    memcpy(c, t, sizeof(struct connection));

    sock = socket(AF_INET, SOCK_STREAM, 0);

    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(c->port_num);
    dest_addr.sin_addr.s_addr = inet_addr(c->ip_addr);

    if ( (flags = fcntl(sock, F_GETFL, 0)) < 0 ||
        fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
        close(sock);
        return -1;
    }

    memset(&(dest_addr.sin_zero), '\0', 8);

    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));

    if (connect(sock, (const struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1) {
        if (errno != EINPROGRESS) {
            close(sock);
            return -1;
        }
    }

    c->fd = sock;
    c->state = conn_connecting;
    c->ev_flags = EV_WRITE;

    event_set(&c->ev, sock, c->ev_flags, client_handler, (void *)c);
    event_base_set(main_base, &c->ev);
    event_add(&c->ev, NULL);

    if (c->iov_count > 0) {
        c->vecs = calloc(c->iov_count, sizeof(struct iovec));
        if (c->vecs == NULL) {
            fprintf(stderr, "Couldn't allocate iovecs\n");
            exit(1);
        }
    }

    return sock;
}
コード例 #5
0
ファイル: server.c プロジェクト: AllenDou/webdis
int
server_start(struct server *s) {

	int i, ret;

	/* initialize libevent */
	s->base = event_base_new();

	if(s->cfg->daemonize) {
		server_daemonize();

		/* sometimes event mech gets lost on fork */
		if(event_reinit(s->base) != 0) {
			fprintf(stderr, "Error: event_reinit failed after fork");
		}
	}

	/* ignore sigpipe */
#ifdef SIGPIPE
	signal(SIGPIPE, SIG_IGN);
#endif

	slog_init(s);

	/* start worker threads */
	for(i = 0; i < s->cfg->http_threads; ++i) {
		worker_start(s->w[i]);
	}

	/* create socket */
	s->fd = socket_setup(s->cfg->http_host, s->cfg->http_port);
	if(s->fd < 0) {
		return -1;
	}

	/* start http server */
	event_set(&s->ev, s->fd, EV_READ | EV_PERSIST, server_can_accept, s);
	event_base_set(s->base, &s->ev);
	ret = event_add(&s->ev, NULL);

	if(ret < 0) {
		slog(s, WEBDIS_ERROR, "Error calling event_add on socket", 0);
		return -1;
	}

	event_base_dispatch(s->base);

	return 0;
}
コード例 #6
0
ファイル: event.c プロジェクト: Adam-/oftc-ircservices
struct event *
events_setup(int fd, short events, void(*cb)(int, short, void *), void *arg)
{
  struct event *ev;

  ilog(L_DEBUG, "Adding event for %d (%d %p %p)", fd, events, cb, arg);
  ev = MyMalloc(sizeof(struct event));
  if(ev == NULL)
    return NULL;

  event_set(ev, fd, events, cb, arg);
  event_base_set(ev_base, ev);

  return ev;
}
コード例 #7
0
ファイル: mc_connection.c プロジェクト: AllenDou/twemcache
rstatus_t
conn_set_event(struct conn *c, struct event_base *base)
{
    int status;

    event_set(&c->event, c->sd, c->ev_flags, core_event_handler, c);
    event_base_set(base, &c->event);

    status = event_add(&c->event, 0);
    if (status < 0) {
        return MC_ERROR;
    }

    return MC_OK;
}
コード例 #8
0
void chassis_event_add_by_thread(chassis_event_thread_t* thread, struct event* ev, int timeout_s, int timeout_us)
{
    struct event_base *event_base = thread->event_base;
    event_base_set(event_base, ev);

    if (timeout_s < 0 || timeout_us < 0 || (timeout_s == 0 && timeout_us == 0))
    {
        event_add(ev, NULL);
    }
    else
    {
        struct timeval tm = {timeout_s, timeout_us};
        event_add(ev, &tm);
    }
}
コード例 #9
0
ファイル: chassis-event.c プロジェクト: hewei-chn/resurgence
/**
 * setup the notification-fd 
 *
 * @see chassis_event_handle()
 */ 
int chassis_event_init(chassis_event_t *loop, chassis *chas) {
	loop->event_base = event_base_new();
	loop->chas = chas;
	loop->notify_fd = dup(chas->event_notify_fds[0]);
	if (-1 == loop->notify_fd) {
		g_critical("%s: Could not create duplicated socket: %s (%d)", G_STRLOC, g_strerror(errno), errno);
		return -1;
	}

	event_set(&(loop->notify_fd_event), loop->notify_fd, EV_READ | EV_PERSIST, chassis_event_handle, loop);
	event_base_set(loop->event_base, &(loop->notify_fd_event));
	event_add(&(loop->notify_fd_event), NULL);

	return 0;
}
コード例 #10
0
ファイル: thread.c プロジェクト: baotiao/Asenal
void setup_thread(wk_thread *work_thread)
{
    work_thread->base = event_init();
    if (!work_thread->base) {
        fprintf(stdout, "Can't allocate event base\n");
        exit(1);
    }

    event_set(&work_thread->notify_event, work_thread->notify_receive_fd, EV_READ | EV_PERSIST, thread_libevent_process, work_thread);
    event_base_set(work_thread->base, &work_thread->notify_event);
    if (event_add(&work_thread->notify_event, 0) == -1) {
        fprintf(stdout, "Can't add libevent notify pipe\n");
        exit(1);
    }
}
コード例 #11
0
ファイル: server.c プロジェクト: seanxh/minute
void on_accept(int sock, short event, void* arg)
{
    struct sockaddr_in cli_addr;
    int newfd;
    socklen_t sin_size;
    // read_ev must allocate from heap memory, otherwise the program would crash from segmant fault
    struct event* read_ev = (struct event*)malloc(sizeof(struct event));
    sin_size = sizeof(struct sockaddr_in);
    newfd = accept(sock, (struct sockaddr*)&cli_addr, &sin_size);
    printf("accept client %s\n",inet_ntoa(cli_addr.sin_addr));
    send(newfd, "welcom to server\n", 21, 0);
    event_set(read_ev, newfd, EV_READ|EV_PERSIST, on_read, read_ev);
    event_base_set(base, read_ev);
    event_add(read_ev, NULL);
}
コード例 #12
0
ファイル: worker_util.c プロジェクト: bryongloden/rspamd
struct event_base *
rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
	void (*accept_handler)(int, short, void *))
{
	struct event_base *ev_base;
	struct event *accept_events;
	GList *cur;
	struct rspamd_worker_listen_socket *ls;

#ifdef WITH_PROFILER
	extern void _start (void), etext (void);
	monstartup ((u_long) & _start, (u_long) & etext);
#endif

	gperf_profiler_init (worker->srv->cfg, name);

	worker->srv->pid = getpid ();
	worker->signal_events = g_hash_table_new_full (g_direct_hash, g_direct_equal,
			NULL, g_free);

	ev_base = event_init ();

	rspamd_worker_init_signals (worker, ev_base);
	rspamd_control_worker_add_default_handler (worker, ev_base);

	/* Accept all sockets */
	if (accept_handler) {
		cur = worker->cf->listen_socks;

		while (cur) {
			ls = cur->data;

			if (ls->fd != -1) {
				accept_events = g_slice_alloc0 (sizeof (struct event) * 2);
				event_set (&accept_events[0], ls->fd, EV_READ | EV_PERSIST,
						accept_handler, worker);
				event_base_set (ev_base, &accept_events[0]);
				event_add (&accept_events[0], NULL);
				worker->accept_events = g_list_prepend (worker->accept_events,
						accept_events);
			}

			cur = g_list_next (cur);
		}
	}

	return ev_base;
}
コード例 #13
0
ファイル: event-extra.c プロジェクト: ccin2p3/remctl
/*
 * Allocate a new event struct and initialize it.  This uses the form that
 * explicitly sets an event base.  If we can't set an event base, return NULL,
 * since that's the only error reporting mechanism we have.
 */
struct event *
event_new(struct event_base *base, evutil_socket_t fd, short what,
          event_callback_fn cb, void *arg)
{
    struct event *ev;

    ev = calloc(1, sizeof(struct event));
    if (ev == NULL)
        return NULL;
    event_set(ev, fd, what, cb, arg);
    if (event_base_set(base, ev) < 0) {
        free(ev);
        return NULL;
    }
    return ev;
}
コード例 #14
0
ファイル: server2.cpp プロジェクト: amumu/gaston_c
void on_accept(int sock, short event, void* arg)
{
    struct sockaddr cli_addr;
    //struct sockadd cli_add_2 = (struct sockaddr*) cli_addr;
printf("%s\n", "on_accept");
    int newfd;
    socklen_t sin_size;
    struct sock_ev* ev = (struct sock_ev*)malloc(sizeof(struct sock_ev));
    ev->read_ev = (struct event*)malloc(sizeof(struct event));
    ev->write_ev = (struct event*)malloc(sizeof(struct event));
    sin_size = sizeof(struct sockaddr_in);
    newfd = accept(sock, &cli_addr, &sin_size);
    event_set(ev->read_ev, newfd, EV_WRITE|EV_PERSIST, on_read, ev);
    event_base_set(base, ev->read_ev);
    event_add(ev->read_ev, NULL);
}
コード例 #15
0
ファイル: ub_event.c プロジェクト: gokzy/netbsd-src
struct ub_event*
ub_event_new(struct ub_event_base* base, int fd, short bits,
	void (*cb)(int, short, void*), void* arg)
{
	struct event *ev = (struct event*)calloc(1, sizeof(struct event));

	if (!ev)
		return NULL;

	event_set(ev, fd, NATIVE_BITS(bits), NATIVE_BITS_CB(cb), arg);
	if (event_base_set(AS_EVENT_BASE(base), ev) != 0) {
		free(ev);
		return NULL;
	}
	return AS_UB_EVENT(ev);
}
コード例 #16
0
ファイル: Powersave.cpp プロジェクト: CarlEbeling/OpenHT
static void ClaimFdEvents(CWorker *me, CWorker *other) {
    short i;

    assert(me);
    assert(other);
    
    for (i=0; i < other->m_connAllocCnt; i++) {
        CConn *c = other->m_connList[i];
        if (c) {
            if (!c->m_bFree && c->m_bEventActive) {
                event_base_set(me->m_base, &c->m_event);
                event_add(&c->m_event, 0);
            }
        }
    }
}
コード例 #17
0
ファイル: ub_event.c プロジェクト: rfminelli/unbound
struct ub_event*
ub_signal_new(struct ub_event_base* base, int fd,
	void (*cb)(int, short, void*), void* arg)
{
	struct event *ev = (struct event*)calloc(1, sizeof(struct event));

	if (!ev)
		return NULL;

	signal_set(ev, fd, cb, arg);
	if (event_base_set(AS_EVENT_BASE(base), ev) != 0) {
		free(ev);
		return NULL;
	}
	return AS_UB_EVENT(ev);
}
コード例 #18
0
ファイル: mdns_avahi.c プロジェクト: peterjc/forked-daapd
static int
_ev_watch_add(AvahiWatch *w, int fd, AvahiWatchEvent a_events)
{
  short ev_events;

  ev_events = 0;
  if (a_events & AVAHI_WATCH_IN)
    ev_events |= EV_READ;
  if (a_events & AVAHI_WATCH_OUT)
    ev_events |= EV_WRITE;

  event_set(&w->ev, fd, ev_events, evcb_watch, w);
  event_base_set(evbase_main, &w->ev);

  return event_add(&w->ev, NULL);
}
コード例 #19
0
void TimeoutThread::onTimer(int index) {
  Lock l(this);
  ASSERT(mapContains(m_clients, index));
  ClientThread& ct = m_clients[index];
  if (ct.data == nullptr) {
    // The thread has been deleted but we haven't processed it
    // yet. This is ok: just do nothing.
    return;
  }

  event *e = &ct.e;
  event_del(e);

  RequestInjectionData *data = ct.data;
  ASSERT(data);
  struct timeval timeout;
  timeout.tv_usec = 0;
  if (data->started > 0) {
    time_t now = time(0);
    int delta = now - data->started;
    if (delta >= m_timeoutSeconds) {
      timeout.tv_sec = m_timeoutSeconds + 2;
      if (hhvm) {
        Lock l(data->surpriseLock);
        data->setTimedOutFlag();
        if (data->surprisePage) {
          mprotect(data->surprisePage, sizeof(void*), PROT_NONE);
        }
      } else {
        data->setTimedOutFlag();
      }
    } else {
      // Negative delta means start time was adjusted forward to give more time
      if (delta < 0) delta = 0;

      // otherwise, a new request started after we started the timer
      timeout.tv_sec = m_timeoutSeconds - delta + 2;
    }
  } else {
    // Another cycle of m_timeoutSeconds
    timeout.tv_sec = m_timeoutSeconds;
  }

  event_set(e, index, 0, on_timer, this);
  event_base_set(m_eventBase, e);
  event_add(e, &timeout);
}
コード例 #20
0
ファイル: mc-crusher.c プロジェクト: dormando/mc-crusher
static int update_conn_event_sleep(struct connection *c)
{
    struct timeval t = {.tv_sec = 0, .tv_usec = 0};
    struct timeval now;
    if (event_del(&c->ev) == -1) return 0;

    c->ev_flags = 0; // clear event flags in case we ping-pong to other modes
    evtimer_set(&c->ev, sleep_handler, (void *)c);
    event_base_set(c->t->base, &c->ev);

    gettimeofday(&now, NULL);

    // every time we come into this loop, we've run once. which means we
    // always have to advance the next_sleep timer.
    if (c->next_sleep.tv_sec == 0) {
        // initialize next_sleep as late as possible to avoid spamming.
        gettimeofday(&c->next_sleep, NULL);
    }
    memcpy(&t, &c->next_sleep, sizeof(struct timeval));
    timeradd(&t, &c->tosleep, &c->next_sleep);

    timersub(&c->next_sleep, &now, &t);
    // so far as I can tell, it treats times in the past as "Wake up
    // immediately".
    evtimer_add(&c->ev, &t);

    return 1;
}

/* TODO: Be more wary of IOV_MAX */
static void drain_iovecs(struct iovec *vecs, const int iov_count, const int written) {
    int i;
    int todrain = written;
    for (i = 0; i < iov_count; i++) {
        if (vecs[i].iov_len > 0) {
            if (todrain >= vecs[i].iov_len) {
                todrain -= vecs[i].iov_len;
                vecs[i].iov_base = NULL;
                vecs[i].iov_len  = 0;
            } else {
                vecs[i].iov_len -= todrain;
                vecs[i].iov_base += todrain;
                break;
            }
        }
    }
}
コード例 #21
0
ファイル: main.c プロジェクト: phpxin/socketclient
static void *thread_func(void *udata)
{

	/*
	ep_ok = (struct epoll_event *)calloc( MAX_EFDS, sizeof(struct epoll_event));
	int i = 0;
	while(1)
	{
		int isok = epoll_wait(ep_fd, ep_ok, 1, -1) ;
		for( i=0; i<isok; i++)
		{
			if(ep_ok[i].events & EPOLLIN)
			{
				recv_msg();
			}
		}
	}
	
	signal(SIGKILL,sig_func);
	*/
	
	printf("socket client fd is %d \n", *((int *)udata));
	
	struct event_base* client_ebase;
	
	client_ebase = event_base_new();  /* 创建一个 event 的描述符 */
	
	/* 连接注册为新事件 (EV_PERSIST为事件触发后不默认删除)   sizeof(struct event) */
	struct event *pEvRead = (struct event *)malloc( sizeof(struct event) );
	event_set(pEvRead, *((int *)udata), EV_READ|EV_PERSIST, onRead, NULL);
	/*event_set(pEvRead, clientSockFlag, EV_READ|EV_PERSIST, onRead, NULL);*/
	event_base_set(client_ebase, pEvRead);
	int flag = event_add(pEvRead, NULL);
	
	if(flag == -1){
		printf("event_add failed \n");
		perror("event_add");
	}
	
	event_base_dispatch(client_ebase); /* 开始事件循环 */

	


	pthread_exit( NULL );
	
}
コード例 #22
0
ファイル: master.c プロジェクト: cygsxak/memlink
void
mb_conn_destroy_delay(Conn *conn)
{
    if (conn->is_destroy)
        return;
    conn->is_destroy = TRUE;
    event_del(&conn->evt);

    struct timeval tm;
    evtimer_set(&conn->evt, mb_conn_destroy, conn);
    evutil_timerclear(&tm);
    tm.tv_sec = 0;
    event_base_set(conn->base, &conn->evt);
    event_add(&conn->evt, &tm);

    return;
}
コード例 #23
0
ファイル: mc_thread.c プロジェクト: shantanusharma/twemcache
/*
 * Setup a worker thread
 */
static rstatus_t
thread_setup(struct thread_worker *t)
{
    size_t suffix_size;
    rstatus_t status;

    t->base = event_base_new();
    if (t->base == NULL) {
        log_error("event init failed: %s", strerror(errno));
        return MC_ERROR;
    }

    /* listen for notifications from other threads */
    event_set(&t->notify_event, t->notify_receive_fd, EV_READ | EV_PERSIST,
              thread_libevent_process, t);
    event_base_set(t->base, &t->notify_event);

    status = event_add(&t->notify_event, NULL);
    if (status < 0) {
        log_error("event add failed: %s", strerror(errno));
        return MC_ERROR;
    }

    status = thread_setup_stats(t);
    if (status != MC_OK) {
        return status;
    }

    t->kbuf = klog_buf_create();
    if (t->kbuf == NULL) {
        log_error("klog buf create failed: %s", strerror(errno));
        return MC_ENOMEM;
    }

    conn_cq_init(&t->new_cq);

    suffix_size = settings.use_cas ? (CAS_SUFFIX_SIZE + SUFFIX_SIZE + 1) :
                  (SUFFIX_SIZE + 1);
    t->suffix_cache = cache_create("suffix", suffix_size, sizeof(char *));
    if (t->suffix_cache == NULL) {
        log_error("cache create of suffix cache failed: %s", strerror(errno));
        return MC_ENOMEM;
    }

    return MC_OK;
}
コード例 #24
0
void setup_thread(LIBEVENT_WORK_THREAD *me) {
    me->base = event_init();
    if (! me->base) {
        d_log("error Can't allocate event base");
        exit(1);
    }

    /* Listen for notifications from other threads */
    event_set(&me->notify_event, me->notify_read_fd,
              EV_READ | EV_PERSIST, libevent_work_thread, me);
    event_base_set(me->base, &me->notify_event);

    if (event_add(&me->notify_event, 0) == -1) {
        d_log("Can't monitor libevent notify pipe");
        exit(1);
    }
}
コード例 #25
0
void TimeoutThread::run() {
  event_set(&m_eventPipe, m_pipe.getOut(), EV_READ|EV_PERSIST,
            on_thread_stop, m_eventBase);
  event_base_set(m_eventBase, &m_eventPipe);
  event_add(&m_eventPipe, NULL);

  while (!m_stopped) {
    checkForNewWorkers();
    event_base_loop(m_eventBase, EVLOOP_ONCE);
    drainPipe();
  }

  for (auto& pair : m_clients) {
    event_del(&pair.second.e);
  }
  event_del(&m_eventPipe);
}
コード例 #26
0
ファイル: connection.c プロジェクト: alex-135/bolt
int
bolt_connection_install_revent(bolt_connection_t *c,
    void (*handler)(int, short, void *))
{
    if (!c->revset) {
        event_set(&c->revent, c->sock,
                  EV_READ|EV_PERSIST, handler, c);
        event_base_set(service->ebase, &c->revent);
        if (event_add(&c->revent, NULL) == -1) {
            bolt_log(BOLT_LOG_ERROR,
                     "Failed to install read event, socket(%d)", c->sock);
            return -1;
        }
        c->revset = 1;
    }
    return 0;
}
コード例 #27
0
ファイル: memcached_light.c プロジェクト: SANL-2015/SANL-2015
/**
 * Callback for accepting new connections
 * @param fd the socket for the server socket
 * @param which identifying the event that occurred (not used)
 * @param arg the connection structure for the server
 */
static void accept_handler(memcached_socket_t fd, short which, void *arg)
{
    (void)which;
    struct connection *server= arg;
    /* accept new client */
    struct sockaddr_storage addr;
    socklen_t addrlen= sizeof(addr);
    memcached_socket_t sock= accept(fd, (struct sockaddr *)&addr, &addrlen);

    if (sock == INVALID_SOCKET)
    {
        perror("Failed to accept client");
        return ;
    }

#ifndef WIN32
    if (sock >= maxconns)
    {
        (void)fprintf(stderr, "Client outside socket range (specified with -c)\n");
        closesocket(sock);
        return ;
    }
#endif

    struct memcached_protocol_client_st* c;
    c= memcached_protocol_create_client(server->userdata, sock);
    if (c == NULL)
    {
        (void)fprintf(stderr, "Failed to create client\n");
        closesocket(sock);
    }
    else
    {
        struct connection *client = &socket_userdata_map[sock];
        client->userdata= c;

        event_set(&client->event, (intptr_t)sock, EV_READ, drive_client, client);
        event_base_set(event_base, &client->event);
        if (event_add(&client->event, 0) == -1)
        {
            (void)fprintf(stderr, "Failed to add event for %d\n", sock);
            memcached_protocol_client_destroy(c);
            closesocket(sock);
        }
    }
}
コード例 #28
0
ファイル: test.c プロジェクト: weiweikaikai/My_libevent
void signal_test()
{
       // 初始化  
       base=event_init();  
       struct event evsignal;  
	   int arg = SIGINT;
      // 设置信号事件  
        signal_set(&evsignal, SIGINT,handler,&arg);  
      // 添加信号事件  
        event_add(&evsignal, NULL); 
		//设置为base事件
		event_base_set(base, &evsignal); 
        // 事件调度循环  
        event_dispatch();  
	   event_base_free(base);
		
}
コード例 #29
0
/*
 * Set up a thread's information.
 */
static void setup_thread(LIBEVENT_THREAD *me, bool tap) {
    me->type = tap ? TAP : GENERAL;
    me->base = event_init();
    if (! me->base) {
        settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL,
                                        "Can't allocate event base\n");
        exit(1);
    }

    /* Listen for notifications from other threads */
    event_set(&me->notify_event, me->notify[0],
              EV_READ | EV_PERSIST,
              tap ? libevent_tap_process : thread_libevent_process, me);
    event_base_set(me->base, &me->notify_event);

    if (event_add(&me->notify_event, 0) == -1) {
        settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL,
                                        "Can't monitor libevent notify pipe\n");
        exit(1);
    }

    if (!tap) {
        me->new_conn_queue = malloc(sizeof(struct conn_queue));
        if (me->new_conn_queue == NULL) {
            settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL,
                                            "Failed to allocate memory for connection queue");
            exit(EXIT_FAILURE);
        }
        cq_init(me->new_conn_queue);
    }

    if ((pthread_mutex_init(&me->mutex, NULL) != 0)) {
        settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL,
                                        "Failed to initialize mutex: %s\n",
                                        strerror(errno));
        exit(EXIT_FAILURE);
    }

    me->suffix_cache = cache_create("suffix", SUFFIX_SIZE, sizeof(char*),
                                    NULL, NULL);
    if (me->suffix_cache == NULL) {
        settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL,
                                        "Failed to create suffix cache\n");
        exit(EXIT_FAILURE);
    }
}
コード例 #30
0
ファイル: work.c プロジェクト: membase/moxi
/** A work queue is a mechanism to allow thread-to-thread
 *  communication in a libevent-based, multithreaded system.
 *
 *  One thread can send work to another thread.  The receiving thread
 *  should be libevent-based, with a processing loop handled by
 *  libevent.
 *
 *  Use work_queue_init() to initialize a work_queue structure,
 *  where the work_queue structure memory is owned by the caller.
 *
 *  Returns true on success.
 */
bool work_queue_init(work_queue *m, struct event_base *event_base) {
    cb_assert(m != NULL);

    memset(m, 0, sizeof(work_queue));

    cb_mutex_initialize(&m->work_lock);

    m->work_head = NULL;
    m->work_tail = NULL;

    m->num_items = 0;
    m->tot_sends = 0;
    m->tot_recvs = 0;

    m->event_base = event_base;
    cb_assert(m->event_base != NULL);

    if (!create_notification_pipe(m)) {
        return false;
    }

    event_set(&m->event, m->recv_fd,
              EV_READ | EV_PERSIST, work_recv, m);
    event_base_set(m->event_base, &m->event);

    if (event_add(&m->event, 0) == 0) {
#ifdef WORK_DEBUG
            moxi_log_write("work_queue_init %x %x %x %d %d %u %llu\n",
                    (int) pthread_self(),
                    (int) m,
                    (int) m->event_base,
                    m->send_fd,
                    m->recv_fd,
                    m->work_head != NULL,
                    m->tot_sends);
#endif

        return true;
    }

#ifdef WORK_DEBUG
    moxi_log_write("work_queue_init error\n");
#endif

    return false;
}