Skip to content

Commit

Permalink
fix mistakes in C and throw an error from Koka
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberglot committed Oct 15, 2021
1 parent 6c8d8f7 commit e312a0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions kklib/src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,19 +530,24 @@ kk_lvar_t kk_lvar_alloc(kk_box_t lattice_bottom, kk_context_t* ctx) {
// add value to lvar
void kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx) {
lvar_t *lv = (lvar_t *)kk_cptr_raw_unbox(lvar);
kk_box_t result = null;

if (lv->is_frozen != 0) goto err;

pthread_mutex_lock(&lv->lock);
lv->result =
result =
kk_function_call(kk_box_t, (kk_function_t, kk_box_t, kk_context_t *),
update, (update, lv->result, ctx));

kk_box_mark_shared(lv->result, ctx); // TODO: can we mark outside the mutex?

// null means that update went to top
if (kk_box_is_null(lv->result)) goto err;
if (kk_box_is_null(result)) {
pthread_mutex_unlock(&lv->lock);
goto err;
}

lv->result = result;
pthread_mutex_unlock(&lv->lock);

pthread_cond_broadcast(&lv->available);
Expand Down Expand Up @@ -594,12 +599,12 @@ kk_box_t kk_lvar_get( kk_lvar_t lvar, kk_function_t in_threshold_set, kk_context
kk_lvar_wait(lvar, ctx);
pthread_mutex_unlock(&lv->lock);
}

kk_function_drop(in_threshold_set, ctx);
kk_box_drop(lvar,ctx);
return result;

err:
// get should block forever in this situation
// but maybe we can do something better?
pthread_mutex_lock(&lv->lock);
pthread_cond_wait(&lv->available, &lv->lock);
return kk_box_null;
Expand Down
7 changes: 5 additions & 2 deletions lib/std/os/lvar.kk
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ public fun put( lvar : lvar<a>, d : a ) : pure () {
lvar.put( fn (d') { lvar.join(d, d') } )
}

public fun get( lvar : lvar<a>, in-threshold-set : (a, bool) -> maybe<a> ) : pure a {
lvar.lv.unsafe-get( fn (d, frz) { in-threshold-set(d, frz.bool).null } )
public fun get( lvar : lvar<a>, in-threshold-set : (a, bool) -> maybe<a> ) : exn a {
match (lvar.lv.unsafe-get( fn (d, frz) { in-threshold-set(d, frz.bool).null } )) {
Nothing -> throw("Threshold set could not be reached")
Just a -> a
}
}

// Variant of `get` which assumes every value in the threshold set is unfrozen.
Expand Down

0 comments on commit e312a0d

Please sign in to comment.