Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
EliasC committed Jul 18, 2014
2 parents 3940f33 + 34f3452 commit ca56077
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
10 changes: 5 additions & 5 deletions src/runtime/future/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ void yield(pony_actor_t* a) {
suspend(r);
}

inline volatile bool future_fulfilled(future_t *fut) {
inline bool future_fulfilled(future_t *fut) {
return future_actor_get_fulfilled(fut);
}

inline volatile void *future_read_value(future_t *fut) {
inline void *future_read_value(future_t *fut) {
return future_actor_get_value(fut);
}

volatile void *future_get(future_t *fut, pony_actor_t* actor) {
volatile void *result;
void *future_get(future_t *fut, pony_actor_t* actor) {
void *result;
if (future_actor_get_value_and_fulfillment(fut, &result)) {
return result;
}
future_block(fut, actor);
return future_read_value(fut);
}

void future_fulfil(future_t *f, volatile void *value) {
void future_fulfil(future_t *f, void *value) {
future_actor_set_value(f, value);
#ifdef DEBUG_PRINT
fprintf(stderr, "[%p]\t%p <--- fulfil\n", (void *)pthread_self(), f);
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/future/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ typedef enum {
// =============================================================================

future_t *future_mk();
volatile bool future_fulfilled(future_t *future);
volatile void *future_read_value(future_t *future);
void future_fulfil(future_t *future, volatile void *value);
bool future_fulfilled(future_t *future);
void *future_read_value(future_t *future);
void future_fulfil(future_t *future, void *value);

// =============================================================================
// Actor-specific parts of the future library
Expand All @@ -29,7 +29,7 @@ void future_fulfil(future_t *future, volatile void *value);
void future_chain(future_t *future, pony_actor_t* actor_owning_closure, struct closure *closure);
void future_await(future_t *future, pony_actor_t* actor_awaiting_future);
void future_block(future_t *future, pony_actor_t* actor_blocking_on_future);
volatile void *future_get(future_t *future, pony_actor_t* actor_blocking_on_future);
void *future_get(future_t *future, pony_actor_t* actor_blocking_on_future);

// =============================================================================
// Task-specific parts of the future library
Expand Down
17 changes: 8 additions & 9 deletions src/runtime/future/future_actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#define DEBUG_PRINT 1

typedef struct future_actor_fields {
volatile bool fulfilled;
volatile void *value;
bool fulfilled;
void *value;
set_t *blocked;
set_t *chained;
set_t *awaiting;
Expand Down Expand Up @@ -189,8 +189,7 @@ void future_actor_dispatch(pony_actor_t* this, void* p, uint64_t id, int argc, p
}
case FUT_MSG_FULFIL:
{
// Discarding volatile here because it does not matter
volatile void *value;
void *value;
bool fulfilled;
fulfilled = future_actor_get_value_and_fulfillment(this, &value);

Expand Down Expand Up @@ -227,24 +226,24 @@ future_actor_fields *get_fields(pony_actor_t *this) {
return *(future_actor_fields **)this;
}

void future_actor_set_value(pony_actor_t* this, volatile void *value) {
void future_actor_set_value(pony_actor_t* this, void *value) {
future_actor_fields *state = get_fields(this);
state->value = value;
__sync_synchronize();
state->fulfilled = true;
}
volatile bool future_actor_get_value_and_fulfillment(pony_actor_t* this, volatile void **value) {
bool future_actor_get_value_and_fulfillment(pony_actor_t* this, void **value) {
future_actor_fields *state = get_fields(this);
volatile bool fulfilled = state->fulfilled;
bool fulfilled = state->fulfilled;
__sync_synchronize();
*value = state->value;
return fulfilled;
}
volatile bool future_actor_get_fulfilled(pony_actor_t* this) {
bool future_actor_get_fulfilled(pony_actor_t* this) {
future_actor_fields *state = get_fields(this);
return state->fulfilled;
}
volatile void *future_actor_get_value(pony_actor_t* this) {
void *future_actor_get_value(pony_actor_t* this) {
future_actor_fields *state = get_fields(this);
return state->value;
}
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/future/future_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ enum
pony_actor_t* future_create();
pony_msg_t* future_actor_message_type(uint64_t id);
void future_actor_dispatch(pony_actor_t* this, void* p, uint64_t id, int argc, pony_arg_t* argv);
volatile bool future_actor_get_value_and_fulfillment(pony_actor_t* this, volatile void **value);
void future_actor_set_value(pony_actor_t* this, volatile void *value);
volatile bool future_actor_get_fulfilled(pony_actor_t* this);
volatile void *future_actor_get_value(pony_actor_t* this);
bool future_actor_get_value_and_fulfillment(pony_actor_t* this, void **value);
void future_actor_set_value(pony_actor_t* this, void *value);
bool future_actor_get_fulfilled(pony_actor_t* this);
void *future_actor_get_value(pony_actor_t* this);

#endif
20 changes: 11 additions & 9 deletions src/runtime/future/tit_lazy.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static pthread_once_t stack_pool_is_initialized = PTHREAD_ONCE_INIT;
static mpmcq_t proper_stacks_for_reuse;
static mpmcq_t side_stacks_for_reuse;
static __thread lazy_tit_t *current;
static volatile unsigned int cached_side_stacks;
static unsigned int side_stack_cache_size;
static uint32_t cached_side_stacks;
static uint32_t side_stack_cache_size;

static void trampoline_0(lazy_tit_t *new, fun_t_0 fun);
// static void trampoline_1(lazy_tit_t *new, fun_t_1 fun, void *a);
Expand All @@ -47,14 +47,15 @@ static inline void __end_trampoline();

void mk_stack(ucontext_t *fork) {
if (side_stack_cache_size > 0) {
while (fork->uc_stack.ss_sp == NULL && cached_side_stacks > 0) {
while (fork->uc_stack.ss_sp == NULL && __atomic_load_n(&cached_side_stacks, __ATOMIC_CONSUME) > 0) {
fork->uc_stack.ss_sp = mpmcq_pop(&side_stacks_for_reuse);
}
if (fork->uc_stack.ss_sp) {
int cache_size;
uint32_t cache_size;
do {
cache_size = cached_side_stacks;
} while (!__sync_bool_compare_and_swap(&cached_side_stacks, cache_size, cache_size - 1));
cache_size = __atomic_load_n(&cached_side_stacks, __ATOMIC_CONSUME);
} while (!__atomic_compare_exchange_n(&cached_side_stacks, &cache_size, cache_size - 1, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED));
// TODO: check the correctness and optimality of line above
}
}

Expand All @@ -69,10 +70,11 @@ void mk_stack(ucontext_t *fork) {

void return_allocated_stack_to_pool(void *stack_pointer) {
bool free_stack = true;
int current_cache_size;
uint32_t current_cache_size;
do {
current_cache_size = cached_side_stacks;
if (__sync_bool_compare_and_swap(&cached_side_stacks, current_cache_size, current_cache_size + 1)) {
current_cache_size = __atomic_load_n(&cached_side_stacks, __ATOMIC_CONSUME);
// TODO: check the correctness and optimality of line below
if (__atomic_compare_exchange_n(&cached_side_stacks, &current_cache_size, current_cache_size + 1, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {
mpmcq_push(&side_stacks_for_reuse, stack_pointer);
free_stack = false;
break;
Expand Down

0 comments on commit ca56077

Please sign in to comment.