Skip to content

Commit 507f881

Browse files
committed
LVar: put now can throw an error
1 parent b6c4947 commit 507f881

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

kklib/include/kklib.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef KKLIB_H
33
#define KKLIB_H
44

5-
#define KKLIB_BUILD 91 // modify on changes to trigger recompilation
5+
#define KKLIB_BUILD 95 // modify on changes to trigger recompilation
66
#define KK_MULTI_THREADED 1 // set to 0 to be used single threaded only
77
// #define KK_DEBUG_FULL 1
88

kklib/include/kklib/thread.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <sys/_types/_int32_t.h>
23
#ifndef KK_THREAD_H
34
#define KK_THREAD_H
45
/*---------------------------------------------------------------------------
@@ -32,7 +33,7 @@ kk_decl_export kk_promise_t kk_task_schedule_n( kk_ssize_t count, kk_ssize_t str
3233
typedef kk_box_t kk_lvar_t;
3334

3435
kk_decl_export kk_lvar_t kk_lvar_alloc(kk_box_t lattice_bottom, kk_context_t *ctx);
35-
kk_decl_export void kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx);
36+
kk_decl_export int32_t kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx);
3637
kk_decl_export kk_box_t kk_lvar_get( kk_lvar_t lvar, kk_function_t in_threshold_set, kk_context_t* ctx );
3738
kk_decl_export kk_box_t kk_lvar_freeze(kk_lvar_t lvar, kk_context_t *ctx);
3839

kklib/src/thread.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ typedef struct lvar_s {
493493
typedef kk_box_t kk_lvar_t;
494494

495495
kk_lvar_t kk_lvar_alloc( kk_box_t lattice_bottom, kk_context_t* ctx );
496-
void kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx);
496+
int32_t kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx);
497497
kk_box_t kk_lvar_get( kk_lvar_t lvar, kk_function_t in_threshold_set, kk_context_t* ctx );
498498
kk_box_t kk_lvar_freeze(kk_lvar_t lvar, kk_context_t *ctx);
499499

@@ -528,34 +528,41 @@ kk_lvar_t kk_lvar_alloc(kk_box_t lattice_bottom, kk_context_t* ctx) {
528528
}
529529

530530
// add value to lvar
531-
void kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx) {
531+
// int32 boolean flag for error
532+
int32_t kk_lvar_put(kk_lvar_t lvar, kk_function_t update, kk_context_t *ctx) {
532533
lvar_t *lv = (lvar_t *)kk_cptr_raw_unbox(lvar);
534+
kk_box_t result;
533535

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

536538
pthread_mutex_lock(&lv->lock);
537-
lv->result =
539+
result = lv->result;
540+
// add some dup stuff I don't understand
541+
result =
538542
kk_function_call(kk_box_t, (kk_function_t, kk_box_t, kk_context_t *),
539-
update, (update, lv->result, ctx));
543+
update, (update, result, ctx));
540544

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

543547
// null means that update went to top
544-
if (kk_box_is_null(lv->result)) goto err;
548+
if (kk_box_is_null(result)) {
549+
pthread_mutex_unlock(&lv->lock);
550+
goto err;
551+
}
545552

553+
lv->result = result;
554+
// drop stuff here?
546555
pthread_mutex_unlock(&lv->lock);
547-
548556
pthread_cond_broadcast(&lv->available);
549557

550558
kk_box_drop(lvar, ctx);
551559
kk_function_drop(update, ctx);
552-
return;
560+
return 1;
553561

554562
err:
555-
// TODO: how to throw an error?
556563
kk_box_drop(lvar, ctx);
557564
kk_function_drop(update, ctx);
558-
return;
565+
return 0;
559566
}
560567

561568
static void kk_lvar_wait (kk_lvar_t lvar, kk_context_t* ctx) {

lib/std/os/lvar.kk

+10-7
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ abstract struct lvar<a> {
2424
compare : (a, a) -> maybe<order>;
2525
}
2626

27-
noinline extern unsafe-lvar( bottom : a ) : pure any {
27+
noinline extern unsafe-lvar( bottom : a ) : total any {
2828
c "kk_lvar_alloc"
2929
}
3030

31-
noinline extern unsafe-put( lv : any, update : a -> null<a>) : pure () {
31+
noinline extern unsafe-put( lv : any, update : a -> null<a>) : total int32 {
3232
c "kk_lvar_put"
3333
}
3434

35-
noinline extern unsafe-get( lv : any, in-threshold-set : (a, int32) -> null<a> ) : pure a {
35+
noinline extern unsafe-get( lv : any, in-threshold-set : (a, int32) -> null<a> ) : total a {
3636
c "kk_lvar_get"
3737
}
3838

39-
noinline extern unsafe-freeze( lv : any ) : pure a {
39+
noinline extern unsafe-freeze( lv : any ) : total a {
4040
c "kk_lvar_freeze"
4141
}
4242

43-
public noinline fun lvar( bottom : a, join : (a, a) -> maybe<a>, compare : (a, a) -> maybe<order>) : pure lvar<a> {
43+
public noinline fun lvar( bottom : a, join : (a, a) -> maybe<a>, compare : (a, a) -> maybe<order>) : total lvar<a> {
4444
Lvar( unsafe-lvar(bottom), join, compare )
4545
}
4646

@@ -49,8 +49,11 @@ public noinline fun lvar( bottom : a, join : (a, a) -> maybe<a>, compare : (a, a
4949
// d <= update(d) -- monotonicity
5050
// update1(update2(d)) == update2(update1(d)) -- commutativity
5151
//
52-
public fun put( lvar : lvar<a>, update : a -> maybe<a> ) : pure () {
53-
lvar.lv.unsafe-put( fn (d) { d.update.null } )
52+
public fun put( lvar : lvar<a>, update : a -> maybe<a> ) : exn () {
53+
match (lvar.lv.unsafe-put( fn (d) { d.update.null } ).bool) {
54+
True -> ()
55+
False -> throw("LVar was frozen when `put` was attempted")
56+
}
5457
}
5558

5659
public fun put( lvar : lvar<a>, d : a ) : pure () {

0 commit comments

Comments
 (0)