diff options
author | Luis R. Rodriguez <mcgrof@kernel.org> | 2016-05-05 15:48:12 -0700 |
---|---|---|
committer | Luis R. Rodriguez <mcgrof@kernel.org> | 2016-05-06 16:01:50 -0700 |
commit | 0c81b9b9cd4e961bdc4f982c2667855a18c428d4 (patch) | |
tree | 674bd38dd05b278a6ae759f4c53e681ecb70938f | |
download | cocci-tact-0c81b9b9cd4e961bdc4f982c2667855a18c428d4.tar.gz |
add initial demo pthread party
Everything in order here. Party results:
Before party:
FOOD: [ *********** ]
TRASH: [ ]
After party:
FOOD: [ ]
TRASH: [ *********** ]
Everyone ate in order and then placed their trash accordingly.
Locking is in place so this is expected.
Signed-off-by: Luis R. Rodriguez <mcgrof@kernel.org>
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | CONTRIBUTING | 50 | ||||
-rw-r--r-- | LICENSE | 8 | ||||
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | copyleft-next-0.3.1 | 220 | ||||
-rw-r--r-- | main.c | 137 |
6 files changed, 424 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..87e54c2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main +*.o diff --git a/CONTRIBUTING b/CONTRIBUTING new file mode 100644 index 0000000..0ad2ce6 --- /dev/null +++ b/CONTRIBUTING @@ -0,0 +1,50 @@ +Send me patches: mcgrof@kernel.org + +This project embraces the Developer Certificate of Origin (DCO) for +contributions. This means you must agree to the following prior to submitting +patches, if you agree with this developer certificate you acknowledge this by +adding a Signed-off-by tag to your patch commit log. Every submitted patch +must have this. + +The source for the DCO: + +http://developercertificate.org/ + +----------------------------------------------------------------------- + +Developer Certificate of Origin +Version 1.1 + +Copyright (C) 2004, 2006 The Linux Foundation and its contributors. +660 York Street, Suite 102, +San Francisco, CA 94110 USA + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + + +Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +(a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +(b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +(c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +(d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. @@ -0,0 +1,8 @@ +Copyright (c) 2016, Luis R. Rodriguez <mcgrof@kernel.org> + +CRDA is licensed under copyleft-next 0.3.1 + +Refer to copyleft-next-0.3.1 for license details, you can +also always get the latest updates of copyleft-next from: + +https://github.com/richardfontana/copyleft-next diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..122bbfb --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: main + +main: main.c + gcc -lpthread -o main main.c + +clean: + rm -f main diff --git a/copyleft-next-0.3.1 b/copyleft-next-0.3.1 new file mode 100644 index 0000000..36d8515 --- /dev/null +++ b/copyleft-next-0.3.1 @@ -0,0 +1,220 @@ + copyleft-next 0.3.1 ("this License") + Release date: 2016-04-29 + +1. License Grants; No Trademark License + + Subject to the terms of this License, I grant You: + + a) A non-exclusive, worldwide, perpetual, royalty-free, irrevocable + copyright license, to reproduce, Distribute, prepare derivative works + of, publicly perform and publicly display My Work. + + b) A non-exclusive, worldwide, perpetual, royalty-free, irrevocable + patent license under Licensed Patents to make, have made, use, sell, + offer for sale, and import Covered Works. + + This License does not grant any rights in My name, trademarks, service + marks, or logos. + +2. Distribution: General Conditions + + You may Distribute Covered Works, provided that You (i) inform + recipients how they can obtain a copy of this License; (ii) satisfy the + applicable conditions of sections 3 through 6; and (iii) preserve all + Legal Notices contained in My Work (to the extent they remain + pertinent). "Legal Notices" means copyright notices, license notices, + license texts, and author attributions, but does not include logos, + other graphical images, trademarks or trademark legends. + +3. Conditions for Distributing Derived Works; Outbound GPL Compatibility + + If You Distribute a Derived Work, You must license the entire Derived + Work as a whole under this License, with prominent notice of such + licensing. This condition may not be avoided through such means as + separate Distribution of portions of the Derived Work. + + If the Derived Work includes material licensed under the GPL, You may + instead license the Derived Work under the GPL. + +4. Condition Against Further Restrictions; Inbound License Compatibility + + When Distributing a Covered Work, You may not impose further + restrictions on the exercise of rights in the Covered Work granted under + this License. This condition is not excused merely because such + restrictions result from Your compliance with conditions or obligations + extrinsic to this License (such as a court order or an agreement with a + third party). + + However, You may Distribute a Covered Work incorporating material + governed by a license that is both OSI-Approved and FSF-Free as of the + release date of this License, provided that compliance with such + other license would not conflict with any conditions stated in other + sections of this License. + +5. Conditions for Distributing Object Code + + You may Distribute an Object Code form of a Covered Work, provided that + you accompany the Object Code with a URL through which the Corresponding + Source is made available, at no charge, by some standard or customary + means of providing network access to source code. + + If you Distribute the Object Code in a physical product or tangible + storage medium ("Product"), the Corresponding Source must be available + through such URL for two years from the date of Your most recent + Distribution of the Object Code in the Product. However, if the Product + itself contains or is accompanied by the Corresponding Source (made + available in a customarily accessible manner), You need not also comply + with the first paragraph of this section. + + Each direct and indirect recipient of the Covered Work from You is an + intended third-party beneficiary of this License solely as to this + section 5, with the right to enforce its terms. + +6. Symmetrical Licensing Condition for Upstream Contributions + + If You Distribute a work to Me specifically for inclusion in or + modification of a Covered Work (a "Patch"), and no explicit licensing + terms apply to the Patch, You license the Patch under this License, to + the extent of Your copyright in the Patch. This condition does not + negate the other conditions of this License, if applicable to the Patch. + +7. Nullification of Copyleft/Proprietary Dual Licensing + + If I offer to license, for a fee, a Covered Work under terms other than + a license that is OSI-Approved or FSF-Free as of the release date of this + License or a numbered version of copyleft-next released by the + Copyleft-Next Project, then the license I grant You under section 1 is no + longer subject to the conditions in sections 3 through 5. + +8. Copyleft Sunset + + The conditions in sections 3 through 5 no longer apply once fifteen + years have elapsed from the date of My first Distribution of My Work + under this License. + +9. Pass-Through + + When You Distribute a Covered Work, the recipient automatically receives + a license to My Work from Me, subject to the terms of this License. + +10. Termination + + Your license grants under section 1 are automatically terminated if You + + a) fail to comply with the conditions of this License, unless You cure + such noncompliance within thirty days after becoming aware of it, or + + b) initiate a patent infringement litigation claim (excluding + declaratory judgment actions, counterclaims, and cross-claims) + alleging that any part of My Work directly or indirectly infringes + any patent. + + Termination of Your license grants extends to all copies of Covered + Works You subsequently obtain. Termination does not terminate the + rights of those who have received copies or rights from You subject to + this License. + + To the extent permission to make copies of a Covered Work is necessary + merely for running it, such permission is not terminable. + +11. Later License Versions + + The Copyleft-Next Project may release new versions of copyleft-next, + designated by a distinguishing version number ("Later Versions"). + Unless I explicitly remove the option of Distributing Covered Works + under Later Versions, You may Distribute Covered Works under any Later + Version. + +** 12. No Warranty ** +** ** +** My Work is provided "as-is", without warranty. You bear the risk ** +** of using it. To the extent permitted by applicable law, each ** +** Distributor of My Work excludes the implied warranties of title, ** +** merchantability, fitness for a particular purpose and ** +** non-infringement. ** + +** 13. Limitation of Liability ** +** ** +** To the extent permitted by applicable law, in no event will any ** +** Distributor of My Work be liable to You for any damages ** +** whatsoever, whether direct, indirect, special, incidental, or ** +** consequential damages, whether arising under contract, tort ** +** (including negligence), or otherwise, even where the Distributor ** +** knew or should have known about the possibility of such damages. ** + +14. Severability + + The invalidity or unenforceability of any provision of this License + does not affect the validity or enforceability of the remainder of + this License. Such provision is to be reformed to the minimum extent + necessary to make it valid and enforceable. + +15. Definitions + + "Copyleft-Next Project" means the project that maintains the source + code repository at <https://github.com/copyleft-next/copyleft-next.git/> + as of the release date of this License. + + "Corresponding Source" of a Covered Work in Object Code form means (i) + the Source Code form of the Covered Work; (ii) all scripts, + instructions and similar information that are reasonably necessary for + a skilled developer to generate such Object Code from the Source Code + provided under (i); and (iii) a list clearly identifying all Separate + Works (other than those provided in compliance with (ii)) that were + specifically used in building and (if applicable) installing the + Covered Work (for example, a specified proprietary compiler including + its version number). Corresponding Source must be machine-readable. + + "Covered Work" means My Work or a Derived Work. + + "Derived Work" means a work of authorship that copies from, modifies, + adapts, is based on, is a derivative work of, transforms, translates or + contains all or part of My Work, such that copyright permission is + required. The following are not Derived Works: (i) Mere Aggregation; + (ii) a mere reproduction of My Work; and (iii) if My Work fails to + explicitly state an expectation otherwise, a work that merely makes + reference to My Work. + + "Distribute" means to distribute, transfer or make a copy available to + someone else, such that copyright permission is required. + + "Distributor" means Me and anyone else who Distributes a Covered Work. + + "FSF-Free" means classified as 'free' by the Free Software Foundation. + + "GPL" means a version of the GNU General Public License or the GNU + Affero General Public License. + + "I"/"Me"/"My" refers to the individual or legal entity that places My + Work under this License. "You"/"Your" refers to the individual or legal + entity exercising rights in My Work under this License. A legal entity + includes each entity that controls, is controlled by, or is under + common control with such legal entity. "Control" means (a) the power to + direct the actions of such legal entity, whether by contract or + otherwise, or (b) ownership of more than fifty percent of the + outstanding shares or beneficial ownership of such legal entity. + + "Licensed Patents" means all patent claims licensable royalty-free by + Me, now or in the future, that are necessarily infringed by making, + using, or selling My Work, and excludes claims that would be infringed + only as a consequence of further modification of My Work. + + "Mere Aggregation" means an aggregation of a Covered Work with a + Separate Work. + + "My Work" means the particular work of authorship I license to You + under this License. + + "Object Code" means any form of a work that is not Source Code. + + "OSI-Approved" means approved as 'Open Source' by the Open Source + Initiative. + + "Separate Work" means a work that is separate from and independent of a + particular Covered Work and is not by its nature an extension or + enhancement of the Covered Work, and/or a runtime library, standard + library or similar component that is used to generate an Object Code + form of a Covered Work. + + "Source Code" means the preferred form of a work for making + modifications to it. @@ -0,0 +1,137 @@ +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <stdbool.h> +#include <string.h> +#define NUM_THREADS 11 + +struct bin { + bool ready; + int food[NUM_THREADS+1]; + int trash[NUM_THREADS+1]; + pthread_mutex_t mutex; +}; + +struct party_ticket { + long threadid; + struct bin *party; +}; + +struct bin *party; + +void eat(long tid) +{ + bool eat_in_order = true; + long t; + + /* If anyone ate out of place lets take notice of that... */ + for (t=0; t < tid; t++) { + if (party->food[t]) + eat_in_order = false; + } + + if (party->food[tid]) + party->food[tid] = 0; +} + +void cleanup(long tid) +{ + bool clean_follow = true; + long t; + + for (t=0; t < tid; t++) { + if (!party->trash[t]) + clean_follow = false; + } + + party->trash[tid] = 1; +} + +void *thread_party(void *t) +{ + long tid = (long)t; + + pthread_mutex_lock(&party->mutex); + eat(tid); + + if (tid % 2 == 1) + sleep(1); + + cleanup(tid); + pthread_mutex_unlock(&party->mutex); + + pthread_exit(NULL); +} + +void show_party(struct bin *party) +{ + long t; + + printf("FOOD: [ "); + + for (t=0; t < NUM_THREADS; t++) { + if (party->food[t]) + printf("*"); + else + printf(" "); + } + printf(" ]\n"); + + printf("TRASH: [ "); + for (t=0; t < NUM_THREADS; t++) { + if (party->trash[t]) + printf("*"); + else + printf(" "); + } + printf(" ]\n"); +} + +int main(int argc, char *argv[]) +{ + pthread_t threads[NUM_THREADS]; + int ret; + long t; + pthread_attr_t attr; + void *status; + + party = malloc(sizeof(struct bin)); + if (!party) + return -ENOMEM; + memset(party, 0, sizeof(struct bin)); + for (t=0; t <= NUM_THREADS; t++) { + party->food[t] = 1; + party->trash[t] = 0; + } + party->ready = true; + + printf("Before party:\n"); + show_party(party); + + pthread_mutex_init(&party->mutex, NULL); + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + for (t=0; t < NUM_THREADS; t++) { + ret = pthread_create(&threads[t], &attr, thread_party, (void *)t); + if (ret){ + printf("ERROR; return code from pthread_create() is %d\n", ret); + exit(-1); + } + } + + pthread_attr_destroy(&attr); + for (t=0; t < NUM_THREADS; t++) + pthread_join(threads[t], &status); + + printf("\nAfter party:\n"); + show_party(party); + + pthread_mutex_destroy(&party->mutex); + pthread_exit(NULL); + + free(party); +} |