Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AtomicInt #7

Merged
merged 17 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.18)
project(cereggii-dev)
project(cereggii)


add_subdirectory(src)
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ ignore = [
"T201",
# Import block is un-sorted or un-formatted
"I001",
# Unnecessary `list` / `tuple` / `dict` call (rewrite as a literal)
"C408",
]
unfixable = [
# Don't touch unused imports
Expand Down
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ find_package(Python3 REQUIRED COMPONENTS Development.Module)

# Add the module to compile
Python3_add_library(_cereggii MODULE
"cereggii/cereggii.c"
"cereggii/atomic_ref.c"
"cereggii/atomic_dict/atomic_dict.c"
"cereggii/atomic_dict/blocks.c"
"cereggii/atomic_dict/delete.c"
Expand All @@ -32,6 +30,10 @@ Python3_add_library(_cereggii MODULE
"cereggii/atomic_dict/node_ops.c"
"cereggii/atomic_dict/node_sizes_table.c"
"cereggii/atomic_dict/reservation_buffer.c"
"cereggii/atomic_int/atomic_int.c"
"cereggii/atomic_int/handle.c"
"cereggii/atomic_ref.c"
"cereggii/cereggii.c"
)

# Install the module
Expand Down
2 changes: 2 additions & 0 deletions src/cereggii/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

from .__about__ import __license__, __version__, __version_tuple__ # noqa: F401
from .atomic_dict import AtomicDict # noqa: F401
from .atomic_int import AtomicInt, AtomicIntHandle # noqa: F401
from .atomic_ref import AtomicRef # noqa: F401


if not getattr(sys.flags, "nogil", False):
warnings.warn(
"this library is meant to be used with nogil python: https://github.com/colesbury/nogil", stacklevel=1
Expand Down
56 changes: 51 additions & 5 deletions src/cereggii/_cereggii.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import NewType
from typing import Callable, NewType, SupportsComplex, SupportsFloat, SupportsInt

Key = NewType("Key", object)
Value = NewType("Value", object)
Cancel = NewType("Cancel", object)

Number = SupportsInt | SupportsFloat | SupportsComplex

class AtomicDict:
"""A thread-safe dictionary (hashmap), that's almost-lock-free™."""

Expand Down Expand Up @@ -128,8 +130,52 @@ class AtomicDict:
class AtomicRef:
"""An object reference that may be updated atomically."""

def __init__(self, reference: object): ...
def compare_and_set(self, expected: object, new: object) -> bool: ...
def __init__(self, reference: object | None = None): ...
def compare_and_set(self, expected: object, updated: object) -> bool: ...
def get(self) -> object: ...
def get_and_set(self, new: object) -> object: ...
def set(self, new: object): ... # noqa: A003
def get_and_set(self, updated: object) -> object: ...
def set(self, updated: object): ... # noqa: A003

class AtomicInt(int):
"""An int that may be updated atomically."""

def __init__(self, initial_value: int | None = None): ...
def compare_and_set(self, expected: int, updated: int) -> bool: ...
def get(self) -> int: ...
def get_and_set(self, updated: int) -> int: ...
def set(self, updated: int) -> None: ... # noqa: A003
def increment_and_get(self, amount: int | None = None) -> int: ...
def get_and_increment(self, amount: int | None = None) -> int: ...
def decrement_and_get(self, amount: int | None = None) -> int: ...
def get_and_decrement(self, amount: int | None = None) -> int: ...
def update_and_get(self, other: Callable[[int], int]) -> int: ...
def get_and_update(self, other: Callable[[int], int]) -> int: ...
def get_handle(self) -> AtomicIntHandle: ...
def __itruediv__(self, other):
raise NotImplementedError
def as_integer_ratio(self):
raise NotImplementedError
def bit_length(self):
raise NotImplementedError
def conjugate(self):
raise NotImplementedError
@classmethod
def from_bytes(cls, *args, **kwargs):
raise NotImplementedError
def to_bytes(self, *args, **kwargs):
raise NotImplementedError
@property
def denominator(self):
raise NotImplementedError
@property
def numerator(self):
raise NotImplementedError
@property
def imag(self):
raise NotImplementedError
@property
def real(self):
raise NotImplementedError

class AtomicIntHandle(AtomicInt):
pass
24 changes: 24 additions & 0 deletions src/cereggii/atomic_int/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: 2023-present dpdani <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0

import warnings


try:
from cereggii import _cereggii
except ImportError as exc: # building sdist (without compiled modules)

class AtomicInt:
def __init__(self):
print("dummy")

class AtomicIntHandle:
def __init__(self):
print("dummy")

warnings.warn(str(exc), stacklevel=1) # "UserWarning: No module named 'cereggii'" is expected during sdist build

else:
AtomicInt = _cereggii.AtomicInt
AtomicIntHandle = _cereggii.AtomicIntHandle
Loading
Loading