├── .gitignore ├── .travis.deploy.sh ├── .travis.yml ├── Cargo.toml ├── LICENSE ├── LICENSE.LESSER ├── README.md └── src ├── atomic.rs ├── lib.rs └── map_inner.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | /target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | -------------------------------------------------------------------------------- /.travis.deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit -o nounset 4 | 5 | if [ "$TRAVIS_BRANCH" != "master" ] 6 | then 7 | echo "This commit was made against the $TRAVIS_BRANCH and not the master! No deploy!" 8 | exit 0 9 | fi 10 | if [ -z "${GH_TOKEN}" ] ; then 11 | echo "GH_TOKEN is not set. Exiting..." 12 | exit 1 13 | fi 14 | [ "$TRAVIS_RUST_VERSION" = "stable" ] || exit 0 15 | [ "$TRAVIS_PULL_REQUEST" = false ] || exit 0 16 | 17 | rev=$(git rev-parse --short HEAD) 18 | 19 | RELEASE="master" 20 | CRATE_NAME="lockfreehashmap" 21 | COMMIT_COMMENT="Update developer docs for commit ${rev}" 22 | echo "Publishing documentation for $RELEASE" 23 | 24 | if [ ! -d "target/doc/$CRATE_NAME" ]; then 25 | echo "Cannot find target/doc/$CRATE_NAME" 26 | exit 1 27 | fi 28 | 29 | git clone --depth=50 --branch=gh-pages "https://github.com/${TRAVIS_REPO_SLUG}.git" gh-pages 30 | rm -Rf "gh-pages/${RELEASE}" 31 | mkdir "gh-pages/${RELEASE}" 32 | 33 | echo "" > target/doc/index.html 34 | cp -R target/doc/* "gh-pages/${RELEASE}/" 35 | 36 | INDEX="gh-pages/index.html" 37 | echo "${TRAVIS_REPO_SLUG} API documentation" > $INDEX 38 | echo "

API documentation for crate ${CRATE_NAME}

" >> $INDEX 39 | echo "Select a crate version :
" >> $INDEX 40 | for entry in $(ls -1 gh-pages); do 41 | [ -d "gh-pages/$entry" ] && echo "${entry}
" >> $INDEX 42 | done 43 | echo "" >> $INDEX 44 | 45 | cd gh-pages || exit 1 46 | git config user.name "travis-ci" 47 | git config user.email "travis@travis-ci.org" 48 | 49 | git add --all 50 | git commit -m "$COMMIT_COMMENT" 51 | 52 | git push -fq "https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git" gh-pages || exit 1 53 | cd .. 54 | rm -Rf "gh-pages" 55 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - stable 4 | - beta 5 | - nightly 6 | matrix: 7 | allow_failures: 8 | - rust: nightly 9 | fast_finish: true 10 | 11 | cache: cargo 12 | 13 | script: 14 | - | 15 | cargo build && 16 | cargo test && 17 | cargo rustdoc -- --document-private-items 18 | 19 | after_success: 20 | - bash .travis.deploy.sh 21 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lockfreehashmap" 3 | description = "A concurrent, lock-free hash map." 4 | version = "0.1.2" 5 | authors = [" <>"] 6 | license = "LGPL-3.0+" 7 | keywords = ["lock-free", "hashmap", "hash", "concurrent"] 8 | categories = ["concurrency", "data-structures"] 9 | documentation = "https://docs.rs/lockfreehashmap/" 10 | repository = "https://github.com/rolag/lockfreehashmap-rs" 11 | readme = "README.md" 12 | 13 | [lib] 14 | name = "lockfreehashmap" 15 | 16 | [dependencies] 17 | crossbeam-utils = "0.3" 18 | crossbeam-epoch = "0.4.0" 19 | rand = "0.4" 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /LICENSE.LESSER: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | 167 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LockFreeHashMap-rs 2 | 3 | [![License](https://img.shields.io/badge/license-LGPL--3.0+-blue.svg)](https://github.com/rolag/lockfreehashmap) 4 | [![Cargo](https://img.shields.io/crates/v/lockfreehashmap.svg)](https://crates.io/crates/lockfreehashmap) 5 | [![Documentation](https://docs.rs/lockfreehashmap/badge.svg)](https://docs.rs/lockfreehashmap) 6 | [![Continuous Integration](https://api.travis-ci.org/rolag/lockfreehashmap-rs.svg?branch=master)](https://travis-ci.org/rolag/lockfreehashmap-rs) 7 | 8 | A concurrent, lock-free hash map for Rust. 9 | 10 | This is an implementation of the lock-free hash map created by Dr. Cliff Click. 11 | Click released a [talk](https://www.youtube.com/watch?v=HJ-719EGIts) about his hash map. 12 | Additionally, "reference" Java code is available 13 | [here](https://github.com/boundary/high-scale-lib/blob/master/src/main/java/org/cliffc/high_scale_lib/NonBlockingHashMap.java) 14 | and more recently 15 | [here](https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMap.java). 16 | 17 | ## Getting Started 18 | 19 | This crate is available on [crates.io](https://crates.io/crates/lockfreehashmap). 20 | 21 | To use this crate in your project, add the following to your `Cargo.toml` file: 22 | ```toml 23 | [dependencies] 24 | lockfreehashmap = "0.1" 25 | ``` 26 | and then add to your project root file: 27 | ```rust 28 | extern crate lockfreehashmap; 29 | ``` 30 | 31 | ## Example 32 | ```rust 33 | extern crate lockfreehashmap; 34 | use lockfreehashmap::LockFreeHashMap; 35 | 36 | fn main() { 37 | let map = LockFreeHashMap::::new(); 38 | let insert_guard = lockfreehashmap::pin(); 39 | for i in 1..4 { 40 | map.insert(i, i, &insert_guard); 41 | } 42 | drop(insert_guard); 43 | 44 | let map = ↦ 45 | lockfreehashmap::scope(|scope| { 46 | // Spawn multiple threads, e.g. for a server that executes some actions on a loop 47 | for _ in 0..16 { 48 | scope.spawn(|| { 49 | loop { 50 | let mut line = String::new(); 51 | ::std::io::stdin().read_line(&mut line).unwrap(); 52 | let mut iter = line.split_whitespace(); 53 | let command: &str = iter.next().unwrap(); 54 | let key: u8 = iter.next().unwrap().parse().unwrap(); 55 | let value: u8 = iter.next().unwrap().parse().unwrap(); 56 | let guard = lockfreehashmap::pin(); 57 | let _result = match command { 58 | "insert" => map.insert(key, value, &guard), 59 | _ => unimplemented!(), 60 | }; 61 | } 62 | }); 63 | } 64 | }); 65 | } 66 | ``` 67 | 68 | ## Documentation 69 | Documentation is available on [docs.rs](https://docs.rs/lockfreehashmap). 70 | 71 | Developer documentation of private types is available [here](https://rolag.github.io/lockfreehashmap-rs/master/lockfreehashmap/). 72 | 73 | ## Debugging 74 | To use valgrind, add the following lines to the top of the `src/lib.rs` file. 75 | 76 | ```rust 77 | #![feature(alloc_system, global_allocator, allocator_api)] 78 | extern crate alloc_system; 79 | use alloc_system::System; 80 | #[global_allocator] 81 | static A: System = System; 82 | ``` 83 | 84 | Then call `valgrind --leak-check=full --show-leak-kinds=all ./target/debug/deps/lockfreehashmap-*` 85 | 86 | ## License 87 | GNU Lesser General Public License v3.0 or any later version 88 | 89 | See [LICENSE](LICENSE) and [LICENSE.LESSER](LICENSE.LESSER) for details. 90 | -------------------------------------------------------------------------------- /src/atomic.rs: -------------------------------------------------------------------------------- 1 | // LockFreeHashMap -- A concurrent, lock-free hash map for Rust. 2 | // Copyright (C) 2018 rolag 3 | // 4 | // This program is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Lesser General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU Lesser General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU Lesser General Public License 15 | // along with this program. If not, see . 16 | 17 | use crossbeam_epoch::{Atomic, Guard, Owned, Shared}; 18 | use std::fmt; 19 | use std::ops::Deref; 20 | use std::sync::atomic::Ordering; 21 | 22 | pub const ORDERING: Ordering = Ordering::SeqCst; 23 | 24 | /// A wrapper around [Shared], for values that we know are not null and are safe to dereference. 25 | pub struct NotNull<'t, T: 't>(Shared<'t, T>); 26 | 27 | impl<'t, T> NotNull<'t, T> { 28 | /// This is effectively just weakening the guarantees around this type. 29 | pub fn as_maybe_null(&self) -> MaybeNull<'t, T> { 30 | MaybeNull(self.0) 31 | } 32 | pub fn as_shared(&self) -> Shared<'t, T> { 33 | self.0 34 | } 35 | /// Stronger version of `Deref`. This returns `&'t T`, rather than `&'f T`. 36 | pub fn deref<'f>(&'f self) -> &'t T { 37 | // This is safe because 38 | // 1) This type is only created in situations it's guaranteed not to be null 39 | // 2) This type only created from types in this module, which never uses 40 | // `Ordering::Relaxed`. 41 | // 3) `T: 't`, i.e. T outlives 't, so we can return a reference to it 42 | debug_assert!(!self.0.is_null()); 43 | unsafe { self.0.deref() } 44 | } 45 | /// Drop the underlying value behind this pointer. 46 | /// 47 | /// # Unsafe 48 | /// This is unsafe because only one thread can call this for any single underlying value. 49 | pub unsafe fn drop(self) { 50 | let shared = self.0; 51 | debug_assert!(!shared.is_null()); 52 | drop(shared.into_owned()); 53 | } 54 | } 55 | 56 | impl<'t, T: fmt::Debug> fmt::Debug for NotNull<'t, T> { 57 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 58 | write!(f, "{:?}", self.as_maybe_null()) 59 | } 60 | } 61 | 62 | impl<'t, T> Clone for NotNull<'t, T> { 63 | fn clone<'b>(&'b self) -> Self { 64 | NotNull(self.0) 65 | } 66 | } 67 | 68 | impl<'t, T> Copy for NotNull<'t, T> { } 69 | 70 | impl<'t, T> Deref for NotNull<'t, T> { 71 | type Target = T; 72 | fn deref(&self) -> &T { 73 | self.deref() 74 | } 75 | } 76 | 77 | /// A wrapper around [Owned], for values that we know are not null. 78 | /// This is the owned version of [NotNull]. 79 | pub struct NotNullOwned(Owned); 80 | 81 | impl NotNullOwned { 82 | pub fn new(value: T) -> Self { 83 | NotNullOwned(Owned::new(value)) 84 | } 85 | pub fn into_owned(self) -> Owned { 86 | self.0 87 | } 88 | } 89 | 90 | impl fmt::Debug for NotNullOwned { 91 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 92 | write!(f, "{:?}", self.deref()) 93 | } 94 | } 95 | 96 | impl Deref for NotNullOwned { 97 | type Target = T; 98 | fn deref(&self) -> &T { 99 | // This is safe because this is only created from types in this module, which never uses 100 | // `Ordering::Relaxed` (or at least never returns pointers to values retrieved with 101 | // Relaxed). 102 | self.0.deref() 103 | } 104 | } 105 | 106 | 107 | /// Similarly to [NotNull], this is a wrapper around [Shared] for values where the pointer could be 108 | /// null. 109 | pub struct MaybeNull<'t, T: 't>(Shared<'t, T>); 110 | 111 | impl<'t, T> Clone for MaybeNull<'t, T> { 112 | fn clone<'b>(&'b self) -> Self { 113 | MaybeNull(self.0) 114 | } 115 | } 116 | 117 | impl<'t, T> Copy for MaybeNull<'t, T> { } 118 | 119 | 120 | impl<'t, T> MaybeNull<'t, T> { 121 | pub fn from_shared(shared: Shared<'t, T>) -> Self { 122 | MaybeNull(shared) 123 | } 124 | pub fn as_shared(&self) -> Shared { 125 | self.0 126 | } 127 | pub fn as_option(&self) -> Option> { 128 | match self.0.is_null() { 129 | true => None, 130 | false => Some(NotNull(self.0)), 131 | } 132 | } 133 | /// Drop the underlying value behind this pointer if it's not null and after enough epochs have 134 | /// passed. 135 | /// 136 | /// # Unsafe 137 | /// This is unsafe because only one thread can call this for any single underlying value. 138 | pub unsafe fn try_defer_drop(self, guard: &Guard) { 139 | if !self.as_shared().is_null() { 140 | guard.defer(move || { 141 | self.as_shared().into_owned(); 142 | }) 143 | } 144 | } 145 | } 146 | 147 | impl<'t, T: fmt::Debug> fmt::Debug for MaybeNull<'t, T> { 148 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 149 | match self.as_option() { 150 | Some(not_null) => write!(f, "{:?}", not_null.deref()), 151 | None => write!(f, "(Null)"), 152 | } 153 | } 154 | } 155 | 156 | /// Wrapper around [Atomic]. Represents a pointer that can be null at first but never again 157 | /// afterwards. 158 | pub struct AtomicPtr(Atomic); 159 | 160 | impl AtomicPtr { 161 | pub fn new(value: Option) -> Self { 162 | match value { 163 | Some(t) => AtomicPtr(Atomic::new(t)), 164 | None => AtomicPtr(Atomic::null()), 165 | } 166 | } 167 | 168 | pub fn load<'g>(&self, guard: &'g Guard) -> MaybeNull<'g, T> { 169 | MaybeNull(self.0.load(ORDERING, guard)) 170 | } 171 | 172 | /// Check if the pointer is not null (i.e. the value exists), using `Ordering::Relaxed`. 173 | pub fn relaxed_exists<'g>(&self, guard: &'g Guard) -> bool { 174 | // It is safe to use `Ordering::Relaxed` as long as we don't dereference it 175 | !self.0.load(Ordering::Relaxed, guard).is_null() 176 | } 177 | 178 | /// Swaps out the current value, leaving null in its place. Equivalent to [Option::take()]. 179 | /// 180 | /// # Unsafe 181 | /// This is unsafe because there must be no other pointers that can ever access this 182 | /// `AtomicPtr` after this function is called. This is intended to be used for [Drop::drop()]. 183 | pub unsafe fn take<'g>(&self, guard: &'g Guard) -> Option> { 184 | let shared = self.0.swap(Shared::null(), ORDERING, guard); 185 | match shared.is_null() { 186 | true => None, 187 | false => Some(shared.into_owned().into_box()) 188 | } 189 | } 190 | 191 | pub fn compare_and_set<'g>(&self, compare: MaybeNull, set: NotNull<'g, T>, guard: &'g Guard) 192 | -> Result, (MaybeNull<'g, T>, NotNull<'g, T>)> 193 | { 194 | self.0.compare_and_set(compare.as_shared(), set.as_shared(), ORDERING, guard) 195 | .map(|set| NotNull(set)) 196 | .map_err(|e| (MaybeNull(e.current), NotNull(e.new))) 197 | } 198 | 199 | pub fn compare_null_and_set<'g>( 200 | &self, 201 | set: NotNull<'g, T>, 202 | guard: &'g Guard 203 | ) -> Result, (NotNull<'g, T>, NotNull<'g, T>)> 204 | { 205 | self.0.compare_and_set(Shared::null(), set.as_shared(), ORDERING, guard) 206 | .map(|set| NotNull(set)) 207 | .map_err(|e| (NotNull(e.current), NotNull(e.new))) 208 | } 209 | 210 | pub fn compare_null_and_set_owned<'g>( 211 | &self, 212 | set: NotNullOwned, 213 | guard: &'g Guard 214 | ) -> Result, (NotNull<'g, T>, NotNullOwned)> 215 | { 216 | self.0.compare_and_set(Shared::null(), set.into_owned(), ORDERING, guard) 217 | .map(|set| NotNull(set)) 218 | .map_err(|e| (NotNull(e.current), NotNullOwned(e.new))) 219 | } 220 | 221 | pub fn compare_and_set_owned<'g>( 222 | &self, 223 | compare: MaybeNull, 224 | set: NotNullOwned, 225 | guard: &'g Guard 226 | ) -> Result, (MaybeNull<'g, T>, NotNullOwned)> 227 | { 228 | self.0.compare_and_set(compare.as_shared(), set.into_owned(), ORDERING, guard) 229 | .map(|set| NotNull(set)) 230 | .map_err(|e| (MaybeNull(e.current), NotNullOwned(e.new))) 231 | } 232 | 233 | pub fn compare_and_set_owned_weak<'g>( 234 | &self, 235 | compare: MaybeNull, 236 | set: NotNullOwned, 237 | guard: &'g Guard 238 | ) -> Result, (MaybeNull<'g, T>, NotNullOwned)> 239 | { 240 | self.0.compare_and_set_weak(compare.as_shared(), set.into_owned(), ORDERING, guard) 241 | .map(|set| NotNull(set)) 242 | .map_err(|e| (MaybeNull(e.current), NotNullOwned(e.new))) 243 | } 244 | 245 | pub fn tag<'g>(&self, guard: &'g Guard) { 246 | self.0.fetch_or(1, ORDERING, guard); 247 | } 248 | 249 | pub fn is_tagged<'g>(&self, guard: &'g Guard) -> bool { 250 | self.0.fetch_or(0, ORDERING, guard).tag() == 1 251 | } 252 | 253 | pub unsafe fn try_drop(&mut self, guard: &Guard) { 254 | let inner = self.0.swap(Shared::null(), ORDERING, &guard); 255 | if !inner.is_null() { 256 | inner.into_owned(); 257 | } 258 | } 259 | } 260 | 261 | impl fmt::Debug for AtomicPtr { 262 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 263 | let guard = ::pin(); 264 | write!(f, "{:?}", self.load(&guard)) 265 | } 266 | } 267 | 268 | /// Wrapper around [Atomic]. Represents a pointer that can never be null. 269 | pub struct AtomicBox(Atomic); 270 | 271 | impl AtomicBox { 272 | pub fn new(value: T) -> Self { 273 | AtomicBox(Atomic::new(value)) 274 | } 275 | 276 | pub fn load<'g>(&self, guard: &'g Guard) -> NotNull<'g, T> { 277 | NotNull(self.0.load(ORDERING, guard)) 278 | } 279 | 280 | pub fn replace<'g>(&self, value: T) { 281 | let guard = &::pin(); 282 | let contents = self.0.swap(Owned::new(value), ORDERING, &guard); 283 | unsafe { guard.defer(move || contents.into_owned()); } 284 | } 285 | 286 | pub fn compare_and_set_shared<'g>( 287 | &'g self, 288 | compare: NotNull, 289 | set: NotNull<'g, T>, 290 | guard: &'g Guard 291 | ) 292 | -> Result, (NotNull<'g, T>, NotNull)> 293 | { 294 | self.0.compare_and_set(compare.0, set.0, ORDERING, guard) 295 | .map(|set| NotNull(set)) 296 | .map_err(|e| (NotNull(e.current), NotNull(e.new))) 297 | } 298 | } 299 | 300 | impl fmt::Debug for AtomicBox { 301 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 302 | let guard = ::pin(); 303 | write!(f, "{:?}", self.load(&guard).as_maybe_null()) 304 | } 305 | } 306 | 307 | impl Drop for AtomicBox { 308 | fn drop(&mut self) { 309 | let guard = ::pin(); 310 | let inner = self.0.swap(Shared::null(), ORDERING, &guard); 311 | debug_assert!(!inner.is_null()); 312 | if !inner.is_null() { 313 | unsafe { inner.into_owned(); } 314 | } 315 | } 316 | } 317 | 318 | #[cfg(test)] 319 | mod test { 320 | use super::*; 321 | 322 | #[test] 323 | fn test_atomic_ptr() { 324 | let ptr = AtomicPtr::new(None); 325 | let guard = ::pin(); 326 | assert!(!ptr.relaxed_exists(&guard)); 327 | assert!(ptr.load(&guard).as_option().is_none()); 328 | let _ok = ptr.compare_null_and_set_owned(NotNullOwned::new(String::from("first")), &guard); 329 | let err = ptr.compare_null_and_set_owned(NotNullOwned::new(String::from("second")), &guard) 330 | .unwrap_err(); 331 | let current = err.0; 332 | let tried_to_insert = err.1; 333 | assert_eq!(&*tried_to_insert, "second"); 334 | assert_eq!(&*current, "first"); 335 | let _ok = ptr.compare_and_set_owned( 336 | current.as_maybe_null(), NotNullOwned::new(String::from("third")), &guard 337 | ).unwrap(); 338 | unsafe { guard.defer(move || current.drop()); } 339 | assert_eq!(*_ok, "third"); 340 | assert!(!ptr.is_tagged(&guard)); 341 | ptr.tag(&guard); 342 | assert!(ptr.is_tagged(&guard)); 343 | let inner = unsafe { ptr.take(&guard).unwrap() }; 344 | assert_eq!(*inner, "third"); 345 | drop(ptr); 346 | } 347 | } 348 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // LockFreeHashMap -- A concurrent, lock-free hash map for Rust. 2 | // Copyright (C) 2018 rolag 3 | // 4 | // This program is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Lesser General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU Lesser General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU Lesser General Public License 15 | // along with this program. If not, see . 16 | 17 | //! LockFreeHashMap 18 | //! 19 | //! This is an implementation of the lock-free hash map created by Dr. Cliff Click. 20 | //! 21 | //! Originally, this implementation 22 | //! [here](https://github.com/boundary/high-scale-lib/blob/master/src/main/java/org/cliffc/high_scale_lib/NonBlockingHashMap.java) 23 | //! and 24 | //! [recently here](https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/maps/NonBlockingHashMap.java#L770-L770) 25 | //! was created for Java, using garbage collection where necessary. 26 | //! This library is a Rust implementation, using epoch-based memory management to compensate for 27 | //! the lack of garbage collection. 28 | //! The `crossbeam` crate is used for epoch-based memory management. 29 | //! 30 | //! For details on the hash map's design and implementation, see the (private) [map_inner] module. 31 | //! 32 | //! At the time of writing, other concurrent hash maps available don't appear to allow reading and 33 | //! writing at the same time. This map does. 34 | //! Effectively, this map has the same guarantees as having a certain amount of global variables 35 | //! that can be changed atomically. 36 | 37 | 38 | extern crate crossbeam_epoch; 39 | extern crate crossbeam_utils as crossbeam; 40 | 41 | use std::borrow::Borrow; 42 | use std::collections::hash_map::RandomState; 43 | use std::fmt; 44 | use std::hash::{BuildHasher, Hash}; 45 | 46 | mod atomic; 47 | mod map_inner; 48 | 49 | /// Re-export `crossbeam::epoch::pin()` and its return type for convenience. 50 | pub use crossbeam_epoch::{pin, Guard}; 51 | /// Re-export `crossbeam::scope()` and its return type for convenience. 52 | pub use crossbeam::scoped::{scope, Scope}; 53 | 54 | use atomic::AtomicBox; 55 | use map_inner::{KeyCompare, KeySlot, MapInner, Match, PutValue, ValueSlot}; 56 | 57 | pub const COPY_CHUNK_SIZE: usize = 32; 58 | 59 | pub struct LockFreeHashMap<'v, K, V: 'v, S = RandomState> { 60 | /// Points to the newest map (after it's been fully resized). Always non-null. 61 | inner: AtomicBox>, 62 | } 63 | 64 | impl<'guard, 'v: 'guard, K, V, S> LockFreeHashMap<'v,K,V,S> 65 | where K: 'guard + Hash + Eq, 66 | V: PartialEq, 67 | S: 'guard + BuildHasher + Clone, 68 | { 69 | /// The default size of a new `LockFreeHashMap` when created by `LockFreeHashMap::new()`. 70 | pub const DEFAULT_CAPACITY: usize = 8; 71 | 72 | /// Creates an empty `LockFreeHashMap` with the specified capacity, using `hasher` to hash the 73 | /// keys. 74 | /// 75 | /// The hash map will be able to hold at least `capacity` elements without 76 | /// reallocating. If `capacity` is 0, the hash map will use the next power of 2 (i.e. 1). 77 | /// 78 | /// # Examples 79 | /// 80 | /// ``` 81 | /// use lockfreehashmap::LockFreeHashMap; 82 | /// use std::collections::hash_map::RandomState; 83 | /// 84 | /// let s = RandomState::new(); 85 | /// let mut map = LockFreeHashMap::with_capacity_and_hasher(10, s); 86 | /// let guard = lockfreehashmap::pin(); 87 | /// map.insert(1, 2, &guard); 88 | /// ``` 89 | pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self { 90 | LockFreeHashMap { 91 | inner: AtomicBox::new(MapInner::with_capacity_and_hasher(capacity, hasher)) 92 | } 93 | } 94 | 95 | /// Private helper method to load the `inner` field as a &[MapInner]. 96 | pub(crate) fn load_inner<'s: 'guard>(&'s self, guard: &'guard Guard) 97 | -> &'guard MapInner<'v,K,V,S> 98 | { 99 | self.inner.load(&guard).deref() 100 | } 101 | 102 | /// Returns the number of elements the map can hold without reallocating. 103 | /// 104 | /// # Examples 105 | /// ``` 106 | /// # use lockfreehashmap::LockFreeHashMap; 107 | /// let map = LockFreeHashMap::::with_capacity(8); 108 | /// assert_eq!(map.capacity(), 8); 109 | /// ``` 110 | pub fn capacity(&self) -> usize { 111 | let guard = pin(); 112 | self.load_inner(&guard).capacity() 113 | } 114 | 115 | /// Returns the number of elements in the map. 116 | /// 117 | /// # Examples 118 | /// ``` 119 | /// # use lockfreehashmap::*; 120 | /// let map = LockFreeHashMap::::with_capacity(8); 121 | /// assert_eq!(map.capacity(), 8); 122 | /// assert_eq!(map.len(), 0); 123 | /// let guard = lockfreehashmap::pin(); 124 | /// map.insert(5, String::from("five"), &guard); 125 | /// assert_eq!(map.capacity(), 8); 126 | /// assert_eq!(map.len(), 1); 127 | /// ``` 128 | pub fn len(&self) -> usize { 129 | let guard = pin(); 130 | self.load_inner(&guard).len() 131 | } 132 | 133 | /// Clears the entire map. 134 | /// 135 | /// This has the same effects as if calling `LockFreeHashMap::with_capacity()`, but its effects 136 | /// are visible to all threads. Because of this, new memory is always allocated before the old 137 | /// map's memory is dropped. 138 | /// 139 | /// # Examples 140 | /// ``` 141 | /// # use lockfreehashmap::*; 142 | /// let map = LockFreeHashMap::::with_capacity(8); 143 | /// let guard = lockfreehashmap::pin(); 144 | /// map.insert(5, String::from("five"), &guard); 145 | /// assert_eq!(map.capacity(), 8); 146 | /// assert_eq!(map.len(), 1); 147 | /// map.clear(); 148 | /// assert_eq!(map.capacity(), 8); 149 | /// assert_eq!(map.len(), 0); 150 | /// ``` 151 | pub fn clear(&self) { 152 | self.clear_with_capacity(Self::DEFAULT_CAPACITY); 153 | } 154 | 155 | /// Clears the entire map. 156 | /// 157 | /// This has the same effects as if calling `LockFreeHashMap::with_capacity()`, but its effects 158 | /// are visible to all threads. Because of this, new memory is always allocated before the old 159 | /// map's memory is dropped. 160 | /// 161 | /// # Examples 162 | /// ``` 163 | /// # use lockfreehashmap::*; 164 | /// let map = LockFreeHashMap::::with_capacity(8); 165 | /// let guard = lockfreehashmap::pin(); 166 | /// map.insert(5, String::from("five"), &guard); 167 | /// assert_eq!(map.capacity(), 8); 168 | /// assert_eq!(map.len(), 1); 169 | /// map.clear_with_capacity(15); 170 | /// assert_eq!(map.capacity(), 16); 171 | /// assert_eq!(map.len(), 0); 172 | /// ``` 173 | pub fn clear_with_capacity(&self, capacity: usize) { 174 | let guard = pin(); 175 | let hasher = self.load_inner(&guard).clone_hasher(); 176 | let newer_map = MapInner::with_capacity_and_hasher(capacity, hasher); 177 | self.inner.replace(newer_map); 178 | } 179 | 180 | /// Returns true if the map contains a value for the specified key. 181 | /// 182 | /// The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed 183 | /// form must match those for the key type. 184 | /// 185 | /// # Examples 186 | /// ``` 187 | /// # use lockfreehashmap::*; 188 | /// let map = LockFreeHashMap::::new(); 189 | /// assert!(!map.contains_key(&3)); 190 | /// let guard = lockfreehashmap::pin(); 191 | /// map.insert(3, 8934, &guard); 192 | /// assert!(map.contains_key(&3)); 193 | /// map.remove(&3, &guard); 194 | /// assert!(!map.contains_key(&3)); 195 | /// ``` 196 | pub fn contains_key(&self, key: &Q) -> bool 197 | where K: Borrow, 198 | Q: Hash + Eq + PartialEq, 199 | { 200 | let guard = pin(); 201 | self.get(key, &guard).is_some() 202 | } 203 | 204 | /// Returns a reference to the value corresponding to the key. The key may be any borrowed 205 | /// form of the map's key type, but Hash and Eq on the borrowed form must match those for the 206 | /// key type. 207 | /// 208 | /// # Examples 209 | /// ``` 210 | /// # use lockfreehashmap::*; 211 | /// let map = LockFreeHashMap::::new(); 212 | /// let guard = lockfreehashmap::pin(); 213 | /// assert_eq!(map.get(&1, &guard), None); 214 | /// map.insert(1, 15, &guard); 215 | /// assert_eq!(map.get(&1, &guard), Some(&15)); 216 | /// ``` 217 | pub fn get<'s: 'guard, Q: ?Sized>(&'s self, key: &Q, guard: &'guard Guard) -> Option<&'guard V> 218 | where K: Borrow, 219 | Q: Hash + Eq + PartialEq, 220 | { 221 | return self.load_inner(guard).get(key, &self.inner, guard); 222 | } 223 | 224 | /// Inserts a key-value pair into the map. If the map did not have this key present, None is 225 | /// returned. If the map did have this key present, the value is updated, and the old value is 226 | /// returned. The key is not updated, though; this matters for types that can be `==` without 227 | /// being identical. 228 | /// 229 | /// # Examples 230 | /// ``` 231 | /// # use lockfreehashmap::*; 232 | /// let map = LockFreeHashMap::::new(); 233 | /// let guard = lockfreehashmap::pin(); 234 | /// let key = "key".to_string(); 235 | /// let equal_key = "key".to_string(); 236 | /// assert_eq!(key, equal_key); // The keys are equal 237 | /// assert_ne!(&key as *const _, &equal_key as *const _); // But not identical 238 | /// assert_eq!(map.insert(key, "value".to_string(), &guard), None); 239 | /// assert_eq!(map.insert(equal_key, "other".to_string(), &guard), Some(&"value".to_string())); 240 | /// // `map` now contains `key` as its key, rather than `equal_key`. 241 | /// ``` 242 | pub fn insert<'s: 'guard>(&'s self, key: K, value: V, guard: &'guard Guard) 243 | -> Option<&'guard V> 244 | { 245 | let value_slot: Option<&ValueSlot> = self.load_inner(guard).put_if_match( 246 | KeyCompare::new(key), 247 | PutValue::new(value), 248 | Match::Always, 249 | &self.inner, 250 | &guard 251 | ); 252 | return ValueSlot::as_inner(value_slot); 253 | } 254 | 255 | /// Inserts a key-value pair into the map, but only if there is already an existing value that 256 | /// corresponds to the key in the map. If the map did not have this key present, None is 257 | /// returned. If the map did have this key present, the value is updated, and the old value is 258 | /// returned. The key is not updated, though; this matters for types that can be `==` without 259 | /// being identical. 260 | /// 261 | /// # Examples 262 | /// ``` 263 | /// # use lockfreehashmap::*; 264 | /// let map = LockFreeHashMap::::new(); 265 | /// let guard = lockfreehashmap::pin(); 266 | /// assert_eq!(map.replace(&1, 1, &guard), None); 267 | /// assert_eq!(map.replace(&1, 1, &guard), None); 268 | /// assert_eq!(map.insert(1, 1, &guard), None); 269 | /// assert_eq!(map.replace(&1, 3, &guard), Some(&1)); 270 | /// ``` 271 | pub fn replace<'s: 'guard, Q: ?Sized>(&'s self, key: &Q, value: V, guard: &'guard Guard) 272 | -> Option<&'guard V> 273 | where K: Borrow, 274 | Q: Hash + Eq + PartialEq, 275 | { 276 | let value_slot: Option<&ValueSlot> = self.load_inner(guard).put_if_match( 277 | KeyCompare::OnlyCompare(key), 278 | PutValue::new(value), 279 | Match::AnyKeyValuePair, 280 | &self.inner, 281 | &guard 282 | ); 283 | return ValueSlot::as_inner(value_slot); 284 | } 285 | 286 | /// Removes a key from the map, returning the value at the key if the key was previously in the 287 | /// map. The key may be any borrowed form of the map's key type, but Hash and Eq on the 288 | /// borrowed form must match those for the key type. 289 | /// 290 | /// # Examples 291 | /// ``` 292 | /// # use lockfreehashmap::*; 293 | /// let map = LockFreeHashMap::::new(); 294 | /// let guard = lockfreehashmap::pin(); 295 | /// assert_eq!(map.remove(&1, &guard), None); 296 | /// map.insert(1, 1, &guard); 297 | /// assert_eq!(map.remove(&1, &guard), Some(&1)); 298 | /// ``` 299 | pub fn remove<'s: 'guard, Q: ?Sized>(&'s self, key: &Q, guard: &'guard Guard) 300 | -> Option<&'guard V> 301 | where K: Borrow, 302 | Q: Hash + Eq + PartialEq, 303 | { 304 | let value_slot: Option<&ValueSlot> = self.load_inner(guard).put_if_match( 305 | KeyCompare::OnlyCompare(key), 306 | PutValue::new_tombstone(), 307 | Match::Always, 308 | &self.inner, 309 | &guard 310 | ); 311 | return ValueSlot::as_inner(value_slot); 312 | } 313 | 314 | /// Returns an iterator over the keys in the map at one point in time. Any keys 315 | /// inserted or removed after this point in time may or may not be returned by this iterator. 316 | /// 317 | /// # Examples 318 | /// ``` 319 | /// # use lockfreehashmap::*; 320 | /// let map = LockFreeHashMap::::new(); 321 | /// let guard = lockfreehashmap::pin(); 322 | /// map.insert(4, "Four".to_string(), &guard); 323 | /// map.insert(8, "Eight".to_string(), &guard); 324 | /// map.insert(15, "Fifteen".to_string(), &guard); 325 | /// map.insert(16, "Sixteen".to_string(), &guard); 326 | /// map.insert(23, "TwentyThree".to_string(), &guard); 327 | /// map.insert(42, "FortyTwo".to_string(), &guard); 328 | /// 329 | /// let mut keys = map.keys(&guard).cloned().collect::>(); 330 | /// keys.sort(); 331 | /// assert_eq!(vec![4, 8, 15, 16, 23, 42], keys); 332 | /// 333 | /// map.remove(&16, &guard); 334 | /// let mut keys = map.keys(&guard).cloned().collect::>(); 335 | /// keys.sort(); 336 | /// assert_eq!(vec![4, 8, 15, 23, 42], keys); 337 | /// ``` 338 | pub fn keys(&self, guard: &'guard Guard) -> Keys<'guard, 'v, K, V, S> { 339 | let mut inner = self.inner.load(guard); 340 | while let Some(newer_map) = inner.newer_map.load(guard).as_option() { 341 | inner.help_copy(newer_map, true, &self.inner, guard); 342 | inner = self.inner.load(guard); 343 | } 344 | Keys { 345 | position: 0, 346 | guard: guard, 347 | map: inner.deref(), 348 | } 349 | } 350 | } 351 | 352 | impl<'guard, 'v: 'guard, K: Hash + Eq + 'guard, V: PartialEq> LockFreeHashMap<'v,K,V> { 353 | 354 | /// Creates a new `LockFreeHashMap`. 355 | /// 356 | /// # Examples 357 | /// ``` 358 | /// # #![allow(unused_variables)] 359 | /// # use lockfreehashmap::LockFreeHashMap; 360 | /// let map = LockFreeHashMap::::new(); 361 | /// ``` 362 | pub fn new() -> Self { 363 | Self::with_capacity(Self::DEFAULT_CAPACITY) 364 | } 365 | 366 | /// Creates a new `LockFreeHashMap` of a given size. Uses the next power of two if size is not 367 | /// a power of two. 368 | /// 369 | /// # Examples 370 | /// ``` 371 | /// # use lockfreehashmap::LockFreeHashMap; 372 | /// let map = LockFreeHashMap::::with_capacity(12); 373 | /// assert_eq!(map.capacity(), 12usize.next_power_of_two()); 374 | /// assert_eq!(map.capacity(), 16); 375 | /// ``` 376 | pub fn with_capacity(size: usize) -> Self { 377 | LockFreeHashMap { inner: AtomicBox::new(MapInner::with_capacity(size)) } 378 | } 379 | } 380 | 381 | 382 | impl<'v, K, V, S> Drop for LockFreeHashMap<'v, K, V, S> { 383 | fn drop(&mut self) { 384 | let guard = pin(); 385 | // self.inner will be dropped because Drop is implemented on `AtomicBox` 386 | // But if self.inner has pointers to newer maps, then those need to be explicitely dropped. 387 | unsafe { 388 | self.inner.load(&guard).deref().drop_newer_maps(&guard); 389 | } 390 | } 391 | } 392 | 393 | impl<'guard, 'v: 'guard, K: Hash + Eq + fmt::Debug, V: fmt::Debug + PartialEq> 394 | fmt::Debug for LockFreeHashMap<'v,K,V> 395 | { 396 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 397 | let guard = pin(); 398 | write!(f, "LockFreeHashMap {{ {:?} }}", self.load_inner(&guard)) 399 | } 400 | } 401 | 402 | 403 | #[derive(Debug)] 404 | pub struct Keys<'guard, 'v, K, V, S> { 405 | position: usize, 406 | guard: &'guard Guard, 407 | map: &'guard MapInner<'v, K, V, S>, 408 | } 409 | 410 | impl<'guard, 'v, K, V, S> Iterator for Keys<'guard, 'v, K, V, S> { 411 | type Item = &'guard K; 412 | fn next(&mut self) -> Option<&'guard K> { 413 | while self.position < self.map.capacity() { 414 | let (k, v) = self.map.get_at(self.position) 415 | .expect("called Vec::get() at a position less than capacity"); 416 | if let (Some(not_null_k), Some(not_null_v)) = 417 | (k.load(self.guard).as_option(), v.load(self.guard).as_option()) 418 | { 419 | if let &KeySlot::Key(ref k) = not_null_k.deref() { 420 | if not_null_v.is_value() || not_null_v.is_valueprime() { 421 | self.position += 1; 422 | return Some(k); 423 | } 424 | } 425 | } 426 | self.position += 1; 427 | } 428 | return None; 429 | } 430 | } 431 | 432 | 433 | #[cfg(test)] 434 | mod test { 435 | extern crate rand; 436 | use super::*; 437 | use self::rand::Rng; 438 | 439 | #[test] 440 | fn test_basic() { 441 | let map = LockFreeHashMap::::new(); 442 | let test_guard = pin(); 443 | for i in 1..4 { 444 | map.insert(i, i, &test_guard); 445 | } 446 | let map = ↦ 447 | scope(|scope| { 448 | scope.spawn(|| { 449 | let test_guard = pin(); 450 | assert_eq!(map.get(&1, &test_guard), Some(&1)); 451 | }); 452 | scope.spawn(|| { 453 | let test_guard = pin(); 454 | assert_eq!(map.insert(100, 101, &test_guard), None); 455 | }); 456 | scope.spawn(|| { 457 | let test_guard = pin(); 458 | assert_eq!(map.insert(5, 4, &test_guard), None); 459 | }); 460 | scope.spawn(|| { 461 | let test_guard = pin(); 462 | assert_eq!(map.get(&4, &test_guard), None); 463 | assert_eq!(map.insert(3, 4, &test_guard), Some(&3)); 464 | assert_eq!(map.get(&3, &test_guard), Some(&4)); 465 | }); 466 | }); 467 | } 468 | 469 | #[test] 470 | fn test_single_thread() { 471 | for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 16, 17, 31, 32, 33].iter() { 472 | test_single_thread_insert(*i); 473 | } 474 | test_single_thread_insert(256); 475 | } 476 | 477 | fn test_single_thread_insert(size: usize) { 478 | let map = &LockFreeHashMap::::new(); 479 | let test_guard = pin(); 480 | for i in 0..size { 481 | map.insert(i, i.to_string(), &test_guard); 482 | assert_eq!(i + 1, map.len()); 483 | for j in 0..(i+1) { 484 | assert_eq!(Some(&j.to_string()), map.get(&j, &test_guard)); 485 | } 486 | } 487 | let mut one = ""; 488 | if size > 1 { 489 | one = map.get(&1, &test_guard).expect("map should have at least one"); 490 | } 491 | for i in 0..size { 492 | assert_eq!(Some(&i.to_string()), map.get(&i, &test_guard)); 493 | } 494 | if size > 1 { 495 | assert_eq!(one, "1"); 496 | } 497 | } 498 | 499 | 500 | use std::sync::{Arc, Mutex}; 501 | #[derive(Clone)] 502 | pub struct NumberWithDrop { 503 | number: u64, 504 | arcmutex: Arc> 505 | } 506 | impl ::std::hash::Hash for NumberWithDrop { 507 | fn hash(&self, state: &mut H) { 508 | self.number.hash(state); 509 | } 510 | } 511 | impl PartialEq for NumberWithDrop { 512 | fn eq(&self, other: &Self) -> bool { 513 | self.number == other.number 514 | } 515 | } 516 | impl Eq for NumberWithDrop { } 517 | impl Drop for NumberWithDrop { 518 | fn drop(&mut self) { 519 | *self.arcmutex 520 | .lock() 521 | .expect(&format!("NumberWithDrop failed: number={}", self.number)) 522 | += self.number; 523 | } 524 | } 525 | 526 | #[test] 527 | fn test_resize() { 528 | let map = &LockFreeHashMap::>::with_capacity(4); 529 | scope(|scope| { 530 | for i in 1..256 { 531 | scope.spawn(move || { 532 | let guard = pin(); 533 | map.insert(i, Box::new(i), &guard); 534 | let &_i = &**map.get(&i, &guard).expect(&format!("test_resize get {}", i)); 535 | assert_eq!(i, _i); 536 | }); 537 | } 538 | }); 539 | let guard = pin(); 540 | for i in 1..256 { 541 | let &_i = &**map.remove(&i, &guard).expect(&format!("test_resize remove {}", i)); 542 | assert_eq!(i, _i); 543 | } 544 | } 545 | 546 | #[test] 547 | fn test_heavy_usage() { 548 | const NUMBER_OF_KEYS: usize = 100; 549 | const NUMBER_OF_VALUES_PER_KEY: usize = 5; 550 | const NUMBER_OF_THREADS: usize = 30; 551 | const NUMBER_OF_OPERATIONS_PER_THREAD: usize = 1000; 552 | let mut valid_states: Vec<(Box, Vec>)> = Vec::new(); 553 | let mut rng = rand::thread_rng(); 554 | for _ in 0..NUMBER_OF_KEYS { 555 | let key = Box::new(rng.gen()); 556 | let mut valid_values = Vec::new(); 557 | for _ in 0..NUMBER_OF_VALUES_PER_KEY { 558 | valid_values.push(Box::new(rng.gen())); 559 | } 560 | valid_values.sort(); 561 | valid_states.push((key, valid_values)); 562 | } 563 | let valid_states = &valid_states; 564 | let map = &LockFreeHashMap::, Box>::new(); 565 | scope(|scope| { 566 | for _ in 0..NUMBER_OF_THREADS { 567 | scope.spawn(move || { 568 | let mut rng = rand::thread_rng(); 569 | let guard = pin(); 570 | for _ in 0..NUMBER_OF_OPERATIONS_PER_THREAD { 571 | match (rng.gen_range(0, 3), 572 | rng.gen_range::(0, NUMBER_OF_KEYS), 573 | rng.gen_range::(0, NUMBER_OF_VALUES_PER_KEY)) 574 | { 575 | (0, key, value) => if let Some(previous) = map.insert( 576 | valid_states[key].0.clone(), 577 | valid_states[key].1[value].clone(), 578 | &guard) 579 | { 580 | valid_states[key].1.binary_search(previous).expect("test_heavy_usage 0"); 581 | }, 582 | (1, key, _) => if let Some(previous) = map.remove( 583 | &valid_states[key].0, &guard) 584 | { 585 | valid_states[key].1.binary_search(previous).expect("test_heavy_usage 1"); 586 | }, 587 | (2, key, _) => if let Some(get) = map.get( 588 | &valid_states[key].0, &guard) 589 | { 590 | valid_states[key].1.binary_search(get).expect("test_heavy_usage 2"); 591 | }, 592 | _ => unreachable!(), 593 | } 594 | } 595 | }); 596 | } 597 | }); 598 | } 599 | 600 | } 601 | -------------------------------------------------------------------------------- /src/map_inner.rs: -------------------------------------------------------------------------------- 1 | // LockFreeHashMap -- A concurrent, lock-free hash map for Rust. 2 | // Copyright (C) 2018 rolag 3 | // 4 | // This program is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Lesser General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU Lesser General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU Lesser General Public License 15 | // along with this program. If not, see . 16 | 17 | //! This module implements most of the logic behind the [::LockFreeHashMap]. 18 | //! 19 | //! The talk that Dr. Click gave is available [here](https://www.youtube.com/watch?v=HJ-719EGIts). 20 | //! However, the information below should ideally be enough to understand all the necessary code. 21 | //! 22 | //! The Rust standard library has an implementation of a [HashMap](::std::collections::HashMap). 23 | //! However, to use it concurrently (and safely), one must put a [lock](::std::sync::Mutex) on it. 24 | //! This is very inefficient and can lead to deadlocks. 25 | //! In order to make something lock-free, at least one thread has to make progress after some time. 26 | //! One of the main advantages of using lock-free structures is to avoid deadlocks and livelocks. 27 | //! Concurrent algorithms typically make use of the atomic operation "compare and swap" (CAS), 28 | //! which atomically swaps one value for another 29 | //! only if the current value is equal to an expected value. 30 | //! (They can also equivalently use Load Linked/Store Conditional) 31 | //! Considering that a hash map stores a key/value pair (i.e. a key with some associated value), 32 | //! it is important that a key is always associated with a value it's supposed to be associated 33 | //! to. 34 | //! This means avoiding an inconsistent state where you have a key/value pair `(K, V)`, 35 | //! where `V` could never be associated with `K`. 36 | //! Lock-free hash maps have use cases in databases, web caching and large-scale business programs. 37 | //! 38 | //! # Guarantees 39 | //! - This map has the same "guarantees" as simply having some number of global variables 40 | //! that can be updated atomically. 41 | //! - `O(n)` time complexity, like any hash map. 42 | //! 43 | //! # Valid States 44 | //! 45 | //! ## Key for State Diagram 46 | //! | Value | Meaning | 47 | //! | ------ | ----------------------------------------------------------------- | 48 | //! | ∅/null | Empty; No value here. | 49 | //! | K | A key is here. | 50 | //! | X | This key slot is taken because there is a newer table available. | 51 | //! | V | A value is here. | 52 | //! | V' | A value is here but the table is currently being resized. | 53 | //! | T | Value was removed. | 54 | //! 55 | //! ## State Diagram 56 | //! ```text 57 | //! (∅, ∅) ---------> (X, ∅) 58 | //! | 59 | //! | 60 | //! \|/ 61 | //! (K, ∅) ---------> (K, V) <=======> (K, T) 62 | //! | | | 63 | //! | | | 64 | //! | \|/ \|/ 65 | //! | (K, V') -------> (K, X) 66 | //! | /|\ 67 | //! | | 68 | //! └----------------------------------┘ 69 | //! ``` 70 | //! From this diagram, 71 | //! you can see that once a key slot is taken, 72 | //! it will always point to that key and only that key. 73 | //! Therefore, 74 | //! if you have two threads (thread 1, thread 2) 75 | //! inserting `(K1, V1)` and `(K2, V2)` respectively, 76 | //! where `hash(K1) == hash(K2) == 5 (for example)`, 77 | //! and `K1 != K2`, 78 | //! and the array containing the key/value pairs is `(null, null)` at index 5. 79 | //! Then one of the threads, e.g. thread 1, 80 | //! will perform the transition `(null, null) -> (K1, null)` at array index 5, 81 | //! allowing it afterwards to insert its value `V1`, 82 | //! The other thread needs to continue probing (at index 6, 7, ...) to find a different key slot. 83 | //! If another thread (thread 3) already inserted a key `K2` at some index after 5, 84 | //! then obviously thread 2 does not need to insert its key into the map at all. 85 | //! Thus, there are no inconsistent states where you have a value that is paired with a key 86 | //! that it's not associated to. 87 | //! 88 | //! 89 | //! ## Resizing 90 | //! To resize the map, 91 | //! a new bigger array needs to be allocated 92 | //! and all the key/value pairs 93 | //! have to be moved from their current slots in the current array 94 | //! into slots in the new array. 95 | //! Because other threads can call `insert()` and `remove()` while the key/value pairs are moved, 96 | //! there needs to be some way of determining what order this happened in 97 | //! and how to copy the slot into the newer table. 98 | //! This is done by having any calls that try to access the current slot try and help complete 99 | //! the copy if they find a `V'` value. 100 | //! So if a thread calls e.g. `get()` while this is happening, 101 | //! it needs to copy the current slot into the new map before returning. 102 | //! (As an implementation detail, it also helps to copy other slots while it's at it.) 103 | //! 104 | //! The transitions necessary in both the old and new map are shown below: 105 | //! ```text 106 | //! Transition# | [1] | [2] | [3] | [4] 107 | //! Old Map | (K, V) -> (K, V') | | | (K, V') -> (K, X) 108 | //! New Map | | (∅, ∅) -> (K, ∅) | (K, ∅) -> (K, V) | 109 | //! ``` 110 | //! - Transition [1] marks the value slot as being copied. 111 | //! If another thread tries to access it while it's `V'`, 112 | //! then it must help complete copying this key/value pair into the newer map. 113 | //! Note that if V is actually null or `T`, 114 | //! we can simply set it to `X` 115 | //! and skip inserting a tombstone/null value into the newer map, 116 | //! which just wastes a key slot. 117 | //! - Transition [2] and [3] copy the old value into the new map. 118 | //! They are separate states to remind you that separate threads can perform either. 119 | //! If inserting it fails, 120 | //! then we know that some thread didn't care about the current value 121 | //! and just `insert()`ed a new value anyway. 122 | //! This is fine and just means that `V` needs to be deallocated. 123 | //! This is because if the slot wasn't being copied, 124 | //! it would have overridden the current `V` with another, 125 | //! which would have ended up being copied into the newer table afterwards. 126 | //! - Finally, transition [4] completes the copy by marking the valueslot as `X`, 127 | //! meaning there could be something here (or not) 128 | //! but you need to see the newer table to find out. 129 | //! 130 | //! If multiple threads are trying to `replace()` data at index `i` in the map, 131 | //! they have to either do it before transition [1] happens, 132 | //! or after transition [4] happens. 133 | //! The main goal of this data structure is to be completely lock-free/non-blocking. 134 | //! Therefore, instead of looping and waiting until all transitions have finished, 135 | //! which is essentially a blocking algorithm, 136 | //! each thread can help make progress by doing any (or all) of the 4 transitions above. 137 | 138 | use crossbeam_epoch::{Guard, Shared}; 139 | use std::borrow::Borrow; 140 | use std::collections::hash_map::RandomState; 141 | use std::hash::{BuildHasher, Hash, Hasher}; 142 | use std::num::Wrapping; 143 | use std::sync::atomic::{AtomicUsize, Ordering}; 144 | use std::time::Duration; 145 | 146 | use atomic::{AtomicBox, AtomicPtr, MaybeNull, NotNull, NotNullOwned}; 147 | 148 | #[derive(Debug)] 149 | /// The hash map is implemented as an array of key-value pairs, where each key and value can be one 150 | /// of several states. This enum represents the various states that a key can be in, excluding the 151 | /// null/empty state. 152 | pub enum KeySlot { 153 | /// A key has been inserted into table. The key's associated value may or may not have been 154 | /// removed. Once a key is in this state it can't go into any other state. 155 | Key(K), 156 | /// This was an empty slot that is now taken. There is a newer (resized) table that should be 157 | /// used if this key slot was needed. Once a key is in this state it can't go into any other 158 | /// state. 159 | SeeNewTable, 160 | } 161 | 162 | #[derive(Debug, PartialEq)] 163 | /// The hash map is implemented as an array of key-value pairs, where each key and value can be one 164 | /// of several states. This enum represents the various states that a value can be in, excluding 165 | /// the null/empty state. 166 | pub enum ValueSlot<'v, V: 'v> { 167 | /// A value has been inserted into the table. 168 | Value(V), 169 | /// This state represents that a key has been inserted but then removed. 170 | Tombstone, 171 | /// The table is being resized currently and the value here still needs to be inserted into the 172 | /// newer table. 173 | ValuePrime(&'v ValueSlot<'v, V>), 174 | /// This state represents one of two things: 175 | /// 1) This was a `ValueSlot::Tombstone(_)` slot that is now taken. There is a newer 176 | /// (resized) table that should be used if this value slot was needed. 177 | /// 2) This used to be a `ValueSlot::Value(_)` slot that has now been copied into the 178 | /// newer table. 179 | /// This is the final state for any `ValueSlot`. 180 | SeeNewTable, 181 | } 182 | 183 | impl<'v, V> ValueSlot<'v, V> { 184 | /// Returns true if and only if the `ValueSlot` has discriminant `Tombstone`. 185 | pub fn is_tombstone(&self) -> bool { 186 | match self { 187 | &ValueSlot::Tombstone => true, 188 | _ => false, 189 | } 190 | } 191 | 192 | /// Returns true if and only if the `ValueSlot` has discriminant `ValuePrime`. 193 | pub fn is_valueprime(&self) -> bool { 194 | match self { 195 | &ValueSlot::ValuePrime(_) => true, 196 | _ => false, 197 | } 198 | } 199 | 200 | /// Returns true if and only if the `ValueSlot` has discriminant `Value`. 201 | pub fn is_value(&self) -> bool { 202 | match self { 203 | &ValueSlot::Value(_) => true, 204 | _ => false, 205 | } 206 | } 207 | 208 | /// Returns true if and only if the `ValueSlot` has discriminant `SeeNewTable`. 209 | pub fn is_seenewtable(&self) -> bool { 210 | match self { 211 | &ValueSlot::SeeNewTable => true, 212 | _ => false, 213 | } 214 | } 215 | 216 | /// Returns true if and only if the `ValueSlot` has either discriminant `ValuePrime` or 217 | /// `SeeNewTable`. 218 | pub fn is_prime(&self) -> bool { 219 | match self { 220 | &ValueSlot::SeeNewTable | &ValueSlot::ValuePrime(_) => true, 221 | _ => false, 222 | } 223 | } 224 | 225 | /// Return an `Option` reference to the inner value of generic type `V`. 226 | pub fn as_inner(value: Option<&Self>) -> Option<&V> { 227 | match value { 228 | Some(&ValueSlot::Value(ref v)) => Some(&v), 229 | Some(&ValueSlot::ValuePrime(v)) => ValueSlot::as_inner(Some(v)), 230 | _ => None, 231 | } 232 | } 233 | } 234 | 235 | /// Sometimes, when inserting a new value into the hash map, we only want to insert something if 236 | /// the value already matches something. 237 | /// 238 | /// This enum represents what key/value pair to match when searching in `put_if_match()`. 239 | #[derive(Debug)] 240 | pub enum Match { 241 | /// Match if there is no key/value pair in the map 242 | Empty, 243 | /// Match if there is a key/value pair in the map 244 | AnyKeyValuePair, 245 | /// Always match 246 | Always, 247 | } 248 | 249 | /// Sometimes when calling `put_if_match()` we want to insert a key and sometimes we just want to 250 | /// compare it with some variable of type `Q`. This enum represents which one is intended. 251 | pub enum KeyCompare<'k, 'q, K: 'k + Borrow, Q: 'q + ?Sized> { 252 | Owned(NotNullOwned>), 253 | Shared(NotNull<'k, KeySlot>), 254 | OnlyCompare(&'q Q), 255 | } 256 | 257 | impl<'k, 'q, K: Borrow, Q: ?Sized> KeyCompare<'k, 'q, K, Q> { 258 | pub fn new(key: K) -> Self { 259 | KeyCompare::Owned(NotNullOwned::new(KeySlot::Key(key))) 260 | } 261 | /// The purpose of this function is to ultimately get a value of type `&Q`. 262 | /// Because we need to call `deref()` and `borrow()` a few times, we need to put the result of 263 | /// these functions somewhere in order to return a reference. Thus, `QRef` and `QRef2` are 264 | /// introduced as helper types to place these values somewhere. 265 | fn as_qref(&self) -> QRef { 266 | match self { 267 | &KeyCompare::Owned(ref owned) => QRef::Shared(owned), 268 | &KeyCompare::Shared(ref not_null) => QRef::Shared(not_null), 269 | &KeyCompare::OnlyCompare(q) => QRef::Borrow(q), 270 | } 271 | } 272 | } 273 | 274 | /// See `KeyCompare::as_qref()` for the motivation behind this type. 275 | enum QRef<'k, 'q, K: 'k + Borrow, Q: 'q + ?Sized> { 276 | Shared(&'k KeySlot), 277 | Borrow(&'q Q), 278 | } 279 | 280 | impl<'k, 'q, K: Borrow, Q: ?Sized> QRef<'k, 'q, K, Q> { 281 | fn as_qref2(&self) -> QRef2 { 282 | match self { 283 | &QRef::Shared(&KeySlot::Key(ref k)) => QRef2::Shared(k), 284 | &QRef::Shared(&KeySlot::SeeNewTable) => 285 | unreachable!("KeyCompare must contain a `NotNull(KeySlot::Key(K))`"), 286 | &QRef::Borrow(q) => QRef2::Borrow(q), 287 | } 288 | } 289 | } 290 | 291 | /// See `KeyCompare::as_qref()` for the motivation behind this type. 292 | enum QRef2<'k, 'q, K: 'k + Borrow, Q: 'q + ?Sized> { 293 | Shared(&'k K), 294 | Borrow(&'q Q), 295 | } 296 | 297 | impl<'k, 'q, K: Borrow, Q: ?Sized> QRef2<'k, 'q, K, Q> { 298 | fn as_q(&self) -> &Q { 299 | match self { 300 | &QRef2::Shared(k) => k.borrow(), 301 | &QRef2::Borrow(q) => q, 302 | } 303 | } 304 | } 305 | 306 | /// This enum represents the value to insert when calling `put_if_match()`, which is usually owned 307 | /// when called from `LockFreeHashMap` and shared if its been copied from a previous, smaller map. 308 | #[derive(Debug)] 309 | pub enum PutValue<'v, V: 'v> { 310 | Owned(NotNullOwned>), 311 | Shared(NotNull<'v, ValueSlot<'v, V>>), 312 | } 313 | 314 | impl<'v, V: PartialEq> PutValue<'v, V> { 315 | pub fn new(value: V) -> Self { 316 | PutValue::Owned(NotNullOwned::new(ValueSlot::Value(value))) 317 | } 318 | /// Returns a new `PutValue` containing an owned `ValueSlot::Tombstone` value. 319 | pub fn new_tombstone() -> Self { 320 | PutValue::Owned(NotNullOwned::new(ValueSlot::Tombstone)) 321 | } 322 | /// Returns true if and only if the inner `ValueSlot` has discriminant `Tombstone`. 323 | pub fn is_tombstone(&self) -> bool { 324 | match self { 325 | &PutValue::Owned(ref owned) => if let ValueSlot::Tombstone = **owned { 326 | true 327 | } else { 328 | false 329 | }, 330 | &PutValue::Shared(ref not_null) => { 331 | if let &ValueSlot::Tombstone = &**not_null { 332 | true 333 | } else { 334 | false 335 | } 336 | }, 337 | } 338 | } 339 | 340 | pub fn ptr_equals(&self, value: NotNull>) -> bool { 341 | if (&*value as *const _) == self.as_raw() { 342 | true 343 | } else { 344 | false 345 | } 346 | } 347 | 348 | pub fn as_raw(&self) -> *const ValueSlot<'v, V> { 349 | match self { 350 | &PutValue::Owned(ref not_null) => &**not_null as *const _, 351 | &PutValue::Shared(ref not_null) => &**not_null as *const _, 352 | } 353 | } 354 | } 355 | 356 | pub type KVPair<'v, K, V> = (AtomicPtr>, AtomicPtr>); 357 | 358 | /// A map containing a unique, non-resizable array to the Key/Value pairs. If the map needs to be 359 | /// resized, a new `MapInner` must be created and its Key/Value pairs must be copied from this one. 360 | /// Logically, this struct owns its keys and values, and so is responsible for freeing them when 361 | /// dropped. 362 | #[derive(Debug)] 363 | pub struct MapInner<'v, K, V: 'v, S = RandomState> { 364 | /// The key/value pairs in this map, allocated as an array of pairs. 365 | map: Vec>, 366 | /// The amount of key/value pairs in the array, if any. 367 | size: AtomicUsize, 368 | /// Points to the newer map or null if none. 369 | pub(crate) newer_map: AtomicPtr>, 370 | /// Any thread can allocate memory to resize the map and create `newer_map`. Thus, we want to 371 | /// try and limit the amount of allocations done. This is a monotonically increasing count of 372 | /// the number of threads currently trying to allocate a new map, which is used as a heuristic. 373 | /// See its use in the `MapInner::create_newer_map()` function. 374 | resizers_count: AtomicUsize, 375 | /// The number of `::COPY_CHUNK_SIZE = 32` element chunks that some thread has commited to 376 | /// copying to the newer table. Once this reaches `capacity/COPY_CHUNK_SIZE`, we know that the 377 | /// entire map has been copied into the large `newer_map`. 378 | chunks_copied: AtomicUsize, 379 | /// The actual number of key/value pairs that have been copied into the newer map. 380 | slots_copied: AtomicUsize, 381 | /// The hasher used to hash keys. 382 | hash_builder: S, 383 | } 384 | 385 | 386 | impl<'v, K, V, S> MapInner<'v, K, V, S> { 387 | /// The default size of a new `LockFreeHashMap`. 388 | pub const DEFAULT_CAPACITY: usize = ::LockFreeHashMap::<(), (), RandomState>::DEFAULT_CAPACITY; 389 | 390 | /// Returns the capacity of the current map; i.e. the length of the `Vec` storing the key/value 391 | /// pairs. 392 | pub fn capacity(&self) -> usize { 393 | self.map.capacity() 394 | } 395 | 396 | /// Returns the size of the current map at some point in time; i.e. the number of key/value 397 | /// pairs in the map. 398 | pub fn len(&self) -> usize { 399 | self.size.load(Ordering::SeqCst) 400 | } 401 | 402 | pub fn get_at(&self, pos: usize) -> Option<&KVPair<'v, K, V>> { 403 | self.map.get(pos) 404 | } 405 | 406 | /// Drops `self.newer_map` and any newer maps that `self.newer_map` points to. 407 | pub unsafe fn drop_newer_maps(&self, guard: &Guard) { 408 | if let Some(newer_map) = self.newer_map.take(guard) { 409 | newer_map.drop_self_and_newer_maps(guard); 410 | } 411 | } 412 | 413 | /// Drops self, `self.newer_map` and any newer maps that `self.newer_map` points to. 414 | pub unsafe fn drop_self_and_newer_maps(self, guard: &Guard) { 415 | let newer_map = self.newer_map.take(guard); 416 | drop(self); 417 | if let Some(newer_map) = newer_map { 418 | newer_map.drop_self_and_newer_maps(guard); 419 | } 420 | } 421 | } 422 | 423 | impl<'v, K: Hash + Eq, V: PartialEq> MapInner<'v,K,V,RandomState> { 424 | /// Creates a new `MapInner`. Uses the next power of two if size is not a power of two. 425 | pub fn with_capacity(size: usize) -> Self { 426 | MapInner::with_capacity_and_hasher(size, RandomState::new()) 427 | } 428 | } 429 | 430 | impl<'guard, 'v: 'guard, K, V, S> MapInner<'v, K,V,S> 431 | where K: Hash + Eq, 432 | V: PartialEq, 433 | S: BuildHasher + Clone, 434 | { 435 | pub fn with_capacity_and_hasher(size: usize, hasher: S) -> Self { 436 | let size = usize::checked_next_power_of_two(size).unwrap_or(Self::DEFAULT_CAPACITY); 437 | let mut map = Vec::with_capacity(size); 438 | for _ in 0..size { 439 | map.push((AtomicPtr::new(None), AtomicPtr::new(None))); 440 | } 441 | MapInner { 442 | map: map, 443 | size: AtomicUsize::new(0), 444 | newer_map: AtomicPtr::new(None), 445 | resizers_count: AtomicUsize::new(0), 446 | chunks_copied: AtomicUsize::new(0), 447 | slots_copied: AtomicUsize::new(0), 448 | hash_builder: hasher, 449 | } 450 | } 451 | 452 | /// Help copy a small chunk of the map to the `newer_map`. See `::COPY_CHUNK_SIZE` for the 453 | /// default chunk size. 454 | pub fn help_copy( 455 | &self, 456 | newer_map: NotNull, 457 | copy_everything: bool, 458 | outer_map: &AtomicBox, 459 | guard: &'guard Guard, 460 | ) { 461 | /// Checked multiplication that gives an `upper_bound` value if the multiplication exceeds 462 | /// the bound (or if overflow occurs). 463 | fn checked_times(first: usize, second: usize, upper_bound: usize) -> usize { 464 | let result = first * second; 465 | if first != 0 && result/first != second { 466 | upper_bound 467 | } else if result > upper_bound { 468 | upper_bound 469 | } else { 470 | result 471 | } 472 | } 473 | loop { 474 | // `chunks_copied` is an atomic variable that keeps track of which chunk will be copied 475 | // into the newer table. 476 | let chunks_copied = self.chunks_copied.fetch_add(1, Ordering::SeqCst); 477 | let next_chunk = chunks_copied + 1; 478 | // Next find the element-wise lower and upper bounds respectively. 479 | let lower_bound = checked_times(chunks_copied, ::COPY_CHUNK_SIZE, self.capacity()); 480 | let upper_bound = checked_times(next_chunk, ::COPY_CHUNK_SIZE, self.capacity()); 481 | debug_assert!(lower_bound <= upper_bound); 482 | // If they're equal, then we know another thread incremented `chunks_copied` such that 483 | // `(chunks_copied + 1) * COPY_CHUNK_SIZE` will be equal to the size of the current 484 | // map. Therefore, we have nothing left to copy and can return. 485 | if lower_bound >= upper_bound { 486 | // But before we do, we should decrement `chunks_copied` by 1, just to make sure it 487 | // won't overflow. It can still overflow if you have `usize::MAX` amount of threads 488 | // calling `help_copy`, but it will be assumed that this never happens. 489 | self.chunks_copied.fetch_sub(1, Ordering::SeqCst); 490 | // In the rare event that the `newer_map` has finished copying all its elements 491 | // into an even `newer_map`, it can call `promote()` and fail, because it's not the 492 | // current map. Thus, we call it again here, even if some other thread was 493 | // "supposed" to have called it. 494 | self.try_promote(newer_map, 0, outer_map, guard); 495 | return; 496 | } 497 | let mut slots_copied = 0; 498 | // Now because `lower_bound` must be less than `upper_bound`, and since we already 499 | // assumed that any thread that gets some `chunks_copied` MUST then copy all elements 500 | // in that chunk, we MUST do so. Notice that the `..upper_bound` is exclusive, so it 501 | // never exceeds (self.capacity() - 1). 502 | for i in lower_bound..upper_bound { 503 | // Now simply copy_slot() for each element in the chunk of the array that we're 504 | // assigned. 505 | if self.copy_slot(&*newer_map, i, outer_map, guard) { 506 | slots_copied += 1; 507 | } 508 | } 509 | self.try_promote(newer_map, slots_copied, outer_map, guard); 510 | if upper_bound == self.capacity() { 511 | return; 512 | } else if !copy_everything { 513 | // Otherwise, we did not copy everything and there is still more to be done, 514 | // or at least there was more when we last checked `chunks_copied`. If we are not 515 | // required to copy everything, then just return and let some other thread do it. 516 | return; 517 | } 518 | // Otherwise, continue the loop and keep copying until everything is copied. 519 | } 520 | } 521 | 522 | /// Once a `MapInner` has had all its elements copied to its `newer_map` field, 523 | /// the LockFreeHashMap's `inner` field must be promoted so that its effects are visible 524 | /// globally. 525 | pub fn promote( 526 | &self, 527 | new_map: NotNull, 528 | outer_map: &AtomicBox, 529 | guard: &'guard Guard, 530 | ) -> bool 531 | { 532 | // We only have a `&self` reference to the current `MapInner`. Thus, we need to load it 533 | // manually from `outer_map`, which must be passed as a parameter throughout various 534 | // function calls... 535 | let current_map_shared: NotNull<_> = outer_map.load(guard); 536 | let current_map: &MapInner<_,_,_> = &*current_map_shared; 537 | // This appears to be that some other thread already promoted us, or the rare event in 538 | // which we called `promote()` before the previous map called `promote()`. 539 | // Just return here. 540 | if current_map as *const _ != self as *const _ { 541 | return false; 542 | } 543 | match outer_map.compare_and_set_shared(current_map_shared, new_map, guard) { 544 | Ok(_) => { 545 | // We successfully swapped the value of the `AtomicBox` and are therefore 546 | // responsible for freeing the old map's memory. 547 | unsafe { guard.defer(move || current_map_shared.as_shared().into_owned()); } 548 | return true; 549 | }, 550 | Err((_current, _)) => { 551 | debug_assert!(&*_current as *const _ != self as *const _); 552 | // We know that `current_map` was `&self` at some point previously, so some other 553 | // thread successfully promoted the map. 554 | return false; 555 | }, 556 | } 557 | } 558 | 559 | fn ensure_slot_copied( 560 | &self, 561 | copy_index: usize, 562 | outer_map: &AtomicBox, 563 | guard: &'guard Guard, 564 | ) -> NotNull<'guard, Self> { 565 | let newer_map_shared = self.newer_map.load(&guard); 566 | if let Some(new_map) = newer_map_shared.as_option() { 567 | if self.copy_slot(&*new_map, copy_index, outer_map, guard) { 568 | self.slots_copied.fetch_add(1, Ordering::SeqCst); 569 | } 570 | self.help_copy(new_map, false, outer_map, guard); 571 | new_map 572 | } else { 573 | unreachable!("can't call ensure_slot_copied() unless found a prime value"); 574 | } 575 | } 576 | 577 | pub fn try_promote( 578 | &self, 579 | new_map: NotNull, 580 | current_slots_copied: usize, 581 | outer_map: &AtomicBox, 582 | guard: &'guard Guard 583 | ) -> bool 584 | { 585 | let previous_slots_copied = self.slots_copied.fetch_add(current_slots_copied, Ordering::SeqCst); 586 | if current_slots_copied > 0 { 587 | debug_assert!(previous_slots_copied + current_slots_copied <= self.capacity(), 588 | format!("previous: {} current: {}", previous_slots_copied, current_slots_copied) 589 | ); 590 | } 591 | if previous_slots_copied + current_slots_copied == self.capacity() { 592 | self.promote(new_map, outer_map, guard) 593 | } else { 594 | false 595 | } 596 | } 597 | 598 | /// Copies a single key/value pair from the map in `&self` to the map in `&self.newer_map`. 599 | /// 600 | /// Returns whether or not this thread was the one to copy the slot. Since copying takes 601 | /// several transitions that could happen from any thread, it doesn't matter which transition 602 | /// we pick as long as exactly one thread reports true for copying a specific slot. 603 | pub fn copy_slot( 604 | &self, 605 | new_map: &Self, 606 | old_map_index: usize, 607 | outer_map: &AtomicBox, 608 | guard: &'guard Guard 609 | ) -> bool 610 | { 611 | /// This is necessary because we're copying a value slot from an older map into a newer 612 | /// map. Thus, when we call `AtomicPtr::load(_, guard)`, we get a pointer that is only 613 | /// valid for the guard's lifetime. But it needs to be inserted into the newer_map and 614 | /// therefore must be valid for the lifetime in newer map. 615 | /// FIXME: Is this necessary? It seems like you would need recursive lifetimes to express 616 | /// that `newer_map` has a different lifetime to `self`. 617 | fn cheat_lifetime<'guard, 'v, V>(maybe: MaybeNull<'guard, V>) -> MaybeNull<'v, V> { 618 | MaybeNull::from_shared(Shared::from(maybe.as_shared().as_raw())) 619 | } 620 | let (ref atomic_key_slot, ref atomic_value_slot) = self.map[old_map_index]; 621 | let old_key: NotNull<_>; 622 | let mut new_key = NotNullOwned::new(KeySlot::SeeNewTable); 623 | 624 | // Preemptively set an empty key slot to `SeeNewTable`. 625 | loop { 626 | let cas_key_result = atomic_key_slot.compare_and_set_owned_weak( 627 | MaybeNull::from_shared(Shared::null()), new_key, guard); 628 | match cas_key_result { 629 | Ok(_new_key) => { 630 | debug_assert!(if let &KeySlot::SeeNewTable = &*_new_key {true} else {false}); 631 | return true; 632 | }, 633 | Err((current, new)) => { 634 | new_key = new; // Return ownership 635 | let _old_key_shared = current; 636 | // Because `compare_and_set_weak()` can spuriously fail and therefore still be 637 | // null. Thus, just retry with `continue` if it's still null. 638 | match current.as_option() { 639 | // No one updated the key slot from `empty` to something else, due to using 640 | // a weak version of CAS here. Thus, we can try again. 641 | None => continue, 642 | Some(k) => { 643 | match k.deref() { 644 | &KeySlot::SeeNewTable => return false, 645 | &KeySlot::Key(_) => { 646 | debug_assert!(current.as_option().is_some()); 647 | old_key = k; 648 | break; 649 | }, 650 | } 651 | } 652 | } 653 | }, 654 | } 655 | } 656 | 657 | // If we got to this point, then we know that there is an existing, non-null key. Thus, we 658 | // need to do the following state transitions: 659 | // 660 | // | [1] | [2] | [3] 661 | // --------+-------------------+---------------------------+------------------- 662 | // Old Map | (K, V) -> (K, V') | | (K, V') -> (K, X) 663 | // --------+-------------------+---------------------------+------------------- 664 | // New Map | | (null, null) -> (K, null) | 665 | // | | (K, null) -> (K, V) | 666 | // 667 | // However, if, in transition [1]: 668 | // 1) V is null here, then just set it to X and let the thread that's trying to 669 | // copy in its (K,V) pair insert it into the newer map. 670 | // 2) V is a tombstone (T) here, then just set it to X and don't copy a key 671 | // without a value into the newer map. 672 | // Note that also both operations in transition [2] can happen on two different threads. 673 | // In addition, care needs to be taken for the rest of this function to ensure that we 674 | // (defer) drop destructors that we need to, but only once. 675 | let mut old_value: MaybeNull<_> = cheat_lifetime(atomic_value_slot.load(guard)); 676 | let not_null_old_value: NotNull<_>; 677 | let primed_old_value: NotNull>; 678 | let mut original_valueslot_value = None; 679 | 680 | loop { 681 | match old_value.as_option() { 682 | // Swap `None`/`Null` values with `SeeNewTable`. 683 | None => { 684 | match atomic_value_slot.compare_and_set_owned( 685 | MaybeNull::from_shared(Shared::null()), 686 | NotNullOwned::new(ValueSlot::SeeNewTable), 687 | guard, 688 | ) { 689 | Err((current, _)) => { 690 | debug_assert!(current.as_option().is_some()); 691 | old_value = cheat_lifetime(current); 692 | continue; 693 | }, 694 | Ok(current) => { 695 | // Successfully did (K,null) -> (K, X). Thus there's nothing more to do 696 | // here, and we obviously don't need to free the null pointer. 697 | debug_assert!(current.deref().is_seenewtable()); 698 | return true; 699 | }, 700 | } 701 | }, 702 | // Otherwise we have a `ValueSlot` here. Let's take a little peek inside. 703 | Some(not_null) => match not_null.deref() { 704 | // Some other thread copied the slot already. Nothing to do or free here. 705 | &ValueSlot::SeeNewTable => return false, 706 | &ValueSlot::Tombstone => { 707 | match atomic_value_slot.compare_and_set_owned( 708 | old_value, 709 | NotNullOwned::new(ValueSlot::SeeNewTable), 710 | guard, 711 | ) { 712 | Err((current, _)) => { 713 | // Assert that `Tombstone` can't turn into `Null`. But it can still 714 | // be V/V'/T/X 715 | debug_assert!(current.as_option().is_some()); 716 | old_value = cheat_lifetime(current); 717 | continue; 718 | }, 719 | Ok(_new) => { 720 | // Successfully did (K, T) -> (K, X). Remember that `old_value` 721 | // here is just the atomic pointer to the tombstone. However, all 722 | // `ValueSlot`s are behind pointers and therefore need to be freed. 723 | debug_assert!(_new.is_seenewtable()); 724 | unsafe { guard.defer(move || not_null.drop()); } 725 | return true; 726 | } 727 | } 728 | }, 729 | // There's a value here. So (K, V) -> (K, V') needs to happen. 730 | &ValueSlot::Value(_) => { 731 | // old_value was `Value` and not `ValuePrime`. 732 | let primed_old_value_owned = NotNullOwned::new(ValueSlot::ValuePrime(not_null.deref())); 733 | match atomic_value_slot.compare_and_set_owned( 734 | old_value, // `ValueSlot::Value(_)` 735 | primed_old_value_owned, 736 | guard 737 | ) { 738 | Err((current, _dropped_because_owned)) => { 739 | debug_assert!(current.as_option().is_some()); 740 | old_value = cheat_lifetime(current); 741 | continue; 742 | }, 743 | Ok(shared_primed_value) => { 744 | // We are the ones that successfully did (K, V) -> (K, V'). 745 | // We are the only ones who performed this exact transition, so 746 | // we will be the ones who will free V if V has not been 747 | // successfully inserted into the newer map. We are the only one's 748 | // who will do this so that we can avoid a double free. So let's 749 | // store the `ValueSlot` that we need to free in this variable. 750 | original_valueslot_value = Some(not_null); 751 | debug_assert!(shared_primed_value.is_valueprime()); 752 | not_null_old_value = not_null; 753 | primed_old_value = shared_primed_value; 754 | break; 755 | }, 756 | } 757 | } 758 | &ValueSlot::ValuePrime(_) => { 759 | not_null_old_value = not_null; 760 | primed_old_value = not_null; 761 | break; 762 | } 763 | } 764 | } 765 | } 766 | 767 | // If we have gotten this far, then we know that at least the first transition has 768 | // occurred, i.e. (K, V) -> (K, V'). 769 | // Also, `not_null_old_value` and `primed_old_value` are assigned, with 770 | // `not_null_old_value` being either `Value` or `ValuePrime`. 771 | debug_assert!(not_null_old_value.is_value() || not_null_old_value.is_valueprime()); 772 | debug_assert!(primed_old_value.is_valueprime()); 773 | 774 | // Now, we know that `old_value` must be either V or V', depending on whether 775 | // `not_null` was a `ValueSlot::Value(_)` or `ValueSlot::ValuePrime(_)`. 776 | let put_value = match not_null_old_value.deref() { 777 | &ValueSlot::Value(_) => PutValue::Shared(not_null_old_value), 778 | &ValueSlot::ValuePrime(v) => match v { 779 | &ValueSlot::Value(_) => 780 | PutValue::Shared(MaybeNull::from_shared(Shared::from(v as *const _)) 781 | .as_option().expect("v is a reference and can't be null") 782 | ), 783 | _ => unreachable!("`ValuePrime` can only be a reference to a `Value`"), 784 | } 785 | &ValueSlot::Tombstone => unreachable!(), 786 | &ValueSlot::SeeNewTable => unreachable!(), 787 | }; 788 | // Now we try to copy the original value into the newer map, but only if there is 789 | // no value in there already. If this fails, then it was copied and/or updated in 790 | // the newer map. 791 | // 792 | // We copied the key/value pair into the new map if the previous value associated 793 | // with the key `is_none()`. 794 | let copied_into_new = new_map.put_if_match( 795 | KeyCompare::Shared(old_key), 796 | put_value, 797 | Match::Empty, 798 | outer_map, 799 | guard 800 | ).is_none(); 801 | if copied_into_new { 802 | debug_assert!(!atomic_key_slot.is_tagged(guard)); 803 | debug_assert!(!atomic_value_slot.is_tagged(guard)); 804 | atomic_key_slot.tag(guard); 805 | debug_assert!(atomic_key_slot.is_tagged(guard)); 806 | } 807 | 808 | // Now we simply need to just do (K, V') -> (K, X). 809 | let primed_old_value_maybe: MaybeNull<_> = primed_old_value.as_maybe_null(); 810 | match atomic_value_slot.compare_and_set_owned( 811 | primed_old_value_maybe, NotNullOwned::new(ValueSlot::SeeNewTable), guard 812 | ) { 813 | Ok(_current) => { 814 | debug_assert!(_current.is_seenewtable()); 815 | unsafe { primed_old_value_maybe.try_defer_drop(guard); } 816 | }, 817 | Err((current, _)) => { 818 | debug_assert!(current.as_option() 819 | .map(|v| v.is_seenewtable()) 820 | .unwrap_or(false), 821 | "can't be null again" 822 | ); 823 | }, 824 | } 825 | // This is only `Some` if we are the thread that did (K, V) -> (K, V'). 826 | if let Some(original_value) = original_valueslot_value { 827 | unsafe { guard.defer(move || { 828 | // We only want to drop this value if it was never copied to the new map. 829 | if !atomic_key_slot.is_tagged(guard) { 830 | original_value.drop(); 831 | } 832 | })} 833 | } 834 | return copied_into_new; 835 | } 836 | 837 | /// If `newer_map` doesn't exist, then this function tries to allocate a newer map that's 838 | /// double the size of `self`. 839 | /// 840 | /// Returns a `Shared` pointer to the newer map 841 | pub fn create_newer_map(&self, guard: &'guard Guard) -> NotNull<'guard, Self> 842 | { 843 | fn try_double(current_size: usize) -> usize { 844 | let doubled_size = current_size << 1; 845 | if doubled_size < current_size { 846 | current_size 847 | } else { 848 | doubled_size 849 | } 850 | } 851 | let newer_map: MaybeNull = self.newer_map.load(guard); 852 | if let Some(not_null) = newer_map.as_option() { 853 | return not_null; 854 | } 855 | let size = self.capacity(); 856 | let mut new_size = size; 857 | // Double size if map is >25% full 858 | if size > (self.capacity() >> 2) { 859 | new_size = try_double(new_size); 860 | // Double size if map is >50% full 861 | if size > (self.capacity() >> 1) { 862 | new_size = try_double(new_size); 863 | } 864 | } 865 | let array_element_byte_size: usize = ::std::mem::size_of::>(); 866 | // This doesn't need to be accurate, so it can be wrapping to ensure it never panics. 867 | let Wrapping(size_in_megabytes) 868 | = (Wrapping(array_element_byte_size) * Wrapping(size)) >> (2^10 * 2^10); 869 | let current_resizers = self.resizers_count.fetch_add(1, Ordering::SeqCst); 870 | if current_resizers >= 2 && size_in_megabytes > 0 { 871 | let newer_map: MaybeNull = self.newer_map.load(guard); 872 | if let Some(not_null) = newer_map.as_option() { 873 | return not_null; 874 | } 875 | ::std::thread::sleep(Duration::from_millis(size_in_megabytes as u64)); 876 | } 877 | let newer_map: MaybeNull = self.newer_map.load(guard); 878 | if let Some(not_null) = newer_map.as_option() { 879 | return not_null; 880 | } 881 | debug_assert!(new_size >= self.capacity()); 882 | match self.newer_map.compare_null_and_set_owned( 883 | NotNullOwned::new(Self::with_capacity_and_hasher(new_size, self.hash_builder.clone())), 884 | guard 885 | ) { 886 | Ok(shared_newer_map) => { 887 | shared_newer_map 888 | }, 889 | Err((current, _drop_our_map)) => { 890 | debug_assert!((&*current as *const _) != (self as *const _)); 891 | current 892 | }, 893 | } 894 | } 895 | 896 | pub fn hash_key(&self, key: &Q) -> usize 897 | where K: Borrow, 898 | Q: Hash + Eq, 899 | { 900 | let mut hasher = self.hash_builder.build_hasher(); 901 | key.hash(&mut hasher); 902 | // Assumes usize <= u64 903 | let hash = hasher.finish() as usize; 904 | // Since the len()/capacity() of the map is always a power of two, we can use a bitwise-and 905 | // operation 906 | hash & (self.capacity() - 1) 907 | } 908 | 909 | pub fn keys_are_equal(&self, first: &T1, second: &T2) -> bool 910 | where T2: PartialEq, 911 | { 912 | second == first 913 | } 914 | 915 | /// Returns the current value associated with some key, if any. 916 | pub fn get( 917 | &self, 918 | key: &Q, 919 | outer_map: &AtomicBox, 920 | guard: &'guard Guard 921 | ) -> Option<&'guard V> 922 | where K: Borrow, 923 | Q: Hash + Eq + PartialEq, 924 | { 925 | // First we need to find/probe the index of the key. 926 | let initial_index = self.hash_key(key); 927 | let len = self.capacity(); 928 | for index in (initial_index..len).chain(0..initial_index) { 929 | let (ref atomic_key_slot, ref atomic_value_slot) = self.map[index]; 930 | // Early exit if neither the key nor value are fully inserted. 931 | if !atomic_key_slot.relaxed_exists(&guard) || !atomic_value_slot.relaxed_exists(&guard) 932 | { 933 | return None; 934 | } 935 | match &*atomic_key_slot.load(&guard).as_option()? { 936 | &KeySlot::Key(ref k) => if self.keys_are_equal(k, key) { 937 | match atomic_value_slot.load(&guard).as_option()?.deref() { 938 | &ValueSlot::Value(ref v) => return Some(&v), 939 | &ValueSlot::Tombstone => return None, 940 | // We call ensure_slot_copied() even on `SeeNewTable` because it calls 941 | // try_promote(). 942 | &ValueSlot::ValuePrime(_) | &ValueSlot::SeeNewTable => { 943 | return self.ensure_slot_copied(index, outer_map, guard) 944 | .get(key, outer_map, guard) 945 | } 946 | } 947 | } else { 948 | continue 949 | }, 950 | &KeySlot::SeeNewTable => { 951 | return self.newer_map.load(&guard) 952 | .as_option() 953 | // It is safe to `unwrap()` because a newer table must exist before any 954 | // `KeySlot`s are set to `SeeNewTable`. 955 | .expect("Can't set `KeySlot` to `SeeNewTable` before setting `newer_map`") 956 | .get(key, outer_map, guard); 957 | }, 958 | } 959 | } 960 | // We exhausted the entire map, so the value could still be inserted into the newer map 961 | return self.newer_map.load(&guard) 962 | .as_option() 963 | .map(|newer_map| newer_map.get(key, outer_map, guard)) 964 | .unwrap_or(None) 965 | } 966 | 967 | /// Increments or decrements the current size of the map, returning the previous value in the 968 | /// map. 969 | pub fn update_size_and_defer( 970 | &'guard self, 971 | old_value_slot: MaybeNull<'guard, ValueSlot>, 972 | insert_tombstone: bool, 973 | guard: &'guard Guard, 974 | ) -> Option<&'guard ValueSlot> 975 | { 976 | // If we did not insert a tombstone, then we incremented if the old value was null or 977 | // tombstone. 978 | let increment = if !insert_tombstone { 979 | match old_value_slot.as_option() { 980 | None => true, 981 | Some(ref old_value) if old_value.is_tombstone() => true, 982 | Some(ref old_value) if old_value.is_seenewtable() => unreachable!(), 983 | Some(ref old_value) if old_value.is_valueprime() => unreachable!(), 984 | Some(_) => false, 985 | } 986 | } else { 987 | false 988 | }; 989 | if increment { 990 | self.size.fetch_add(1, Ordering::SeqCst); 991 | } 992 | // If we did insert a tombstone, then we decremented if the old value was V 993 | let decrement = if insert_tombstone { 994 | match old_value_slot.as_option() { 995 | None => false, 996 | Some(ref old_value) if old_value.is_tombstone() => false, 997 | Some(ref old_value) if old_value.is_seenewtable() => unreachable!(), 998 | Some(ref old_value) if old_value.is_valueprime() => unreachable!(), 999 | Some(_) => true, 1000 | } 1001 | } else { 1002 | false 1003 | }; 1004 | if decrement { 1005 | self.size.fetch_sub(1, Ordering::SeqCst); 1006 | } 1007 | match old_value_slot.as_option() { 1008 | None => None, 1009 | Some(value) => { 1010 | unsafe { guard.defer(move || { value.drop(); })} 1011 | Some(value.deref()) 1012 | } 1013 | } 1014 | } 1015 | 1016 | pub fn put_if_match( 1017 | &'guard self, 1018 | key: KeyCompare, 1019 | mut put: PutValue<'v, V>, 1020 | matcher: Match, 1021 | outer_map: &AtomicBox, 1022 | guard: &'guard Guard 1023 | ) -> Option<&'guard ValueSlot> 1024 | where K: Borrow, 1025 | Q: Hash + Eq + PartialEq + ?Sized, 1026 | { 1027 | /// FIXME: See other cheat_lifetime() FIXME note above 1028 | fn cheat_lifetime<'guard, 'v, V>(maybe: NotNull<'guard, V>) -> NotNull<'v, V> { 1029 | MaybeNull::from_shared(Shared::from(maybe.as_shared().as_raw())) 1030 | .as_option() 1031 | .expect("parameter was `NotNull` to begin with") 1032 | } 1033 | let initial_index = self.hash_key(key.as_qref().as_qref2().as_q()); 1034 | let len = self.capacity(); 1035 | let mut key_index = None; 1036 | let mut key = key; 1037 | // First we need to find the key slot for the key. 1038 | 'find_key_loop: 1039 | for index in (initial_index..len).chain(0..initial_index) { 1040 | let atomic_key_slot: &AtomicPtr> = &self.map[index].0; 1041 | let option_key: Option<_> = atomic_key_slot.load(&guard) 1042 | .as_option(); 1043 | let current_key: NotNull> = match option_key { 1044 | Some(existing_key) => existing_key, 1045 | None => if put.is_tombstone() { 1046 | // The key is not taken, so we don't put a Tombstone value here 1047 | return None; 1048 | } else if let Match::AnyKeyValuePair = matcher { 1049 | // If key is not taken, return None if we weren't going to insert something 1050 | // anyway 1051 | return None; 1052 | } else { 1053 | match key { 1054 | KeyCompare::Owned(owned) => { 1055 | match atomic_key_slot.compare_null_and_set_owned(owned, guard) { 1056 | Ok(shared_key) => { 1057 | // TODO: Raise keyslots-used count 1058 | key = KeyCompare::Shared(shared_key); 1059 | key_index = Some(index); 1060 | break 'find_key_loop; 1061 | }, 1062 | Err((not_null, _return)) => { 1063 | key = KeyCompare::Owned(_return); 1064 | not_null 1065 | }, 1066 | } 1067 | }, 1068 | KeyCompare::Shared(not_null) => { 1069 | match atomic_key_slot.compare_null_and_set(not_null, guard) { 1070 | Ok(shared_key) => { 1071 | key = KeyCompare::Shared(shared_key); 1072 | key_index = Some(index); 1073 | break 'find_key_loop; 1074 | }, 1075 | Err((not_null, _return)) => { 1076 | key = KeyCompare::Shared(_return); 1077 | not_null 1078 | } 1079 | } 1080 | }, 1081 | KeyCompare::OnlyCompare(_) => { 1082 | // We are only comparing the keys and don't want to insert it if there 1083 | // is no key slot taken. 1084 | return None; 1085 | } 1086 | } 1087 | }, 1088 | }; 1089 | match &*current_key { 1090 | &KeySlot::Key(ref current_key) => if self.keys_are_equal(key.as_qref().as_qref2().as_q(), current_key.borrow()) { 1091 | key_index = Some(index); 1092 | break 'find_key_loop; 1093 | }, // else continue 1094 | &KeySlot::SeeNewTable => { 1095 | break 'find_key_loop; 1096 | }, 1097 | } 1098 | } 1099 | 1100 | let key_index: usize = match key_index { 1101 | Some(k) => k, 1102 | None => { 1103 | // We have exhausted the entire probing range, so there are no key slots available 1104 | // and need to resize. 1105 | let new_table: NotNull = self.create_newer_map(guard); 1106 | self.help_copy(new_table, true, outer_map, guard); 1107 | return new_table.deref().put_if_match(key, put, matcher, outer_map, guard); 1108 | }, 1109 | }; 1110 | 1111 | // We have now found the key slot to use. This key slot will never change now so we know 1112 | // that we may insert the value into the index `key_index`. 1113 | 1114 | let atomic_value_slot = &self.map[key_index].1; 1115 | let mut old_value_slot: MaybeNull<_> = atomic_value_slot.load(&guard); 1116 | 1117 | // Now try to put the value into the map. 1118 | let insert_tombstone = put.is_tombstone(); 1119 | loop { 1120 | let value_slot_option = old_value_slot.as_option(); 1121 | // If the value we're trying to insert equals the current value, pretend we replaced it 1122 | // with CAS and just return the current value. 1123 | if let Some(v) = value_slot_option { 1124 | if put.ptr_equals(v) { 1125 | return Some(v.deref()); 1126 | } 1127 | } 1128 | // Early return if the expected value in `matcher` doesn't equal the current value. 1129 | match matcher { 1130 | Match::Empty => if let Some(v) = value_slot_option { 1131 | return Some(v.deref()) 1132 | }, 1133 | Match::AnyKeyValuePair => match value_slot_option.map(|v| v.deref()) { 1134 | Some(&ValueSlot::Tombstone) | None => return None, 1135 | _ => (), 1136 | } 1137 | Match::Always => (), 1138 | } 1139 | // If it's prime then we need to copy the slot and try again in the new map. 1140 | if value_slot_option.map_or(false, |v| v.is_prime()) { 1141 | let newer_map = self.newer_map 1142 | .load(&guard) 1143 | .as_option() 1144 | .expect("Can't set a `ValueSlot` to `ValuePrime` before setting `newer_map`"); 1145 | self.copy_slot(&*newer_map, key_index, outer_map, guard); 1146 | return newer_map.deref().put_if_match(key, put, matcher, outer_map, guard); 1147 | } 1148 | // If the new map exists, help copy the current slot and some others and try again. 1149 | if self.newer_map.relaxed_exists(guard) { 1150 | // TODO: if newer_map == None AND ((current_value is None AND table full) OR value 1151 | // is prime) then resize 1152 | return self.ensure_slot_copied(key_index, outer_map, guard) 1153 | .deref() 1154 | .put_if_match(key, put, matcher, outer_map, guard); 1155 | } 1156 | debug_assert!(value_slot_option.map_or(true, |v| !v.is_prime())); 1157 | // Otherwise, try to CAS the value. 1158 | match put { 1159 | PutValue::Owned(owned) => match atomic_value_slot.compare_and_set_owned( 1160 | old_value_slot, owned, &guard 1161 | ) { 1162 | Ok(_) => { 1163 | return self.update_size_and_defer(old_value_slot, insert_tombstone, guard); 1164 | }, 1165 | Err((current, _return_ownership)) => { 1166 | debug_assert!(current.as_option().is_some()); 1167 | old_value_slot = current; 1168 | put = PutValue::Owned(_return_ownership); 1169 | }, 1170 | }, 1171 | PutValue::Shared(shared) => match atomic_value_slot.compare_and_set( 1172 | old_value_slot, shared, &guard 1173 | ) { 1174 | Ok(_) => { 1175 | return self.update_size_and_defer(old_value_slot, insert_tombstone, guard); 1176 | }, 1177 | Err((current, _return_ownership)) => { 1178 | debug_assert!(current.as_option().is_some()); 1179 | old_value_slot = current; 1180 | put = PutValue::Shared(cheat_lifetime(_return_ownership)); 1181 | }, 1182 | }, 1183 | } 1184 | } 1185 | } 1186 | 1187 | pub fn clone_hasher(&self) -> S { 1188 | self.hash_builder.clone() 1189 | } 1190 | } 1191 | 1192 | impl<'v, K, V, S> Drop for MapInner<'v, K, V, S> { 1193 | fn drop(&mut self) { 1194 | let guard = &::pin(); 1195 | for (mut k_ptr, mut v_ptr) in self.map.drain(..) { 1196 | unsafe { 1197 | guard.defer(move || { 1198 | if !k_ptr.is_tagged(&guard) { 1199 | k_ptr.try_drop(&guard); 1200 | } 1201 | v_ptr.try_drop(&guard); 1202 | }) 1203 | } 1204 | } 1205 | // Don't drop the `newer_map` ptr, because `self` could have been dropped from `promote()`. 1206 | } 1207 | } 1208 | 1209 | --------------------------------------------------------------------------------