├── LICENSE ├── Makefile ├── README ├── profiling_hooks.h ├── pthread.c ├── pthread_defs.h ├── spinlock_alpha.h ├── spinlock_arm.h ├── spinlock_sparc.h ├── spinlock_x86.h ├── tests ├── Makefile ├── test___thread.cpp ├── test_atomic.cpp ├── test_barrier.cpp ├── test_lock.cpp ├── test_malloc.cpp ├── test_omp.cpp ├── test_pthread.cpp ├── test_pthreadbasic.cpp ├── test_sieve.cpp └── test_stackgrow.cpp └── tls_defs.h /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | GNU LESSER GENERAL PUBLIC LICENSE 3 | Version 2.1, February 1999 4 | 5 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 6 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | [This is the first released version of the Lesser GPL. It also counts 11 | as the successor of the GNU Library Public License, version 2, hence 12 | the version number 2.1.] 13 | 14 | Preamble 15 | 16 | The licenses for most software are designed to take away your 17 | freedom to share and change it. By contrast, the GNU General Public 18 | Licenses are intended to guarantee your freedom to share and change 19 | free software--to make sure the software is free for all its users. 20 | 21 | This license, the Lesser General Public License, applies to some 22 | specially designated software packages--typically libraries--of the 23 | Free Software Foundation and other authors who decide to use it. You 24 | can use it too, but we suggest you first think carefully about whether 25 | this license or the ordinary General Public License is the better 26 | strategy to use in any particular case, based on the explanations 27 | below. 28 | 29 | When we speak of free software, we are referring to freedom of use, 30 | not price. Our General Public Licenses are designed to make sure that 31 | you have the freedom to distribute copies of free software (and charge 32 | for this service if you wish); that you receive source code or can get 33 | it if you want it; that you can change the software and use pieces of 34 | it in new free programs; and that you are informed that you can do 35 | these things. 36 | 37 | To protect your rights, we need to make restrictions that forbid 38 | distributors to deny you these rights or to ask you to surrender these 39 | rights. These restrictions translate to certain responsibilities for 40 | you if you distribute copies of the library or if you modify it. 41 | 42 | For example, if you distribute copies of the library, whether gratis 43 | or for a fee, you must give the recipients all the rights that we gave 44 | you. You must make sure that they, too, receive or can get the source 45 | code. If you link other code with the library, you must provide 46 | complete object files to the recipients, so that they can relink them 47 | with the library after making changes to the library and recompiling 48 | it. And you must show them these terms so they know their rights. 49 | 50 | We protect your rights with a two-step method: (1) we copyright the 51 | library, and (2) we offer you this license, which gives you legal 52 | permission to copy, distribute and/or modify the library. 53 | 54 | To protect each distributor, we want to make it very clear that 55 | there is no warranty for the free library. Also, if the library is 56 | modified by someone else and passed on, the recipients should know 57 | that what they have is not the original version, so that the original 58 | author's reputation will not be affected by problems that might be 59 | introduced by others. 60 | ^L 61 | Finally, software patents pose a constant threat to the existence of 62 | any free program. We wish to make sure that a company cannot 63 | effectively restrict the users of a free program by obtaining a 64 | restrictive license from a patent holder. Therefore, we insist that 65 | any patent license obtained for a version of the library must be 66 | consistent with the full freedom of use specified in this license. 67 | 68 | Most GNU software, including some libraries, is covered by the 69 | ordinary GNU General Public License. This license, the GNU Lesser 70 | General Public License, applies to certain designated libraries, and 71 | is quite different from the ordinary General Public License. We use 72 | this license for certain libraries in order to permit linking those 73 | libraries into non-free programs. 74 | 75 | When a program is linked with a library, whether statically or using 76 | a shared library, the combination of the two is legally speaking a 77 | combined work, a derivative of the original library. The ordinary 78 | General Public License therefore permits such linking only if the 79 | entire combination fits its criteria of freedom. The Lesser General 80 | Public License permits more lax criteria for linking other code with 81 | the library. 82 | 83 | We call this license the "Lesser" General Public License because it 84 | does Less to protect the user's freedom than the ordinary General 85 | Public License. It also provides other free software developers Less 86 | of an advantage over competing non-free programs. These disadvantages 87 | are the reason we use the ordinary General Public License for many 88 | libraries. However, the Lesser license provides advantages in certain 89 | special circumstances. 90 | 91 | For example, on rare occasions, there may be a special need to 92 | encourage the widest possible use of a certain library, so that it 93 | becomes a de-facto standard. To achieve this, non-free programs must 94 | be allowed to use the library. A more frequent case is that a free 95 | library does the same job as widely used non-free libraries. In this 96 | case, there is little to gain by limiting the free library to free 97 | software only, so we use the Lesser General Public License. 98 | 99 | In other cases, permission to use a particular library in non-free 100 | programs enables a greater number of people to use a large body of 101 | free software. For example, permission to use the GNU C Library in 102 | non-free programs enables many more people to use the whole GNU 103 | operating system, as well as its variant, the GNU/Linux operating 104 | system. 105 | 106 | Although the Lesser General Public License is Less protective of the 107 | users' freedom, it does ensure that the user of a program that is 108 | linked with the Library has the freedom and the wherewithal to run 109 | that program using a modified version of the Library. 110 | 111 | The precise terms and conditions for copying, distribution and 112 | modification follow. Pay close attention to the difference between a 113 | "work based on the library" and a "work that uses the library". The 114 | former contains code derived from the library, whereas the latter must 115 | be combined with the library in order to run. 116 | ^L 117 | GNU LESSER GENERAL PUBLIC LICENSE 118 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 119 | 120 | 0. This License Agreement applies to any software library or other 121 | program which contains a notice placed by the copyright holder or 122 | other authorized party saying it may be distributed under the terms of 123 | this Lesser General Public License (also called "this License"). 124 | Each licensee is addressed as "you". 125 | 126 | A "library" means a collection of software functions and/or data 127 | prepared so as to be conveniently linked with application programs 128 | (which use some of those functions and data) to form executables. 129 | 130 | The "Library", below, refers to any such software library or work 131 | which has been distributed under these terms. A "work based on the 132 | Library" means either the Library or any derivative work under 133 | copyright law: that is to say, a work containing the Library or a 134 | portion of it, either verbatim or with modifications and/or translated 135 | straightforwardly into another language. (Hereinafter, translation is 136 | included without limitation in the term "modification".) 137 | 138 | "Source code" for a work means the preferred form of the work for 139 | making modifications to it. For a library, complete source code means 140 | all the source code for all modules it contains, plus any associated 141 | interface definition files, plus the scripts used to control 142 | compilation and installation of the library. 143 | 144 | Activities other than copying, distribution and modification are not 145 | covered by this License; they are outside its scope. The act of 146 | running a program using the Library is not restricted, and output from 147 | such a program is covered only if its contents constitute a work based 148 | on the Library (independent of the use of the Library in a tool for 149 | writing it). Whether that is true depends on what the Library does 150 | and what the program that uses the Library does. 151 | 152 | 1. You may copy and distribute verbatim copies of the Library's 153 | complete source code as you receive it, in any medium, provided that 154 | you conspicuously and appropriately publish on each copy an 155 | appropriate copyright notice and disclaimer of warranty; keep intact 156 | all the notices that refer to this License and to the absence of any 157 | warranty; and distribute a copy of this License along with the 158 | Library. 159 | 160 | You may charge a fee for the physical act of transferring a copy, 161 | and you may at your option offer warranty protection in exchange for a 162 | fee. 163 | 164 | 2. You may modify your copy or copies of the Library or any portion 165 | of it, thus forming a work based on the Library, and copy and 166 | distribute such modifications or work under the terms of Section 1 167 | above, provided that you also meet all of these conditions: 168 | 169 | a) The modified work must itself be a software library. 170 | 171 | b) You must cause the files modified to carry prominent notices 172 | stating that you changed the files and the date of any change. 173 | 174 | c) You must cause the whole of the work to be licensed at no 175 | charge to all third parties under the terms of this License. 176 | 177 | d) If a facility in the modified Library refers to a function or a 178 | table of data to be supplied by an application program that uses 179 | the facility, other than as an argument passed when the facility 180 | is invoked, then you must make a good faith effort to ensure that, 181 | in the event an application does not supply such function or 182 | table, the facility still operates, and performs whatever part of 183 | its purpose remains meaningful. 184 | 185 | (For example, a function in a library to compute square roots has 186 | a purpose that is entirely well-defined independent of the 187 | application. Therefore, Subsection 2d requires that any 188 | application-supplied function or table used by this function must 189 | be optional: if the application does not supply it, the square 190 | root function must still compute square roots.) 191 | 192 | These requirements apply to the modified work as a whole. If 193 | identifiable sections of that work are not derived from the Library, 194 | and can be reasonably considered independent and separate works in 195 | themselves, then this License, and its terms, do not apply to those 196 | sections when you distribute them as separate works. But when you 197 | distribute the same sections as part of a whole which is a work based 198 | on the Library, the distribution of the whole must be on the terms of 199 | this License, whose permissions for other licensees extend to the 200 | entire whole, and thus to each and every part regardless of who wrote 201 | it. 202 | 203 | Thus, it is not the intent of this section to claim rights or contest 204 | your rights to work written entirely by you; rather, the intent is to 205 | exercise the right to control the distribution of derivative or 206 | collective works based on the Library. 207 | 208 | In addition, mere aggregation of another work not based on the Library 209 | with the Library (or with a work based on the Library) on a volume of 210 | a storage or distribution medium does not bring the other work under 211 | the scope of this License. 212 | 213 | 3. You may opt to apply the terms of the ordinary GNU General Public 214 | License instead of this License to a given copy of the Library. To do 215 | this, you must alter all the notices that refer to this License, so 216 | that they refer to the ordinary GNU General Public License, version 2, 217 | instead of to this License. (If a newer version than version 2 of the 218 | ordinary GNU General Public License has appeared, then you can specify 219 | that version instead if you wish.) Do not make any other change in 220 | these notices. 221 | ^L 222 | Once this change is made in a given copy, it is irreversible for 223 | that copy, so the ordinary GNU General Public License applies to all 224 | subsequent copies and derivative works made from that copy. 225 | 226 | This option is useful when you wish to copy part of the code of 227 | the Library into a program that is not a library. 228 | 229 | 4. You may copy and distribute the Library (or a portion or 230 | derivative of it, under Section 2) in object code or executable form 231 | under the terms of Sections 1 and 2 above provided that you accompany 232 | it with the complete corresponding machine-readable source code, which 233 | must be distributed under the terms of Sections 1 and 2 above on a 234 | medium customarily used for software interchange. 235 | 236 | If distribution of object code is made by offering access to copy 237 | from a designated place, then offering equivalent access to copy the 238 | source code from the same place satisfies the requirement to 239 | distribute the source code, even though third parties are not 240 | compelled to copy the source along with the object code. 241 | 242 | 5. A program that contains no derivative of any portion of the 243 | Library, but is designed to work with the Library by being compiled or 244 | linked with it, is called a "work that uses the Library". Such a 245 | work, in isolation, is not a derivative work of the Library, and 246 | therefore falls outside the scope of this License. 247 | 248 | However, linking a "work that uses the Library" with the Library 249 | creates an executable that is a derivative of the Library (because it 250 | contains portions of the Library), rather than a "work that uses the 251 | library". The executable is therefore covered by this License. 252 | Section 6 states terms for distribution of such executables. 253 | 254 | When a "work that uses the Library" uses material from a header file 255 | that is part of the Library, the object code for the work may be a 256 | derivative work of the Library even though the source code is not. 257 | Whether this is true is especially significant if the work can be 258 | linked without the Library, or if the work is itself a library. The 259 | threshold for this to be true is not precisely defined by law. 260 | 261 | If such an object file uses only numerical parameters, data 262 | structure layouts and accessors, and small macros and small inline 263 | functions (ten lines or less in length), then the use of the object 264 | file is unrestricted, regardless of whether it is legally a derivative 265 | work. (Executables containing this object code plus portions of the 266 | Library will still fall under Section 6.) 267 | 268 | Otherwise, if the work is a derivative of the Library, you may 269 | distribute the object code for the work under the terms of Section 6. 270 | Any executables containing that work also fall under Section 6, 271 | whether or not they are linked directly with the Library itself. 272 | ^L 273 | 6. As an exception to the Sections above, you may also combine or 274 | link a "work that uses the Library" with the Library to produce a 275 | work containing portions of the Library, and distribute that work 276 | under terms of your choice, provided that the terms permit 277 | modification of the work for the customer's own use and reverse 278 | engineering for debugging such modifications. 279 | 280 | You must give prominent notice with each copy of the work that the 281 | Library is used in it and that the Library and its use are covered by 282 | this License. You must supply a copy of this License. If the work 283 | during execution displays copyright notices, you must include the 284 | copyright notice for the Library among them, as well as a reference 285 | directing the user to the copy of this License. Also, you must do one 286 | of these things: 287 | 288 | a) Accompany the work with the complete corresponding 289 | machine-readable source code for the Library including whatever 290 | changes were used in the work (which must be distributed under 291 | Sections 1 and 2 above); and, if the work is an executable linked 292 | with the Library, with the complete machine-readable "work that 293 | uses the Library", as object code and/or source code, so that the 294 | user can modify the Library and then relink to produce a modified 295 | executable containing the modified Library. (It is understood 296 | that the user who changes the contents of definitions files in the 297 | Library will not necessarily be able to recompile the application 298 | to use the modified definitions.) 299 | 300 | b) Use a suitable shared library mechanism for linking with the 301 | Library. A suitable mechanism is one that (1) uses at run time a 302 | copy of the library already present on the user's computer system, 303 | rather than copying library functions into the executable, and (2) 304 | will operate properly with a modified version of the library, if 305 | the user installs one, as long as the modified version is 306 | interface-compatible with the version that the work was made with. 307 | 308 | c) Accompany the work with a written offer, valid for at least 309 | three years, to give the same user the materials specified in 310 | Subsection 6a, above, for a charge no more than the cost of 311 | performing this distribution. 312 | 313 | d) If distribution of the work is made by offering access to copy 314 | from a designated place, offer equivalent access to copy the above 315 | specified materials from the same place. 316 | 317 | e) Verify that the user has already received a copy of these 318 | materials or that you have already sent this user a copy. 319 | 320 | For an executable, the required form of the "work that uses the 321 | Library" must include any data and utility programs needed for 322 | reproducing the executable from it. However, as a special exception, 323 | the materials to be distributed need not include anything that is 324 | normally distributed (in either source or binary form) with the major 325 | components (compiler, kernel, and so on) of the operating system on 326 | which the executable runs, unless that component itself accompanies 327 | the executable. 328 | 329 | It may happen that this requirement contradicts the license 330 | restrictions of other proprietary libraries that do not normally 331 | accompany the operating system. Such a contradiction means you cannot 332 | use both them and the Library together in an executable that you 333 | distribute. 334 | ^L 335 | 7. You may place library facilities that are a work based on the 336 | Library side-by-side in a single library together with other library 337 | facilities not covered by this License, and distribute such a combined 338 | library, provided that the separate distribution of the work based on 339 | the Library and of the other library facilities is otherwise 340 | permitted, and provided that you do these two things: 341 | 342 | a) Accompany the combined library with a copy of the same work 343 | based on the Library, uncombined with any other library 344 | facilities. This must be distributed under the terms of the 345 | Sections above. 346 | 347 | b) Give prominent notice with the combined library of the fact 348 | that part of it is a work based on the Library, and explaining 349 | where to find the accompanying uncombined form of the same work. 350 | 351 | 8. You may not copy, modify, sublicense, link with, or distribute 352 | the Library except as expressly provided under this License. Any 353 | attempt otherwise to copy, modify, sublicense, link with, or 354 | distribute the Library is void, and will automatically terminate your 355 | rights under this License. However, parties who have received copies, 356 | or rights, from you under this License will not have their licenses 357 | terminated so long as such parties remain in full compliance. 358 | 359 | 9. You are not required to accept this License, since you have not 360 | signed it. However, nothing else grants you permission to modify or 361 | distribute the Library or its derivative works. These actions are 362 | prohibited by law if you do not accept this License. Therefore, by 363 | modifying or distributing the Library (or any work based on the 364 | Library), you indicate your acceptance of this License to do so, and 365 | all its terms and conditions for copying, distributing or modifying 366 | the Library or works based on it. 367 | 368 | 10. Each time you redistribute the Library (or any work based on the 369 | Library), the recipient automatically receives a license from the 370 | original licensor to copy, distribute, link with or modify the Library 371 | subject to these terms and conditions. You may not impose any further 372 | restrictions on the recipients' exercise of the rights granted herein. 373 | You are not responsible for enforcing compliance by third parties with 374 | this License. 375 | ^L 376 | 11. If, as a consequence of a court judgment or allegation of patent 377 | infringement or for any other reason (not limited to patent issues), 378 | conditions are imposed on you (whether by court order, agreement or 379 | otherwise) that contradict the conditions of this License, they do not 380 | excuse you from the conditions of this License. If you cannot 381 | distribute so as to satisfy simultaneously your obligations under this 382 | License and any other pertinent obligations, then as a consequence you 383 | may not distribute the Library at all. For example, if a patent 384 | license would not permit royalty-free redistribution of the Library by 385 | all those who receive copies directly or indirectly through you, then 386 | the only way you could satisfy both it and this License would be to 387 | refrain entirely from distribution of the Library. 388 | 389 | If any portion of this section is held invalid or unenforceable under 390 | any particular circumstance, the balance of the section is intended to 391 | apply, and the section as a whole is intended to apply in other 392 | circumstances. 393 | 394 | It is not the purpose of this section to induce you to infringe any 395 | patents or other property right claims or to contest validity of any 396 | such claims; this section has the sole purpose of protecting the 397 | integrity of the free software distribution system which is 398 | implemented by public license practices. Many people have made 399 | generous contributions to the wide range of software distributed 400 | through that system in reliance on consistent application of that 401 | system; it is up to the author/donor to decide if he or she is willing 402 | to distribute software through any other system and a licensee cannot 403 | impose that choice. 404 | 405 | This section is intended to make thoroughly clear what is believed to 406 | be a consequence of the rest of this License. 407 | 408 | 12. If the distribution and/or use of the Library is restricted in 409 | certain countries either by patents or by copyrighted interfaces, the 410 | original copyright holder who places the Library under this License 411 | may add an explicit geographical distribution limitation excluding those 412 | countries, so that distribution is permitted only in or among 413 | countries not thus excluded. In such case, this License incorporates 414 | the limitation as if written in the body of this License. 415 | 416 | 13. The Free Software Foundation may publish revised and/or new 417 | versions of the Lesser General Public License from time to time. 418 | Such new versions will be similar in spirit to the present version, 419 | but may differ in detail to address new problems or concerns. 420 | 421 | Each version is given a distinguishing version number. If the Library 422 | specifies a version number of this License which applies to it and 423 | "any later version", you have the option of following the terms and 424 | conditions either of that version or of any later version published by 425 | the Free Software Foundation. If the Library does not specify a 426 | license version number, you may choose any version ever published by 427 | the Free Software Foundation. 428 | ^L 429 | 14. If you wish to incorporate parts of the Library into other free 430 | programs whose distribution conditions are incompatible with these, 431 | write to the author to ask for permission. For software which is 432 | copyrighted by the Free Software Foundation, write to the Free 433 | Software Foundation; we sometimes make exceptions for this. Our 434 | decision will be guided by the two goals of preserving the free status 435 | of all derivatives of our free software and of promoting the sharing 436 | and reuse of software generally. 437 | 438 | NO WARRANTY 439 | 440 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 441 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 442 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 443 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 444 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 445 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 446 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 447 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 448 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 449 | 450 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 451 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 452 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 453 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 454 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 455 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 456 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 457 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 458 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 459 | DAMAGES. 460 | 461 | END OF TERMS AND CONDITIONS 462 | ^L 463 | How to Apply These Terms to Your New Libraries 464 | 465 | If you develop a new library, and you want it to be of the greatest 466 | possible use to the public, we recommend making it free software that 467 | everyone can redistribute and change. You can do so by permitting 468 | redistribution under these terms (or, alternatively, under the terms 469 | of the ordinary General Public License). 470 | 471 | To apply these terms, attach the following notices to the library. 472 | It is safest to attach them to the start of each source file to most 473 | effectively convey the exclusion of warranty; and each file should 474 | have at least the "copyright" line and a pointer to where the full 475 | notice is found. 476 | 477 | 478 | 479 | Copyright (C) 480 | 481 | This library is free software; you can redistribute it and/or 482 | modify it under the terms of the GNU Lesser General Public 483 | License as published by the Free Software Foundation; either 484 | version 2.1 of the License, or (at your option) any later version. 485 | 486 | This library is distributed in the hope that it will be useful, 487 | but WITHOUT ANY WARRANTY; without even the implied warranty of 488 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 489 | Lesser General Public License for more details. 490 | 491 | You should have received a copy of the GNU Lesser General Public 492 | License along with this library; if not, write to the Free Software 493 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 494 | 495 | Also add information on how to contact you by electronic and paper mail. 496 | 497 | You should also get your employer (if you work as a programmer) or 498 | your school, if any, to sign a "copyright disclaimer" for the library, 499 | if necessary. Here is a sample; alter the names: 500 | 501 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 502 | library `Frob' (a library for tweaking knobs) written by James 503 | Random Hacker. 504 | 505 | , 1 April 1990 506 | Ty Coon, President of Vice 507 | 508 | That's all there is to it! 509 | 510 | 511 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -O3 -static 3 | 4 | .PHONY: all clean 5 | 6 | all: libpthread.a(pthread.o) 7 | 8 | clean: 9 | $(RM) *.o libpthread.a 10 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | m5threads -- A pthread library for the M5 simulator 2 | =================================================== 3 | Daniel Sanchez, Stanford University 4 | 5 | 6 | Changelog 7 | --------- 8 | 9 | 14-Feb-09 10 | - Added support for OpenMP in SPARC. 11 | - Fixed stack guard to work in SPARC64 (stack bias was insufficient). 12 | - Added optional profiling hooks to measure synchronization use. Compile with -DM5_PROFILING to use M5 profiling syscalls. 13 | - The Makefile now builds test programs linked with both m5threads (test_XXX) and the standard pthread library (test_XXX_p). This is done for debugging purposes, but note that **the _p binaries won't work in M5**. 14 | 15 | 27-Jan-09 16 | - Added support for TLS in SPARC and x86-64 in static binaries. Alpha no longer works due to having unimplemented TLS support. 17 | - Fixed a race condition in rwlocks and condition variables. 18 | - Added support for detached threads. 19 | - Added thread-specific data (TSD) functions: key_create/delete/getspecific/setspecific. 20 | - Integrated with NPTL/LinuxThreads-based glibc (libc aliases, specific functions, intialization routines, and libc-specific TSD). libc calls are now MT-safe and the library runs the full test suite correctly. Tested on SPARC/glibc2.3.6/Linux 2.6.11 (LinuxThreads) in M5, and x86-64/glibc2.6/Linux 2.6.26 (NPTL) in an 8-core machine. 21 | - Added support for OpenMP programs (see test_opm.cpp) -- for now, works in x86 (real machine and M5), but not in SPARC. 22 | - Licensed under GPLv2. 23 | - Extended this README. 24 | 25 | 23-Jan-2009 26 | - Added support for SPARC in pthread_exit 27 | - Substituted tree barriers by counter barriers. Now barriers work regardless of which threads take them. 28 | 29 | 21-Jan-2009 30 | - Initial version 31 | 32 | License 33 | ------- 34 | 35 | This software is licensed under the GPLv2. See the LICENSE file for a full copy of the license. 36 | 37 | This software contains portions of code from the Linux kernel and glibc 2.3.6. Both are redistributed under the terms of the GPL. 38 | 39 | Description 40 | ----------- 41 | 42 | This library enables M5 to simulate multithreaded apps in system call emulation mode. It is intended as a replacement of NPTL/LinuxThreads implementations of libpthread. Instead of using a large portion of the Linux system calls, this library does as much as possible in user-level code. It requires just two system calls: clone, to spawn a new process, and exit, to finish a thread. As a result, it is easy to support in a syscall-emulation simulator. However, this is not a full implementation of pthreads, and the library lacks a thread scheduler. In M5, you will not be able to schedule more threads than thread contexts. 43 | 44 | This library works in M5, but in real systems too. Both x86-64 and SPARC systems running Linux should execute programs correctly. In real systems, you will be able to allocate more threads than CPUs, but performance will degrade in this case since there is no thread scheduler (and thread switching occurs at the granularity of the OS scheduler). 45 | 46 | Only a subset of the pthread specification is supported. This includes: 47 | - Creation and join of joinable and detached threads; pthread_exit 48 | - Regular mutexes (NOT recursive or other rare modes) 49 | - Regular read-write locks 50 | - Barriers 51 | - Condition variables 52 | - Keys (key_create/delete, get/setspecific) 53 | - Miscellaneous functions: 54 | In particular, the following thinks are not supported: 55 | - pthread_cancel and related functions 56 | - pthread_kill 57 | - Anything else that has to do with signals 58 | - pthread_cleanup_XXX, pthread_unwind 59 | If your program uses a non-implemented pthread function, it will fail an assertion. 60 | 61 | This library should compile with GNU toolchains implementing LinuxThreads (Linux <=2.4 or 2.6) or NPTL (2.6 only) pthreads. If you compile it with an NPTL glibc, you may get futex() system calls if you try to do concurrent calls to multithreaded-safe glibc functions (e.g. printf). These are unimplemented in M5. To avoid them, enclose these calls in a global lock. Additionally, NPTL apps tend to use more system calls, so it is recommended to use M5 with a glibc compiled with LinuxThreads. Performance should be practically identical with both versions, as we are substituting the threading library. 62 | 63 | This library includes support for thread-local storage (TLS), but only for the SPARC and x86-64 ABIs (which are nearly identical). Alpha is no longer supported. Supporting Alpha would require implementing its TLS ABI. 64 | 65 | Compiling & using 66 | ----------------- 67 | 68 | Applications compiled with this library should be built statically, and should link against the built pthread.o object file. Again, see the Makefile in the tests/ directory for the exact commands used. 69 | 70 | By default, the tests/Makefile builds all the tests using your system's g++. You can build sparc binaries by building a cross-compiler. 71 | 72 | TODOs 73 | ----- 74 | - Ticket/MCS locks 75 | - Tree barriers 76 | - Add a scheduler, turning the library to an M:N model 77 | 78 | Implementation details 79 | ====================== 80 | 81 | What follows is recommended reading if you want to understand how the library works in more detail and extend it. 82 | 83 | This library implements mutexes as TTS spinlocks (taken from the Linux source code tree), and barriers as counter barriers. Compatibility with NPTL and LinuxThreads data structures is maintained by using a variety of macros, defined in pthread_defs.h 84 | 85 | All the memory regions needed to spawn a thread are contained in a single memory segment, the thread block, which has the following format: 86 | 87 | ---------------------- <- lower addresses 88 | Thread Control Block 89 | ---------------------- 90 | TLS data 91 | ---------------------- <- TLS pointer 92 | (Unused) "real" TCB 93 | ---------------------- 94 | [empty space] 95 | ---------------------- 96 | Stack 97 | ---------------------- <- Initial stack pointer (grows to lower addresses) 98 | Stack Guard 99 | ---------------------- <- upper addresses 100 | 101 | The thread control block contains the information relative to the current thread (status, flags, etc). The thread ID (returned by pthread_self()) is simply a pointer to the TCB. This enables distributed thread create/join, and there are no global structures for tracking thread data. 102 | 103 | The "real" TCB is the TCB defined by LinuxThreads or NPTL. We don't use it directly and initialize its contents to 0, but reserve some space because some variables (most notably errno) are in this area. 104 | 105 | The thread block conforms to the TLS ABI for x86-64 and SPARC architectures, which follows variant II as described in http://people.redhat.com/drepper/tls.pdf If you wish to extend this to other architectures, e.g. Alpha, be sure to read sections 1-3 of that document. Also, most of the code for the TLS part of the library is taken from glibc2.3.6 (search for the __libc_setup_tls function). 106 | 107 | In pthread_create, the parent mmaps the thread block, populates the TCB, and spawns a new child. The child sets up TLS before starting execution. In joinable threads, it is the responsibility of the parent to munmap the thread block. If the thread is detached, it will munmap its own thread block when exiting. The thread block is allocated with mmap because 1) it is a sizable chunk of memory and 2) this way, the child can delete its own stack (since munmap is a system call, not a function call, a stack is not required on return). 108 | 109 | The library includes function aliases, extra definitions, libc-specific keys and initialization code to work correctly with glibc. When linking with this library, glibc function calls *should* be MT-safe. However, how libc and libpthread interact has changed over time, and this may not work correctly with glibc versions >2.6. Modifying the code to support other versions of glibc should be straightworward, if you have some idea for what you are doing. I recommend using nm (to see what symbols are defined) and objdump (to dissasemble) on whatever version of libc.a you'll be using, and see if there is any mismatch with the "glibc glue" code in the library (mostly, at the end of pthread.c). 110 | 111 | 112 | -------------------------------------------------------------------------------- /profiling_hooks.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | /* Profiling hooks used by m5threads to measure synchronization usage */ 23 | 24 | //TODO: Profiling hooks for non-M5 mode 25 | 26 | #if defined(M5_PROFILING) 27 | 28 | /* M5 profiling syscall asm */ 29 | #if defined (__sparc) 30 | #define m5_prof_syscall(syscall_num, arg) __asm__ __volatile__ ( \ 31 | "mov " #syscall_num ", %%g1\n\t" \ 32 | "mov %0, %%o0\n\t" \ 33 | "ta 0x6d\n\t" \ 34 | :: "r"(arg) : "g1", "o0" \ 35 | ); 36 | #else 37 | #error "M5 profiling hooks not implemented for your architecture, write them" 38 | #endif 39 | 40 | #define PROFILE_LOCK_START(addr) m5_prof_syscall(1040, addr) 41 | #define PROFILE_LOCK_END(addr) m5_prof_syscall(1041, addr) 42 | 43 | #define PROFILE_UNLOCK_START(addr) m5_prof_syscall(1042, addr) 44 | #define PROFILE_UNLOCK_END(addr) m5_prof_syscall(1043, addr) 45 | 46 | #define PROFILE_BARRIER_WAIT_START(addr) m5_prof_syscall(1044, addr) 47 | #define PROFILE_BARRIER_WAIT_END(addr) m5_prof_syscall(1045, addr) 48 | 49 | #define PROFILE_COND_WAIT_START(addr) m5_prof_syscall(1046, addr) 50 | #define PROFILE_COND_WAIT_END(addr) m5_prof_syscall(1047, addr) 51 | 52 | #else 53 | /* Empty hooks */ 54 | #define PROFILE_LOCK_START(addr) 55 | #define PROFILE_LOCK_END(addr) 56 | 57 | #define PROFILE_UNLOCK_START(addr) 58 | #define PROFILE_UNLOCK_END(addr) 59 | 60 | #define PROFILE_BARRIER_WAIT_START(addr) 61 | #define PROFILE_BARRIER_WAIT_END(addr) 62 | 63 | #define PROFILE_COND_WAIT_START(addr) 64 | #define PROFILE_COND_WAIT_END(addr) 65 | #endif 66 | 67 | -------------------------------------------------------------------------------- /pthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | //Spinlock assembly 37 | #if defined(__x86) || defined(__x86_64) 38 | #include "spinlock_x86.h" 39 | #elif defined(__alpha) 40 | #include "spinlock_alpha.h" 41 | #elif defined(__sparc) 42 | #include "spinlock_sparc.h" 43 | #elif defined (__arm__) 44 | #include "spinlock_arm.h" 45 | #else 46 | #error "spinlock routines not available for your arch!\n" 47 | #endif 48 | 49 | #include "pthread_defs.h" 50 | #include "tls_defs.h" 51 | #include "profiling_hooks.h" 52 | 53 | #define restrict 54 | 55 | //64KB stack, change to your taste... 56 | #define CHILD_STACK_BITS 16 57 | #define CHILD_STACK_SIZE (1 << CHILD_STACK_BITS) 58 | 59 | //Debug macro 60 | #ifdef __DEBUG 61 | #define DEBUG(args...) printf(args) 62 | #else 63 | #define DEBUG(args...) 64 | #endif 65 | 66 | //Size and alignment requirements of "real" (NPTL/LinuxThreads) thread control block 67 | #define NPTL_TCB_SIZE 1184 // sizeof (struct pthread) 68 | #define NPTL_TCB_ALIGN sizeof(double) 69 | #define NPTL_TCBHEAD_T_SIZE (sizeof(tcbhead_t)) 70 | 71 | //Thread control structure 72 | typedef struct { 73 | pthread_t tid; 74 | unsigned int is_detached; //0 if joinable, 1 if detached 75 | volatile int child_finished; 76 | void* result; //written by child on exit 77 | void *(*start_routine)(void*); 78 | void* arg; 79 | //thread block limits 80 | void* tls_start_addr; 81 | void* stack_start_addr; 82 | } pthread_tcb_t; 83 | 84 | 85 | //Information about the thread block (TLS, sizes) 86 | static struct { 87 | size_t tls_memsz; 88 | size_t tls_filesz; 89 | void* tls_initimage; 90 | size_t tls_align; 91 | size_t total_size; 92 | size_t stack_guard_size; 93 | } thread_block_info; 94 | 95 | 96 | /* Thread-local data */ 97 | 98 | //Pointer to our TCB (NULL for main thread) 99 | __thread pthread_tcb_t* __tcb; 100 | 101 | // Used for TSD (getspecific, setspecific, etc.) 102 | __thread void** pthread_specifics = NULL; //dynamically allocated, since this is rarely used 103 | __thread uint32_t pthread_specifics_size = 0; 104 | 105 | 106 | /* Initialization, create/exit/join functions */ 107 | 108 | // Search ELF segments, pull out TLS block info, campute thread block sizes 109 | static void populate_thread_block_info() { 110 | ElfW(Phdr) *phdr; 111 | 112 | //If there is no TLS segment... 113 | thread_block_info.tls_memsz = 0; 114 | thread_block_info.tls_filesz = 0; 115 | thread_block_info.tls_initimage = NULL; 116 | thread_block_info.tls_align = 0; 117 | 118 | /* Look through the TLS segment if there is any. */ 119 | if (_dl_phdr != NULL) { 120 | for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) { 121 | if (phdr->p_type == PT_TLS) { 122 | /* Gather the values we need. */ 123 | thread_block_info.tls_memsz = phdr->p_memsz; 124 | thread_block_info.tls_filesz = phdr->p_filesz; 125 | thread_block_info.tls_initimage = (void *) phdr->p_vaddr; 126 | thread_block_info.tls_align = phdr->p_align; 127 | break; 128 | } 129 | } 130 | } 131 | 132 | //Set a stack guard size 133 | //In SPARC, this is actually needed to avoid out-of-range accesses on register saves... 134 | //Largest I have seen is 2048 (sparc64) 135 | //You could avoid this in theory by compiling with -mnostack-bias 136 | thread_block_info.stack_guard_size = 2048; 137 | 138 | //Total thread block size -- this is what we'll request to mmap 139 | #if TLS_TCB_AT_TP 140 | size_t sz = sizeof(pthread_tcb_t) + thread_block_info.tls_memsz + NPTL_TCBHEAD_T_SIZE + thread_block_info.stack_guard_size + CHILD_STACK_SIZE; 141 | #elif TLS_DTV_AT_TP 142 | size_t sz = sizeof(pthread_tcb_t) + thread_block_info.tls_memsz + NPTL_TCB_SIZE + NPTL_TCBHEAD_T_SIZE + thread_block_info.stack_guard_size + CHILD_STACK_SIZE; 143 | #else 144 | #error "TLS_TCB_AT_TP xor TLS_DTV_AT_TP must be defined" 145 | #endif 146 | //Note that TCB_SIZE is the "real" TCB size, not ours, which we leave zeroed (but some variables, notably errno, are somewhere inside there) 147 | 148 | //Align to multiple of CHILD_STACK_SIZE 149 | sz += CHILD_STACK_SIZE - 1; 150 | thread_block_info.total_size = (sz>>CHILD_STACK_BITS)<tls_start_addr); 221 | __tcb = tcb; 222 | DEBUG("Child in trampoline, TID=%llx\n", tcb->tid); 223 | 224 | void* result = tcb->start_routine(tcb->arg); 225 | pthread_exit(result); 226 | assert(0); //should never be reached 227 | } 228 | 229 | int pthread_create (pthread_t* thread, 230 | const pthread_attr_t* attr, 231 | void *(*start_routine)(void*), 232 | void* arg) { 233 | DEBUG("pthread_create: start\n"); 234 | 235 | //Allocate the child thread block (TCB+TLS+stack area) 236 | //We use mmap so that the child can munmap it at exit without using a stack (it's a system call) 237 | void* thread_block; 238 | size_t thread_block_size = thread_block_info.total_size; 239 | thread_block = mmap(0, thread_block_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 240 | DEBUG("pthread_create: mmapped child thread block 0x%llx -- 0x%llx\n", thread_block, ((char*)thread_block) + CHILD_STACK_SIZE) ; 241 | 242 | //Populate the thread control block 243 | pthread_tcb_t* tcb = (pthread_tcb_t*) thread_block; 244 | tcb->tid = (pthread_t) thread_block; //thread ID is tcb address itself 245 | tcb->is_detached = 0; //joinable 246 | tcb->child_finished = 0; 247 | tcb->start_routine = start_routine; 248 | tcb->arg = arg; 249 | tcb->tls_start_addr = (void*)(((char*)thread_block) + sizeof(pthread_tcb_t)); //right after m5's tcb 250 | tcb->stack_start_addr = (void*) (((char*) thread_block) + thread_block_size - thread_block_info.stack_guard_size); //end of thread_block 251 | 252 | *thread=(pthread_t) thread_block; 253 | 254 | //Call clone() 255 | DEBUG("pthread_create: prior to clone()\n"); 256 | clone(__pthread_trampoline, tcb->stack_start_addr, CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD, tcb); 257 | DEBUG("pthread_create: after clone()\n"); 258 | return 0; 259 | } 260 | 261 | pthread_t pthread_self() { 262 | if (__tcb == NULL) return 0; //main thread 263 | return __tcb->tid; 264 | } 265 | 266 | int pthread_join (pthread_t thread, void** status) { 267 | DEBUG("pthread_join: started\n"); 268 | pthread_tcb_t* child_tcb = (pthread_tcb_t*) thread; 269 | assert(child_tcb->tid == thread); // checks that this is really a tcb 270 | assert(!child_tcb->is_detached); // thread should be joinable 271 | volatile int child_done = 0; 272 | while (child_done == 0) { // spin until child done 273 | child_done = child_tcb->child_finished; 274 | } 275 | DEBUG("pthread_join: child joined\n"); 276 | //Get result 277 | if (status) *status = child_tcb->result; 278 | 279 | //Deallocate child block 280 | //munmap(child_tcb, thread_block_info.total_size); 281 | 282 | return 0; 283 | 284 | } 285 | 286 | 287 | void pthread_exit (void* status) { 288 | // TODO: The good way to solve this is to have the child, not its parent, free 289 | // its own stack (and TLS segment). This enables detached threads. But to do this 290 | // you need an extra stack. A way to do this is to have a global, lock-protected 291 | // manager stack, or have the M5 exit system call do it... Anyhow, I'm deferring 292 | // this problem until we have TLS. 293 | 294 | //From point (XXX) on, the thread **does not exist**, 295 | //as its parent may have already freed the stack. 296 | //So we must call sys_exit without using the stack => asm 297 | 298 | // NOTE: You may be tempted to call exit(0) or _exit(0) here, but there call exit_group, 299 | // killing the whole process and not just the current thread 300 | 301 | //If the keys array was allocated, free it 302 | if (pthread_specifics != NULL) free(pthread_specifics); 303 | 304 | //Main thread 305 | if (__tcb == NULL) _exit(0); 306 | 307 | DEBUG("Child TID=0x%llx in pthread_exit...\n", pthread_self() ); 308 | __tcb->result = status; 309 | //TODO mem barrier here... 310 | __tcb->child_finished = 1; 311 | //XXX 312 | syscall(__NR_exit,0); 313 | assert(0); //should never be reached 314 | 315 | /*#if defined(__x86) or defined(__x86_64) 316 | __asm__ __volatile__ ( 317 | "\nmov $0x3c,%%eax\n\t" \ 318 | "syscall\n\t" 319 | ::: "eax"); 320 | #elif defined(__alpha) 321 | __asm__ __volatile__ ( 322 | "\nldi $0,1\n\t" \ 323 | "callsys\n\t"); 324 | #elif defined(__sparc) 325 | // Since this part of the code is provisional, don't bother with asm for now 326 | syscall(__NR_exit,0); 327 | #else 328 | #error "No pthread_exit asm for your arch, sorry!\n" 329 | #endif 330 | 331 | assert(0);*/ 332 | } 333 | 334 | 335 | // mutex functions 336 | 337 | int pthread_mutex_init (pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) { 338 | DEBUG("%s: start\n", __FUNCTION__); 339 | mutex->PTHREAD_MUTEX_T_COUNT = 0; 340 | return 0; 341 | } 342 | 343 | int pthread_mutex_lock (pthread_mutex_t* lock) { 344 | DEBUG("%s: start\n", __FUNCTION__); 345 | PROFILE_LOCK_START(lock); 346 | spin_lock((int*)&lock->PTHREAD_MUTEX_T_COUNT); 347 | PROFILE_LOCK_END(lock); 348 | return 0; 349 | } 350 | 351 | int pthread_mutex_unlock (pthread_mutex_t* lock) { 352 | DEBUG("%s: start\n", __FUNCTION__); 353 | PROFILE_UNLOCK_START(lock); 354 | spin_unlock((int*)&lock->PTHREAD_MUTEX_T_COUNT); 355 | PROFILE_UNLOCK_END(lock); 356 | return 0; 357 | } 358 | 359 | int pthread_mutex_destroy (pthread_mutex_t* mutex) { 360 | DEBUG("%s: start\n", __FUNCTION__); 361 | return 0; 362 | } 363 | 364 | int pthread_mutex_trylock (pthread_mutex_t* mutex) { 365 | DEBUG("%s: start\n", __FUNCTION__); 366 | int acquired = trylock((int*)&mutex->PTHREAD_MUTEX_T_COUNT); 367 | if (acquired == 1) { 368 | //Profiling not really accurate here... 369 | PROFILE_LOCK_START(mutex); 370 | PROFILE_LOCK_END(mutex); 371 | return 0; 372 | } 373 | return EBUSY; 374 | } 375 | 376 | // rwlock functions 377 | 378 | int pthread_rwlock_init (pthread_rwlock_t* lock, const pthread_rwlockattr_t* attr) { 379 | DEBUG("%s: start\n", __FUNCTION__); 380 | PTHREAD_RWLOCK_T_LOCK(lock) = 0; // used only with spin_lock, so we know to initilize to zero 381 | PTHREAD_RWLOCK_T_READERS(lock) = 0; 382 | PTHREAD_RWLOCK_T_WRITER(lock) = -1; // -1 means no one owns the write lock 383 | 384 | return 0; 385 | } 386 | 387 | int pthread_rwlock_destroy (pthread_rwlock_t* lock) { 388 | DEBUG("%s: start\n", __FUNCTION__); 389 | return 0; 390 | } 391 | 392 | int pthread_rwlock_rdlock (pthread_rwlock_t* lock) { 393 | DEBUG("%s: start\n", __FUNCTION__); 394 | PROFILE_LOCK_START(lock); 395 | do { 396 | // this is to reduce the contention and a possible live-lock to lock->access_lock 397 | while (1) { 398 | pthread_t writer = PTHREAD_RWLOCK_T_WRITER(lock); 399 | if (writer == -1) { 400 | break; 401 | } 402 | } 403 | 404 | spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 405 | if ((pthread_t)PTHREAD_RWLOCK_T_WRITER(lock) == -1) { 406 | PTHREAD_RWLOCK_T_READERS(lock)++; 407 | spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 408 | PROFILE_LOCK_END(lock); 409 | return 0; 410 | } 411 | spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 412 | } while (1); 413 | PROFILE_LOCK_END(lock); 414 | return 0; 415 | } 416 | 417 | int pthread_rwlock_wrlock (pthread_rwlock_t* lock) { 418 | DEBUG("%s: start\n", __FUNCTION__); 419 | PROFILE_LOCK_START(lock); 420 | do { 421 | while (1) { 422 | pthread_t writer = PTHREAD_RWLOCK_T_WRITER(lock); 423 | if (writer == -1) { 424 | break; 425 | } 426 | int num_readers = PTHREAD_RWLOCK_T_READERS(lock); 427 | if (num_readers == 0) { 428 | break; 429 | } 430 | } 431 | 432 | spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 433 | if ((pthread_t)PTHREAD_RWLOCK_T_WRITER(lock) == -1 && PTHREAD_RWLOCK_T_READERS(lock) == 0) { 434 | PTHREAD_RWLOCK_T_WRITER(lock) = pthread_self(); 435 | spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 436 | PROFILE_LOCK_END(lock); 437 | return 0; 438 | } 439 | spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 440 | } while (1); 441 | PROFILE_LOCK_END(lock); 442 | return 0; 443 | } 444 | 445 | int pthread_rwlock_unlock (pthread_rwlock_t* lock) { 446 | DEBUG("%s: start\n", __FUNCTION__); 447 | PROFILE_UNLOCK_START(lock); 448 | spin_lock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 449 | if (pthread_self() == PTHREAD_RWLOCK_T_WRITER(lock)) { 450 | // the write lock will be released 451 | PTHREAD_RWLOCK_T_WRITER(lock) = -1; 452 | } else { 453 | // one of the read locks will be released 454 | PTHREAD_RWLOCK_T_READERS(lock) = PTHREAD_RWLOCK_T_READERS(lock) - 1; 455 | } 456 | spin_unlock((int*)&(PTHREAD_RWLOCK_T_LOCK(lock))); 457 | PROFILE_UNLOCK_END(lock); 458 | return 0; 459 | } 460 | 461 | 462 | // key functions 463 | #ifndef PTHREAD_KEYS_MAX 464 | #define PTHREAD_KEYS_MAX 1024 465 | #endif 466 | 467 | typedef struct { 468 | int in_use; 469 | void (*destr)(void*); 470 | } pthread_key_struct; 471 | 472 | static pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX]; 473 | static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER; 474 | 475 | int pthread_key_create (pthread_key_t* key, void (*destructor)(void*)) { 476 | int i; 477 | DEBUG("%s: start\n", __FUNCTION__); 478 | 479 | pthread_mutex_lock(&pthread_keys_mutex); 480 | for (i = 0; i < PTHREAD_KEYS_MAX; i++) { 481 | if (! pthread_keys[i].in_use) { 482 | /* Mark key in use */ 483 | pthread_keys[i].in_use = 1; 484 | pthread_keys[i].destr = destructor; 485 | pthread_mutex_unlock(&pthread_keys_mutex); 486 | *key = i; 487 | return 0; 488 | } 489 | } 490 | pthread_mutex_unlock(&pthread_keys_mutex); 491 | return EAGAIN; 492 | } 493 | 494 | int pthread_key_delete (pthread_key_t key) 495 | { 496 | DEBUG("%s: start\n", __FUNCTION__); 497 | pthread_mutex_lock(&pthread_keys_mutex); 498 | if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) { 499 | pthread_mutex_unlock(&pthread_keys_mutex); 500 | return EINVAL; 501 | } 502 | pthread_keys[key].in_use = 0; 503 | pthread_keys[key].destr = NULL; 504 | 505 | /* NOTE: The LinuxThreads implementation actually zeroes deleted keys on 506 | spawned threads. I don't care, the spec says that if you are access a 507 | key after if has been deleted, you're on your own. */ 508 | 509 | pthread_mutex_unlock(&pthread_keys_mutex); 510 | return 0; 511 | } 512 | 513 | int pthread_setspecific (pthread_key_t key, const void* value) { 514 | int m_size; 515 | DEBUG("%s: start\n", __FUNCTION__); 516 | if (key < 0 || key >= PTHREAD_KEYS_MAX) return EINVAL; 517 | if (pthread_specifics_size == 0) { 518 | pthread_specifics = (void**) calloc(PTHREAD_KEYS_MAX + 1, sizeof(void*)); 519 | DEBUG("pthread_setspecific: malloc of size %d bytes, got 0x%llx\n", m_size, pthread_specifics); 520 | pthread_specifics_size = key+1; 521 | } 522 | pthread_specifics[key] = (void*) value; 523 | return 0; 524 | } 525 | 526 | void* pthread_getspecific (pthread_key_t key) { 527 | if (key < 0 || key >= pthread_specifics_size) return NULL; 528 | DEBUG("pthread_getspecific: key=%d pthread_specifics_size=%d\n", key, pthread_specifics_size); 529 | return pthread_specifics[key]; 530 | } 531 | 532 | // condition variable functions 533 | 534 | int pthread_cond_init (pthread_cond_t* cond, const pthread_condattr_t* attr) { 535 | DEBUG("%s: start\n", __FUNCTION__); 536 | PTHREAD_COND_T_FLAG(cond) = 0; 537 | PTHREAD_COND_T_THREAD_COUNT(cond) = 0; 538 | PTHREAD_COND_T_COUNT_LOCK(cond) = 0; 539 | return 0; 540 | } 541 | 542 | int pthread_cond_destroy (pthread_cond_t* cond) { 543 | DEBUG("%s: start\n", __FUNCTION__); 544 | return 0; 545 | } 546 | 547 | int pthread_cond_broadcast (pthread_cond_t* cond) { 548 | DEBUG("%s: start\n", __FUNCTION__); 549 | PTHREAD_COND_T_FLAG(cond) = 1; 550 | return 0; 551 | } 552 | 553 | int pthread_cond_wait (pthread_cond_t* cond, pthread_mutex_t* lock) { 554 | DEBUG("%s: start\n", __FUNCTION__); 555 | PROFILE_COND_WAIT_START(cond); 556 | volatile int* thread_count = &(PTHREAD_COND_T_THREAD_COUNT(cond)); 557 | volatile int* flag = &(PTHREAD_COND_T_FLAG(cond)); 558 | volatile int* count_lock = &(PTHREAD_COND_T_COUNT_LOCK(cond)); 559 | 560 | // dsm: ++/-- have higher precedence than *, so *thread_count++ 561 | // increments *the pointer*, then dereferences it (!) 562 | (*thread_count)++; 563 | 564 | pthread_mutex_unlock(lock); 565 | while (1) { 566 | volatile int f = *flag; 567 | if (f == 1) { 568 | break; 569 | } 570 | } 571 | 572 | spin_lock(count_lock); 573 | 574 | (*thread_count)--; 575 | 576 | if (*thread_count == 0) { 577 | *flag = 0; 578 | } 579 | spin_unlock(count_lock); 580 | pthread_mutex_lock(lock); 581 | PROFILE_COND_WAIT_END(cond); 582 | return 0; 583 | } 584 | 585 | int pthread_cond_signal (pthread_cond_t* cond) { 586 | DEBUG("%s: start\n", __FUNCTION__); 587 | //Could also signal only one thread, but this is compliant too 588 | //TODO: Just wake one thread up 589 | return pthread_cond_broadcast(cond); 590 | } 591 | 592 | 593 | //barrier functions 594 | 595 | //These funny tree barriers will only work with consecutive TIDs starting from 0, e.g. a barrier initialized for 8 thread will need to be taken by TIDs 0-7 596 | //TODO: Adapt to work with arbitrary TIDs 597 | /*int pthread_barrier_init (pthread_barrier_t *restrict barrier, 598 | const pthread_barrierattr_t *restrict attr, unsigned count) 599 | { 600 | assert(barrier != NULL); 601 | //assert(0 < count && count <= MAX_NUM_CPUS); 602 | 603 | PTHREAD_BARRIER_T_NUM_THREADS(barrier) = count; 604 | 605 | // add one to avoid false sharing 606 | tree_barrier_t* ptr 607 | = ((tree_barrier_t*)malloc((count + 1) * sizeof(tree_barrier_t))) + 1; 608 | for (unsigned i = 0; i < count; ++i) { 609 | ptr[i].value = 0; 610 | } 611 | 612 | PTHREAD_BARRIER_T_BARRIER_PTR(barrier) = ptr; 613 | 614 | return 0; 615 | } 616 | 617 | int pthread_barrier_destroy (pthread_barrier_t *barrier) 618 | { 619 | free(PTHREAD_BARRIER_T_BARRIER_PTR(barrier) - 1); 620 | return 0; 621 | } 622 | 623 | int pthread_barrier_wait (pthread_barrier_t* barrier) 624 | { 625 | int const num_threads = PTHREAD_BARRIER_T_NUM_THREADS(barrier); 626 | int const self = pthread_self(); 627 | tree_barrier_t * const barrier_ptr = PTHREAD_BARRIER_T_BARRIER_PTR(barrier); 628 | 629 | int const goal = 1 - barrier_ptr[self].value; 630 | 631 | int round_mask = 3; 632 | while ((self & round_mask) == 0 && round_mask < (num_threads << 2)) { 633 | int const spacing = (round_mask + 1) >> 2; 634 | for (int i = 1; i <= 3 && self + i*spacing < num_threads; ++i) { 635 | while (barrier_ptr[self + i*spacing].value != goal) { 636 | // spin 637 | } 638 | } 639 | round_mask = (round_mask << 2) + 3; 640 | } 641 | 642 | barrier_ptr[self].value = goal; 643 | while (barrier_ptr[0].value != goal) { 644 | // spin 645 | } 646 | 647 | return 0; 648 | }*/ 649 | 650 | int pthread_barrier_init (pthread_barrier_t *restrict barrier, 651 | const pthread_barrierattr_t *restrict attr, unsigned count) 652 | { 653 | assert(barrier != NULL); 654 | DEBUG("%s: start\n", __FUNCTION__); 655 | 656 | PTHREAD_BARRIER_T_NUM_THREADS(barrier) = count; 657 | PTHREAD_BARRIER_T_SPINLOCK(barrier) = 0; 658 | PTHREAD_BARRIER_T_COUNTER(barrier) = 0; 659 | PTHREAD_BARRIER_T_DIRECTION(barrier) = 0; //up 660 | 661 | return 0; 662 | } 663 | 664 | int pthread_barrier_destroy (pthread_barrier_t *barrier) 665 | { 666 | DEBUG("%s: start\n", __FUNCTION__); 667 | //Nothing to do 668 | return 0; 669 | } 670 | 671 | int pthread_barrier_wait (pthread_barrier_t* barrier) 672 | { 673 | DEBUG("%s: start\n", __FUNCTION__); 674 | PROFILE_BARRIER_WAIT_START(barrier); 675 | int const initial_direction = PTHREAD_BARRIER_T_DIRECTION(barrier); //0 == up, 1 == down 676 | 677 | if (initial_direction == 0) { 678 | spin_lock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier))); 679 | PTHREAD_BARRIER_T_COUNTER(barrier)++; 680 | if (PTHREAD_BARRIER_T_COUNTER(barrier) == PTHREAD_BARRIER_T_NUM_THREADS(barrier)) { 681 | //reverse direction, now down 682 | PTHREAD_BARRIER_T_DIRECTION(barrier) = 1; 683 | } 684 | spin_unlock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier))); 685 | } else { 686 | spin_lock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier))); 687 | PTHREAD_BARRIER_T_COUNTER(barrier)--; 688 | if (PTHREAD_BARRIER_T_COUNTER(barrier) == 0) { 689 | //reverse direction, now up 690 | PTHREAD_BARRIER_T_DIRECTION(barrier) = 0; 691 | } 692 | spin_unlock(&(PTHREAD_BARRIER_T_SPINLOCK(barrier))); 693 | } 694 | 695 | volatile int direction = PTHREAD_BARRIER_T_DIRECTION(barrier); 696 | while (initial_direction == direction) { 697 | //spin 698 | direction = PTHREAD_BARRIER_T_DIRECTION(barrier); 699 | } 700 | PROFILE_BARRIER_WAIT_END(barrier); 701 | return 0; 702 | } 703 | 704 | //misc functions 705 | 706 | static pthread_mutex_t __once_mutex = PTHREAD_MUTEX_INITIALIZER; 707 | int pthread_once (pthread_once_t* once, 708 | void (*init)(void)) 709 | { 710 | DEBUG("%s: start\n", __FUNCTION__); 711 | //fast path 712 | if (*once != PTHREAD_ONCE_INIT) return 0; 713 | pthread_mutex_lock(&__once_mutex); 714 | if (*once != PTHREAD_ONCE_INIT) { 715 | pthread_mutex_unlock(&__once_mutex); 716 | return 0; 717 | } 718 | *once = PTHREAD_ONCE_INIT+1; 719 | pthread_mutex_unlock(&__once_mutex); 720 | init(); 721 | return 0; 722 | } 723 | 724 | #ifndef __USE_EXTERN_INLINES 725 | int pthread_equal (pthread_t t1, pthread_t t2) 726 | { 727 | return t1 == t2; //that was hard :-) 728 | } 729 | #endif 730 | 731 | // Functions that we want defined, but we don't use them 732 | // All other functions are not defined so that they will cause a compile time 733 | // error and we can decide if we need to do something with them 734 | 735 | // functions really don't need to do anything 736 | 737 | int pthread_yield() { 738 | DEBUG("%s: start\n", __FUNCTION__); 739 | // nothing else to yield to 740 | return 0; 741 | } 742 | 743 | int pthread_attr_init (pthread_attr_t* attr) { 744 | DEBUG("%s: start\n", __FUNCTION__); 745 | return 0; 746 | } 747 | 748 | int pthread_attr_setscope (pthread_attr_t* attr, int scope) { 749 | DEBUG("%s: start\n", __FUNCTION__); 750 | return 0; 751 | } 752 | 753 | int pthread_rwlockattr_init (pthread_rwlockattr_t* attr) { 754 | DEBUG("%s: start\n", __FUNCTION__); 755 | return 0; 756 | } 757 | 758 | int pthread_attr_setstacksize (pthread_attr_t* attr, size_t stacksize) { 759 | DEBUG("%s: start\n", __FUNCTION__); 760 | return 0; 761 | } 762 | 763 | int pthread_attr_setschedpolicy (pthread_attr_t* attr, int policy) { 764 | DEBUG("%s: start\n", __FUNCTION__); 765 | return 0; 766 | } 767 | 768 | // some functions that we don't really support 769 | 770 | int pthread_setconcurrency (int new_level) { 771 | DEBUG("%s: start\n", __FUNCTION__); 772 | return 0; 773 | } 774 | 775 | int pthread_setcancelstate (int p0, int* p1) 776 | { 777 | DEBUG("%s: start\n", __FUNCTION__); 778 | //NPTL uses this 779 | return 0; 780 | } 781 | 782 | //and some affinity functions (used by libgomp, openmp) 783 | int pthread_getaffinity_np(pthread_t thread, size_t size, cpu_set_t *set) { 784 | DEBUG("%s: start\n", __FUNCTION__); 785 | char *p = (char*)set; 786 | while ( size-- ) *p++ = 0; 787 | return 0; 788 | } 789 | 790 | int pthread_setaffinity_np(pthread_t thread, size_t size, cpu_set_t *set) { 791 | DEBUG("%s: start\n", __FUNCTION__); 792 | return 0; 793 | } 794 | 795 | int pthread_attr_setaffinity_np(pthread_attr_t attr, size_t cpusetsize, const cpu_set_t *cpuset) { 796 | DEBUG("%s: start\n", __FUNCTION__); 797 | return 0; 798 | } 799 | 800 | int pthread_attr_getaffinity_np(pthread_attr_t attr, size_t cpusetsize, cpu_set_t *cpuset) { 801 | DEBUG("%s: start\n", __FUNCTION__); 802 | return 0; 803 | } 804 | 805 | 806 | // ... including any dealing with thread-level signal handling 807 | // (maybe we should throw an error message instead?) 808 | 809 | int pthread_sigmask (int how, const sigset_t* set, sigset_t* oset) { 810 | DEBUG("%s: start\n", __FUNCTION__); 811 | return 0; 812 | } 813 | 814 | int pthread_kill (pthread_t thread, int sig) { 815 | assert(0); 816 | } 817 | 818 | // unimplemented pthread functions 819 | 820 | int pthread_atfork (void (*f0)(void), 821 | void (*f1)(void), 822 | void (*f2)(void)) 823 | { 824 | assert(0); 825 | } 826 | 827 | int pthread_attr_destroy (pthread_attr_t* attr) 828 | { 829 | assert(0); 830 | } 831 | 832 | int pthread_attr_getdetachstate (const pthread_attr_t* attr, 833 | int* b) 834 | { 835 | assert(0); 836 | } 837 | 838 | int pthread_attr_getguardsize (const pthread_attr_t* restrict a, 839 | size_t *restrict b) 840 | { 841 | assert(0); 842 | } 843 | 844 | int pthread_attr_getinheritsched (const pthread_attr_t *restrict a, 845 | int *restrict b) 846 | { 847 | assert(0); 848 | } 849 | 850 | int pthread_attr_getschedparam (const pthread_attr_t *restrict a, 851 | struct sched_param *restrict b) 852 | { 853 | assert(0); 854 | } 855 | 856 | int pthread_attr_getschedpolicy (const pthread_attr_t *restrict a, 857 | int *restrict b) 858 | { 859 | assert(0); 860 | } 861 | 862 | int pthread_attr_getscope (const pthread_attr_t *restrict a, 863 | int *restrict b) 864 | { 865 | assert(0); 866 | } 867 | 868 | int pthread_attr_getstack (const pthread_attr_t *restrict a, 869 | void* *restrict b, 870 | size_t *restrict c) 871 | { 872 | assert(0); 873 | } 874 | 875 | int pthread_attr_getstackaddr (const pthread_attr_t *restrict a, 876 | void* *restrict b) 877 | { 878 | assert(0); 879 | } 880 | 881 | int pthread_attr_getstacksize (const pthread_attr_t *restrict a, 882 | size_t *restrict b) 883 | { 884 | assert(0); 885 | } 886 | 887 | int pthread_attr_setdetachstate (pthread_attr_t* a, 888 | int b) 889 | { 890 | return 0; //FIXME 891 | } 892 | int pthread_attr_setguardsize (pthread_attr_t* a, 893 | size_t b) 894 | { 895 | assert(0); 896 | } 897 | 898 | int pthread_attr_setinheritsched (pthread_attr_t* a, 899 | int b) 900 | { 901 | assert(0); 902 | } 903 | 904 | int pthread_attr_setschedparam (pthread_attr_t *restrict a, 905 | const struct sched_param *restrict b) 906 | { 907 | assert(0); 908 | } 909 | 910 | int pthread_attr_setstack (pthread_attr_t* a, 911 | void* b, 912 | size_t c) 913 | { 914 | assert(0); 915 | } 916 | 917 | int pthread_attr_setstackaddr (pthread_attr_t* a, 918 | void* b) 919 | { 920 | assert(0); 921 | } 922 | 923 | int pthread_cancel (pthread_t a) 924 | { 925 | assert(0); 926 | } 927 | 928 | void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, 929 | void (*__routine) (void *), 930 | void *__arg) 931 | { 932 | assert(0); 933 | } 934 | 935 | void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, 936 | int __execute) 937 | { 938 | assert(0); 939 | } 940 | 941 | int pthread_cond_timedwait (pthread_cond_t *restrict a, 942 | pthread_mutex_t *restrict b, 943 | const struct timespec *restrict c) 944 | { 945 | assert(0); 946 | } 947 | 948 | int pthread_condattr_destroy (pthread_condattr_t* a) 949 | { 950 | assert(0); 951 | } 952 | 953 | int pthread_condattr_getpshared (const pthread_condattr_t *restrict a, 954 | int *restrict b) 955 | { 956 | assert(0); 957 | } 958 | 959 | int pthread_condattr_init (pthread_condattr_t* a) 960 | { 961 | assert(0); 962 | } 963 | 964 | int pthread_condattr_setpshared (pthread_condattr_t* a, 965 | int b) 966 | { 967 | assert(0); 968 | } 969 | 970 | int pthread_detach (pthread_t a) 971 | { 972 | assert(0); 973 | } 974 | 975 | 976 | int pthread_getconcurrency () 977 | { 978 | assert(0); 979 | } 980 | 981 | int pthread_getschedparam(pthread_t a, 982 | int *restrict b, 983 | struct sched_param *restrict c) 984 | { 985 | assert(0); 986 | } 987 | 988 | int pthread_mutex_getprioceiling (const pthread_mutex_t *restrict a, 989 | int *restrict b) 990 | { 991 | assert(0); 992 | } 993 | 994 | int pthread_mutex_setprioceiling (pthread_mutex_t *restrict a, 995 | int b, 996 | int *restrict c) 997 | { 998 | assert(0); 999 | } 1000 | 1001 | int pthread_mutex_timedlock (pthread_mutex_t* a, 1002 | const struct timespec* b) 1003 | { 1004 | assert(0); 1005 | } 1006 | 1007 | int pthread_mutexattr_destroy (pthread_mutexattr_t* a) 1008 | { 1009 | //assert(0); 1010 | //used by libc 1011 | return 0; 1012 | } 1013 | 1014 | int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *restrict a, 1015 | int *restrict b) 1016 | { 1017 | assert(0); 1018 | } 1019 | 1020 | int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *restrict a, 1021 | int *restrict b) 1022 | { 1023 | assert(0); 1024 | } 1025 | 1026 | int pthread_mutexattr_getpshared (const pthread_mutexattr_t *restrict a, 1027 | int *restrict b) 1028 | { 1029 | assert(0); 1030 | } 1031 | 1032 | int pthread_mutexattr_gettype (const pthread_mutexattr_t *restrict a, 1033 | int *restrict b) 1034 | { 1035 | assert(0); 1036 | } 1037 | 1038 | int pthread_mutexattr_init (pthread_mutexattr_t* a) 1039 | { 1040 | //assert(0); 1041 | //used by libc 1042 | return 0; 1043 | } 1044 | 1045 | int pthread_mutexattr_setprioceiling (pthread_mutexattr_t* a, 1046 | int b) 1047 | { 1048 | assert(0); 1049 | } 1050 | 1051 | int pthread_mutexattr_setprotocol (pthread_mutexattr_t* a, 1052 | int b) 1053 | { 1054 | assert(0); 1055 | } 1056 | 1057 | int pthread_mutexattr_setpshared (pthread_mutexattr_t* a, 1058 | int b) 1059 | { 1060 | assert(0); 1061 | } 1062 | 1063 | int pthread_mutexattr_settype (pthread_mutexattr_t* a, 1064 | int b) 1065 | { 1066 | //assert(0); 1067 | //used by libc 1068 | //yeah, and the freaking libc just needs a recursive lock.... screw it 1069 | //if (b == PTHREAD_MUTEX_RECURSIVE_NP) assert(0); 1070 | return 0; 1071 | } 1072 | 1073 | int pthread_rwlock_timedrdlock (pthread_rwlock_t *restrict a, 1074 | const struct timespec *restrict b) 1075 | { 1076 | assert(0); 1077 | } 1078 | 1079 | int pthread_rwlock_timedwrlock (pthread_rwlock_t *restrict a, 1080 | const struct timespec *restrict b) 1081 | { 1082 | assert(0); 1083 | } 1084 | 1085 | int pthread_rwlock_tryrdlock (pthread_rwlock_t* a) 1086 | { 1087 | assert(0); 1088 | } 1089 | 1090 | int pthread_rwlock_trywrlock (pthread_rwlock_t* a) 1091 | { 1092 | assert(0); 1093 | } 1094 | 1095 | int pthread_rwlockattr_destroy (pthread_rwlockattr_t* a) 1096 | { 1097 | assert(0); 1098 | } 1099 | 1100 | int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *restrict a, 1101 | int *restrict b) 1102 | { 1103 | assert(0); 1104 | } 1105 | 1106 | int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* a, 1107 | int b) 1108 | { 1109 | assert(0); 1110 | } 1111 | 1112 | int pthread_setcanceltype (int a, 1113 | int* b) 1114 | { 1115 | assert(0); 1116 | } 1117 | 1118 | int pthread_setschedparam (pthread_t a, 1119 | int b, 1120 | const struct sched_param* c) 1121 | { 1122 | assert(0); 1123 | } 1124 | 1125 | int pthread_setschedprio (pthread_t a, 1126 | int b) 1127 | { 1128 | assert(0); 1129 | } 1130 | 1131 | void pthread_testcancel () 1132 | { 1133 | assert(0); 1134 | } 1135 | 1136 | 1137 | /* Stuff to properly glue with glibc */ 1138 | 1139 | // glibc keys 1140 | 1141 | //For NPTL, or LinuxThreads with TLS defined and used 1142 | __thread void* __libc_tsd_MALLOC; 1143 | __thread void* __libc_tsd_DL_ERROR; 1144 | __thread void* __libc_tsd_RPC_VARS; 1145 | //__thread void* __libc_tsd_LOCALE; seems to be defined in my libc already, but your glibc might not dfine it... 1146 | //Defined in libgomp (OpenMP) 1147 | //__thread void* __libc_tsd_CTYPE_B; 1148 | //__thread void* __libc_tsd_CTYPE_TOLOWER; 1149 | //__thread void* __libc_tsd_CTYPE_TOUPPER; 1150 | 1151 | //If glibc was not compiled with __thread, it uses __pthread_internal_tsd_get/set/address for its internal keys 1152 | //These are from linuxthreads-0.7.1/specific.c 1153 | 1154 | //FIXME: When enabled, SPARC/M5 crashes (for some weird reason, libc calls a tsd_get on an uninitialized key at initialization, and uses its result). Are we supposed to initialize these values?? 1155 | //libc can live without these, so it's not critical 1156 | #if 0 1157 | enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0, 1158 | _LIBC_TSD_KEY_DL_ERROR, 1159 | _LIBC_TSD_KEY_RPC_VARS, 1160 | _LIBC_TSD_KEY_LOCALE, 1161 | _LIBC_TSD_KEY_CTYPE_B, 1162 | _LIBC_TSD_KEY_CTYPE_TOLOWER, 1163 | _LIBC_TSD_KEY_CTYPE_TOUPPER, 1164 | _LIBC_TSD_KEY_N }; 1165 | __thread void* p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */ 1166 | 1167 | int 1168 | __pthread_internal_tsd_set (int key, const void * pointer) 1169 | { 1170 | p_libc_specific[key] = (void*) pointer; 1171 | return 0; 1172 | } 1173 | 1174 | void * 1175 | __pthread_internal_tsd_get (int key) 1176 | { 1177 | return p_libc_specific[key]; 1178 | } 1179 | 1180 | void ** __attribute__ ((__const__)) 1181 | __pthread_internal_tsd_address (int key) 1182 | { 1183 | return &p_libc_specific[key]; 1184 | } 1185 | #endif //0 1186 | 1187 | 1188 | //Aliases for glibc 1189 | int __pthread_mutex_init (pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) __attribute__ ((weak, alias ("pthread_mutex_init"))); 1190 | int __pthread_mutex_lock (pthread_mutex_t* lock) __attribute__ ((weak, alias ("pthread_mutex_lock"))); 1191 | int __pthread_mutex_trylock (pthread_mutex_t* lock) __attribute__ ((weak, alias ("pthread_mutex_trylock"))); 1192 | int __pthread_mutex_unlock (pthread_mutex_t* lock) __attribute__ ((weak, alias ("pthread_mutex_unlock"))); 1193 | 1194 | int __pthread_mutexattr_destroy (pthread_mutexattr_t* a) __attribute__ ((weak, alias ("pthread_mutexattr_destroy"))); 1195 | int __pthread_mutexattr_init (pthread_mutexattr_t* a) __attribute__ ((weak, alias ("pthread_mutexattr_init"))); 1196 | int __pthread_mutexattr_settype (pthread_mutexattr_t* a, int b) __attribute__ ((weak, alias ("pthread_mutexattr_settype"))); 1197 | 1198 | int __pthread_rwlock_init (pthread_rwlock_t* lock, const pthread_rwlockattr_t* attr) __attribute__ ((weak, alias ("pthread_rwlock_init"))); 1199 | int __pthread_rwlock_rdlock (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_rdlock"))); 1200 | int __pthread_rwlock_wrlock (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_wrlock"))); 1201 | int __pthread_rwlock_unlock (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_unlock"))); 1202 | int __pthread_rwlock_destroy (pthread_rwlock_t* lock) __attribute__ ((weak, alias ("pthread_rwlock_destroy"))); 1203 | /* 1204 | int __pthread_key_create(pthread_key_t *, void (*)(void *)) __attribute__ ((weak, alias ("pthread_key_create"))); 1205 | int __pthread_key_delete(pthread_key_t) __attribute__ ((weak, alias ("pthread_key_delete"))); 1206 | void* __pthread_getspecific(pthread_key_t) __attribute__ ((weak, alias ("pthread_getspecific"))); 1207 | int __pthread_setspecific(pthread_key_t, const void *) __attribute__ ((weak, alias ("pthread_setspecific"))); 1208 | */ 1209 | int __pthread_once (pthread_once_t* once, void (*init)(void)) __attribute__ ((weak, alias ("pthread_once"))); 1210 | 1211 | 1212 | //No effect, NPTL-specific, may cause leaks? (TODO: Check!) 1213 | void __nptl_deallocate_tsd() {} 1214 | 1215 | 1216 | -------------------------------------------------------------------------------- /pthread_defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | 22 | #ifndef __PTHREAD_DEFS_H__ 23 | #define __PTHREAD_DEFS_H__ 24 | 25 | 26 | /*typedef struct { 27 | volatile int value; 28 | long _padding[15]; // to prevent false sharing 29 | } tree_barrier_t;*/ 30 | 31 | // old LinuxThreads needs different magic than newer NPTL implementation 32 | // definitions for LinuxThreads 33 | #ifdef __linux__ 34 | 35 | //XOPEN2K and UNIX98 defines to avoid for rwlocks/barriers when compiling with gcc... 36 | //see 37 | #if !defined(__USE_UNIX98) && !defined(__USE_XOPEN2K) && !defined(__SIZEOF_PTHREAD_MUTEX_T) 38 | /* Read-write locks. */ 39 | typedef struct _pthread_rwlock_t 40 | { 41 | struct _pthread_fastlock __rw_lock; /* Lock to guarantee mutual exclusion */ 42 | int __rw_readers; /* Number of readers */ 43 | _pthread_descr __rw_writer; /* Identity of writer, or NULL if none */ 44 | _pthread_descr __rw_read_waiting; /* Threads waiting for reading */ 45 | _pthread_descr __rw_write_waiting; /* Threads waiting for writing */ 46 | int __rw_kind; /* Reader/Writer preference selection */ 47 | int __rw_pshared; /* Shared between processes or not */ 48 | } pthread_rwlock_t; 49 | 50 | 51 | /* Attribute for read-write locks. */ 52 | typedef struct 53 | { 54 | int __lockkind; 55 | int __pshared; 56 | } pthread_rwlockattr_t; 57 | #endif 58 | #if !defined(__USE_XOPEN2K) && !defined(__SIZEOF_PTHREAD_MUTEX_T) 59 | /* POSIX spinlock data type. */ 60 | typedef volatile int pthread_spinlock_t; 61 | 62 | /* POSIX barrier. */ 63 | typedef struct { 64 | struct _pthread_fastlock __ba_lock; /* Lock to guarantee mutual exclusion */ 65 | int __ba_required; /* Threads needed for completion */ 66 | int __ba_present; /* Threads waiting */ 67 | _pthread_descr __ba_waiting; /* Queue of waiting threads */ 68 | } pthread_barrier_t; 69 | 70 | /* barrier attribute */ 71 | typedef struct { 72 | int __pshared; 73 | } pthread_barrierattr_t; 74 | 75 | #endif 76 | 77 | 78 | #ifndef __SIZEOF_PTHREAD_MUTEX_T 79 | #define PTHREAD_MUTEX_T_COUNT __m_count 80 | 81 | #define PTHREAD_COND_T_FLAG(cond) (*(volatile int*)(&(cond->__c_lock.__status))) 82 | #define PTHREAD_COND_T_THREAD_COUNT(cond) (*(volatile int*)(&(cond-> __c_waiting))) 83 | #define PTHREAD_COND_T_COUNT_LOCK(cond) (*(volatile int*)(&(cond->__c_lock.__spinlock))) 84 | 85 | #define PTHREAD_RWLOCK_T_LOCK(rwlock) (*(volatile int*)(&rwlock->__rw_lock)) 86 | #define PTHREAD_RWLOCK_T_READERS(rwlock) (*(volatile int*)(&rwlock->__rw_readers)) 87 | #define PTHREAD_RWLOCK_T_WRITER(rwlock) (*(volatile pthread_t*)(&rwlock->__rw_kind)) 88 | 89 | //For tree barriers 90 | //#define PTHREAD_BARRIER_T_NUM_THREADS(barrier) (*(int*)(&barrier->__ba_lock.__spinlock)) 91 | //#define PTHREAD_BARRIER_T_BARRIER_PTR(barrier) (*(tree_barrier_t**)(&barrier->__ba_required)) 92 | 93 | #define PTHREAD_BARRIER_T_SPINLOCK(barrier) (*(volatile int*)(&barrier->__ba_lock.__spinlock)) 94 | #define PTHREAD_BARRIER_T_NUM_THREADS(barrier) (*((volatile int*)(&barrier->__ba_required))) 95 | #define PTHREAD_BARRIER_T_COUNTER(barrier) (*((volatile int*)(&barrier->__ba_present))) 96 | #define PTHREAD_BARRIER_T_DIRECTION(barrier) (*((volatile int*)(&barrier->__ba_waiting))) 97 | 98 | // definitions for NPTL implementation 99 | #else /* __SIZEOF_PTHREAD_MUTEX_T defined */ 100 | #define PTHREAD_MUTEX_T_COUNT __data.__count 101 | 102 | #define PTHREAD_RWLOCK_T_LOCK(rwlock) (*(volatile int*)(&rwlock->__data.__lock)) 103 | #define PTHREAD_RWLOCK_T_READERS(rwlock) (*(volatile int*)(&rwlock->__data.__nr_readers)) 104 | #define PTHREAD_RWLOCK_T_WRITER(rwlock) (*(volatile int*)(&rwlock->__data.__writer)) 105 | 106 | #if defined(__GNUC__) && __GNUC__ >= 4 107 | #define PTHREAD_COND_T_FLAG(cond) (*(volatile int*)(&(cond->__data.__lock))) 108 | #define PTHREAD_COND_T_THREAD_COUNT(cond) (*(volatile int*)(&(cond-> __data.__futex))) 109 | #define PTHREAD_COND_T_COUNT_LOCK(cond) (*(volatile int*)(&(cond->__data.__nwaiters))) 110 | 111 | //For tree barriers 112 | //#define PTHREAD_BARRIER_T_NUM_THREADS(barrier) (*((int*)(barrier->__size+(0*sizeof(int))))) 113 | //#define PTHREAD_BARRIER_T_BARRIER_PTR(barrier) (*(tree_barrier_t**)(barrier->__size+(1*sizeof(int)))) 114 | 115 | #define PTHREAD_BARRIER_T_SPINLOCK(barrier) (*((volatile int*)(barrier->__size+(0*sizeof(int))))) 116 | #define PTHREAD_BARRIER_T_NUM_THREADS(barrier) (*((volatile int*)(barrier->__size+(1*sizeof(int))))) 117 | #define PTHREAD_BARRIER_T_COUNTER(barrier) (*((volatile int*)(barrier->__size+(2*sizeof(int))))) 118 | #define PTHREAD_BARRIER_T_DIRECTION(barrier) (*((volatile int*)(barrier->__size+(3*sizeof(int))))) 119 | 120 | //Tree barrier-related 121 | #if 0 122 | #ifndef __SIZEOF_PTHREAD_BARRIER_T 123 | #error __SIZEOF_PTHREAD_BARRIER_T not defined 124 | #endif 125 | #if ((4/*fields*/*4/*sizeof(int32)*/) > __SIZEOF_PTHREAD_BARRIER_T) 126 | #error barrier size __SIZEOF_PTHREAD_BARRIER_T not large enough for our implementation 127 | #endif 128 | #endif 129 | 130 | #else // gnuc >= 4 131 | //gnuc < 4 132 | #error "This library requires gcc 4.0+ (3.x should work, but you'll need to change pthread_defs.h)" 133 | #endif // gnuc >= 4 134 | 135 | #endif // LinuxThreads / NPTL 136 | 137 | // non-linux definitions... fill this in? 138 | #else // !__linux__ 139 | #error "Non-Linux pthread definitions not available" 140 | #endif //!__linux__ 141 | 142 | #endif // __PTHREAD_DEFS_H__ 143 | -------------------------------------------------------------------------------- /spinlock_alpha.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | 22 | #ifndef __SPINLOCK_ALPHA_H__ 23 | #define __SPINLOCK_ALPHA_H__ 24 | 25 | // routines adapted from /usr/src/linux/include/asm-alpha/spinlock.h 26 | 27 | static __inline__ void spin_lock (volatile int* lock) { 28 | long tmp; 29 | __asm__ __volatile__( 30 | "1: ldl_l %0,%1\n" 31 | " bne %0,2f\n" 32 | " lda %0,1\n" 33 | " stl_c %0,%1\n" 34 | " beq %0,2f\n" 35 | " mb\n" 36 | ".subsection 2\n" 37 | "2: ldl %0,%1\n" 38 | " bne %0,2b\n" 39 | " br 1b\n" 40 | ".previous" 41 | : "=&r" (tmp), "=m" (*lock) 42 | : "m"(*lock) : "memory"); 43 | } 44 | 45 | static __inline__ void spin_unlock (volatile int* lock) { 46 | __asm__ __volatile__ ("mb\n"); 47 | *lock = 0; 48 | } 49 | 50 | static __inline__ int trylock (volatile int* lock) { 51 | 52 | 53 | long regx; 54 | int success; 55 | 56 | __asm__ __volatile__( 57 | "1: ldl_l %1,%0\n" 58 | " lda %2,0\n" 59 | " bne %1,2f\n" 60 | " lda %2,1\n" 61 | " stl_c %2,%0\n" 62 | " beq %2,6f\n" 63 | "2: mb\n" 64 | ".subsection 2\n" 65 | "6: br 1b\n" 66 | ".previous" 67 | : "=m" (*lock), "=&r" (regx), "=&r" (success) 68 | : "m" (*lock) : "memory"); 69 | 70 | return success; 71 | 72 | 73 | } 74 | 75 | #endif // __SPINLOCK_H__ 76 | -------------------------------------------------------------------------------- /spinlock_arm.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | #ifndef __SPINLOCK_ARM_H__ 22 | #define __SPINLOCK_ARM_H__ 23 | 24 | static __inline__ void spin_lock (volatile int* lock) { 25 | unsigned long tmp; 26 | 27 | __asm__ __volatile__( 28 | "1: ldrex %0, [%1]\n" 29 | " cmp %0, #0\n" 30 | " strexeq %0, %2, [%1]\n" 31 | " cmpeq %0, #0\n" 32 | " bne 1b\n" 33 | " dmb\n" 34 | : "=&r" (tmp) 35 | : "r" (lock), "r" (1) 36 | : "cc"); 37 | 38 | } 39 | 40 | static __inline__ void spin_unlock (volatile int* lock) { 41 | 42 | 43 | __asm__ __volatile__( 44 | " dmb\n" 45 | " str %1, [%0]\n" 46 | : 47 | : "r" (lock), "r" (0) 48 | : "cc"); 49 | } 50 | 51 | 52 | static __inline__ int trylock (volatile int* lock) { 53 | unsigned long tmp; 54 | 55 | __asm__ __volatile__( 56 | " ldrex %0, [%1]\n" 57 | " cmp %0, #0\n" 58 | " strexeq %0, %2, [%1]\n" 59 | " eor %0, %0, #1\n" 60 | " bne fail\n" 61 | " dmb\n" 62 | "fail: nop\n" 63 | : "=&r" (tmp) 64 | : "r" (lock), "r" (1) 65 | : "cc", "memory"); 66 | 67 | return tmp; 68 | } 69 | 70 | #endif // __SPINLOCK_ARM_H__ 71 | -------------------------------------------------------------------------------- /spinlock_sparc.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | #ifndef __SPINLOCK_SPARC_H__ 22 | #define __SPINLOCK_SPARC_H__ 23 | 24 | // routines from /usr/src/linux/include/asm-sparc/spinlock_64.h 25 | // Note: these work even with RMO, but a few barriers could be eliminated for TSO 26 | 27 | static __inline__ void spin_lock(volatile int* lock) 28 | { 29 | unsigned long tmp; 30 | 31 | __asm__ __volatile__( 32 | "1: ldstub [%1], %0\n" 33 | " membar #StoreLoad | #StoreStore\n" 34 | " brnz,pn %0, 2f\n" 35 | " nop\n" 36 | " .subsection 2\n" 37 | "2: ldub [%1], %0\n" 38 | " membar #LoadLoad\n" 39 | " brnz,pt %0, 2b\n" 40 | " nop\n" 41 | " ba,a,pt %%xcc, 1b\n" 42 | " .previous" 43 | : "=&r" (tmp) 44 | : "r" (lock) 45 | : "memory"); 46 | } 47 | 48 | static __inline__ int trylock(volatile int* lock) 49 | { 50 | unsigned long result; 51 | 52 | __asm__ __volatile__( 53 | " ldstub [%1], %0\n" 54 | " membar #StoreLoad | #StoreStore" 55 | : "=r" (result) 56 | : "r" (lock) 57 | : "memory"); 58 | 59 | return (result == 0); 60 | } 61 | 62 | static __inline__ void spin_unlock(volatile int* lock) 63 | { 64 | __asm__ __volatile__( 65 | " membar #StoreStore | #LoadStore\n" 66 | " stb %%g0, [%0]" 67 | : // No outputs 68 | : "r" (lock) 69 | : "memory"); 70 | } 71 | 72 | 73 | #endif // __SPINLOCK_SPARC_H__ 74 | -------------------------------------------------------------------------------- /spinlock_x86.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | #ifndef __SPINLOCK_X86_H__ 22 | #define __SPINLOCK_X86_H__ 23 | 24 | // routines from /usr/src/linux/include/asm-x86/spinlock.h 25 | 26 | static __inline__ void spin_lock (volatile int* lock) { 27 | char oldval; 28 | __asm__ __volatile__ 29 | ( 30 | "\n1:\t" \ 31 | "cmpb $0,%1\n\t" \ 32 | "ja 1b\n\t" \ 33 | "xchgb %b0, %1\n\t" \ 34 | "cmpb $0,%0\n" \ 35 | "ja 1b\n\t" 36 | :"=q"(oldval), "=m"(*lock) 37 | : "0"(1) 38 | : "memory"); 39 | } 40 | 41 | static __inline__ void spin_unlock (volatile int* lock) { 42 | __asm__ __volatile__ 43 | ("movb $0,%0" \ 44 | :"=m" (*lock) : : "memory"); 45 | } 46 | 47 | static __inline__ int trylock (volatile int* lock) { 48 | char oldval; 49 | __asm__ __volatile__ 50 | ( 51 | "xchgb %b0,%1" 52 | :"=q" (oldval), 53 | "=m" (*lock) 54 | :"0" (1) 55 | : "memory"); 56 | return oldval == 0; 57 | } 58 | 59 | #endif // __SPINLOCK_X86_H__ 60 | -------------------------------------------------------------------------------- /tests/Makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | # ==== Variables ============================================================== 4 | 5 | # 64-bit compiles 6 | #Uncomment to use sparc/alpha cross-compilers 7 | #CC := sparc64-unknown-linux-gnu-gcc 8 | #CPP := sparc64-unknown-linux-gnu-g++ 9 | #CC := alpha-unknown-linux-gnu-gcc 10 | #CPP := alpha-unknown-linux-gnu-g++ 11 | CC := arm-linux-gnueabi-gcc 12 | CPP := arm-linux-gnueabi-g++ 13 | #CC := gcc 14 | #CPP := g++ 15 | 16 | # Needed for support of v7 assembly instructions on ARM architecture 17 | ARM_FLAGS := -march=armv7-a -marm 18 | 19 | #CFLAGS := -ggdb3 -O3 -D__DEBUG 20 | #CFLAGS := -g -O3 -DM5_PROFILING 21 | CFLAGS := -g -O3 $(ARM_FLAGS) 22 | 23 | CPPFLAGS := $(CFLAGS) 24 | 25 | # ARM support for OpenMP not tested (test_omp.o) 26 | TEST_OBJS := test_stackgrow.o test_pthreadbasic.o test_pthread.o test_atomic.o test_barrier.o test_lock.o test_malloc.o test_sieve.o test___thread.o test_omp.o 27 | #TEST_OBJS := test_stackgrow.o test_pthreadbasic.o test_pthread.o test_atomic.o test_barrier.o test_lock.o test_malloc.o test_sieve.o test___thread.o 28 | 29 | TEST_PROGS := $(TEST_OBJS:.o=) 30 | 31 | 32 | # ==== Rules ================================================================== 33 | 34 | .PHONY: default clean 35 | 36 | default: $(TEST_PROGS) 37 | 38 | clean: 39 | $(RM) $(TEST_OBJS) $(TEST_PROGS) $(TEST_OBJS:.o=_p) ../pthread.o 40 | 41 | $(TEST_PROGS): $(TEST_OBJS) ../pthread.o 42 | $(CPP) -static -o $@ $@.o ../pthread.o 43 | $(CPP) -static -o $@_p $@.o -lpthread 44 | 45 | %.o: %.cpp Makefile 46 | $(CPP) $(CPPFLAGS) -c -o $@ $*.cpp 47 | 48 | #Special rules for OpenMP programs 49 | test_omp: test_omp.o 50 | $(CPP) -static -o $@ $@.o -lgomp ../pthread.o -lgomp 51 | $(CPP) -static -o $@_p $@.o -lgomp -lpthread 52 | 53 | test_omp.o: test_omp.cpp ../pthread.o 54 | $(CPP) $(CPPFLAGS) -fopenmp -c -o $@ $*.cpp 55 | 56 | ../pthread.o: ../pthread.c ../pthread_defs.h ../tls_defs.h Makefile 57 | $(CC) $(CFLAGS) -c ../pthread.c -o ../pthread.o 58 | 59 | 60 | -------------------------------------------------------------------------------- /tests/test___thread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | // without volatile, simulator test works even if __thread support is broken 30 | __thread volatile int local = 7; 31 | 32 | volatile long long int jjjs = 0; 33 | 34 | static const int count = 1024; 35 | 36 | void* run (void* arg) 37 | { 38 | long long int id = (long long int)arg; 39 | int i; 40 | printf("&local[%lld]=%p\n", id, &local); 41 | local += id; 42 | for (i = 0; i < count; i++) { 43 | local++; 44 | } 45 | 46 | //Some calculations to delay last read 47 | long long int jjj = 0; 48 | for (i = 0; i < 10000; i++) { 49 | jjj = 2*jjj +4 -i/5 + local; 50 | } 51 | jjjs = jjj; 52 | 53 | //assert(local == count +id); 54 | return (void*)local; 55 | } 56 | 57 | int main (int argc, char** argv) 58 | { 59 | if (argc != 2) { 60 | printf("usage: %s \n", argv[0]); 61 | exit(1); 62 | } 63 | int thread_count = atoi(argv[1]); 64 | 65 | printf("Starting %d threads...\n", thread_count); 66 | 67 | //struct timeval startTime; 68 | //int startResult = gettimeofday(&startTime, NULL); 69 | //assert(startResult == 0); 70 | int i; 71 | pthread_t* threads = (pthread_t*)calloc(thread_count, sizeof(pthread_t)); 72 | assert(threads != NULL); 73 | for (i = 1 ; i < thread_count; i++) { 74 | int createResult = pthread_create(&threads[i], 75 | NULL, 76 | run, 77 | (void*)i); 78 | assert(createResult == 0); 79 | } 80 | 81 | long long int local = (long long int)run((void*)0); 82 | printf("local[0] = %lld\n", local); 83 | 84 | for (i = 1 ; i < thread_count; i++) { 85 | int joinResult = pthread_join(threads[i], 86 | (void**)&local); 87 | assert(joinResult == 0); 88 | printf("local[%d] = %lld\n", i, local); 89 | } 90 | 91 | /*struct timeval endTime; 92 | int endResult = gettimeofday(&endTime, NULL); 93 | assert(endResult == 0); 94 | 95 | long startMillis = (((long)startTime.tv_sec)*1000) + (((long)startTime.tv_usec)/1000); 96 | long endMillis = (((long)endTime.tv_sec)*1000) + (((long)endTime.tv_usec)/1000); 97 | */ 98 | /*printf("End Time (s) = %d\n", (int)endTime.tv_sec); 99 | printf("Start Time (s) = %d\n", (int)startTime.tv_sec); 100 | printf("Time (s) = %d\n", (int)(endTime.tv_sec-startTime.tv_sec)); 101 | printf("\n"); 102 | 103 | printf("End Time (us) = %d\n", (int)endTime.tv_usec); 104 | printf("Start Time (us) = %d\n", (int)startTime.tv_usec); 105 | printf("Time (us) = %d\n", (int)(endTime.tv_usec-startTime.tv_usec)); 106 | printf("\n"); 107 | 108 | printf("End Time (ms) = %d\n", (int)endMillis); 109 | printf("Start Time (ms) = %d\n", (int)startMillis); 110 | printf("Time (ms) = %d\n", (int)(endMillis-startMillis)); 111 | printf("\n");*/ 112 | 113 | /*double difference=(double)(endTime.tv_sec-startTime.tv_sec)+(double)(endTime.tv_usec-startTime.tv_usec)*1e-6; 114 | printf("Time (s) = %f\n", difference); 115 | printf("\n");*/ 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /tests/test_atomic.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | /** 28 | * test_atomic: 29 | * This benchmark is intended to stress-test atomic operations by using heavily 30 | * contended locks. 31 | */ 32 | 33 | static pthread_mutex_t lock; 34 | static pthread_barrier_t barrier; 35 | 36 | int* intArray; 37 | int next; 38 | 39 | int iteration; 40 | 41 | void* chain(void* arglist) 42 | { 43 | int iteration; 44 | for(iteration = 1; iteration <= 10; iteration++) { 45 | long long int id = (long long int) arglist; 46 | pthread_barrier_wait(&barrier); 47 | 48 | pthread_mutex_lock(&lock); 49 | 50 | int current = next; 51 | printf("[Iteration %d, Thread %lld] Got lock\n", iteration, id); 52 | intArray[current]++; 53 | 54 | //Uncomment this snip for longer-running critical section 55 | /*int i; 56 | for (i = 0; i < 5000; i++) { 57 | next = (i + next)/2; 58 | Sim_Print0(""); //so that gcc does not optimize this out 59 | }*/ 60 | 61 | next = id; 62 | 63 | printf("[Iteration %d, Thread %lld] Critical section done, previously next=%d, now next=%d\n", iteration, id, current, next); 64 | pthread_mutex_unlock(&lock); 65 | 66 | pthread_barrier_wait(&barrier); 67 | } 68 | 69 | return NULL; 70 | } 71 | 72 | 73 | int main(int argc, const char** const argv) { 74 | if (argc != 2) { 75 | printf("Usage: ./test_atomic \n"); 76 | exit(1); 77 | } 78 | int nthreads = atoi(argv[1]); 79 | 80 | if (nthreads < 2) { 81 | printf("\nthis test requires at least 2 cpus\n"); 82 | exit(0); 83 | } 84 | 85 | pthread_t pth[nthreads]; 86 | pthread_attr_t attr; 87 | 88 | pthread_attr_init(&attr); 89 | pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 90 | 91 | 92 | pthread_mutex_init(&lock, NULL); 93 | printf("Init done\n"); 94 | 95 | int j; 96 | 97 | //for(iteration = 1; iteration <= 10; iteration++) { 98 | pthread_barrier_init(&barrier, NULL, nthreads); 99 | intArray = (int*) calloc(nthreads, sizeof(int)); 100 | next = 0; 101 | for (j = 1; j < nthreads; j++) { 102 | pthread_create(&pth[j], &attr, chain, (void*) j); 103 | } 104 | 105 | 106 | for(iteration = 1; iteration <= 10; iteration++) { 107 | pthread_barrier_wait(&barrier); 108 | /*for (j = 1; j < Sim_GetNumCpus(); j++) { 109 | pthread_join(pth[j], NULL); 110 | }*/ 111 | pthread_barrier_wait(&barrier); 112 | 113 | intArray[next]++; 114 | int failed = 0; 115 | for (j = 0; j < nthreads; j++) { 116 | if (intArray[j] != 1) { 117 | printf("FAILED: Position %d had %d instead of 1\n", j, intArray[j]); 118 | failed = 1; 119 | } 120 | } 121 | if (failed) exit(1); 122 | //pthread_barrier_destroy(&barrier); 123 | //free(intArray); 124 | for (j = 0; j < nthreads; j++) intArray[j] = 0; 125 | next = 0; 126 | //intArray = (int*) calloc(Sim_GetNumCpus(), sizeof(int)); 127 | 128 | printf("Iteration %d completed\n", iteration); 129 | } 130 | 131 | printf("PASSED :-)\n"); 132 | } 133 | -------------------------------------------------------------------------------- /tests/test_barrier.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | pthread_barrier_t barrier; 27 | //int A[MAX_NUM_CPUS][64]; 28 | 29 | void* run (void* arg) { 30 | long long int my_id = (long long int) arg; 31 | //A[my_id][0]++; 32 | printf("%lli BEFORE\n", my_id); 33 | pthread_barrier_wait(&barrier); 34 | printf("%lli AFTER\n", my_id); 35 | //A[my_id][0]++; 36 | return NULL; 37 | } 38 | int main (int argc, const char** const argv) { 39 | int i; 40 | 41 | if (argc != 2) { 42 | printf("Usage: ./test_atomic \n"); 43 | exit(1); 44 | } 45 | 46 | int nthreads = atoi(argv[1]); 47 | 48 | pthread_t* pth = (pthread_t*) calloc(nthreads, sizeof(pthread_t)); 49 | 50 | pthread_barrier_init(&barrier, NULL, nthreads); 51 | 52 | for (i=1; i < nthreads; i++) { 53 | pthread_create(&pth[i], NULL, run, (void*) i); 54 | } 55 | run((void*)0); 56 | for (i=1; i < nthreads; i++) { 57 | pthread_join(pth[i], NULL); 58 | } 59 | 60 | pthread_barrier_destroy(&barrier); 61 | free(pth); 62 | } 63 | -------------------------------------------------------------------------------- /tests/test_lock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | static pthread_rwlock_t lock; 25 | static pthread_mutex_t trylock = PTHREAD_MUTEX_INITIALIZER; 26 | 27 | void* run1(void* arglist) 28 | { 29 | pthread_t id = pthread_self(); 30 | printf("[run1] TID=%d\n", (int)id); 31 | 32 | printf("[run1] started\n"); 33 | 34 | pthread_rwlock_rdlock(&lock); 35 | printf("[run1] a read lock is obtained\n"); 36 | 37 | pthread_rwlock_unlock(&lock); 38 | printf("[run1] a read lock is released\n"); 39 | 40 | return NULL; 41 | } 42 | 43 | 44 | void* run2(void* arglist) 45 | { 46 | printf("[run2]started\n"); 47 | 48 | int res = pthread_mutex_trylock(&trylock); 49 | printf("[run2] try lock result %d\n", res); 50 | 51 | if (res == 0) { 52 | pthread_mutex_unlock(&trylock); 53 | } 54 | return NULL; 55 | } 56 | 57 | int main(int argc, const char** const argv) { 58 | 59 | pthread_t pth; 60 | pthread_attr_t attr; 61 | int arg; 62 | 63 | pthread_attr_init(&attr); 64 | pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 65 | 66 | pthread_rwlock_init(&lock, NULL); 67 | printf("[main]a rwlock is initialized\n"); 68 | 69 | // test 1 : read lock 70 | printf("\n1. read lock test\n"); 71 | 72 | pthread_rwlock_rdlock(&lock); 73 | printf("[main]a read lock is obtained\n"); 74 | 75 | pthread_create(&pth, &attr, run1, &arg); 76 | printf("[main]thread created with run1\n"); 77 | 78 | pthread_join(pth, NULL); 79 | printf("[main]thread joined\n"); 80 | 81 | pthread_rwlock_unlock(&lock); 82 | printf("[main]a read lock is released\n"); 83 | 84 | // test 2 : write lock 85 | printf("\n2. write lock test\n"); 86 | 87 | pthread_rwlock_wrlock(&lock); 88 | printf("[main]a write lock is obtained\n"); 89 | 90 | pthread_create(&pth, &attr, run1, &arg); 91 | printf("[main]thread created with run1\n"); 92 | 93 | int i; 94 | for (i = 0; i < 10; i++) { 95 | printf("[main]idling %d\n", i); 96 | } 97 | 98 | pthread_rwlock_unlock(&lock); 99 | printf("[main]a write lock is released\n"); 100 | 101 | pthread_rwlock_destroy(&lock); 102 | 103 | pthread_join(pth, NULL); 104 | printf("[main]thread joined\n"); 105 | 106 | // test 3 : try lock 107 | printf("\n3. try lock test\n"); 108 | 109 | // 3.1 trylock will be tried to an occupied lock 110 | pthread_mutex_lock(&trylock); 111 | printf("[main]a lock is obtained\n"); 112 | 113 | pthread_create(&pth, &attr, run2, &arg); 114 | printf("[main]thread created with run2\n"); 115 | 116 | pthread_join(pth, NULL); 117 | printf("[main]thread joined\n"); 118 | 119 | pthread_mutex_unlock(&trylock); 120 | printf("[main]a lock is released\n"); 121 | 122 | // 3.2 trylock will be tried to a free lock 123 | pthread_create(&pth, &attr, run2, &arg); 124 | printf("[main]thread created with run2\n"); 125 | 126 | pthread_join(pth, NULL); 127 | printf("[main]thread joined\n"); 128 | } 129 | -------------------------------------------------------------------------------- /tests/test_malloc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | /** 29 | * test_malloc: 30 | * This benchmark tests malloc/free by allocating memory concurrently 31 | */ 32 | 33 | static pthread_barrier_t barrier; 34 | 35 | 36 | void*** ptr_matrix; 37 | 38 | int iteration; 39 | 40 | int nthreads; 41 | 42 | typedef unsigned long int uint32; 43 | 44 | void* alloc(void* arglist) 45 | { 46 | long long int id = (long long int) arglist; 47 | 48 | pthread_barrier_wait(&barrier); 49 | 50 | int bytes = iteration*(id +1); 51 | void* ptr = malloc(bytes); 52 | ptr_matrix[iteration][id] = ptr; 53 | printf("[ALLOC %d, Thread %lld] Allocated %d bytes, from %p to %p\n", iteration, id, bytes, ptr, ((char*)ptr) + bytes - 1); 54 | 55 | pthread_barrier_wait(&barrier); 56 | int target = (id + iteration) % nthreads; 57 | free(ptr_matrix[iteration][target]); 58 | printf("[ALLOC %d, Thread %lld] Freed %d's allocation, %p\n", iteration, id, target, ptr_matrix[iteration][target]); 59 | //free(ptr_matrix[iteration][target]); 60 | return NULL; 61 | } 62 | 63 | 64 | int main(int argc, const char** const argv) { 65 | 66 | if (argc != 2) { 67 | printf("Usage: ./test_malloc \n"); 68 | exit(1); 69 | } 70 | 71 | nthreads = atoi(argv[1]); 72 | 73 | pthread_t* pth = (pthread_t*) calloc(nthreads, sizeof(pthread_t)); 74 | 75 | pthread_attr_t attr; 76 | 77 | pthread_attr_init(&attr); 78 | pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 79 | 80 | 81 | printf("Init done\n"); 82 | 83 | int j; 84 | ptr_matrix = (void***) calloc(20, sizeof(void**)); 85 | for(iteration = 1; iteration <= 20; iteration++) { 86 | pthread_barrier_init(&barrier, NULL, nthreads); 87 | ptr_matrix[iteration] = (void**) calloc(nthreads, sizeof(void*)); 88 | for (j = 1; j < nthreads; j++) { 89 | pthread_create(&pth[j], &attr, alloc, (void*) j); 90 | } 91 | alloc((void *)0); 92 | for (j = 1; j < nthreads; j++) { 93 | pthread_join(pth[j], NULL); 94 | } 95 | 96 | pthread_barrier_destroy(&barrier); 97 | 98 | printf("Iteration %d completed\n", iteration); 99 | } 100 | printf("PASSED\n"); 101 | } 102 | -------------------------------------------------------------------------------- /tests/test_omp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | //Good old matrix multiply using openmp 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | int64_t* A; 31 | int64_t* B; 32 | int64_t* C; 33 | 34 | int main(int argc, const char** argv) { 35 | 36 | if (argc != 3) { 37 | printf("Usage: ./test_omp \n"); 38 | exit(1); 39 | } 40 | 41 | int nthreads = atoi(argv[1]); 42 | 43 | if (nthreads < 1) { 44 | printf("nthreads must be 1 or more\n"); 45 | exit(1); 46 | } 47 | 48 | int size = atoi(argv[2]); 49 | 50 | if (size < 1) { 51 | printf("size must be 1 or more\n"); 52 | exit(1); 53 | } 54 | 55 | printf("Setting OMP threads to %d\n", nthreads); 56 | omp_set_num_threads(nthreads); 57 | 58 | A = (int64_t*) calloc(size*size, sizeof(int64_t)); 59 | B = (int64_t*) calloc(size*size, sizeof(int64_t)); 60 | C = (int64_t*) calloc(size*size, sizeof(int64_t)); 61 | 62 | printf("Starting with row/col size=%d\n",size); 63 | 64 | for(int x = 0; x < size; x++) { 65 | for (int y = 0; y < size; y++) { 66 | A[x*size + y] = x*y; 67 | } 68 | } 69 | printf("A initialized\n"); 70 | 71 | for(int x = 0; x < size; x++) { 72 | for (int y = 0; y < size; y++) { 73 | B[x*size + y] = x*y - y; 74 | } 75 | } 76 | printf("B initialized\n"); 77 | 78 | printf("Computing A*B with %d threads\n", nthreads); 79 | 80 | #pragma omp parallel for 81 | for(int x = 0; x < size; x++) { 82 | for (int y = 0; y < size; y++) { 83 | int64_t tot; 84 | for (int m = 0; m < size; m++) { 85 | tot += A[x*size + m]*B[m*size + y]; 86 | } 87 | C[x*size + y] = tot; 88 | } 89 | } 90 | 91 | printf("Done\n"); 92 | 93 | return 0; 94 | } 95 | 96 | 97 | -------------------------------------------------------------------------------- /tests/test_pthread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | void* run1(void* arglist) 29 | { 30 | int* args = (int*)arglist; 31 | printf("[run1] argument passed %d %d\n", args[0], args[1]); 32 | 33 | // yield() does nothing 34 | pthread_yield(); 35 | 36 | pthread_t run1th = pthread_self(); 37 | printf("[run1] thread id : %d \n", (int)run1th); 38 | 39 | pthread_exit(0); 40 | assert(false); 41 | return NULL; 42 | } 43 | 44 | int gl_counter = 0; 45 | 46 | void funcWithCriticalSection() 47 | { 48 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 49 | int result = pthread_mutex_lock(&lock); 50 | assert(result == 0); 51 | gl_counter++; 52 | printf("%d\n",gl_counter); 53 | result = pthread_mutex_unlock(&lock); 54 | assert(result == 0); 55 | } 56 | 57 | void* run2(void* arglist) 58 | { 59 | int i; 60 | for (i = 0; i <5; i++) { 61 | funcWithCriticalSection(); 62 | } 63 | return NULL; 64 | } 65 | 66 | pthread_key_t key; 67 | 68 | void* run3(void* arglist) 69 | { 70 | int j; 71 | int i = pthread_self(); 72 | int result = pthread_setspecific(key, (void*)&i); 73 | assert(result == 0); 74 | void* value = pthread_getspecific(key); 75 | assert(value != NULL); 76 | 77 | for (j=5; j >= 0 ; j--) { 78 | printf(""); 79 | } 80 | 81 | if (*((int*)value) == (int) pthread_self()) { 82 | printf("[run3]thread-private value matches 2 : %d \n", *((int*)value)); 83 | } 84 | else { 85 | printf("[run3]thread-private value doesn't match 2 : %d \n", *((int*)value)); 86 | } 87 | return NULL; 88 | } 89 | 90 | pthread_cond_t cond_sync = PTHREAD_COND_INITIALIZER; 91 | pthread_mutex_t cond_lock = PTHREAD_MUTEX_INITIALIZER; 92 | 93 | void* run4(void* arglist) 94 | { 95 | int j; 96 | for (j=5; j >= 0 ; j--) { 97 | printf("[run4] goofing around for a moment %d \n", j); 98 | } 99 | 100 | int result = pthread_mutex_lock(&cond_lock); 101 | assert(result == 0); 102 | 103 | printf("[run4] about to call broadcast\n"); { 104 | result = pthread_cond_broadcast(&cond_sync); 105 | assert(result == 0); 106 | } 107 | 108 | result = pthread_mutex_unlock(&cond_lock); 109 | assert(result == 0); 110 | return NULL; 111 | } 112 | 113 | int main(int argc, const char** const argv) { 114 | 115 | // test 1 : creation & join 116 | printf("\n1. thread creation and join test\n"); 117 | 118 | pthread_t pth; 119 | pthread_attr_t attr; 120 | int arg[2]; 121 | 122 | arg[0] = 2; 123 | arg[1] = 4; 124 | 125 | int result = pthread_attr_init(&attr); 126 | assert(result == 0); 127 | result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 128 | assert(result == 0); 129 | 130 | result = pthread_create(&pth, &attr, run1, &arg); 131 | assert(result == 0); 132 | printf("[main]thread(%d) created with run1\n", (int)pth); 133 | 134 | result = pthread_join(pth, NULL); 135 | assert(result == 0); 136 | printf("[main]thread(%d) joined\n", (int)pth); 137 | 138 | 139 | // test 2 : mutex 140 | printf("\n2. mutex test\n"); 141 | result = pthread_create(&pth, &attr, run2, &arg); 142 | assert(result == 0); 143 | printf("[main]thread(%d) created with run2\n", (int)pth); 144 | 145 | int i; 146 | for (i = 0; i <5; i++) { 147 | funcWithCriticalSection(); 148 | } 149 | 150 | result = pthread_join(pth, NULL); 151 | assert(result == 0); 152 | 153 | 154 | // test 3 : key 155 | printf("\n3. thread-private storage test\n"); 156 | result = pthread_key_create(&key, NULL); 157 | assert(result == 0); 158 | 159 | result = pthread_create(&pth, &attr, run3, &arg); 160 | assert(result == 0); 161 | 162 | printf("[main]thread(%d) created with run3\n", (int)pth); 163 | 164 | i = 1; 165 | result = pthread_setspecific(key, (void*)&i); 166 | assert(result == 0); 167 | void* value = pthread_getspecific(key); 168 | assert(value != NULL); 169 | 170 | if (*((int*)value) == 1) { 171 | printf("[main]thread-private value matches 1 : %d \n", *((int*)value)); 172 | } 173 | else { 174 | printf("[main]thread-private value doesn't match 1 : %d \n", *((int*)value)); 175 | } 176 | 177 | result = pthread_join(pth, NULL); 178 | assert(result == 0); 179 | 180 | 181 | // test 4 : wait / notifyall 182 | printf("\n4. wait-notifyall test\n"); 183 | result = pthread_create(&pth, &attr, run4, &arg); 184 | assert(result == 0); 185 | printf("[main]thread(%d) created with run4\n", (int)pth); 186 | 187 | result = pthread_mutex_lock(&cond_lock); 188 | assert(result == 0); 189 | 190 | printf("[main]going into wait()\n"); 191 | result = pthread_cond_wait(&cond_sync, &cond_lock); 192 | assert(result == 0); 193 | 194 | result = pthread_mutex_unlock(&cond_lock); 195 | assert(result == 0); 196 | 197 | result = pthread_join(pth, NULL); 198 | assert(result == 0); 199 | } 200 | -------------------------------------------------------------------------------- /tests/test_pthreadbasic.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | 29 | void* run (void* arg) { 30 | printf("Hello from a child thread! (thread ID: %d).\n", (int)pthread_self()); 31 | return NULL; 32 | } 33 | 34 | int main(int argc, const char** argv) { 35 | pthread_t pth; 36 | pthread_attr_t attr; 37 | 38 | printf("Main thread initialized. TID=%d\n", (int)pthread_self()); 39 | int result = pthread_attr_init(&attr); 40 | assert(result == 0); 41 | printf("Main thread called pthread_attr_init\n"); 42 | 43 | result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 44 | assert(result == 0); 45 | 46 | printf("Main thread called pthread_attr_setscope\n"); 47 | 48 | printf("Main thread creating 1st thread...\n"); 49 | result = pthread_create(&pth, &attr, run, NULL); 50 | 51 | pthread_t pth2; 52 | printf("Main thread creating 2nd thread...\n"); 53 | result = pthread_create(&pth2, &attr, run, NULL); 54 | 55 | printf("Main thread calling join w/ 1st thread (id=%lx)... (self=%lx)\n", pth, pthread_self()); 56 | pthread_join(pth, NULL); 57 | printf("Main thread calling join w/ 2nd thread (id=%lx)... (self=%lx)\n", pth2, pthread_self()); 58 | pthread_join(pth2, NULL); 59 | printf("Main thread has self=%d\n", (int)pthread_self()); 60 | 61 | printf("Main thread done.\n"); 62 | } 63 | 64 | 65 | -------------------------------------------------------------------------------- /tests/test_sieve.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | 21 | /* 22 | g++ -O3 -o ./sieve -lm -lpthread sieve.cpp && time sieve 1 && time sieve 2 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | static int max_limit; 35 | static int sqrt_max_limit; 36 | static int* not_prime; 37 | 38 | static const int print = 1; 39 | 40 | static const int start = 2; 41 | static int sqrt_limit; 42 | static int limit; 43 | 44 | static void* run (void*); 45 | #ifdef SIMULATOR 46 | void mainX(int argc, const char **argv, const char **envp) 47 | #else 48 | int main (int argc, char** argv) 49 | #endif 50 | { 51 | #ifdef SIMULATOR 52 | if (argc != 1) { 53 | printf("usage: %s\n", argv[0]); 54 | exit(1); 55 | } 56 | int thread_count = Sim_GetNumCpus(); 57 | #else 58 | if (argc != 2) { 59 | printf("usage: %s \n", argv[0]); 60 | exit(1); 61 | } 62 | int thread_count = atoi(argv[1]); 63 | #endif 64 | 65 | #ifdef SIMULATOR 66 | max_limit = 10000; 67 | #else 68 | max_limit = 10000;//0000; 69 | #endif 70 | sqrt_max_limit = (int)ceil(sqrt(max_limit)); 71 | 72 | not_prime = (int*)calloc(max_limit, sizeof(int)); 73 | assert(not_prime != NULL); 74 | 75 | sqrt_limit = (int)ceil(sqrt(sqrt_max_limit)); 76 | limit = sqrt_max_limit; 77 | if (1) { 78 | not_prime[0] = 1; 79 | not_prime[1] = 1; 80 | run(NULL); 81 | } 82 | 83 | sqrt_limit = (int)ceil(sqrt(max_limit)); 84 | limit = max_limit; 85 | 86 | printf("sqrt_max_limit %d\n", sqrt_max_limit); 87 | printf("max_limit %d\n", max_limit); 88 | #ifdef SIMULATOR 89 | printf("Starting threads...\n"); 90 | #else 91 | printf("Starting %d threads...\n", thread_count); 92 | #endif 93 | 94 | #ifndef SIMULATOR 95 | struct timeval startTime; 96 | int startResult = gettimeofday(&startTime, NULL); 97 | assert(startResult == 0); 98 | #endif 99 | 100 | 101 | pthread_t* threads = (pthread_t*)calloc(thread_count, sizeof(pthread_t)); 102 | assert(threads != NULL); 103 | for (int i = 1 ; i < thread_count; i++) { 104 | int createResult = pthread_create(&threads[i], 105 | NULL, 106 | run, 107 | NULL); 108 | assert(createResult == 0); 109 | } 110 | 111 | run(NULL); 112 | 113 | for (int i = 1 ; i < thread_count; i++) { 114 | int joinResult = pthread_join(threads[i], 115 | NULL); 116 | assert(joinResult == 0); 117 | } 118 | 119 | #ifndef SIMULATOR 120 | struct timeval endTime; 121 | int endResult = gettimeofday(&endTime, NULL); 122 | assert(endResult == 0); 123 | 124 | long startMillis = (((long)startTime.tv_sec)*1000) + (((long)startTime.tv_usec)/1000); 125 | long endMillis = (((long)endTime.tv_sec)*1000) + (((long)endTime.tv_usec)/1000); 126 | 127 | printf("%d\n", (int)endTime.tv_sec); 128 | printf("%d\n", (int)startTime.tv_sec); 129 | printf("%d\n", (int)(endTime.tv_sec-startTime.tv_sec)); 130 | printf("\n"); 131 | 132 | printf("%d\n", (int)endTime.tv_usec); 133 | printf("%d\n", (int)startTime.tv_usec); 134 | printf("%d\n", (int)(endTime.tv_usec-startTime.tv_usec)); 135 | printf("\n"); 136 | 137 | printf("%d\n", (int)endMillis); 138 | printf("%d\n", (int)startMillis); 139 | printf("%d\n", (int)(endMillis-startMillis)); 140 | printf("\n"); 141 | 142 | double difference=(double)(endTime.tv_sec-startTime.tv_sec)+(double)(endTime.tv_usec-startTime.tv_usec)*1e-6; 143 | printf("%f\n", difference); 144 | printf("\n"); 145 | #endif 146 | 147 | if (print) { 148 | printf("Primes less than 100:\n"); 149 | for (int i = 0; i < 100; i++) { 150 | if (!not_prime[i]) { 151 | printf("%d\n",i); 152 | } 153 | } 154 | } 155 | 156 | #ifndef SIMULATOR 157 | return 0; 158 | #endif 159 | } 160 | 161 | void* run (void* arg) 162 | { 163 | if (0 /*Sim_GetMode() == MODE_TM*/) { 164 | 165 | #ifdef WITH_TM 166 | for (int my_prime = start; my_prime < sqrt_limit; ++my_prime) { 167 | if (!not_prime[my_prime]) { 168 | TM_BeginClosed(); { 169 | not_prime[my_prime] = true; 170 | } 171 | TM_EndClosed(); 172 | for (int multiple = my_prime*2; multiple < limit; multiple += my_prime) { 173 | not_prime[multiple] = true; 174 | } 175 | TM_BeginClosed(); { 176 | not_prime[my_prime] = false; 177 | } 178 | TM_EndClosed(); 179 | } 180 | } 181 | #else 182 | printf("Somehow mode is MODE_TM but WITH_TM was not defined."); 183 | exit(1); 184 | #endif 185 | } 186 | else { 187 | for (int my_prime = start; my_prime < sqrt_limit; ++my_prime) { 188 | if (!not_prime[my_prime]) { 189 | // Sim_Print1("Found prime: %d\n", my_prime); 190 | not_prime[my_prime] = 1; 191 | for (int multiple = my_prime*2; multiple < limit; multiple += my_prime) { 192 | not_prime[multiple] = 1; 193 | } 194 | not_prime[my_prime] = 0; 195 | } else { 196 | // Sim_Print1("not prime: %d\n", my_prime); 197 | } 198 | } 199 | } 200 | return NULL; 201 | } 202 | -------------------------------------------------------------------------------- /tests/test_stackgrow.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | 19 | Author: Daniel Sanchez 20 | */ 21 | 22 | 23 | 24 | /* Just test which direction the stack grows in this arch/ABI 25 | Kind of a big deal when deciding which end of the stack to 26 | pass as a pointer to clone :-) 27 | */ 28 | 29 | #include 30 | 31 | 32 | void func (int* f1) { 33 | int f2; 34 | printf("Addr frame 1 = %p, Addr frame 2 = %p\n", f1, &f2); 35 | if (&f2 > f1) { 36 | printf("Stack grows up (and this threading library needs to be fixed for your arch...)\n"); 37 | } else { 38 | printf("Stack grows down\n"); 39 | } 40 | } 41 | 42 | int main (int argc, char**argv) { 43 | int f1; 44 | func(&f1); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /tls_defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | m5threads, a pthread library for the M5 simulator 3 | Copyright (C) 2009, Stanford University 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | #ifndef __TLS_DEFS_H__ 21 | #define __TLS_DEFS_H__ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | //These are mostly taken verbatim from glibc 2.3.6 29 | 30 | //32 for ELF32 binaries, 64 for ELF64 31 | #if defined(__LP64__) 32 | #define __ELF_NATIVE_CLASS 64 33 | #else 34 | #define __ELF_NATIVE_CLASS 32 35 | #endif 36 | 37 | //Seems like all non-ARM M5 targets use TLS_TCB_AT_TP (defined in 38 | // platform-specific 'tls.h') 39 | #if defined(__arm__) 40 | #define TLS_DTV_AT_TP 1 41 | #else 42 | #define TLS_TCB_AT_TP 1 43 | #endif 44 | 45 | /* Standard ELF types. */ 46 | 47 | #include 48 | 49 | /* Type for a 16-bit quantity. */ 50 | typedef uint16_t Elf32_Half; 51 | typedef uint16_t Elf64_Half; 52 | 53 | /* Types for signed and unsigned 32-bit quantities. */ 54 | typedef uint32_t Elf32_Word; 55 | typedef int32_t Elf32_Sword; 56 | typedef uint32_t Elf64_Word; 57 | typedef int32_t Elf64_Sword; 58 | 59 | /* Types for signed and unsigned 64-bit quantities. */ 60 | typedef uint64_t Elf32_Xword; 61 | typedef int64_t Elf32_Sxword; 62 | typedef uint64_t Elf64_Xword; 63 | typedef int64_t Elf64_Sxword; 64 | 65 | /* Type of addresses. */ 66 | typedef uint32_t Elf32_Addr; 67 | typedef uint64_t Elf64_Addr; 68 | 69 | /* Type of file offsets. */ 70 | typedef uint32_t Elf32_Off; 71 | typedef uint64_t Elf64_Off; 72 | 73 | /* Type for section indices, which are 16-bit quantities. */ 74 | typedef uint16_t Elf32_Section; 75 | typedef uint16_t Elf64_Section; 76 | 77 | /* Type for version symbol information. */ 78 | typedef Elf32_Half Elf32_Versym; 79 | typedef Elf64_Half Elf64_Versym; 80 | 81 | 82 | typedef struct 83 | { 84 | Elf32_Word p_type; /* Segment type */ 85 | Elf32_Off p_offset; /* Segment file offset */ 86 | Elf32_Addr p_vaddr; /* Segment virtual address */ 87 | Elf32_Addr p_paddr; /* Segment physical address */ 88 | Elf32_Word p_filesz; /* Segment size in file */ 89 | Elf32_Word p_memsz; /* Segment size in memory */ 90 | Elf32_Word p_flags; /* Segment flags */ 91 | Elf32_Word p_align; /* Segment alignment */ 92 | } Elf32_Phdr; 93 | 94 | typedef struct 95 | { 96 | Elf64_Word p_type; /* Segment type */ 97 | Elf64_Word p_flags; /* Segment flags */ 98 | Elf64_Off p_offset; /* Segment file offset */ 99 | Elf64_Addr p_vaddr; /* Segment virtual address */ 100 | Elf64_Addr p_paddr; /* Segment physical address */ 101 | Elf64_Xword p_filesz; /* Segment size in file */ 102 | Elf64_Xword p_memsz; /* Segment size in memory */ 103 | Elf64_Xword p_align; /* Segment alignment */ 104 | } Elf64_Phdr; 105 | 106 | 107 | #define ElfW(type) _ElfW (Elf, __ELF_NATIVE_CLASS, type) 108 | #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t) 109 | #define _ElfW_1(e,w,t) e##w##t 110 | 111 | 112 | #define PT_TLS 7 /* Thread-local storage segment */ 113 | 114 | 115 | # define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 116 | 117 | extern ElfW(Phdr) *_dl_phdr; 118 | extern size_t _dl_phnum; 119 | 120 | //Architecture-specific definitions 121 | 122 | #if defined(__x86_64) || defined(__amd64) 123 | 124 | /* Type for the dtv. */ 125 | typedef union dtv 126 | { 127 | size_t counter; 128 | void *pointer; 129 | } dtv_t; 130 | 131 | typedef struct 132 | { 133 | void *tcb; /* Pointer to the TCB. Not necessary the 134 | thread descriptor used by libpthread. */ 135 | dtv_t *dtv; 136 | void *self; /* Pointer to the thread descriptor. */ 137 | int multiple_threads; 138 | } tcbhead_t; 139 | 140 | #include 141 | #include 142 | #include 143 | 144 | /* Macros to load from and store into segment registers. */ 145 | # define TLS_GET_FS() \ 146 | { int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; } 147 | # define TLS_SET_FS(val) \ 148 | __asm ("movl %0, %%fs" :: "q" (val)) 149 | 150 | # define TLS_INIT_TP(thrdescr, secondcall) \ 151 | { void *_thrdescr = (thrdescr); \ 152 | tcbhead_t *_head = (tcbhead_t *) _thrdescr; \ 153 | int _result; \ 154 | \ 155 | _head->tcb = _thrdescr; \ 156 | /* For now the thread descriptor is at the same address. */ \ 157 | _head->self = _thrdescr; \ 158 | \ 159 | /* It is a simple syscall to set the %fs value for the thread. */ \ 160 | asm volatile ("syscall" \ 161 | : "=a" (_result) \ 162 | : "0" ((unsigned long int) __NR_arch_prctl), \ 163 | "D" ((unsigned long int) ARCH_SET_FS), \ 164 | "S" (_thrdescr) \ 165 | : "memory", "cc", "r11", "cx"); \ 166 | \ 167 | _result ? "cannot set %fs base address for thread-local storage" : 0; \ 168 | } 169 | 170 | #elif defined (__sparc) 171 | 172 | register struct pthread *__thread_self __asm__("%g7"); 173 | 174 | /* Code to initially initialize the thread pointer. */ 175 | # define TLS_INIT_TP(descr, secondcall) \ 176 | (__thread_self = (__typeof (__thread_self)) (descr), NULL) 177 | 178 | #elif defined (__arm__) 179 | 180 | typedef struct 181 | { 182 | void *dtv; 183 | void *private; 184 | } tcbhead_t; 185 | 186 | #define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \ 187 | ({ unsigned int _sys_result; \ 188 | { \ 189 | register int _a1 asm ("a1"); \ 190 | LOAD_ARGS_##nr (args) \ 191 | asm volatile ("mov r7, #0xf0000\n" \ 192 | "add r7, r7, #0x0005\n" \ 193 | "swi #0 @ syscall " #name \ 194 | : "=r" (_a1) \ 195 | : "i" (name) ASM_ARGS_##nr \ 196 | : "memory"); \ 197 | _sys_result = _a1; \ 198 | } \ 199 | (int) _sys_result; }) 200 | 201 | #undef INTERNAL_SYSCALL_ARM 202 | #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \ 203 | INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args) 204 | 205 | #define LOAD_ARGS_0() 206 | 207 | #define ASM_ARGS_0 208 | 209 | #define LOAD_ARGS_1(a1) \ 210 | int _a1tmp = (int) (a1); \ 211 | LOAD_ARGS_0 () \ 212 | _a1 = _a1tmp; 213 | 214 | #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1) 215 | 216 | # define TLS_INIT_TP(descr, secondcall) \ 217 | INTERNAL_SYSCALL_ARM(set_tls, 0, 1, (descr)) 218 | 219 | #else 220 | #error "No TLS defs for your architecture" 221 | #endif 222 | 223 | #endif /*__TLS_DEFS_H__*/ 224 | 225 | --------------------------------------------------------------------------------