Skip to content

Commit

Permalink
Merge branch 'libevent' into freebsd
Browse files Browse the repository at this point in the history
Conflicts:
	src/onion/poller_libev.c
  • Loading branch information
davidmoreno committed Apr 23, 2013
2 parents bd68270 + ad4efef commit 1107f92
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Expand Up @@ -10,8 +10,8 @@ SET(ONION_USE_PNG true)
SET(ONION_USE_XML2 true)
SET(ONION_USE_SYSTEMD true)
SET(ONION_VERSION 0.4.0)
SET(ONION_USE_LIBEVENT false)
SET(ONION_USE_LIBEV true)
SET(ONION_USE_LIBEVENT true)
SET(ONION_USE_LIBEV false)
########

SET(LIBPATH /usr/lib /usr/local/lib)
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/hello.c
Expand Up @@ -25,7 +25,7 @@ int main(int argc, char **argv){
signal(SIGINT,shutdown_server);
signal(SIGTERM,shutdown_server);

o=onion_new(O_POLL);
o=onion_new(O_POOL);
onion_set_timeout(o, 5000);
onion_set_hostname(o,"0.0.0.0");
onion_url *urls=onion_root_url(o);
Expand Down
15 changes: 14 additions & 1 deletion src/onion/poller_libev.c
Expand Up @@ -25,12 +25,15 @@

#include <ev.h>
#include <stdlib.h>
#include <semaphore.h>

#include "poller.h"
#include "log.h"

struct onion_poller_t{
struct ev_loop *loop;
sem_t sem;
volatile int stop;
};

struct onion_poller_slot_t{
Expand Down Expand Up @@ -85,6 +88,7 @@ void onion_poller_slot_set_type(onion_poller_slot *el, int type){
onion_poller *onion_poller_new(int aprox_n){
onion_poller *ret=calloc(1,sizeof(onion_poller));
ret->loop=ev_default_loop(0);
sem_init(&ret->sem, 0, 1);
return ret;
}

Expand Down Expand Up @@ -124,9 +128,18 @@ int onion_poller_remove(onion_poller *poller, int fd){

/// Do the polling. If on several threads, this is done in every thread.
void onion_poller_poll(onion_poller *poller){
ev_run(poller->loop,0);
ev_default_fork();
ev_loop_fork(poller->loop);

poller->stop=0;
while(!poller->stop){
sem_wait(&poller->sem);
ev_run(poller->loop,EVLOOP_ONESHOT);
sem_post(&poller->sem);
}
}
/// Stops the polling. This only marks the flag, and should be cancelled with pthread_cancel.
void onion_poller_stop(onion_poller *poller){
poller->stop=1;
ev_break(poller->loop, EVBREAK_ALL);
}
14 changes: 11 additions & 3 deletions src/onion/poller_libevent.c
Expand Up @@ -26,12 +26,15 @@
#include <event2/event.h>
#include <event2/thread.h>
#include <malloc.h>
#include <semaphore.h>

#include "poller.h"
#include "log.h"

struct onion_poller_t{
struct event_base *base;
sem_t sem;
volatile int stop;
};

struct onion_poller_slot_t{
Expand Down Expand Up @@ -85,12 +88,11 @@ void onion_poller_slot_set_type(onion_poller_slot *el, int type){

/// Create a new poller
onion_poller *onion_poller_new(int aprox_n){
#ifdef HAVE_PTHREADS
evthread_use_pthreads();
#endif

onion_poller *ret=calloc(1,sizeof(onion_poller));
ret->base=event_base_new();
sem_init(&ret->sem, 0, 1);
return ret;
}

Expand Down Expand Up @@ -131,9 +133,15 @@ int onion_poller_remove(onion_poller *poller, int fd){

/// Do the polling. If on several threads, this is done in every thread.
void onion_poller_poll(onion_poller *poller){
event_base_loop(poller->base,0);
poller->stop=0;
while(!poller->stop){
sem_wait(&poller->sem);
event_base_loop(poller->base,EVLOOP_ONCE);
sem_post(&poller->sem);
}
}
/// Stops the polling. This only marks the flag, and should be cancelled with pthread_cancel.
void onion_poller_stop(onion_poller *poller){
poller->stop=1;
event_base_loopbreak(poller->base);
}

0 comments on commit 1107f92

Please sign in to comment.