All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Functions
Asynchronous Event Abstraction

Description

Generic asynchronous events abstraction. Designed to support multiple event libraries such as libev and libuv. Only one library can be supported per build.

Data Structures

struct  as_event_loop
 

Functions

bool as_event_close_loops ()
 
as_event_loopas_event_create_loops (uint32_t capacity)
 
void as_event_destroy_loops ()
 
as_event_loopas_event_loop_find (void *loop)
 
static as_event_loopas_event_loop_get ()
 
static as_event_loopas_event_loop_get_by_index (uint32_t index)
 
as_event_loopas_event_set_external_loop (void *loop)
 
bool as_event_set_external_loop_capacity (uint32_t capacity)
 

Function Documentation

bool as_event_close_loops ( )

Close internal event loops and release watchers for internal and external event loops. The global event loop array will also be destroyed for internal event loops.

This method should be called once on program shutdown if as_event_create_loops() or as_event_set_external_loop_capacity() was called.

The shutdown sequence is slightly different for internal and external event loops.

Internal:

External:

Join on external loop threads.
as_event_destroy_loops();
Returns
True if event loop close was successful. If false, as_event_destroy_loops() should not be called.
as_event_loop* as_event_create_loops ( uint32_t  capacity)

Create new event loops. This method should only be called when asynchronous client commands will be used and the calling program itself is not asynchronous. If this method is used, it must be called before aerospike_connect().

Parameters
capacityNumber of event loops to create.
Returns
Event loop array.
void as_event_destroy_loops ( )

Destroy global event loop array. This function only needs to be called for external event loops.

as_event_loop* as_event_loop_find ( void *  loop)

Find client's event loop abstraction given the external event loop.

Parameters
loopExternal event loop.
Returns
Client's generic event loop abstraction that is used in client async commands. Returns NULL if loop not found.
static as_event_loop* as_event_loop_get ( )
inlinestatic

Retrieve a random event loop using round robin distribution.

Returns
Client's generic event loop abstraction that is used in client async commands.

Definition at line 218 of file as_event.h.

References as_event_loop_current, and as_event_loop::next.

static as_event_loop* as_event_loop_get_by_index ( uint32_t  index)
inlinestatic

Retrieve event loop by array index.

Parameters
indexEvent loop array index.
Returns
Client's generic event loop abstraction that is used in client async commands.

Definition at line 205 of file as_event.h.

References as_event_loop::index.

as_event_loop* as_event_set_external_loop ( void *  loop)

Register an external event loop with the client. This method should be called when the calling program wants to share event loops with the client. This reduces resource usage and can increase performance.

This method must be called in the same thread as the event loop that is being registered.

This method is used in conjunction with as_event_set_external_loop_capacity() to fully define the external loop to the client and obtain a reference the client's event loop abstraction.

struct {
pthread_t thread;
struct ev_loop* loop;
as_event_loop* as_loop;
} my_loop;
static void* my_loop_worker_thread(void* udata) {
struct my_loop* myloop = udata;
myloop->loop = ev_loop_new(EVFLAG_AUTO);
myloop->as_loop = as_event_set_external_loop(myloop->loop);
ev_loop(myloop->loop, 0);
ev_loop_destroy(myloop->loop);
return NULL;
}
int capacity = 8;
struct my_loop* loops = malloc(sizeof(struct my_loop) * capacity);
for (int i = 0; i < capacity; i++) {
struct my_loop* myloop = &loops[i];
return pthread_create(&myloop->thread, NULL, my_loop_worker_thread, myloop) == 0;
}
Parameters
loopExternal event loop.
Returns
Client's generic event loop abstraction that is used in client async commands. Returns NULL if external loop capacity would be exceeded.
bool as_event_set_external_loop_capacity ( uint32_t  capacity)

Set the number of externally created event loops. This method should be called when the calling program wants to share event loops with the client. This reduces resource usage and can increase performance.

This method is used in conjunction with as_event_set_external_loop() to fully define the the external loop to the client and obtain a reference the client's event loop abstraction.

struct {
pthread_t thread;
struct ev_loop* loop;
as_event_loop* as_loop;
} my_loop;
static void* my_loop_worker_thread(void* udata) {
struct my_loop* myloop = udata;
myloop->loop = ev_loop_new(EVFLAG_AUTO);
myloop->as_loop = as_event_set_external_loop(myloop->loop);
ev_loop(myloop->loop, 0);
ev_loop_destroy(myloop->loop);
return NULL;
}
int capacity = 8;
struct my_loop* loops = malloc(sizeof(struct my_loop) * capacity);
for (int i = 0; i < capacity; i++) {
struct my_loop* myloop = &loops[i];
return pthread_create(&myloop->thread, NULL, my_loop_worker_thread, myloop) == 0;
}
Parameters
capacityNumber of externally created event loops.
Returns
True if all external loops were initialized.