Skip to content

Commit

Permalink
Made separate PyronaThread constructor in using.py
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasWrigstad committed Jan 27, 2025
1 parent 1c949a0 commit 7ff6238
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_using.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _():
self.fail()

def test_thread_creation(self):
from threading import Thread as T
from threading import PyronaThread as T

class Mutable: pass
self.assertRaises(RuntimeError, T, kwargs = { 'target' : print, 'args' : (Mutable(),) })
Expand Down
40 changes: 0 additions & 40 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,46 +895,6 @@ class is implemented.
except AttributeError:
pass

# Only check when a program uses pyrona
from sys import getrefcount as rc
# TODO: improve this check for final version of phase 3
# - Revisit the rc checks
# - Consider throwing a different kind of error (e.g. RegionError)
# - Improve error messages
if is_pyrona_program():
def ok_share(o):
if isimmutable(o):
return True
if isinstance(o, Cown):
return True
return False
def ok_move(o):
if isinstance(o, Region):
if rc(o) != 4:
# rc = 4 because:
# 1. ref to o in rc
# 2. ref to o on this frame
# 3. ref to o on the calling frame
# 4. ref to o from kwargs dictionary or args tuple/list
raise RuntimeError("Region passed to thread was not moved into thread")
if o.is_open():
raise RuntimeError("Region passed to thread was open")
return True
return False

for k in kwargs:
# rc(args) == 6 because we need to know that the args list is moved into the thread too
# TODO: Why 6???
v = kwargs[k]
if not (ok_share(v) or (ok_move(v) and rc(kwargs) == 6)):
raise RuntimeError("Thread was passed an object which was neither immutable, a cown, or a unique region")

for a in args:
# rc(args) == 6 because we need to know that the args list is moved into the thread too
# TODO: Why 6???
if not (ok_share(a) or (ok_move(a) and rc(args) == 6)):
raise RuntimeError("Thread was passed an object which was neither immutable, a cown, or a unique region")

self._target = target
self._name = name
self._args = args
Expand Down
53 changes: 53 additions & 0 deletions Lib/using.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,56 @@ def decorator(func):
with CS(cowns, *args):
return func()
return decorator

def PyronaThread(group=None, target=None, name=None,
args=(), kwargs=None, *, daemon=None):
# Only check when a program uses pyrona
from sys import getrefcount as rc
from threading import Thread
# TODO: improve this check for final version of phase 3
# - Revisit the rc checks
# - Consider throwing a different kind of error (e.g. RegionError)
# - Improve error messages
def ok_share(o):
if isimmutable(o):
return True
if isinstance(o, Cown):
return True
return False
def ok_move(o):
if isinstance(o, Region):
if rc(o) != 4:
# rc = 4 because:
# 1. ref to o in rc
# 2. ref to o on this frame
# 3. ref to o on the calling frame
# 4. ref to o from kwargs dictionary or args tuple/list
raise RuntimeError("Region passed to thread was not moved into thread")
if o.is_open():
raise RuntimeError("Region passed to thread was open")
return True
return False

if kwargs is None:
for a in args:
# rc(args) == 3 because we need to know that the args list is moved into the thread too
# rc = 3 because:
# 1. ref to args in rc
# 2. ref to args on this frame
# 3. ref to args on the calling frame
if not (ok_share(a) or (ok_move(a) and rc(args) == 3)):
raise RuntimeError("Thread was passed an object which was neither immutable, a cown, or a unique region")
return Thread(group, target, name, args, daemon)
else:
for k in kwargs:
# rc(args) == 3 because we need to know that keyword dict is moved into the thread too
# rc = 3 because:
# 1. ref to kwargs in rc
# 2. ref to kwargs on this frame
# 3. ref to kwargs on the calling frame
v = kwargs[k]
if not (ok_share(v) or (ok_move(v) and rc(kwargs) == 3)):
raise RuntimeError("Thread was passed an object which was neither immutable, a cown, or a unique region")
return Thread(group, target, name, kwargs, daemon)


0 comments on commit 7ff6238

Please sign in to comment.