Class: Ilios::Cassandra::Future

Inherits:
Object
  • Object
show all
Defined in:
ext/ilios/ilios.c

Instance Method Summary collapse

Instance Method Details

#awaitCassandra::Future

Wait to complete a future’s statement.

Returns:



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'ext/ilios/future.c', line 266

static VALUE future_await(VALUE self)
{
    CassandraFuture *cassandra_future;

    GET_FUTURE(self, cassandra_future);

    if (cassandra_future->already_waited) {
        rb_raise(eExecutionError, "It should not call twice");
    }

    if (cassandra_future->on_success_block || cassandra_future->on_failure_block) {
        nogvl_sem_wait(&cassandra_future->sem);
    } else {
        nogvl_future_wait(cassandra_future->future);
    }
    cassandra_future->already_waited = true;
    return self;
}

#on_failureCassandra::Future

Run block when future resolves to error.

Returns:

Raises:



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ext/ilios/future.c', line 230

static VALUE future_on_failure(VALUE self)
{
    CassandraFuture *cassandra_future;
    bool wakeup_thread = false;

    GET_FUTURE(self, cassandra_future);

    if (cassandra_future->on_failure_block) {
        rb_raise(eExecutionError, "It should not call twice");
    }
    if (!rb_block_given_p()) {
        rb_raise(rb_eArgError, "no block given");
    }

    rb_mutex_lock(cassandra_future->proc_mutex);

    if (!cassandra_future->on_success_block) {
        // Invoke the callback with thread pool only once
        wakeup_thread = true;
    }

    cassandra_future->on_failure_block = rb_block_proc();

    if (wakeup_thread) {
        future_queue_push(future_thread_pool_get(cassandra_future), self);
    }
    rb_mutex_unlock(cassandra_future->proc_mutex);

    return self;
}

#on_success {|value| ... } ⇒ Cassandra::Future

Run block when future resolves to a value.

Yield Parameters:

  • value (Cassandra::Statement, Cassandra::Result)

    A value. Yields Cassandra::Statement object when future was created by Cassandra::Session#prepare_async. Yields Cassandra::Result object when future was created by Cassandra::Session#execute_async.

Returns:

Raises:



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'ext/ilios/future.c', line 192

static VALUE future_on_success(VALUE self)
{
    CassandraFuture *cassandra_future;
    bool wakeup_thread = false;

    GET_FUTURE(self, cassandra_future);

    if (cassandra_future->on_success_block) {
        rb_raise(eExecutionError, "It should not call twice");
    }
    if (!rb_block_given_p()) {
        rb_raise(rb_eArgError, "no block given");
    }

    rb_mutex_lock(cassandra_future->proc_mutex);

    if (!cassandra_future->on_failure_block) {
        // Invoke the callback with thread pool only once
        wakeup_thread = true;
    }

    cassandra_future->on_success_block = rb_block_proc();

    if (wakeup_thread) {
        future_queue_push(future_thread_pool_get(cassandra_future), self);
    }
    rb_mutex_unlock(cassandra_future->proc_mutex);

    return self;
}