@@ -354,22 +354,6 @@ def write(self, vs):
354
354
assert (all (v == () for v in vs ))
355
355
self .progress += len (vs )
356
356
357
- #### Context-Local Storage
358
-
359
- class ContextLocalStorage :
360
- LENGTH = 1
361
- array : list [int ]
362
-
363
- def __init__ (self ):
364
- self .array = [0 ] * ContextLocalStorage .LENGTH
365
-
366
- def set (self , i , v ):
367
- assert (types_match_values (['i32' ], [v ]))
368
- self .array [i ] = v
369
-
370
- def get (self , i ):
371
- return self .array [i ]
372
-
373
357
#### Waitable State
374
358
375
359
class EventCode (IntEnum ):
@@ -471,7 +455,6 @@ class State(Enum):
471
455
supertask : Optional [Task ]
472
456
on_resolve : Callable [[Optional [list [any ]]], None ]
473
457
num_borrows : int
474
- context : ContextLocalStorage
475
458
476
459
def __init__ (self , opts , inst , ft , supertask , on_resolve ):
477
460
self .state = Task .State .INITIAL
@@ -481,7 +464,6 @@ def __init__(self, opts, inst, ft, supertask, on_resolve):
481
464
self .supertask = supertask
482
465
self .on_resolve = on_resolve
483
466
self .num_borrows = 0
484
- self .context = ContextLocalStorage ()
485
467
486
468
async def enter (self , thread ):
487
469
self .trap_if_on_the_stack (self .inst )
@@ -874,13 +856,19 @@ def drop(self):
874
856
875
857
class Thread :
876
858
task : Task
877
- future : Optional [asyncio .Future ]
859
+ index : int
860
+ context : list [int ]
861
+ future : Optional [Awaitable ]
878
862
on_resume : Optional [asyncio .Future ]
879
863
on_suspend_or_exit : Optional [asyncio .Future ]
880
864
returned : bool
881
865
866
+ CONTEXT_LENGTH = 1
867
+
882
868
def __init__ (self , task , coro ):
883
869
self .task = task
870
+ self .index = task .inst .table .add (self )
871
+ self .context = [0 ] * Thread .CONTEXT_LENGTH
884
872
self .future = None
885
873
self .on_resume = asyncio .Future ()
886
874
self .on_suspend_or_exit = None
@@ -890,6 +878,7 @@ async def async_impl():
890
878
self .on_resume = None
891
879
await coro
892
880
self .on_suspend_or_exit .set_result (None )
881
+ self .task .inst .table .remove (self .index )
893
882
self .returned = True
894
883
asyncio .create_task (async_impl ())
895
884
@@ -917,6 +906,30 @@ async def suspend(self, future) -> Cancelled:
917
906
self .on_resume = None
918
907
return cancelled
919
908
909
+ async def switch (self , other : Thread ) -> Cancelled :
910
+ assert (not self .future and not other .future )
911
+ assert (self .on_suspend_or_exit and not other .on_suspend_or_exit )
912
+ other .on_suspend_or_exit = self .on_suspend_or_exit
913
+ self .on_suspend_or_exit = None
914
+ other .on_resume .set_result (Cancelled .FALSE )
915
+ assert (not self .on_resume )
916
+ self .on_resume = asyncio .Future ()
917
+ cancelled = await self .on_resume
918
+ self .on_resume = None
919
+ return cancelled
920
+
921
+ def yield_ (self , other : Thread ) -> Cancelled :
922
+ # deterministically switch to other, but leave this thread unblocked
923
+ TODO
924
+
925
+ def unblock (self , other : Thread ):
926
+ # unblock other, but deterministically keep running here
927
+ TODO
928
+
929
+ def wait (self ) -> Cancelled :
930
+ # perform just the first half of switch
931
+ TODO
932
+
920
933
#### Store State / Embedding API
921
934
922
935
class Store :
@@ -2096,19 +2109,76 @@ async def canon_resource_rep(rt, thread, i):
2096
2109
trap_if (h .rt is not rt )
2097
2110
return [h .rep ]
2098
2111
2112
+ ### 🧵 `canon thread.index`
2113
+
2114
+ async def canon_thread_index (shared , thread ):
2115
+ assert (not shared )
2116
+ return [thread .index ]
2117
+
2118
+ ### 🧵 `canon thread.new_indirect`
2119
+
2120
+ async def canon_thread_new_indirect (shared , ft , ftbl , thread , i , c ):
2121
+ assert (not shared )
2122
+ inst = thread .task .inst
2123
+ trap_if (not inst .may_leave )
2124
+ f = ftbl .get (i )
2125
+ trap_if (f is None )
2126
+ trap_if (f .type != ft )
2127
+ thread = Thread (thread .task , f (c ))
2128
+ return [thread .index ]
2129
+
2130
+ ### 🧵 `canon thread.switch`
2131
+
2132
+ async def canon_thread_switch (shared , thread , i ):
2133
+ assert (not shared )
2134
+ trap_if (not thread .task .inst .may_leave )
2135
+ other = thread .task .inst .table .get (i )
2136
+ trap_if (not isinstance (other , Thread ))
2137
+ cancelled = await thread .switch (other )
2138
+ return [ 1 if cancelled else 0 ]
2139
+
2140
+ ### 🧵 `canon thread.yield`
2141
+
2142
+ async def canon_thread_yield (shared , thread , i ):
2143
+ assert (not shared )
2144
+ trap_if (not thread .task .inst .may_leave )
2145
+ other = thread .task .inst .table .get (i )
2146
+ trap_if (not isinstance (other , Thread ))
2147
+ other .yield_ (other )
2148
+ return []
2149
+
2150
+ ### 🧵 `canon thread.unblock`
2151
+
2152
+ async def canon_thread_unblock (shared , thread , i ):
2153
+ trap_if (not thread .task .inst .may_leave )
2154
+ other = thread .task .inst .table .get (i )
2155
+ trap_if (not isinstance (other , Thread ))
2156
+ thread .unblock ()
2157
+ return []
2158
+
2159
+ ### 🧵 `canon thread.wait`
2160
+
2161
+ async def canon_thread_wait (shared , thread , i ):
2162
+ assert (not shared )
2163
+ trap_if (not thread .task .inst .may_leave )
2164
+ other = thread .task .inst .table .get (i )
2165
+ trap_if (not isinstance (other , Thread ))
2166
+ cancelled = await thread .suspend ()
2167
+ return [ 1 if cancelled else 0 ]
2168
+
2099
2169
### 🔀 `canon context.get`
2100
2170
2101
2171
async def canon_context_get (t , i , thread ):
2102
2172
assert (t == 'i32' )
2103
- assert (i < ContextLocalStorage . LENGTH )
2104
- return [thread .task . context . get ( i ) ]
2173
+ assert (i < Thread . CONTEXT_LENGTH )
2174
+ return [thread .context [ i ] ]
2105
2175
2106
2176
### 🔀 `canon context.set`
2107
2177
2108
2178
async def canon_context_set (t , i , thread , v ):
2109
2179
assert (t == 'i32' )
2110
- assert (i < ContextLocalStorage . LENGTH )
2111
- thread .task . context . set ( i , v )
2180
+ assert (i < Thread . CONTEXT_LENGTH )
2181
+ thread .context [ i ] = v
2112
2182
return []
2113
2183
2114
2184
### 🔀 `canon backpressure.set`
0 commit comments