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

Description

Generic asynchronous events abstraction. Designed to support multiple event libraries. Only one library is supported per build.

Data Structures

struct  as_event_loop
 
struct  as_policy_event
 

Functions

AS_EXTERN as_status as_create_event_loops (as_error *err, as_policy_event *policy, uint32_t capacity, as_event_loop **event_loops)
 
AS_EXTERN bool as_event_close_loops (void)
 
AS_EXTERN as_event_loopas_event_create_loops (uint32_t capacity)
 
AS_EXTERN void as_event_destroy_loops (void)
 
AS_EXTERN 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)
 
static int as_event_loop_get_process_size (as_event_loop *event_loop)
 
static uint32_t as_event_loop_get_queue_size (as_event_loop *event_loop)
 
AS_EXTERN as_event_loopas_event_set_external_loop (void *loop)
 
AS_EXTERN bool as_event_set_external_loop_capacity (uint32_t capacity)
 
static void as_policy_event_init (as_policy_event *policy)
 
AS_EXTERN as_status as_set_external_event_loop (as_error *err, as_policy_event *policy, void *loop, as_event_loop **event_loop)
 

Function Documentation

AS_EXTERN as_status as_create_event_loops ( as_error err,
as_policy_event policy,
uint32_t  capacity,
as_event_loop **  event_loops 
)

Create new event loops with specified event policy.

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

Parameters
errThe as_error to be populated if an error occurs.
policyEvent loop configuration. Pass in NULL for default configuration.
capacityNumber of event loops to create.
event_loopsCreated event loops. Pass in NULL if event loops do not need to be retrieved.
Returns
AEROSPIKE_OK If successful. Otherwise an error.
AS_EXTERN bool as_event_close_loops ( void  )

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_EXTERN as_event_loop* as_event_create_loops ( uint32_t  capacity)

Create new event loops with default event policy.

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

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

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

AS_EXTERN 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 380 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 367 of file as_event.h.

References as_event_loop::index.

static int as_event_loop_get_process_size ( as_event_loop event_loop)
inlinestatic

Return the approximate number of commands currently being processed on the event loop. The value is approximate because the call may be from a different thread than the event loop’s thread and there are no locks or atomics used.

Definition at line 398 of file as_event.h.

References as_event_loop::pending.

static uint32_t as_event_loop_get_queue_size ( as_event_loop event_loop)
inlinestatic

Return the approximate number of commands stored on this event loop's delay queue that have not been started yet. The value is approximate because the call may be from a different thread than the event loop’s thread and there are no locks or atomics used.

Definition at line 412 of file as_event.h.

References as_queue_size(), and as_event_loop::delay_queue.

AS_EXTERN as_event_loop* as_event_set_external_loop ( void *  loop)

Register an external event loop with the client with default event policy.

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.
AS_EXTERN 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.
static void as_policy_event_init ( as_policy_event policy)
inlinestatic

Initialize event loop configuration variables.

Definition at line 159 of file as_event.h.

References as_policy_event::max_commands_in_process, as_policy_event::max_commands_in_queue, and as_policy_event::queue_initial_capacity.

AS_EXTERN as_status as_set_external_event_loop ( as_error err,
as_policy_event policy,
void *  loop,
as_event_loop **  event_loop 
)

Register an external event loop with the client with specified event policy.

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);
as_error err;
if (as_set_external_event_loop(&err, &policy, myloop->loop, &myloop->as_loop) != AEROSPIKE_OK) {
printf("Failed to set event loop: %d %s\n, err.code, err.message);
return NULL;
}
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);
as_event_set_external_loop_capacity(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
errThe as_error to be populated if an error occurs.
policyEvent loop configuration. Pass in NULL for default configuration.
loopExternal event loop.
event_loopCreated event loop.
Returns
AEROSPIKE_OK If successful. Otherwise an error.