├── .gitignore ├── .travis.yml ├── AUTHORS ├── LICENSE ├── MANIFEST.in ├── README.rst ├── atomic └── __init__.py ├── docs ├── Makefile ├── conf.py ├── index.rst ├── installation.rst ├── make.bat └── usage.rst ├── setup.py └── tests ├── __init__.py └── test_atomic.py /.gitignore: -------------------------------------------------------------------------------- 1 | .AppleDouble 2 | *.pyc 3 | :2e_* 4 | *.tmproj 5 | .*.swp 6 | build 7 | dist 8 | MANIFEST 9 | docs/_build/ 10 | *.egg-info 11 | .coverage 12 | coverage/ 13 | .tox/ 14 | .DS_Store 15 | .idea 16 | .venv 17 | __pycache__ 18 | *.so 19 | *$py.class 20 | .vagrant 21 | Vagrantfile 22 | *.egg 23 | .eggs 24 | .vscode 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.7" 4 | - "3.3" 5 | - "pypy" 6 | before_install: 7 | - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test 8 | - sudo apt-get update -qq 9 | - sudo apt-get install -qq gcc-4.7 10 | - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 20 11 | script: python setup.py test 12 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | These people have provided bug fixes, new features, improved the documentation 2 | or just made atomic more awesome. 3 | 4 | * Patrick Strawderman 5 | * Timothée Peignier 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (©) 2012 Timothée Peignier 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include docs/* 2 | include AUTHORS 3 | include LICENSE 4 | include README.rst 5 | include atomic/com/lapanthere/atomic/* -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ====== 2 | Atomic 3 | ====== 4 | 5 | An atomic class that guarantees atomic updates to its contained value. :: 6 | 7 | from atomic import AtomicLong 8 | atomic = AtomicLong(0) 9 | atomic += 1 10 | atomic.value 11 | 12 | 13 | Installation 14 | ============ 15 | 16 | To install atomic, use pip : :: 17 | 18 | pip install atomic 19 | 20 | 21 | Acknowledgement 22 | =============== 23 | 24 | This is heavily inspired by `ruby-atomic `_. 25 | -------------------------------------------------------------------------------- /atomic/__init__.py: -------------------------------------------------------------------------------- 1 | from functools import total_ordering 2 | 3 | from cffi import FFI 4 | 5 | 6 | ffi = FFI() 7 | 8 | ffi.cdef(""" 9 | void long_store(long *, long *); 10 | long long_add_and_fetch(long *, long); 11 | long long_sub_and_fetch(long *, long); 12 | long long_get_and_set(long *, long); 13 | long long_compare_and_set(long *, long *, long); 14 | """) 15 | 16 | atomic = ffi.verify(""" 17 | void long_store(long *v, long *n) { 18 | __atomic_store(v, n, __ATOMIC_SEQ_CST); 19 | }; 20 | long long_add_and_fetch(long *v, long i) { 21 | return __atomic_add_fetch(v, i, __ATOMIC_SEQ_CST); 22 | }; 23 | long long_sub_and_fetch(long *v, long i) { 24 | return __atomic_sub_fetch(v, i, __ATOMIC_SEQ_CST); 25 | }; 26 | long long_get_and_set(long *v, long n) { 27 | return __atomic_exchange_n(v, n, __ATOMIC_SEQ_CST); 28 | }; 29 | long long_compare_and_set(long *v, long *e, long n) { 30 | return __atomic_compare_exchange_n(v, e, n, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); 31 | }; 32 | """) 33 | 34 | 35 | @total_ordering 36 | class AtomicLong(object): 37 | """ 38 | An atomic class that guarantees atomic updates to its contained integer value. 39 | """ 40 | def __init__(self, value=None): 41 | """ 42 | Creates a new AtomicLong with the given initial value. 43 | 44 | :param value: initial value 45 | """ 46 | self._value = ffi.new('long *', value) 47 | 48 | def __repr__(self): 49 | return '<{0} at 0x{1:x}: {2!r}>'.format( 50 | self.__class__.__name__, id(self), self.value) 51 | 52 | @property 53 | def value(self): 54 | return self._value[0] 55 | 56 | @value.setter 57 | def value(self, new): 58 | atomic.long_store(self._value, ffi.new('long *', new)) 59 | 60 | def __iadd__(self, inc): 61 | atomic.long_add_and_fetch(self._value, inc) 62 | return self 63 | 64 | def __isub__(self, dec): 65 | atomic.long_sub_and_fetch(self._value, dec) 66 | return self 67 | 68 | def get_and_set(self, new_value): 69 | """Atomically sets to the given value and returns the old value 70 | 71 | :param new_value: the new value 72 | """ 73 | return atomic.long_get_and_set(self._value, new_value) 74 | 75 | def swap(self, new_value): 76 | return self.get_and_set(new_value) 77 | 78 | def compare_and_set(self, expect_value, new_value): 79 | """ 80 | Atomically sets the value to the given value if the current value is 81 | equal to the expected value. 82 | 83 | :param expect_value: the expected value 84 | :param new_value: the new value 85 | """ 86 | return bool(atomic.long_compare_and_set(self._value, ffi.new('long *', expect_value), new_value)) 87 | 88 | def compare_and_swap(self, expect_value, new_value): 89 | return self.compare_and_set(expect_value, new_value) 90 | 91 | def __eq__(self, a): 92 | if self is a: 93 | return True 94 | elif isinstance(a, AtomicLong): 95 | return self.value == a.value 96 | else: 97 | return self.value == a 98 | 99 | def __ne__(self, a): 100 | return not (self == a) 101 | 102 | def __lt__(self, a): 103 | if self is a: 104 | return False 105 | elif isinstance(a, AtomicLong): 106 | return self.value < a.value 107 | else: 108 | return self.value < a 109 | 110 | 111 | class AtomicLongArray(object): 112 | """ 113 | An atomic class that guarantees atomic updates to its contained integer values. 114 | """ 115 | def __init__(self, array=[]): 116 | """ 117 | Creates a new AtomicLongArray with the given initial array of integers. 118 | 119 | :param array: initial values 120 | """ 121 | self._array = [AtomicLong(x) for x in array] 122 | 123 | def __repr__(self): 124 | return '<{0} at 0x{1:x}: {2!r}>'.format( 125 | self.__class__.__name__, id(self), self.value) 126 | 127 | def __len__(self): 128 | return len(self._array) 129 | 130 | def __getitem__(self, key): 131 | return self._array[key] 132 | 133 | def __setitem__(self, key, value): 134 | if isinstance(value, AtomicLong): 135 | self._array[key] = value 136 | else: 137 | self._array[key].value = value 138 | 139 | def __iter__(self): 140 | for a in self._array: 141 | yield a.value 142 | 143 | @property 144 | def value(self): 145 | return [a.value for a in self._array] 146 | 147 | @value.setter 148 | def value(self, new=[]): 149 | self._array = [AtomicLong(int(x)) for x in new] 150 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | 15 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest 16 | 17 | help: 18 | @echo "Please use \`make ' where is one of" 19 | @echo " html to make standalone HTML files" 20 | @echo " dirhtml to make HTML files named index.html in directories" 21 | @echo " singlehtml to make a single large HTML file" 22 | @echo " pickle to make pickle files" 23 | @echo " json to make JSON files" 24 | @echo " htmlhelp to make HTML files and a HTML help project" 25 | @echo " qthelp to make HTML files and a qthelp project" 26 | @echo " devhelp to make HTML files and a Devhelp project" 27 | @echo " epub to make an epub" 28 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 29 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 30 | @echo " text to make text files" 31 | @echo " man to make manual pages" 32 | @echo " changes to make an overview of all changed/added/deprecated items" 33 | @echo " linkcheck to check all external links for integrity" 34 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 35 | 36 | clean: 37 | -rm -rf $(BUILDDIR)/* 38 | 39 | html: 40 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 41 | @echo 42 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 43 | 44 | dirhtml: 45 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 48 | 49 | singlehtml: 50 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 51 | @echo 52 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 53 | 54 | pickle: 55 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 56 | @echo 57 | @echo "Build finished; now you can process the pickle files." 58 | 59 | json: 60 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 61 | @echo 62 | @echo "Build finished; now you can process the JSON files." 63 | 64 | htmlhelp: 65 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 66 | @echo 67 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 68 | ".hhp project file in $(BUILDDIR)/htmlhelp." 69 | 70 | qthelp: 71 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 72 | @echo 73 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 74 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 75 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/atomic.qhcp" 76 | @echo "To view the help file:" 77 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/atomic.qhc" 78 | 79 | devhelp: 80 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 81 | @echo 82 | @echo "Build finished." 83 | @echo "To view the help file:" 84 | @echo "# mkdir -p $$HOME/.local/share/devhelp/atomic" 85 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/atomic" 86 | @echo "# devhelp" 87 | 88 | epub: 89 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 90 | @echo 91 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 92 | 93 | latex: 94 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 95 | @echo 96 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 97 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 98 | "(use \`make latexpdf' here to do that automatically)." 99 | 100 | latexpdf: 101 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 102 | @echo "Running LaTeX files through pdflatex..." 103 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 104 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 105 | 106 | text: 107 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 108 | @echo 109 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 110 | 111 | man: 112 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 113 | @echo 114 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 115 | 116 | changes: 117 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 118 | @echo 119 | @echo "The overview file is in $(BUILDDIR)/changes." 120 | 121 | linkcheck: 122 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 123 | @echo 124 | @echo "Link check complete; look for any errors in the above output " \ 125 | "or in $(BUILDDIR)/linkcheck/output.txt." 126 | 127 | doctest: 128 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 129 | @echo "Testing of doctests in the sources finished, look at the " \ 130 | "results in $(BUILDDIR)/doctest/output.txt." 131 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # atomic documentation build configuration file, created by 4 | # sphinx-quickstart on Thu Mar 22 11:39:06 2012. 5 | # 6 | # This file is execfile()d with the current directory set to its containing dir. 7 | # 8 | # Note that not all possible configuration values are present in this 9 | # autogenerated file. 10 | # 11 | # All configuration values have a default; values that are commented out 12 | # serve to show the default. 13 | 14 | import sys, os 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | sys.path.insert(0, os.path.abspath('..')) 20 | 21 | # -- General configuration ----------------------------------------------------- 22 | 23 | # If your documentation needs a minimal Sphinx version, state it here. 24 | #needs_sphinx = '1.0' 25 | 26 | # Add any Sphinx extension module names here, as strings. They can be extensions 27 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 28 | extensions = ['sphinx.ext.autodoc'] 29 | 30 | # Add any paths that contain templates here, relative to this directory. 31 | templates_path = ['_templates'] 32 | 33 | # The suffix of source filenames. 34 | source_suffix = '.rst' 35 | 36 | # The encoding of source files. 37 | #source_encoding = 'utf-8-sig' 38 | 39 | # The master toctree document. 40 | master_doc = 'index' 41 | 42 | # General information about the project. 43 | project = u'atomic' 44 | copyright = u'2012, Timothée Peignier' 45 | 46 | # The version info for the project you're documenting, acts as replacement for 47 | # |version| and |release|, also used in various other places throughout the 48 | # built documents. 49 | # 50 | # The short X.Y version. 51 | version = '0.7' 52 | # The full version, including alpha/beta/rc tags. 53 | release = '0.7.3' 54 | 55 | # The language for content autogenerated by Sphinx. Refer to documentation 56 | # for a list of supported languages. 57 | #language = None 58 | 59 | # There are two options for replacing |today|: either, you set today to some 60 | # non-false value, then it is used: 61 | #today = '' 62 | # Else, today_fmt is used as the format for a strftime call. 63 | #today_fmt = '%B %d, %Y' 64 | 65 | # List of patterns, relative to source directory, that match files and 66 | # directories to ignore when looking for source files. 67 | exclude_patterns = ['_build'] 68 | 69 | # The reST default role (used for this markup: `text`) to use for all documents. 70 | #default_role = None 71 | 72 | # If true, '()' will be appended to :func: etc. cross-reference text. 73 | #add_function_parentheses = True 74 | 75 | # If true, the current module name will be prepended to all description 76 | # unit titles (such as .. function::). 77 | #add_module_names = True 78 | 79 | # If true, sectionauthor and moduleauthor directives will be shown in the 80 | # output. They are ignored by default. 81 | #show_authors = False 82 | 83 | # The name of the Pygments (syntax highlighting) style to use. 84 | pygments_style = 'sphinx' 85 | 86 | # A list of ignored prefixes for module index sorting. 87 | #modindex_common_prefix = [] 88 | 89 | 90 | # -- Options for HTML output --------------------------------------------------- 91 | 92 | # The theme to use for HTML and HTML Help pages. See the documentation for 93 | # a list of builtin themes. 94 | html_theme = 'default' 95 | 96 | # Theme options are theme-specific and customize the look and feel of a theme 97 | # further. For a list of options available for each theme, see the 98 | # documentation. 99 | #html_theme_options = {} 100 | 101 | # Add any paths that contain custom themes here, relative to this directory. 102 | #html_theme_path = [] 103 | 104 | # The name for this set of Sphinx documents. If None, it defaults to 105 | # " v documentation". 106 | #html_title = None 107 | 108 | # A shorter title for the navigation bar. Default is the same as html_title. 109 | #html_short_title = None 110 | 111 | # The name of an image file (relative to this directory) to place at the top 112 | # of the sidebar. 113 | #html_logo = None 114 | 115 | # The name of an image file (within the static path) to use as favicon of the 116 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 117 | # pixels large. 118 | #html_favicon = None 119 | 120 | # Add any paths that contain custom static files (such as style sheets) here, 121 | # relative to this directory. They are copied after the builtin static files, 122 | # so a file named "default.css" will overwrite the builtin "default.css". 123 | html_static_path = ['_static'] 124 | 125 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 126 | # using the given strftime format. 127 | #html_last_updated_fmt = '%b %d, %Y' 128 | 129 | # If true, SmartyPants will be used to convert quotes and dashes to 130 | # typographically correct entities. 131 | #html_use_smartypants = True 132 | 133 | # Custom sidebar templates, maps document names to template names. 134 | #html_sidebars = {} 135 | 136 | # Additional templates that should be rendered to pages, maps page names to 137 | # template names. 138 | #html_additional_pages = {} 139 | 140 | # If false, no module index is generated. 141 | #html_domain_indices = True 142 | 143 | # If false, no index is generated. 144 | #html_use_index = True 145 | 146 | # If true, the index is split into individual pages for each letter. 147 | #html_split_index = False 148 | 149 | # If true, links to the reST sources are added to the pages. 150 | #html_show_sourcelink = True 151 | 152 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 153 | #html_show_sphinx = True 154 | 155 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 156 | #html_show_copyright = True 157 | 158 | # If true, an OpenSearch description file will be output, and all pages will 159 | # contain a tag referring to it. The value of this option must be the 160 | # base URL from which the finished HTML is served. 161 | #html_use_opensearch = '' 162 | 163 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 164 | #html_file_suffix = None 165 | 166 | # Output file base name for HTML help builder. 167 | htmlhelp_basename = 'atomicdoc' 168 | 169 | 170 | # -- Options for LaTeX output -------------------------------------------------- 171 | 172 | # The paper size ('letter' or 'a4'). 173 | #latex_paper_size = 'letter' 174 | 175 | # The font size ('10pt', '11pt' or '12pt'). 176 | #latex_font_size = '10pt' 177 | 178 | # Grouping the document tree into LaTeX files. List of tuples 179 | # (source start file, target name, title, author, documentclass [howto/manual]). 180 | latex_documents = [ 181 | ('index', 'atomic.tex', u'atomic Documentation', 182 | u'Timothee Peignier', 'manual'), 183 | ] 184 | 185 | # The name of an image file (relative to this directory) to place at the top of 186 | # the title page. 187 | #latex_logo = None 188 | 189 | # For "manual" documents, if this is true, then toplevel headings are parts, 190 | # not chapters. 191 | #latex_use_parts = False 192 | 193 | # If true, show page references after internal links. 194 | #latex_show_pagerefs = False 195 | 196 | # If true, show URL addresses after external links. 197 | #latex_show_urls = False 198 | 199 | # Additional stuff for the LaTeX preamble. 200 | #latex_preamble = '' 201 | 202 | # Documents to append as an appendix to all manuals. 203 | #latex_appendices = [] 204 | 205 | # If false, no module index is generated. 206 | #latex_domain_indices = True 207 | 208 | 209 | # -- Options for manual page output -------------------------------------------- 210 | 211 | # One entry per manual page. List of tuples 212 | # (source start file, name, description, authors, manual section). 213 | man_pages = [ 214 | ('index', 'atomic', u'atomic Documentation', 215 | [u'Timothee Peignier'], 1) 216 | ] 217 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | An atomic class that guarantees atomic updates to its contained value. 5 | 6 | You can report bugs and discuss features on the `issues page `_. 7 | 8 | Table Of Contents 9 | ================= 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | installation 15 | usage 16 | 17 | Indices and tables 18 | ================== 19 | 20 | * :ref:`genindex` 21 | * :ref:`modindex` 22 | * :ref:`search` 23 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | .. _ref-installation: 2 | 3 | ============ 4 | Installation 5 | ============ 6 | 7 | 1. Either check out Atomic from GitHub_ or to pull a release off PyPI_ :: 8 | 9 | pip install atomic 10 | 11 | 12 | .. _GitHub: http://github.com/cyberdelia/atomic 13 | .. _PyPI: http://pypi.python.org/pypi/atomic 14 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | if NOT "%PAPER%" == "" ( 11 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 12 | ) 13 | 14 | if "%1" == "" goto help 15 | 16 | if "%1" == "help" ( 17 | :help 18 | echo.Please use `make ^` where ^ is one of 19 | echo. html to make standalone HTML files 20 | echo. dirhtml to make HTML files named index.html in directories 21 | echo. singlehtml to make a single large HTML file 22 | echo. pickle to make pickle files 23 | echo. json to make JSON files 24 | echo. htmlhelp to make HTML files and a HTML help project 25 | echo. qthelp to make HTML files and a qthelp project 26 | echo. devhelp to make HTML files and a Devhelp project 27 | echo. epub to make an epub 28 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 29 | echo. text to make text files 30 | echo. man to make manual pages 31 | echo. changes to make an overview over all changed/added/deprecated items 32 | echo. linkcheck to check all external links for integrity 33 | echo. doctest to run all doctests embedded in the documentation if enabled 34 | goto end 35 | ) 36 | 37 | if "%1" == "clean" ( 38 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 39 | del /q /s %BUILDDIR%\* 40 | goto end 41 | ) 42 | 43 | if "%1" == "html" ( 44 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 45 | if errorlevel 1 exit /b 1 46 | echo. 47 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 48 | goto end 49 | ) 50 | 51 | if "%1" == "dirhtml" ( 52 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 53 | if errorlevel 1 exit /b 1 54 | echo. 55 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 56 | goto end 57 | ) 58 | 59 | if "%1" == "singlehtml" ( 60 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 61 | if errorlevel 1 exit /b 1 62 | echo. 63 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 64 | goto end 65 | ) 66 | 67 | if "%1" == "pickle" ( 68 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 69 | if errorlevel 1 exit /b 1 70 | echo. 71 | echo.Build finished; now you can process the pickle files. 72 | goto end 73 | ) 74 | 75 | if "%1" == "json" ( 76 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 77 | if errorlevel 1 exit /b 1 78 | echo. 79 | echo.Build finished; now you can process the JSON files. 80 | goto end 81 | ) 82 | 83 | if "%1" == "htmlhelp" ( 84 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 85 | if errorlevel 1 exit /b 1 86 | echo. 87 | echo.Build finished; now you can run HTML Help Workshop with the ^ 88 | .hhp project file in %BUILDDIR%/htmlhelp. 89 | goto end 90 | ) 91 | 92 | if "%1" == "qthelp" ( 93 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 94 | if errorlevel 1 exit /b 1 95 | echo. 96 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 97 | .qhcp project file in %BUILDDIR%/qthelp, like this: 98 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\atomic.qhcp 99 | echo.To view the help file: 100 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\atomic.ghc 101 | goto end 102 | ) 103 | 104 | if "%1" == "devhelp" ( 105 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 106 | if errorlevel 1 exit /b 1 107 | echo. 108 | echo.Build finished. 109 | goto end 110 | ) 111 | 112 | if "%1" == "epub" ( 113 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 117 | goto end 118 | ) 119 | 120 | if "%1" == "latex" ( 121 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 122 | if errorlevel 1 exit /b 1 123 | echo. 124 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 125 | goto end 126 | ) 127 | 128 | if "%1" == "text" ( 129 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 130 | if errorlevel 1 exit /b 1 131 | echo. 132 | echo.Build finished. The text files are in %BUILDDIR%/text. 133 | goto end 134 | ) 135 | 136 | if "%1" == "man" ( 137 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 138 | if errorlevel 1 exit /b 1 139 | echo. 140 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 141 | goto end 142 | ) 143 | 144 | if "%1" == "changes" ( 145 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 146 | if errorlevel 1 exit /b 1 147 | echo. 148 | echo.The overview file is in %BUILDDIR%/changes. 149 | goto end 150 | ) 151 | 152 | if "%1" == "linkcheck" ( 153 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 154 | if errorlevel 1 exit /b 1 155 | echo. 156 | echo.Link check complete; look for any errors in the above output ^ 157 | or in %BUILDDIR%/linkcheck/output.txt. 158 | goto end 159 | ) 160 | 161 | if "%1" == "doctest" ( 162 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 163 | if errorlevel 1 exit /b 1 164 | echo. 165 | echo.Testing of doctests in the sources finished, look at the ^ 166 | results in %BUILDDIR%/doctest/output.txt. 167 | goto end 168 | ) 169 | 170 | :end 171 | -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | .. _ref-usage: 2 | 3 | ===== 4 | Usage 5 | ===== 6 | 7 | .. automodule:: atomic 8 | :members: 9 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import io 3 | import os 4 | 5 | from setuptools.dist import Distribution 6 | from setuptools import setup, find_packages 7 | 8 | try: 9 | from atomic import ffi 10 | except ImportError: 11 | ext_modules=[] 12 | else: 13 | ext_modules=[ffi.verifier.get_extension()] 14 | 15 | class BinaryDistribution(Distribution): 16 | def is_pure(self): 17 | return False 18 | 19 | with io.open('README.rst', encoding='utf-8') as f: 20 | readme = f.read() 21 | 22 | setup( 23 | name='atomic', 24 | version='0.7.3', 25 | description='An atomic class that guarantees atomic updates to its contained value.', 26 | long_description=readme, 27 | author='Timothée Peignier', 28 | author_email='timothee.peignier@tryphon.org', 29 | url='https://github.com/cyberdelia/atomic', 30 | license='MIT', 31 | packages=find_packages(exclude=('tests',)), 32 | zip_safe=False, 33 | include_package_data=True, 34 | classifiers=[ 35 | 'Intended Audience :: Developers', 36 | 'License :: OSI Approved :: MIT License', 37 | 'Operating System :: OS Independent', 38 | 'Programming Language :: Python', 39 | 'Programming Language :: Python :: 3', 40 | 'Topic :: Utilities', 41 | ], 42 | setup_requires=['cffi'], 43 | install_requires=['cffi'], 44 | test_suite="tests", 45 | ext_modules=ext_modules, 46 | distclass=BinaryDistribution, 47 | ) 48 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyberdelia/atomic/43681dfd5799588ac17298f414c7c42c16a30a8b/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_atomic.py: -------------------------------------------------------------------------------- 1 | try: 2 | import unittest2 as unittest 3 | except ImportError: 4 | import unittest # noqa 5 | 6 | from atomic import AtomicLong, AtomicLongArray 7 | 8 | 9 | class AtomicLongTest(unittest.TestCase): 10 | def test_init(self): 11 | atomic = AtomicLong() 12 | self.assertEqual(0, atomic.value) 13 | 14 | atomic = AtomicLong(0) 15 | self.assertEqual(0, atomic.value) 16 | 17 | def test_value(self): 18 | atomic = AtomicLong(0) 19 | atomic.value = 1 20 | self.assertEqual(1, atomic.value) 21 | 22 | def test_swap(self): 23 | atomic = AtomicLong(1000) 24 | swapped = atomic.swap(1001) 25 | self.assertEqual(1001, atomic.value) 26 | self.assertEqual(1000, swapped) 27 | 28 | def test_compare_and_swap(self): 29 | atomic = AtomicLong(1000) 30 | swapped = atomic.compare_and_swap(1000, 1001) 31 | self.assertEqual(1001, atomic.value) 32 | self.assertEqual(True, swapped) 33 | 34 | swapped = atomic.compare_and_swap(1000, 1024) 35 | self.assertEqual(1001, atomic.value) 36 | self.assertEqual(False, swapped) 37 | 38 | def test_add(self): 39 | atomic = AtomicLong(1000) 40 | atomic += 1 41 | self.assertEqual(1001, atomic.value) 42 | 43 | def test_sub(self): 44 | atomic = AtomicLong(1000) 45 | atomic -= 1 46 | self.assertEqual(999, atomic.value) 47 | 48 | class AtomicLongArrayTest(unittest.TestCase): 49 | def test_init(self): 50 | atomic = AtomicLongArray() 51 | self.assertEqual([], atomic.value) 52 | 53 | atomic = AtomicLongArray([-1, 0]) 54 | self.assertEqual([-1, 0], atomic.value) 55 | 56 | def test_value(self): 57 | atomic = AtomicLongArray([]) 58 | atomic.value = [-1, 0] 59 | self.assertEqual([-1, 0], atomic.value) 60 | 61 | def test_get(self): 62 | atomic = AtomicLongArray([-1, 0]) 63 | self.assertEqual(atomic[0], -1) 64 | self.assertEqual(atomic[1], 0) 65 | 66 | def test_set(self): 67 | atomic = AtomicLongArray([-1, 0]) 68 | atomic[1] = -2 69 | self.assertEqual(atomic[1], -2) 70 | 71 | def test_set_obj(self): 72 | atomic = AtomicLongArray([-1, 0]) 73 | atomic[1] = AtomicLong(-2) 74 | self.assertEqual(atomic[1], -2) 75 | 76 | def test_increment(self): 77 | atomic = AtomicLongArray([-1, 0]) 78 | atomic[1] += 1 79 | self.assertEqual(atomic[1], 1) 80 | 81 | def test_iter(self): 82 | atomic = AtomicLongArray([-1, 0]) 83 | for i, a in enumerate(atomic): 84 | self.assertEqual(atomic[i], a) 85 | --------------------------------------------------------------------------------