Compare commits
37 Commits
Author | SHA1 | Date | |
---|---|---|---|
61893b06fc | |||
3efd29f07c | |||
89a5cfb956 | |||
9902ff0e2d | |||
8ee819a6e1 | |||
4a38787d22 | |||
d5ef0aa31f | |||
9d6f76219f | |||
5be0e06356 | |||
a395da2619 | |||
d5e1e1f47e | |||
5cabc85f98 | |||
a4641e67a3 | |||
af89d15298 | |||
b6db3331a5 | |||
5322b71c00 | |||
d43bd7c9ef | |||
314683ff3a | |||
bb4424342e | |||
2c17a43c99 | |||
bbd5b70b6f | |||
5c6bae6b68 | |||
dffe2a4055 | |||
fa2f8fbfa8 | |||
57425803c5 | |||
e9cf69d2fe | |||
6adfb1a436 | |||
583c2a3525 | |||
3172b07ba4 | |||
30827d8739 | |||
c9b4aa7d54 | |||
175a62e881 | |||
337418a7cb | |||
b6534fcc7f | |||
4dbe9b87b0 | |||
1577a2e1ba | |||
7efca46d5e |
29
.gitignore
vendored
29
.gitignore
vendored
@ -1,28 +1 @@
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.pyc
|
||||
|
510
LICENSE
510
LICENSE
@ -1,201 +1,373 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
Mozilla Public License Version 2.0
|
||||
==================================
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
1. Definitions
|
||||
--------------
|
||||
|
||||
1. Definitions.
|
||||
1.1. "Contributor"
|
||||
means each individual or legal entity that creates, contributes to
|
||||
the creation of, or owns Covered Software.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
1.2. "Contributor Version"
|
||||
means the combination of the Contributions of others (if any) used
|
||||
by a Contributor and that particular Contributor's Contribution.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
1.3. "Contribution"
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
1.4. "Covered Software"
|
||||
means Source Code Form to which the initial Contributor has attached
|
||||
the notice in Exhibit A, the Executable Form of such Source Code
|
||||
Form, and Modifications of such Source Code Form, in each case
|
||||
including portions thereof.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
1.5. "Incompatible With Secondary Licenses"
|
||||
means
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
(a) that the initial Contributor has attached the notice described
|
||||
in Exhibit B to the Covered Software; or
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
(b) that the Covered Software was made available under the terms of
|
||||
version 1.1 or earlier of the License, but not also under the
|
||||
terms of a Secondary License.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
1.6. "Executable Form"
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
1.7. "Larger Work"
|
||||
means a work that combines Covered Software with other material, in
|
||||
a separate file or files, that is not Covered Software.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
1.8. "License"
|
||||
means this document.
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
1.9. "Licensable"
|
||||
means having the right to grant, to the maximum extent possible,
|
||||
whether at the time of the initial grant or subsequently, any and
|
||||
all of the rights conveyed by this License.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
1.10. "Modifications"
|
||||
means any of the following:
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
(a) any file in Source Code Form that results from an addition to,
|
||||
deletion from, or modification of the contents of Covered
|
||||
Software; or
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
(b) any new file in Source Code Form that contains any Covered
|
||||
Software.
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
1.11. "Patent Claims" of a Contributor
|
||||
means any patent claim(s), including without limitation, method,
|
||||
process, and apparatus claims, in any patent Licensable by such
|
||||
Contributor that would be infringed, but for the grant of the
|
||||
License, by the making, using, selling, offering for sale, having
|
||||
made, import, or transfer of either its Contributions or its
|
||||
Contributor Version.
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
1.12. "Secondary License"
|
||||
means either the GNU General Public License, Version 2.0, the GNU
|
||||
Lesser General Public License, Version 2.1, the GNU Affero General
|
||||
Public License, Version 3.0, or any later versions of those
|
||||
licenses.
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
1.13. "Source Code Form"
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
1.14. "You" (or "Your")
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, "You" includes any entity that
|
||||
controls, is controlled by, or is under common control with You. For
|
||||
purposes of this definition, "control" means (a) the power, direct
|
||||
or indirect, to cause the direction or management of such entity,
|
||||
whether by contract or otherwise, or (b) ownership of more than
|
||||
fifty percent (50%) of the outstanding shares or beneficial
|
||||
ownership of such entity.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
2. License Grants and Conditions
|
||||
--------------------------------
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
2.1. Grants
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
(a) under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or
|
||||
as part of a Larger Work; and
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
||||
for sale, have made, import, and otherwise transfer either its
|
||||
Contributions or its Contributor Version.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
2.2. Effective Date
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
The licenses granted in Section 2.1 with respect to any Contribution
|
||||
become effective for each Contribution on the date the Contributor first
|
||||
distributes such Contribution.
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
The licenses granted in this Section 2 are the only rights granted under
|
||||
this License. No additional rights or licenses will be implied from the
|
||||
distribution or licensing of Covered Software under this License.
|
||||
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
||||
Contributor:
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
(a) for any code that a Contributor has removed from Covered Software;
|
||||
or
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
(b) for infringements caused by: (i) Your and any other third party's
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
(c) under Patent Claims infringed by Covered Software in the absence of
|
||||
its Contributions.
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
This License does not grant any rights in the trademarks, service marks,
|
||||
or logos of any Contributor (except as may be necessary to comply with
|
||||
the notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this
|
||||
License (see Section 10.2) or under the terms of a Secondary License (if
|
||||
permitted under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its
|
||||
Contributions are its original creation(s) or it has sufficient rights
|
||||
to grant the rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under
|
||||
applicable copyright doctrines of fair use, fair dealing, or other
|
||||
equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
||||
in Section 2.1.
|
||||
|
||||
3. Responsibilities
|
||||
-------------------
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under
|
||||
the terms of this License. You must inform recipients that the Source
|
||||
Code Form of the Covered Software is governed by the terms of this
|
||||
License, and how they can obtain a copy of this License. You may not
|
||||
attempt to alter or restrict the recipients' rights in the Source Code
|
||||
Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
(a) such Covered Software must also be made available in Source Code
|
||||
Form, as described in Section 3.1, and You must inform recipients of
|
||||
the Executable Form how they can obtain a copy of such Source Code
|
||||
Form by reasonable means in a timely manner, at a charge no more
|
||||
than the cost of distribution to the recipient; and
|
||||
|
||||
(b) You may distribute such Executable Form under the terms of this
|
||||
License, or sublicense it under different terms, provided that the
|
||||
license for the Executable Form does not attempt to limit or alter
|
||||
the recipients' rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for
|
||||
the Covered Software. If the Larger Work is a combination of Covered
|
||||
Software with a work governed by one or more Secondary Licenses, and the
|
||||
Covered Software is not Incompatible With Secondary Licenses, this
|
||||
License permits You to additionally distribute such Covered Software
|
||||
under the terms of such Secondary License(s), so that the recipient of
|
||||
the Larger Work may, at their option, further distribute the Covered
|
||||
Software under the terms of either this License or such Secondary
|
||||
License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices
|
||||
(including copyright notices, patent notices, disclaimers of warranty,
|
||||
or limitations of liability) contained within the Source Code Form of
|
||||
the Covered Software, except that You may alter any license notices to
|
||||
the extent required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on
|
||||
behalf of any Contributor. You must make it absolutely clear that any
|
||||
such warranty, support, indemnity, or liability obligation is offered by
|
||||
You alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
---------------------------------------------------
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this
|
||||
License with respect to some or all of the Covered Software due to
|
||||
statute, judicial order, or regulation then You must: (a) comply with
|
||||
the terms of this License to the maximum extent possible; and (b)
|
||||
describe the limitations and the code they affect. Such description must
|
||||
be placed in a text file included with all distributions of the Covered
|
||||
Software under this License. Except to the extent prohibited by statute
|
||||
or regulation, such description must be sufficiently detailed for a
|
||||
recipient of ordinary skill to be able to understand it.
|
||||
|
||||
5. Termination
|
||||
--------------
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically
|
||||
if You fail to comply with any of its terms. However, if You become
|
||||
compliant, then the rights granted under this License from a particular
|
||||
Contributor are reinstated (a) provisionally, unless and until such
|
||||
Contributor explicitly and finally terminates Your grants, and (b) on an
|
||||
ongoing basis, if such Contributor fails to notify You of the
|
||||
non-compliance by some reasonable means prior to 60 days after You have
|
||||
come back into compliance. Moreover, Your grants from a particular
|
||||
Contributor are reinstated on an ongoing basis if such Contributor
|
||||
notifies You of the non-compliance by some reasonable means, this is the
|
||||
first time You have received notice of non-compliance with this License
|
||||
from such Contributor, and You become compliant prior to 30 days after
|
||||
Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions,
|
||||
counter-claims, and cross-claims) alleging that a Contributor Version
|
||||
directly or indirectly infringes any patent, then the rights granted to
|
||||
You by any and all Contributors for the Covered Software under Section
|
||||
2.1 of this License shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
||||
end user license agreements (excluding distributors and resellers) which
|
||||
have been validly granted by You or Your distributors under this License
|
||||
prior to termination shall survive termination.
|
||||
|
||||
************************************************************************
|
||||
* *
|
||||
* 6. Disclaimer of Warranty *
|
||||
* ------------------------- *
|
||||
* *
|
||||
* Covered Software is provided under this License on an "as is" *
|
||||
* basis, without warranty of any kind, either expressed, implied, or *
|
||||
* statutory, including, without limitation, warranties that the *
|
||||
* Covered Software is free of defects, merchantable, fit for a *
|
||||
* particular purpose or non-infringing. The entire risk as to the *
|
||||
* quality and performance of the Covered Software is with You. *
|
||||
* Should any Covered Software prove defective in any respect, You *
|
||||
* (not any Contributor) assume the cost of any necessary servicing, *
|
||||
* repair, or correction. This disclaimer of warranty constitutes an *
|
||||
* essential part of this License. No use of any Covered Software is *
|
||||
* authorized under this License except under this disclaimer. *
|
||||
* *
|
||||
************************************************************************
|
||||
|
||||
************************************************************************
|
||||
* *
|
||||
* 7. Limitation of Liability *
|
||||
* -------------------------- *
|
||||
* *
|
||||
* Under no circumstances and under no legal theory, whether tort *
|
||||
* (including negligence), contract, or otherwise, shall any *
|
||||
* Contributor, or anyone who distributes Covered Software as *
|
||||
* permitted above, be liable to You for any direct, indirect, *
|
||||
* special, incidental, or consequential damages of any character *
|
||||
* including, without limitation, damages for lost profits, loss of *
|
||||
* goodwill, work stoppage, computer failure or malfunction, or any *
|
||||
* and all other commercial damages or losses, even if such party *
|
||||
* shall have been informed of the possibility of such damages. This *
|
||||
* limitation of liability shall not apply to liability for death or *
|
||||
* personal injury resulting from such party's negligence to the *
|
||||
* extent applicable law prohibits such limitation. Some *
|
||||
* jurisdictions do not allow the exclusion or limitation of *
|
||||
* incidental or consequential damages, so this exclusion and *
|
||||
* limitation may not apply to You. *
|
||||
* *
|
||||
************************************************************************
|
||||
|
||||
8. Litigation
|
||||
-------------
|
||||
|
||||
Any litigation relating to this License may be brought only in the
|
||||
courts of a jurisdiction where the defendant maintains its principal
|
||||
place of business and such litigation shall be governed by laws of that
|
||||
jurisdiction, without reference to its conflict-of-law provisions.
|
||||
Nothing in this Section shall prevent a party's ability to bring
|
||||
cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
----------------
|
||||
|
||||
This License represents the complete agreement concerning the subject
|
||||
matter hereof. If any provision of this License is held to be
|
||||
unenforceable, such provision shall be reformed only to the extent
|
||||
necessary to make it enforceable. Any law or regulation which provides
|
||||
that the language of a contract shall be construed against the drafter
|
||||
shall not be used to construe this License against a Contributor.
|
||||
|
||||
10. Versions of the License
|
||||
---------------------------
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version
|
||||
of the License under which You originally received the Covered Software,
|
||||
or under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a
|
||||
modified version of this License if you rename the license and remove
|
||||
any references to the name of the license steward (except to note that
|
||||
such modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
||||
Licenses
|
||||
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
-------------------------------------------
|
||||
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular
|
||||
file, then You may include the notice in a location (such as a LICENSE
|
||||
file in a relevant directory) where a recipient would be likely to look
|
||||
for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
||||
---------------------------------------------------------
|
||||
|
||||
This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
defined by the Mozilla Public License, v. 2.0.
|
1
authors.txt
Normal file
1
authors.txt
Normal file
@ -0,0 +1 @@
|
||||
MR Edouard DUPIN <yui.heero@gmail.com>
|
38
doc/build.md
Normal file
38
doc/build.md
Normal file
@ -0,0 +1,38 @@
|
||||
Build lib & build sample {#ethread_build}
|
||||
========================
|
||||
|
||||
@tableofcontents
|
||||
|
||||
Download: {#ethread_build_download}
|
||||
=========
|
||||
|
||||
ethread use some tools to manage source and build it:
|
||||
|
||||
lutin (build-system): {#ethread_build_download_lutin}
|
||||
---------------------
|
||||
|
||||
```{.sh}
|
||||
pip install lutin --user
|
||||
# optionnal dependency of lutin (manage image changing size for application release)
|
||||
pip install pillow --user
|
||||
```
|
||||
|
||||
sources: {#ethread_build_download_sources}
|
||||
--------
|
||||
|
||||
```{.sh}
|
||||
cd framework
|
||||
git clone https://github.com/atria-soft/ethread.git
|
||||
cd ..
|
||||
```
|
||||
|
||||
Build: {#ethread_build_build}
|
||||
======
|
||||
|
||||
|
||||
library: {#ethread_build_build_library}
|
||||
--------
|
||||
|
||||
```{.sh}
|
||||
lutin -mdebug ethread
|
||||
```
|
58
doc/mainpage.md
Normal file
58
doc/mainpage.md
Normal file
@ -0,0 +1,58 @@
|
||||
ETHREAD library {#mainpage}
|
||||
===============
|
||||
|
||||
@tableofcontents
|
||||
|
||||
What is ETHREAD: {#ethread_mainpage_what}
|
||||
================
|
||||
|
||||
ETHREAD, or Ewol thread tools (specific platform). this permit to abstaract the platform for somme tools associated with the theead like set priority or naming.
|
||||
|
||||
What it does: {#ethread_mainpage_what_it_does}
|
||||
-------------
|
||||
|
||||
- Set the thread name (usefull for elog) and in the system if possible
|
||||
- set the thread priority if supported by the system
|
||||
|
||||
What it doesn't do: {#ethread_mainpage_what_it_not_does}
|
||||
-------------------
|
||||
|
||||
- thread management
|
||||
- thread abstraction (we have std::thread for this ...)
|
||||
|
||||
What languages are supported? {#ethread_mainpage_language}
|
||||
=============================
|
||||
|
||||
ETHREAD is written in C++.
|
||||
|
||||
|
||||
Are there any licensing restrictions? {#ethread_mainpage_license_restriction}
|
||||
=====================================
|
||||
|
||||
ETHREAD is **FREE software** and _all sub-library are FREE and staticly linkable !!!_
|
||||
|
||||
|
||||
License (MPL v2.0) {#ethread_mainpage_license}
|
||||
==================
|
||||
|
||||
Copyright ETHREAD Edouard DUPIN
|
||||
|
||||
Licensed under the Mozilla Public License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
<https://www.mozilla.org/MPL/2.0>
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
||||
Other pages {#ethread_mainpage_sub_page}
|
||||
===========
|
||||
|
||||
- @ref ethread_build
|
||||
- [**ewol coding style**](http://atria-soft.github.io/ewol/ewol_coding_style.html)
|
||||
|
32
doxy_ethread-tools.py
Normal file
32
doxy_ethread-tools.py
Normal file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/python
|
||||
import os
|
||||
import doxy.module as module
|
||||
import doxy.debug as debug
|
||||
import doxy.tools as tools
|
||||
|
||||
def create(target, module_name):
|
||||
my_module = module.Module(__file__, module_name)
|
||||
my_module.set_version("version.txt")
|
||||
my_module.set_title("ethread: Ewol thread platform tools")
|
||||
my_module.set_website("http://atria-soft.github.io/" + module_name)
|
||||
my_module.set_website_sources("http://github.com/atria-soft/" + module_name)
|
||||
my_module.add_path([
|
||||
module_name,
|
||||
"doc"
|
||||
])
|
||||
my_module.add_file_patterns([
|
||||
'tools.hpp',
|
||||
'*.md',
|
||||
])
|
||||
my_module.add_exclude_symbols([
|
||||
'*operator<<*',
|
||||
])
|
||||
my_module.add_exclude_file([
|
||||
'debug.hpp',
|
||||
])
|
||||
my_module.add_file_patterns([
|
||||
'*.hpp',
|
||||
'*.md',
|
||||
])
|
||||
|
||||
return my_module
|
33
doxy_ethread.py
Normal file
33
doxy_ethread.py
Normal file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python
|
||||
import os
|
||||
import doxy.module as module
|
||||
import doxy.debug as debug
|
||||
import doxy.tools as tools
|
||||
|
||||
def create(target, module_name):
|
||||
my_module = module.Module(__file__, module_name)
|
||||
my_module.set_version("version.txt")
|
||||
my_module.set_title("ethread: Ewol thread platform tools")
|
||||
my_module.set_website("http://atria-soft.github.io/" + module_name)
|
||||
my_module.set_website_sources("http://github.com/atria-soft/" + module_name)
|
||||
my_module.add_path([
|
||||
module_name,
|
||||
"doc"
|
||||
])
|
||||
my_module.add_file_patterns([
|
||||
'*.hpp',
|
||||
'*.md',
|
||||
])
|
||||
my_module.add_exclude_symbols([
|
||||
'*operator<<*',
|
||||
])
|
||||
my_module.add_exclude_file([
|
||||
'debug.hpp',
|
||||
'tools.hpp',
|
||||
])
|
||||
my_module.add_file_patterns([
|
||||
'*.hpp',
|
||||
'*.md',
|
||||
])
|
||||
|
||||
return my_module
|
40
ethread/Future.cpp
Normal file
40
ethread/Future.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include "debug.hpp"
|
||||
#include <ethread/Future.hpp>
|
||||
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Future);
|
||||
|
||||
ethread::Future::Future(ememory::SharedPtr<ethread::Promise> _promise):
|
||||
m_promise(_promise) {
|
||||
|
||||
}
|
||||
|
||||
bool ethread::Future::isFinished() {
|
||||
if (m_promise == null) {
|
||||
ETHREAD_ERROR("Promise does not exist...");
|
||||
return true;
|
||||
}
|
||||
return m_promise->isFinished();
|
||||
}
|
||||
|
||||
bool ethread::Future::wait(echrono::Duration _delay) {
|
||||
if (m_promise == null) {
|
||||
ETHREAD_ERROR("Promise does not exist...");
|
||||
return false;
|
||||
}
|
||||
return m_promise->wait(_delay);
|
||||
}
|
||||
|
||||
void ethread::Future::andThen(etk::Function<void()> _action) {
|
||||
if (m_promise == null) {
|
||||
ETHREAD_ERROR("Promise does not exist...");
|
||||
return;
|
||||
}
|
||||
m_promise->andThen(_action);
|
||||
}
|
43
ethread/Future.hpp
Normal file
43
ethread/Future.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <ethread/Promise.hpp>
|
||||
#include <ememory/memory.hpp>
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief Simple future to process
|
||||
*/
|
||||
class Future {
|
||||
private:
|
||||
ememory::SharedPtr<ethread::Promise> m_promise; //!< the assiciated promise that we are waiting and of action
|
||||
public:
|
||||
/**
|
||||
* @brief Simple Future contructor
|
||||
* @param[in] _promise Associated promise to wait
|
||||
*/
|
||||
Future(ememory::SharedPtr<ethread::Promise> _promise=null);
|
||||
/**
|
||||
* @brief Check if the action is finished
|
||||
* @return true, the action is done, false otherwise
|
||||
*/
|
||||
bool isFinished();
|
||||
/**
|
||||
* @brief Wait some time that the action finished
|
||||
* @param[in] _delay Delay to wait the action is finished
|
||||
* @return true, the action is finished, false, the time-out apear.
|
||||
*/
|
||||
bool wait(echrono::Duration _delay=echrono::seconds(2));
|
||||
/**
|
||||
* @brief Action to do when the action is finished
|
||||
* @param[in] _action New action to do.
|
||||
*/
|
||||
void andThen(etk::Function<void()> _action);
|
||||
};
|
||||
}
|
33
ethread/Mutex.Windows.cpp
Normal file
33
ethread/Mutex.Windows.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Mutex);
|
||||
ETK_DECLARE_TYPE(ethread::UniqueLock);
|
||||
|
||||
ethread::Mutex::Mutex() {
|
||||
InitializeCriticalSection(&m_mutex);
|
||||
}
|
||||
|
||||
ethread::Mutex::~Mutex() {
|
||||
DeleteCriticalSection(&m_mutex);
|
||||
}
|
||||
|
||||
void ethread::Mutex::lock() {
|
||||
EnterCriticalSection(&m_mutex);
|
||||
}
|
||||
|
||||
bool ethread::Mutex::tryLock() {
|
||||
return TryEnterCriticalSection(&m_mutex) != 0;
|
||||
}
|
||||
|
||||
void ethread::Mutex::unLock() {
|
||||
LeaveCriticalSection(&m_mutex);
|
||||
}
|
||||
|
94
ethread/Mutex.hpp
Normal file
94
ethread/Mutex.hpp
Normal file
@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
#include <etk/types.hpp>
|
||||
|
||||
#ifdef __TARGET_OS__Windows
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief Generic mutex interface (OS independent)
|
||||
*/
|
||||
class Mutex {
|
||||
private:
|
||||
#ifdef __TARGET_OS__Windows
|
||||
CRITICAL_SECTION m_mutex;
|
||||
#else
|
||||
pthread_mutex_t m_mutex;
|
||||
#endif
|
||||
public:
|
||||
/**
|
||||
* @brief Create a new mutex
|
||||
*/
|
||||
Mutex();
|
||||
/**
|
||||
* @brief Destroy the mutex.
|
||||
*/
|
||||
~Mutex();
|
||||
/**
|
||||
* @brief Lock the mutex (Wait while the mutex is not lock)
|
||||
*/
|
||||
void lock();
|
||||
/**
|
||||
* @brief Try to lock the mutex (exit if mutex is already locked)
|
||||
* @return true The mutex is locked
|
||||
* @return false The mutex is already locked.
|
||||
*/
|
||||
bool tryLock();
|
||||
/**
|
||||
* @brief Unloc the mutex
|
||||
*/
|
||||
void unLock();
|
||||
};
|
||||
/**
|
||||
* @brief AutoLock and un-lock when exit fuction.
|
||||
*/
|
||||
class UniqueLock {
|
||||
private:
|
||||
// Keep a reference on the mutex
|
||||
ethread::Mutex &m_protect;
|
||||
bool m_lock;
|
||||
public:
|
||||
/**
|
||||
* @brief constructor that automaticly lock the mutex.
|
||||
* @param[in] _protect Mutex to Lock.
|
||||
* @param[in] _notLock Must be lock after by a tryLock.
|
||||
*/
|
||||
UniqueLock(ethread::Mutex& _protect, bool _notLock = false) :
|
||||
m_protect(_protect),
|
||||
m_lock(false) {
|
||||
if (_notLock == false) {
|
||||
m_protect.lock();
|
||||
m_lock = true;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief Try to lock the mutex (exit if mutex is already locked)
|
||||
* @return true The mutex is locked
|
||||
* @return false The mutex is already locked.
|
||||
*/
|
||||
bool tryLock() {
|
||||
if (m_protect.tryLock() == true) {
|
||||
m_lock = true;
|
||||
}
|
||||
return m_lock;
|
||||
}
|
||||
/**
|
||||
* @brief Destructor that Auto Unlock mutex when remove.
|
||||
*/
|
||||
virtual ~UniqueLock(){
|
||||
if (m_lock == true) {
|
||||
m_protect.unLock();
|
||||
m_lock = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
78
ethread/Mutex.pthread.cpp
Normal file
78
ethread/Mutex.pthread.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <ethread/tools.hpp>
|
||||
extern "C" {
|
||||
#include <errno.h>
|
||||
}
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Mutex);
|
||||
ETK_DECLARE_TYPE(ethread::UniqueLock);
|
||||
//#include <ethread/debug.hpp>
|
||||
|
||||
ethread::Mutex::Mutex() {
|
||||
// create interface mutex :
|
||||
int ret = pthread_mutex_init(&m_mutex, null);
|
||||
//ETHREAD_ASSERT(ret == 0, "Error creating Mutex ...");
|
||||
}
|
||||
|
||||
|
||||
ethread::Mutex::~Mutex() {
|
||||
// Remove mutex
|
||||
int ret = pthread_mutex_destroy(&m_mutex);
|
||||
//ETHREAD_ASSERT(ret == 0, "Error destroying Mutex ...");
|
||||
}
|
||||
|
||||
|
||||
void ethread::Mutex::lock() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
}
|
||||
|
||||
|
||||
bool ethread::Mutex::tryLock() {
|
||||
int ret = pthread_mutex_trylock(&m_mutex);
|
||||
if (ret == 0) {
|
||||
return true;
|
||||
}
|
||||
if (ret == EINVAL) {
|
||||
printf("trylock error: EINVAL\n");
|
||||
// The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's
|
||||
// priority is higher than the mutex's current priority ceiling.
|
||||
// The pthread_mutex_trylock() function shall fail if:
|
||||
}
|
||||
if (ret == EBUSY) {
|
||||
printf("trylock error: EBUSY\n");
|
||||
// The mutex could not be acquired because it was already locked.
|
||||
// The pthread_mutex_lock(), pthread_mutex_trylock(), and pthread_mutex_unlock() functions may fail if:
|
||||
}
|
||||
if (ret == EINVAL) {
|
||||
printf("trylock error: EINVAL\n");
|
||||
// The value specified by mutex does not refer to an initialized mutex object.
|
||||
}
|
||||
if (ret == EAGAIN) {
|
||||
printf("trylock error: EAGAIN\n");
|
||||
// The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.
|
||||
// The pthread_mutex_lock() function may fail if:
|
||||
}
|
||||
if (ret == EDEADLK) {
|
||||
printf("trylock error: EDEADLK\n");
|
||||
// The current thread already owns the mutex.
|
||||
// The pthread_mutex_unlock() function may fail if:
|
||||
}
|
||||
if (ret == EPERM) {
|
||||
printf("trylock error: EPERM\n");
|
||||
//The current thread does not own the mutex.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void ethread::Mutex::unLock() {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
99
ethread/MutexRecursive.hpp
Normal file
99
ethread/MutexRecursive.hpp
Normal file
@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
#include <etk/types.hpp>
|
||||
|
||||
#ifdef __TARGET_OS__Windows
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief Generic mutex interface (OS independent)
|
||||
*/
|
||||
// TODO: Create a single class parametrable for all mutex ...
|
||||
class MutexRecursive {
|
||||
private:
|
||||
#ifdef __TARGET_OS__Windows
|
||||
CRITICAL_SECTION m_mutex;
|
||||
#else
|
||||
pthread_mutex_t m_mutex;
|
||||
pthread_mutexattr_t m_attribute;
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
uint32_t m_threadThatHaveLock;
|
||||
#endif
|
||||
public:
|
||||
/**
|
||||
* @brief Create a new mutex recursive
|
||||
*/
|
||||
MutexRecursive();
|
||||
/**
|
||||
* @brief Destroy the mutex.
|
||||
*/
|
||||
~MutexRecursive();
|
||||
/**
|
||||
* @brief Lock the mutex (Wait while the mutex is not lock)
|
||||
*/
|
||||
void lock();
|
||||
/**
|
||||
* @brief Try to lock the mutex (exit if mutex is already locked)
|
||||
* @return true The mutex is locked
|
||||
* @return false The mutex is already locked.
|
||||
*/
|
||||
bool tryLock();
|
||||
/**
|
||||
* @brief Unloc the mutex
|
||||
*/
|
||||
void unLock();
|
||||
};
|
||||
/**
|
||||
* @brief AutoLock and un-lock when exit fuction.
|
||||
*/
|
||||
class RecursiveLock {
|
||||
private:
|
||||
// Keep a reference on the mutex
|
||||
ethread::MutexRecursive &m_protect;
|
||||
bool m_lock;
|
||||
public:
|
||||
/**
|
||||
* @brief constructor that automaticly lock the mutex.
|
||||
* @param[in] _protect Mutex to Lock.
|
||||
* @param[in] _notLock Must be lock after by a tryLock.
|
||||
*/
|
||||
RecursiveLock(ethread::MutexRecursive& _protect, bool _notLock = false) :
|
||||
m_protect(_protect),
|
||||
m_lock(false) {
|
||||
if (_notLock == false) {
|
||||
m_protect.lock();
|
||||
m_lock = true;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief Try to lock the mutex (exit if mutex is already locked)
|
||||
* @return true The mutex is locked
|
||||
* @return false The mutex is already locked.
|
||||
*/
|
||||
bool tryLock() {
|
||||
if (m_protect.tryLock() == true) {
|
||||
m_lock = true;
|
||||
}
|
||||
return m_lock;
|
||||
}
|
||||
/**
|
||||
* @brief Destructor that Auto Unlock mutex when remove.
|
||||
*/
|
||||
virtual ~RecursiveLock(){
|
||||
if (m_lock == true) {
|
||||
m_protect.unLock();
|
||||
m_lock = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
89
ethread/MutexRecursive.pthread.cpp
Normal file
89
ethread/MutexRecursive.pthread.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <etk/types.hpp>
|
||||
#include <ethread/MutexRecursive.hpp>
|
||||
#include <ethread/tools.hpp>
|
||||
extern "C" {
|
||||
#include <errno.h>
|
||||
}
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::MutexRecursive);
|
||||
ETK_DECLARE_TYPE(ethread::RecursiveLock);
|
||||
//#include <ethread/debug.hpp>
|
||||
|
||||
ethread::MutexRecursive::MutexRecursive() {
|
||||
#ifdef DEBUG
|
||||
m_threadThatHaveLock = 0xFFFFFFFF;
|
||||
#endif
|
||||
// create interface mutex :
|
||||
pthread_mutexattr_init(&m_attribute);
|
||||
pthread_mutexattr_settype(&m_attribute, PTHREAD_MUTEX_RECURSIVE);
|
||||
int ret = pthread_mutex_init(&m_mutex, &m_attribute);
|
||||
//ETHREAD_ASSERT(ret == 0, "Error creating Mutex ...");
|
||||
}
|
||||
|
||||
|
||||
ethread::MutexRecursive::~MutexRecursive() {
|
||||
// Remove mutex
|
||||
int ret = pthread_mutex_destroy(&m_mutex);
|
||||
//ETHREAD_ASSERT(ret == 0, "Error destroying Mutex ...");
|
||||
}
|
||||
|
||||
|
||||
void ethread::MutexRecursive::lock() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
#ifdef DEBUG
|
||||
m_threadThatHaveLock = ethread::getId();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool ethread::MutexRecursive::tryLock() {
|
||||
int ret = pthread_mutex_trylock(&m_mutex);
|
||||
if (ret == 0) {
|
||||
return true;
|
||||
}
|
||||
if (ret == EINVAL) {
|
||||
printf("trylock error: EINVAL\n");
|
||||
// The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's
|
||||
// priority is higher than the mutex's current priority ceiling.
|
||||
// The pthread_mutex_trylock() function shall fail if:
|
||||
}
|
||||
if (ret == EBUSY) {
|
||||
printf("trylock error: EBUSY\n");
|
||||
// The mutex could not be acquired because it was already locked.
|
||||
// The pthread_mutex_lock(), pthread_mutex_trylock(), and pthread_mutex_unlock() functions may fail if:
|
||||
}
|
||||
if (ret == EINVAL) {
|
||||
printf("trylock error: EINVAL\n");
|
||||
// The value specified by mutex does not refer to an initialized mutex object.
|
||||
}
|
||||
if (ret == EAGAIN) {
|
||||
printf("trylock error: EAGAIN\n");
|
||||
// The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.
|
||||
// The pthread_mutex_lock() function may fail if:
|
||||
}
|
||||
if (ret == EDEADLK) {
|
||||
printf("trylock error: EDEADLK\n");
|
||||
// The current thread already owns the mutex.
|
||||
// The pthread_mutex_unlock() function may fail if:
|
||||
}
|
||||
if (ret == EPERM) {
|
||||
printf("trylock error: EPERM\n");
|
||||
//The current thread does not own the mutex.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void ethread::MutexRecursive::unLock() {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
#ifdef DEBUG
|
||||
m_threadThatHaveLock = 0xFFFFFFFF;
|
||||
#endif
|
||||
}
|
||||
|
132
ethread/Pool.cpp
Normal file
132
ethread/Pool.cpp
Normal file
@ -0,0 +1,132 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <ethread/Pool.hpp>
|
||||
#include <ethread/PoolExecutor.hpp>
|
||||
#include "debug.hpp"
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Pool);
|
||||
|
||||
ethread::Pool::Pool(uint16_t _numberOfThread):
|
||||
m_lastTrandId(1) {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
for (uint32_t iii=0; iii<_numberOfThread; ++iii) {
|
||||
ememory::SharedPtr<ethread::PoolExecutor> tmp = ememory::makeShared<ethread::PoolExecutor>(*this);
|
||||
if (tmp != null) {
|
||||
tmp->start();
|
||||
m_listThread.pushBack(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ethread::Pool::~Pool() {
|
||||
stop();
|
||||
join();
|
||||
}
|
||||
|
||||
uint32_t ethread::Pool::createGroupId() {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
return m_lastTrandId++;
|
||||
}
|
||||
|
||||
ethread::Future ethread::Pool::async(etk::Function<void()> _call, uint64_t _executionInGroupId) {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
if (_call == null) {
|
||||
ETHREAD_ERROR("Can not add an action with no function to call...");
|
||||
return ethread::Future();
|
||||
}
|
||||
ememory::SharedPtr<ethread::Promise> promise = ememory::makeShared<ethread::Promise>();
|
||||
ememory::SharedPtr<ethread::PoolAction> action = ememory::makeShared<ethread::PoolAction>(_executionInGroupId, promise, _call);
|
||||
m_listActions.pushBack(action);
|
||||
for(auto &it : m_listThread) {
|
||||
if (it == null) {
|
||||
continue;
|
||||
}
|
||||
if (it->touch() == true) {
|
||||
// Find one to force action now ...
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ethread::Future(promise);
|
||||
}
|
||||
|
||||
void ethread::Pool::releaseId(uint64_t _id) {
|
||||
if (_id == 0) {
|
||||
return;
|
||||
}
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
auto it = m_listIdPool.begin();
|
||||
while (it != m_listIdPool.end()) {
|
||||
if (*it == _id) {
|
||||
it = m_listIdPool.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
// get an action to execute ...
|
||||
ememory::SharedPtr<ethread::PoolAction> ethread::Pool::getAction() {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
auto it = m_listActions.begin();
|
||||
while (it != m_listActions.end()) {
|
||||
if (*it == null) {
|
||||
it = m_listActions.erase(it);
|
||||
continue;
|
||||
}
|
||||
// Check if this element is associated at a specific pool...
|
||||
uint64_t uniquId = (*it)->getPoolId();
|
||||
bool alreadyUsed = false;
|
||||
if (uniquId != 0) {
|
||||
for (auto &itId : m_listIdPool) {
|
||||
if (itId == uniquId) {
|
||||
alreadyUsed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (alreadyUsed == false) {
|
||||
ememory::SharedPtr<ethread::PoolAction> out = (*it);
|
||||
if (uniquId != 0) {
|
||||
m_listIdPool.pushBack(uniquId);
|
||||
}
|
||||
it = m_listActions.erase(it);
|
||||
return out;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
void ethread::Pool::stop() {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
auto it = m_listThread.begin();
|
||||
while (it != m_listThread.end()) {
|
||||
if (*it == null) {
|
||||
it = m_listThread.erase(it);
|
||||
continue;
|
||||
}
|
||||
(*it)->stop();
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void ethread::Pool::join() {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
ETHREAD_DEBUG("start join all the threads in pool " << m_listThread.size());
|
||||
for (size_t iii=0; iii<m_listThread.size(); ++iii) {
|
||||
ETHREAD_DEBUG(" join " << iii);
|
||||
if (m_listThread[iii] == null) {
|
||||
continue;
|
||||
}
|
||||
m_listThread[iii]->join();
|
||||
}
|
||||
ETHREAD_DEBUG(" ==> all joined");
|
||||
m_listThread.clear();
|
||||
ETHREAD_DEBUG(" ==> all reset");
|
||||
}
|
||||
|
69
ethread/Pool.hpp
Normal file
69
ethread/Pool.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <etk/Vector.hpp>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <ethread/Future.hpp>
|
||||
#include <ethread/PoolAction.hpp>
|
||||
|
||||
namespace ethread {
|
||||
class PoolExecutor;
|
||||
/**
|
||||
* @brief A pool is an association of a list of active thread that excecute some actions
|
||||
*/
|
||||
class Pool {
|
||||
private:
|
||||
ethread::Mutex m_mutex; //!< global add and release some thread
|
||||
etk::Vector<ememory::SharedPtr<ethread::PoolExecutor>> m_listThread; //!< Thread pool
|
||||
etk::Vector<ememory::SharedPtr<ethread::PoolAction>> m_listActions; //!< Thread pool
|
||||
etk::Vector<uint64_t> m_listIdPool; //!< Thread pool
|
||||
uint32_t m_lastTrandId; //!< to group the action in a single thread
|
||||
public:
|
||||
/**
|
||||
* @brief Contructor of the threadPool
|
||||
* @param[in] _numberOfThread Basic number of thread
|
||||
*/
|
||||
Pool(uint16_t _numberOfThread);
|
||||
/**
|
||||
* @brief DEstructor (virtualized)
|
||||
*/
|
||||
virtual ~Pool();
|
||||
/**
|
||||
* @brief Create a new group ID to manage a single ID
|
||||
* @return A simple incremented group ID (number 0 to 10 are reserved)
|
||||
*/
|
||||
uint32_t createGroupId();
|
||||
/**
|
||||
* @brief Ad a async action to process later
|
||||
* @param[in] _func Function to execure to process action
|
||||
* @param[in] _executionInGroupId Group in which the process must be run
|
||||
* @return A future on the action done
|
||||
*/
|
||||
// Execte in a group != of 0 request ordering the action in a single thread (same as a trand ...)
|
||||
ethread::Future async(etk::Function<void()> _func, uint64_t _executionInGroupId=0); //!< execute an action in the thread pool...
|
||||
// internal:
|
||||
/**
|
||||
* @brief Gan an Action to process
|
||||
* @return A single action to process in a thread
|
||||
*/
|
||||
ememory::SharedPtr<ethread::PoolAction> getAction();
|
||||
/**
|
||||
* @brief Release an action id in the pool of action in progress
|
||||
* @param[in] _id Id to release
|
||||
*/
|
||||
void releaseId(uint64_t _id);
|
||||
/**
|
||||
* @brief Stop all the thread and current actions
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Join all thread to destroy the pool
|
||||
*/
|
||||
void join();
|
||||
};
|
||||
}
|
34
ethread/PoolAction.cpp
Normal file
34
ethread/PoolAction.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <ethread/PoolAction.hpp>
|
||||
#include "debug.hpp"
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::PoolAction);
|
||||
|
||||
ethread::PoolAction::PoolAction(uint64_t _currentPoolId, ememory::SharedPtr<ethread::Promise> _promise, etk::Function<void()> _call) :
|
||||
m_currentPoolId(_currentPoolId),
|
||||
m_promise(_promise),
|
||||
m_call(etk::move(_call)) {
|
||||
|
||||
}
|
||||
|
||||
uint64_t ethread::PoolAction::getPoolId() const {
|
||||
return m_currentPoolId;
|
||||
}
|
||||
|
||||
void ethread::PoolAction::call() {
|
||||
if (m_call == null) {
|
||||
return;
|
||||
}
|
||||
if (m_call != null) {
|
||||
m_call();
|
||||
}
|
||||
if (m_promise != null) {
|
||||
m_promise->finish();
|
||||
}
|
||||
}
|
||||
|
41
ethread/PoolAction.hpp
Normal file
41
ethread/PoolAction.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <etk/Vector.hpp>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <ethread/Future.hpp>
|
||||
#include <ememory/memory.hpp>
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief A pool Action is a link between an action to do (the call) and a group of excecution (poolId) and the result store in a promise.
|
||||
*/
|
||||
class PoolAction {
|
||||
private:
|
||||
uint64_t m_currentPoolId; //!< execution group Id requested
|
||||
ememory::SharedPtr<ethread::Promise> m_promise; //!< Return promise of the action
|
||||
etk::Function<void()> m_call; //!< Action to do ...
|
||||
public:
|
||||
/**
|
||||
* @brief Contuctor of a simple action
|
||||
* @param[in] _currentPoolId Id Of the action might be process inside
|
||||
* @param[in] _promise Promise to call when action is done
|
||||
* @param[in] _call Action to do (callable object)
|
||||
*/
|
||||
PoolAction(uint64_t _currentPoolId, ememory::SharedPtr<ethread::Promise> _promise, etk::Function<void()> _call);
|
||||
/**
|
||||
* @brief Get the Pool id of the Action
|
||||
* @return The pool id of this action (0 for no request)
|
||||
*/
|
||||
uint64_t getPoolId() const;
|
||||
/**
|
||||
* @brief Call the action to do (real action will be done)
|
||||
*/
|
||||
void call();
|
||||
};
|
||||
}
|
102
ethread/PoolExecutor.cpp
Normal file
102
ethread/PoolExecutor.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <etk/Vector.hpp>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <ethread/Future.hpp>
|
||||
#include <ethread/PoolExecutor.hpp>
|
||||
#include <ethread/tools.hpp>
|
||||
#include "debug.hpp"
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::PoolExecutor);
|
||||
|
||||
ethread::PoolExecutor::PoolExecutor(ethread::Pool& _pool):
|
||||
m_needProcess(false),
|
||||
m_isWaiting(false),
|
||||
m_pool(_pool),
|
||||
m_running(false) {
|
||||
|
||||
}
|
||||
|
||||
void ethread::PoolExecutor::threadCallback() {
|
||||
ETHREAD_DEBUG("RUN: thread in Pool [START]");
|
||||
ethread::setName("pool " + etk::toString(ethread::getId()));
|
||||
// get datas:
|
||||
while (m_running == true) {
|
||||
// get an action:
|
||||
m_action = m_pool.getAction();
|
||||
if (m_action == null) {
|
||||
// If no action availlable and not requested to check, just sleep ...
|
||||
if (m_needProcess == false) {
|
||||
m_isWaiting = true;
|
||||
ETHREAD_VERBOSE("RUN: Jump in sleep");
|
||||
if (m_semaphore.wait(60000000) == false) {
|
||||
ETHREAD_VERBOSE("RUN: time-out");
|
||||
continue;
|
||||
}
|
||||
ETHREAD_VERBOSE("RUN: WakeUp");
|
||||
m_isWaiting = false;
|
||||
m_needProcess = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
m_action->call();
|
||||
m_pool.releaseId(m_action->getPoolId());
|
||||
m_action.reset();
|
||||
}
|
||||
m_running = false;
|
||||
ETHREAD_DEBUG("RUN: thread in Pool [STOP]");
|
||||
}
|
||||
|
||||
void ethread::PoolExecutor::start() {
|
||||
ETHREAD_DEBUG("START: thread in Pool [START]");
|
||||
m_running = true;
|
||||
m_semaphore.post();
|
||||
m_thread = ememory::makeShared<ethread::Thread>([&](){ threadCallback();});
|
||||
if (m_thread == null) {
|
||||
m_running = false;
|
||||
ETHREAD_ERROR("START: thread in Pool [STOP] can not intanciate THREAD!");
|
||||
return;
|
||||
}
|
||||
//ethread::setPriority(*m_receiveThread, -6);
|
||||
ETHREAD_DEBUG("START: thread in Pool [STOP]");
|
||||
}
|
||||
|
||||
void ethread::PoolExecutor::stop() {
|
||||
ETHREAD_DEBUG("STOP: thread in Pool [START]");
|
||||
m_semaphore.post();
|
||||
m_running = false;
|
||||
ETHREAD_DEBUG("STOP: thread in Pool [STOP]");
|
||||
}
|
||||
|
||||
void ethread::PoolExecutor::join() {
|
||||
ETHREAD_DEBUG("JOIN: thread in Pool [START]");
|
||||
m_semaphore.post();
|
||||
if (m_thread != null) {
|
||||
ETHREAD_DEBUG("JOIN: waiting ...");
|
||||
m_thread->join();
|
||||
m_thread.reset();
|
||||
}
|
||||
ETHREAD_DEBUG("JOIN: thread in Pool [STOP]");
|
||||
}
|
||||
|
||||
bool ethread::PoolExecutor::touch() {
|
||||
bool ret = false;
|
||||
if ( m_needProcess == false
|
||||
&& m_isWaiting == true) {
|
||||
ETHREAD_VERBOSE("Touch ...");
|
||||
m_needProcess = true;
|
||||
ret = true;
|
||||
m_semaphore.post();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
61
ethread/PoolExecutor.hpp
Normal file
61
ethread/PoolExecutor.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <etk/Vector.hpp>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <ethread/Future.hpp>
|
||||
#include <ethread/PoolAction.hpp>
|
||||
#include <ethread/Pool.hpp>
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <ethread/Semaphore.hpp>
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief A pool Executor is a class that execute some PoolAction. it is contituated wit a simple thread that execute some code).
|
||||
*/
|
||||
class PoolExecutor {
|
||||
private: //section to permit to optimize CPU:
|
||||
ethread::Semaphore m_semaphore; //!< protection of the internal data.
|
||||
bool m_needProcess; //!< Need to do action (no need to wait condition).
|
||||
bool m_isWaiting; //!< The executor is waiting to some action to do.
|
||||
private:
|
||||
ethread::Pool& m_pool; //!< Local reference on the Thread pool that store action to do.
|
||||
ememory::SharedPtr<ethread::Thread> m_thread; //!< Local thread to process action.
|
||||
bool m_running; //!< Thread is running (not stop).
|
||||
ememory::SharedPtr<ethread::PoolAction> m_action; //!< Curent action that is processing.
|
||||
public:
|
||||
/**
|
||||
* @brief Create a thread executor for the specific pool.
|
||||
* @param[in] _pool Reference on the thread pool.
|
||||
*/
|
||||
PoolExecutor(ethread::Pool& _pool);
|
||||
protected:
|
||||
/**
|
||||
* @brief Internal thread callback (for ethread::Thread).
|
||||
*/
|
||||
void threadCallback();
|
||||
public:
|
||||
/**
|
||||
* @brief Start the current thread.
|
||||
*/
|
||||
void start();
|
||||
/**
|
||||
* @brief Stop the current thread.
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Join the current thread.
|
||||
*/
|
||||
void join();
|
||||
/**
|
||||
* @brief Touche the execurot to process some other data.
|
||||
* @return true if the executor is waiting to process something. false otherwise.
|
||||
*/
|
||||
bool touch();
|
||||
};
|
||||
}
|
70
ethread/Promise.cpp
Normal file
70
ethread/Promise.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include "debug.hpp"
|
||||
#include <ethread/Future.hpp>
|
||||
#include <ethread/Promise.hpp>
|
||||
#include <ethread/tools.hpp>
|
||||
#include <echrono/Steady.hpp>
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Promise);
|
||||
|
||||
ethread::Promise::Promise():
|
||||
m_isFinished(false) {
|
||||
|
||||
}
|
||||
|
||||
bool ethread::Promise::isFinished() {
|
||||
return m_isFinished;
|
||||
}
|
||||
|
||||
void ethread::Promise::finish() {
|
||||
etk::Function<void()> callback;
|
||||
{
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
if (m_isFinished == true) {
|
||||
ETHREAD_ERROR("Request 2 time finishing a Promise ...");
|
||||
return;
|
||||
}
|
||||
m_isFinished = true;
|
||||
if (m_callback != null) {
|
||||
// call callbacks ...
|
||||
callback = etk::move(m_callback);
|
||||
}
|
||||
}
|
||||
if (callback != null) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
bool ethread::Promise::wait(echrono::Duration _delay) {
|
||||
echrono::Steady time = echrono::Steady::now();
|
||||
while (_delay >= 0) {
|
||||
{
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
if (m_isFinished == true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
echrono::Steady time2 = echrono::Steady::now();
|
||||
_delay -= (time2-time);
|
||||
time = time2;
|
||||
if (_delay >= 0) {
|
||||
// TODO : This is really bad ==> fast to code and debug but not optimum at all ... use condition instead ...
|
||||
ethread::sleepMilliSeconds((10));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ethread::Promise::andThen(etk::Function<void()> _action) {
|
||||
ethread::UniqueLock lock(m_mutex);
|
||||
m_callback = etk::move(_action);
|
||||
if (m_isFinished == true) {
|
||||
m_callback();
|
||||
}
|
||||
}
|
||||
|
48
ethread/Promise.hpp
Normal file
48
ethread/Promise.hpp
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <ethread/Mutex.hpp>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <etk/Function.hpp>
|
||||
#include <echrono/Duration.hpp>
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief A promise of the specific ation to do in the thread pool
|
||||
*/
|
||||
class Promise {
|
||||
private:
|
||||
ethread::Mutex m_mutex; //!< Simple lock of the interface
|
||||
etk::Function<void()> m_callback; //!< callback to call when processing is ended
|
||||
bool m_isFinished; //!< The process of the action has been done
|
||||
public:
|
||||
/**
|
||||
* @brief Simple empty contructor
|
||||
*/
|
||||
Promise();
|
||||
/**
|
||||
* @brief Call this when the action has been done
|
||||
*/
|
||||
void finish();
|
||||
/**
|
||||
* @brief Check if the action is finished
|
||||
* @return true, the action is done, false otherwise
|
||||
*/
|
||||
bool isFinished();
|
||||
/**
|
||||
* @brief Wait some time that the action finished
|
||||
* @param[in] _delay Delay to wait the action is finished
|
||||
* @return true, the action is finished, false, the time-out apear.
|
||||
*/
|
||||
bool wait(echrono::Duration _delay=echrono::seconds(2));
|
||||
/**
|
||||
* @brief Action to do when the action is finished
|
||||
* @param[in] _action New action to do.
|
||||
*/
|
||||
void andThen(etk::Function<void()> _action);
|
||||
};
|
||||
}
|
48
ethread/Semaphore.Windows.cpp
Normal file
48
ethread/Semaphore.Windows.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#include <etk/types.hpp>
|
||||
#include <etk/os/Semaphore.hpp>
|
||||
#include <etk/debug.hpp>
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Semaphore);
|
||||
|
||||
etk::Semaphore::Semaphore(uint32_t _nbBasicElement, uint32_t _nbMessageMax) {
|
||||
// create interface mutex :
|
||||
m_semaphore = CreateSemaphore(null, _nbBasicElement, _nbMessageMax, null);
|
||||
TK_ASSERT(m_semaphore != 0, "Error creating SEMAPHORE ...");
|
||||
}
|
||||
|
||||
|
||||
etk::Semaphore::~Semaphore() {
|
||||
CloseHandle(m_semaphore);
|
||||
}
|
||||
|
||||
uint32_t etk::Semaphore::getCount() {
|
||||
LONG tmpData = 0;
|
||||
ReleaseSemaphore(m_semaphore, 0, &tmpData);
|
||||
return tmpData;
|
||||
}
|
||||
|
||||
void etk::Semaphore::post() {
|
||||
ReleaseSemaphore(m_semaphore, 1, null);
|
||||
}
|
||||
|
||||
|
||||
void etk::Semaphore::wait() {
|
||||
WaitForSingleObject(m_semaphore, INFINITE);
|
||||
}
|
||||
|
||||
|
||||
bool etk::Semaphore::wait(uint64_t _timeOutInUs) {
|
||||
DWORD result = WaitForSingleObject(m_semaphore, _timeOutInUs);
|
||||
if (result == WAIT_FAILED) {
|
||||
TK_ERROR("Failed to wait for semaphore ");
|
||||
return false;
|
||||
} else {
|
||||
return result == WAIT_OBJECT_0;
|
||||
}
|
||||
}
|
||||
|
63
ethread/Semaphore.hpp
Normal file
63
ethread/Semaphore.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <etk/types.hpp>
|
||||
|
||||
#ifdef __TARGET_OS__Windows
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief Generic semaphore wrapper ( it is os independent)
|
||||
*/
|
||||
class Semaphore {
|
||||
private:
|
||||
#ifdef __TARGET_OS__Windows
|
||||
HANDLE m_semaphore;
|
||||
#else
|
||||
pthread_mutex_t m_mutex;
|
||||
pthread_cond_t m_condition;
|
||||
uint32_t m_data;
|
||||
uint32_t m_maximum;
|
||||
#endif
|
||||
public:
|
||||
/**
|
||||
* @brief Contruct the inithialized semaphore.
|
||||
* @param[in] _nbBasicElement Number of element basicly set in the semaphore list
|
||||
* @param[in] _nbMessageMax Nunber of maximun message that can be set.
|
||||
*/
|
||||
Semaphore(uint32_t _nbBasicElement=0, uint32_t _nbMessageMax=1);
|
||||
/**
|
||||
* @brief Generic destructor.
|
||||
*/
|
||||
~Semaphore();
|
||||
/**
|
||||
* @brief Get the number of element in the semaphore.
|
||||
* @return Number of stored elements.
|
||||
*/
|
||||
uint32_t getCount();
|
||||
/**
|
||||
* @brief Post a new semaphore
|
||||
*/
|
||||
void post();
|
||||
/**
|
||||
* @brief Wait for a new semaphore post by an other thread.
|
||||
*/
|
||||
void wait();
|
||||
/**
|
||||
* @brief Wait for a new semaphore post by an other thread,
|
||||
* with a timeout in micro-second.
|
||||
* @param[in] _timeOutInUs Number of micro-second to wait a semaphore.
|
||||
* @return true The function get a semaphore.
|
||||
* @return false The time-out appear or an error occured.
|
||||
*/
|
||||
bool wait(uint64_t _timeOutInUs);
|
||||
};
|
||||
}
|
89
ethread/Semaphore.pthread.cpp
Normal file
89
ethread/Semaphore.pthread.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#include <etk/types.hpp>
|
||||
#include <ethread/Semaphore.hpp>
|
||||
//#include <ethread/debug.hpp>
|
||||
#include <sys/time.h>
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Semaphore);
|
||||
|
||||
ethread::Semaphore::Semaphore(uint32_t _nbBasicElement, uint32_t _nbMessageMax) {
|
||||
// create interface mutex :
|
||||
int ret = pthread_mutex_init(&m_mutex, null);
|
||||
//TK_ASSERT(ret == 0, "Error creating Mutex ...");
|
||||
// create contition :
|
||||
ret = pthread_cond_init(&m_condition, null);
|
||||
//TK_ASSERT(ret == 0, "Error creating Condition ...");
|
||||
if (ret != 0) {
|
||||
ret = pthread_mutex_destroy(&m_mutex);
|
||||
//TK_ASSERT(ret == 0, "Error destroying Mutex ...");
|
||||
}
|
||||
m_maximum = _nbMessageMax;
|
||||
m_data = _nbBasicElement;
|
||||
}
|
||||
|
||||
|
||||
ethread::Semaphore::~Semaphore() {
|
||||
// Remove condition
|
||||
int ret = pthread_cond_destroy(&m_condition);
|
||||
//TK_ASSERT(ret == 0, "Error destroying Condition ...");
|
||||
// Remove Mutex
|
||||
ret = pthread_mutex_destroy(&m_mutex);
|
||||
//TK_ASSERT(ret == 0, "Error destroying Mutex ...");
|
||||
}
|
||||
|
||||
uint32_t ethread::Semaphore::getCount() {
|
||||
int32_t tmpData = 0;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
tmpData = m_data;
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return tmpData;
|
||||
}
|
||||
|
||||
void ethread::Semaphore::post() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_data>=m_maximum) {
|
||||
m_data = m_maximum;
|
||||
} else {
|
||||
m_data++;
|
||||
}
|
||||
// send message
|
||||
pthread_cond_broadcast(&m_condition);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
|
||||
void ethread::Semaphore::wait() {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
while(m_data == 0) {
|
||||
pthread_cond_wait(&m_condition, &m_mutex);
|
||||
}
|
||||
m_data--;
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
|
||||
bool ethread::Semaphore::wait(uint64_t _timeOutInUs) {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if(m_data == 0) {
|
||||
struct timeval tp;
|
||||
struct timespec ts;
|
||||
gettimeofday(&tp, null);
|
||||
uint64_t totalTimeUS = tp.tv_sec * 1000000 + tp.tv_usec;
|
||||
totalTimeUS += _timeOutInUs;
|
||||
ts.tv_sec = totalTimeUS / 1000000;
|
||||
ts.tv_nsec = (totalTimeUS%1000000) * 1000;
|
||||
int ret = pthread_cond_timedwait(&m_condition, &m_mutex, &ts);
|
||||
if (ret !=0) { //== ETIMEOUT) {
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_data--;
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return true;
|
||||
}
|
||||
|
52
ethread/Thread.hpp
Normal file
52
ethread/Thread.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
#include <etk/types.hpp>
|
||||
#include <etk/Function.hpp>
|
||||
#include <etk/String.hpp>
|
||||
|
||||
#ifdef __TARGET_OS__Windows
|
||||
#error TODO ...
|
||||
#else
|
||||
extern "C" {
|
||||
#include <pthread.h>
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief Generic Thread interface (OS independent)
|
||||
*/
|
||||
class Thread {
|
||||
private:
|
||||
#ifdef __TARGET_OS__Windows
|
||||
|
||||
#else
|
||||
pthread_t m_thread;
|
||||
static void* threadCallback(void* _userData);
|
||||
#endif
|
||||
uint64_t m_uid; //!< unique id of the thread
|
||||
etk::String m_name; //!< Name of the thread (do not get it on the system ==> more portable)
|
||||
etk::Function<void()> m_function; //!< Function to call every cycle of the thead running
|
||||
public:
|
||||
Thread(etk::Function<void()>&& _call, const etk::String& _name = "");
|
||||
~Thread();
|
||||
void join();
|
||||
bool detach();
|
||||
void threadCall();
|
||||
void setName(const etk::String& _name);
|
||||
const etk::String& getName() const;
|
||||
uint64_t getId() const;
|
||||
#ifdef __TARGET_OS__Windows
|
||||
|
||||
#else
|
||||
pthread_t getNativeHandle() {
|
||||
return m_thread;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
71
ethread/Thread.pthread.cpp
Normal file
71
ethread/Thread.pthread.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <ethread/tools.hpp>
|
||||
|
||||
#include <etk/typeInfo.hpp>
|
||||
ETK_DECLARE_TYPE(ethread::Thread);
|
||||
|
||||
namespace ethread {
|
||||
uint32_t getThreadHumanId(uint64_t _id);
|
||||
etk::String getThreadName(uint64_t _id);
|
||||
void setThreadName(ethread::Thread* _thread, const etk::String& _name);
|
||||
}
|
||||
|
||||
void* ethread::Thread::threadCallback(void* _userData) {
|
||||
ethread::Thread* threadHandle = static_cast<ethread::Thread*>(_userData);
|
||||
if (threadHandle != null) {
|
||||
threadHandle->threadCall();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
ethread::Thread::Thread(etk::Function<void()>&& _call, const etk::String& _name):
|
||||
m_thread(0),
|
||||
m_uid(-1),
|
||||
m_name(_name),
|
||||
m_function(etk::move(_call)) {
|
||||
uint32_t iii = ethread::getId();
|
||||
pthread_create(&m_thread, null, ðread::Thread::threadCallback, this);
|
||||
m_uid = ethread::getThreadHumanId(uint64_t(m_thread));
|
||||
printf("New thread: %ld from %d\n", m_uid, iii);
|
||||
}
|
||||
|
||||
ethread::Thread::~Thread() {
|
||||
|
||||
}
|
||||
|
||||
void ethread::Thread::join() {
|
||||
void* ret = null;
|
||||
int val = pthread_join(m_thread, &ret);
|
||||
}
|
||||
|
||||
bool ethread::Thread::detach() {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ethread::Thread::setName(const etk::String& _name) {
|
||||
m_name = _name;
|
||||
ethread::setThreadName(this, m_name);
|
||||
}
|
||||
|
||||
const etk::String& ethread::Thread::getName() const {
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void ethread::Thread::threadCall() {
|
||||
if (m_function != null) {
|
||||
m_function();
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t ethread::Thread::getId() const {
|
||||
return m_uid;
|
||||
}
|
||||
|
||||
|
12
ethread/debug.cpp
Normal file
12
ethread/debug.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
int32_t ethread::getLogId() {
|
||||
static int32_t g_val = elog::registerInstance("ethread");
|
||||
return g_val;
|
||||
}
|
40
ethread/debug.hpp
Normal file
40
ethread/debug.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <elog/log.hpp>
|
||||
|
||||
namespace ethread {
|
||||
int32_t getLogId();
|
||||
};
|
||||
|
||||
#define ETHREAD_BASE(info,data) ELOG_BASE(ethread::getLogId(),info,data)
|
||||
|
||||
#define ETHREAD_PRINT(data) ETHREAD_BASE(-1, data)
|
||||
#define ETHREAD_CRITICAL(data) ETHREAD_BASE(1, data)
|
||||
#define ETHREAD_ERROR(data) ETHREAD_BASE(2, data)
|
||||
#define ETHREAD_WARNING(data) ETHREAD_BASE(3, data)
|
||||
#ifdef DEBUG
|
||||
#define ETHREAD_INFO(data) ETHREAD_BASE(4, data)
|
||||
#define ETHREAD_DEBUG(data) ETHREAD_BASE(5, data)
|
||||
#define ETHREAD_VERBOSE(data) ETHREAD_BASE(6, data)
|
||||
#define ETHREAD_TODO(data) ETHREAD_BASE(4, "TODO : " << data)
|
||||
#else
|
||||
#define ETHREAD_INFO(data) do { } while(false)
|
||||
#define ETHREAD_DEBUG(data) do { } while(false)
|
||||
#define ETHREAD_VERBOSE(data) do { } while(false)
|
||||
#define ETHREAD_TODO(data) do { } while(false)
|
||||
#endif
|
||||
|
||||
#define ETHREAD_ASSERT(cond,data) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
ETHREAD_CRITICAL(data); \
|
||||
assert(!#cond); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
@ -1,74 +1,80 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
*
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
*
|
||||
* @license APACHE v2.0 (see license file)
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <ethread/tools.h>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
|
||||
static std::mutex g_lock;
|
||||
static std::map<uint32_t, std::string>& getThreadList() {
|
||||
static std::map<uint32_t, std::string> g_val;
|
||||
#include <ethread/tools.hpp>
|
||||
#include <etk/Pair.hpp>
|
||||
#include <ethread/Mutex.hpp>
|
||||
// TODO: set mutex back ...
|
||||
#include <etk/Map.hpp>
|
||||
extern "C" {
|
||||
#include <unistd.h>
|
||||
}
|
||||
#ifdef __TARGET_OS__Windows
|
||||
#error TODO ...
|
||||
#else
|
||||
extern "C" {
|
||||
#include <pthread.h>
|
||||
}
|
||||
#endif
|
||||
static ethread::Mutex g_lock;
|
||||
static etk::Map<uint64_t, etk::String>& getThreadList() {
|
||||
static etk::Map<uint64_t, etk::String> g_val;
|
||||
return g_val;
|
||||
}
|
||||
|
||||
static uint32_t getThreadHumanId(std::thread::id _id) {
|
||||
namespace ethread {
|
||||
// Note: Declared in Thread.cpp
|
||||
uint32_t getThreadHumanId(uint64_t _id) {
|
||||
uint32_t out = 0;
|
||||
uint64_t iddd = std::hash<std::thread::id>()(_id);
|
||||
g_lock.lock();
|
||||
static std::map<uint64_t, uint32_t> g_list;
|
||||
std::map<uint64_t, uint32_t>::iterator it = g_list.find(iddd);
|
||||
static etk::Map<uint64_t, uint32_t> g_list;
|
||||
auto it = g_list.find(_id);
|
||||
if (it == g_list.end()) {
|
||||
// attribute new ID :
|
||||
static uint32_t tmpId = 0;
|
||||
g_list.insert(std::pair<uint64_t, uint32_t>(iddd,tmpId));
|
||||
g_list.set(_id, tmpId);
|
||||
out = tmpId;
|
||||
tmpId++;
|
||||
} else {
|
||||
out = it->second;
|
||||
out = it.getValue();
|
||||
}
|
||||
g_lock.unlock();
|
||||
g_lock.unLock();
|
||||
return out;
|
||||
|
||||
}
|
||||
|
||||
static std::string getThreadName(std::thread::id _id) {
|
||||
std::map<uint32_t,std::string>& list = getThreadList();
|
||||
etk::String getThreadName(uint64_t _id) {
|
||||
etk::Map<uint64_t,etk::String>& list = getThreadList();
|
||||
uint32_t threadID = getThreadHumanId(_id);
|
||||
std::string out;
|
||||
etk::String out;
|
||||
g_lock.lock();
|
||||
std::map<uint32_t,std::string>::iterator it = list.find(threadID);
|
||||
auto it = list.find(threadID);
|
||||
if (it != list.end()) {
|
||||
out = it->second;
|
||||
out = it.getValue();
|
||||
}
|
||||
g_lock.unlock();
|
||||
g_lock.unLock();
|
||||
return out;
|
||||
}
|
||||
|
||||
static void setThreadName(std::thread* _thread, const std::string& _name) {
|
||||
std::map<uint32_t,std::string>& list = getThreadList();
|
||||
void setThreadName(ethread::Thread* _thread, const etk::String& _name) {
|
||||
etk::Map<uint64_t,etk::String>& list = getThreadList();
|
||||
uint32_t threadID = ethread::getId();
|
||||
g_lock.lock();
|
||||
std::map<uint32_t,std::string>::iterator it = list.find(threadID);
|
||||
if (it == list.end()) {
|
||||
list.insert(std::pair<uint32_t, std::string>(threadID, _name));
|
||||
} else {
|
||||
it->second = _name;
|
||||
}
|
||||
g_lock.unlock();
|
||||
list.set(threadID, _name);
|
||||
g_lock.unLock();
|
||||
// try now to set the thread name with Pthread
|
||||
#if defined(__TARGET_OS__Linux)
|
||||
#if ( defined(__TARGET_OS__Linux) \
|
||||
|| defined(__TARGET_OS__Android) \
|
||||
) \
|
||||
&& !defined(__TARGET_OS__Web)
|
||||
pthread_t pthreadID;
|
||||
if (_thread == nullptr) {
|
||||
if (_thread == null) {
|
||||
pthreadID = pthread_self();
|
||||
} else {
|
||||
pthreadID = (pthread_t) _thread->native_handle();
|
||||
pthreadID = _thread->getNativeHandle();
|
||||
}
|
||||
std::string name = _name;
|
||||
etk::String name = _name;
|
||||
if (name.size() > 15) {
|
||||
name.resize(15);
|
||||
}
|
||||
@ -79,32 +85,39 @@ static void setThreadName(std::thread* _thread, const std::string& _name) {
|
||||
//TODO: TK_INFO("Can not set the thread name in this OS (local set)");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t ethread::getId() {
|
||||
return getThreadHumanId(std::this_thread::get_id());
|
||||
pthread_t self;
|
||||
self = pthread_self();
|
||||
return ethread::getThreadHumanId(uint64_t(self));
|
||||
}
|
||||
|
||||
uint32_t ethread::getId(std::thread& _thread) {
|
||||
return getThreadHumanId(_thread.get_id());
|
||||
uint32_t ethread::getId(ethread::Thread& _thread) {
|
||||
return ethread::getThreadHumanId(_thread.getId());
|
||||
}
|
||||
|
||||
void ethread::setName(const std::string& _name) {
|
||||
setThreadName(nullptr, _name);
|
||||
void ethread::setName(const etk::String& _name) {
|
||||
setThreadName(null, _name);
|
||||
}
|
||||
|
||||
void ethread::setName(std::thread& _thread, const std::string& _name) {
|
||||
setThreadName(&_thread, _name);
|
||||
void ethread::setName(ethread::Thread& _thread, const etk::String& _name) {
|
||||
_thread.setName(_name);
|
||||
}
|
||||
|
||||
std::string ethread::getName() {
|
||||
return getThreadName(std::this_thread::get_id());
|
||||
etk::String ethread::getName() {
|
||||
pthread_t self;
|
||||
self = pthread_self();
|
||||
return getThreadName(uint64_t(self));
|
||||
}
|
||||
|
||||
std::string ethread::getName(std::thread& _thread) {
|
||||
return getThreadName(_thread.get_id());
|
||||
etk::String ethread::getName(ethread::Thread& _thread) {
|
||||
return _thread.getName();
|
||||
}
|
||||
|
||||
#if defined(__TARGET_OS__Linux)
|
||||
#if defined(__TARGET_OS__Linux) \
|
||||
&& !defined(__TARGET_OS__Web)
|
||||
static void setThreadPriority(pthread_t _threadID, int32_t _priority) {
|
||||
int retcode;
|
||||
int policy;
|
||||
@ -143,26 +156,30 @@ std::string ethread::getName(std::thread& _thread) {
|
||||
return -param.sched_priority;
|
||||
}
|
||||
return param.sched_priority;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void ethread::setPriority(int32_t _priority) {
|
||||
#if defined(__TARGET_OS__Linux)
|
||||
#if defined(__TARGET_OS__Linux) \
|
||||
&& !defined(__TARGET_OS__Web)
|
||||
pthread_t threadID = pthread_self();
|
||||
setThreadPriority(threadID, _priority);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ethread::setPriority(std::thread& _thread, int32_t _priority) {
|
||||
#if defined(__TARGET_OS__Linux)
|
||||
pthread_t threadID = (pthread_t) _thread.native_handle();
|
||||
void ethread::setPriority(ethread::Thread& _thread, int32_t _priority) {
|
||||
#if defined(__TARGET_OS__Linux) \
|
||||
&& !defined(__TARGET_OS__Web)
|
||||
pthread_t threadID = _thread.getNativeHandle();
|
||||
setThreadPriority(threadID, _priority);
|
||||
#endif
|
||||
}
|
||||
|
||||
int32_t ethread::getPriority() {
|
||||
#if defined(__TARGET_OS__Linux)
|
||||
#if defined(__TARGET_OS__Linux) \
|
||||
&& !defined(__TARGET_OS__Web)
|
||||
pthread_t threadID = pthread_self();
|
||||
return getThreadPriority(threadID);
|
||||
#else
|
||||
@ -170,11 +187,66 @@ int32_t ethread::getPriority() {
|
||||
#endif
|
||||
}
|
||||
|
||||
int32_t ethread::getPriority(std::thread& _thread) {
|
||||
#if defined(__TARGET_OS__Linux)
|
||||
pthread_t threadID = static_cast<pthread_t>(_thread.native_handle());
|
||||
int32_t ethread::getPriority(ethread::Thread& _thread) {
|
||||
#if defined(__TARGET_OS__Linux) \
|
||||
&& !defined(__TARGET_OS__Web)
|
||||
pthread_t threadID = _thread.getNativeHandle();
|
||||
return getThreadPriority(threadID);
|
||||
#else
|
||||
return 20;
|
||||
#endif
|
||||
}
|
||||
|
||||
//static ethread::Mutex g_localMutex;
|
||||
static etk::Map<uint32_t, etk::Map<etk::String, uint64_t>> g_listMetaData;
|
||||
|
||||
void ethread::metadataSet(const etk::String& _key, uint64_t _value) {
|
||||
/*
|
||||
uint32_t currentThreadId = ethread::getId();
|
||||
// TODO: ethread::UniqueLock lock(g_localMutex);
|
||||
auto it = g_listMetaData.find(currentThreadId);
|
||||
if (it != g_listMetaData.end()) {
|
||||
it.getValue().set(_key, _value);
|
||||
} else {
|
||||
etk::Map<etk::String, uint64_t> tmp;
|
||||
tmp.set(_key, _value);
|
||||
g_listMetaData.set(currentThreadId, tmp);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void ethread::metadataRemove(const etk::String& _key) {
|
||||
/*
|
||||
uint32_t currentThreadId = ethread::getId();
|
||||
// TODO: ethread::UniqueLock lock(g_localMutex);
|
||||
etk::Map<uint32_t, etk::Map<etk::String, uint64_t>>::Iterator it = g_listMetaData.find(currentThreadId);
|
||||
if (it != g_listMetaData.end()) {
|
||||
auto it2 = it.getValue().find(_key);
|
||||
if (it2 != it.getValue().end()) {
|
||||
it.getValue().erase(it2);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
uint64_t ethread::metadataGetU64(const etk::String& _key) {
|
||||
/*
|
||||
uint32_t currentThreadId = ethread::getId();
|
||||
// TODO: ethread::UniqueLock lock(g_localMutex);
|
||||
auto it = g_listMetaData.find(currentThreadId);
|
||||
if (it != g_listMetaData.end()) {
|
||||
auto it2 = it.getValue().find(_key);
|
||||
if (it2 != it.getValue().end()) {
|
||||
return it2.getValue();
|
||||
}
|
||||
}
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void ethread::sleepMilliSeconds(uint32_t _timeInMilliSeconds) {
|
||||
usleep(_timeInMilliSeconds*1000);
|
||||
}
|
||||
|
||||
|
@ -1,48 +1,50 @@
|
||||
/**
|
||||
* @author Edouard DUPIN
|
||||
*
|
||||
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||
*
|
||||
* @license APACHE v2.0 (see license file)
|
||||
* @license MPL v2.0 (see license file)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <thread>
|
||||
#include <ethread/Thread.hpp>
|
||||
#include <etk/String.hpp>
|
||||
|
||||
/**
|
||||
* @brief ethread main namespace
|
||||
*/
|
||||
namespace ethread {
|
||||
/**
|
||||
* @brief get human readable thread ID. (not the std::thread::get_id())
|
||||
* @brief get human readable thread ID. (not the ethread::Thread::getId())
|
||||
* @return the ID of the thread.
|
||||
*/
|
||||
uint32_t getId();
|
||||
/**
|
||||
* @brief get human readable thread ID. (not the std::thread::get_id())
|
||||
* @brief get human readable thread ID. (not the ethread::Thread::getId())
|
||||
* @param[in] _thread Thread handle
|
||||
* @return the ID of the thread.
|
||||
*/
|
||||
uint32_t getId(std::thread& _thread);
|
||||
uint32_t getId(ethread::Thread& _thread);
|
||||
/**
|
||||
* @brief Set the Current thread name
|
||||
* @param[in] _name New name of the thread
|
||||
*/
|
||||
void setName(const std::string& _name);
|
||||
void setName(const etk::String& _name);
|
||||
/**
|
||||
* @brief Set an other thread name
|
||||
* @param[in] _thread Thread handle
|
||||
* @param[in] _name New name of the thread
|
||||
*/
|
||||
void setName(std::thread& _thread, const std::string& _name);
|
||||
void setName(ethread::Thread& _thread, const etk::String& _name);
|
||||
/**
|
||||
* @brief Set the Current thread name
|
||||
* @return The current name of the thread
|
||||
*/
|
||||
std::string getName();
|
||||
etk::String getName();
|
||||
/**
|
||||
* @brief Get an other thread name
|
||||
* @praram[in] _thread Thread handle
|
||||
* @param[in] _thread Thread handle
|
||||
* @return The external thread name of the thread
|
||||
*/
|
||||
std::string getName(std::thread& _thread);
|
||||
etk::String getName(ethread::Thread& _thread);
|
||||
/**
|
||||
* @brief Set the Current thread priority [-20..0] for RT and ]0..50] for normal priority
|
||||
* @param[in] _priority New priority of the thread
|
||||
@ -55,7 +57,7 @@ namespace ethread {
|
||||
* @param[in] _priority New priority of the thread
|
||||
* @note If your process have not the right to change thread name, it does not work
|
||||
*/
|
||||
void setPriority(std::thread& _thread, int32_t _priority);
|
||||
void setPriority(ethread::Thread& _thread, int32_t _priority);
|
||||
/**
|
||||
* @brief get the Current thread priority [-20..0] for RT and ]0..50] for normal priority
|
||||
* @return current priority of the thread
|
||||
@ -66,5 +68,24 @@ namespace ethread {
|
||||
* @param[in] _thread Thread handle
|
||||
* @return current priority of the thread
|
||||
*/
|
||||
int32_t getPriority(std::thread& _thread);
|
||||
int32_t getPriority(ethread::Thread& _thread);
|
||||
/**
|
||||
* @brief Set an information with a key on the current thread
|
||||
* @param[in] _key key to store the value
|
||||
* @param[in] _value Value to store
|
||||
*/
|
||||
void metadataSet(const etk::String& _key, uint64_t _value);
|
||||
/**
|
||||
* @brief Remove the information with a key on the current thread
|
||||
* @param[in] _key key to remove
|
||||
*/
|
||||
void metadataRemove(const etk::String& _key);
|
||||
/**
|
||||
* @brief get the information with a key on the current thread
|
||||
* @param[in] _key key to store the value
|
||||
* @return the uint 64 value to stored
|
||||
*/
|
||||
uint64_t metadataGetU64(const etk::String& _key);
|
||||
|
||||
void sleepMilliSeconds(uint32_t _timeInMilliSeconds);
|
||||
}
|
74
lutin_ethread-tools.py
Normal file
74
lutin_ethread-tools.py
Normal file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/python
|
||||
import realog.debug as debug
|
||||
import lutin.tools as tools
|
||||
|
||||
|
||||
def get_type():
|
||||
return "LIBRARY"
|
||||
|
||||
def get_desc():
|
||||
return "Ewol thread tools"
|
||||
|
||||
def get_licence():
|
||||
return "MPL-2"
|
||||
|
||||
def get_compagny_type():
|
||||
return "com"
|
||||
|
||||
def get_compagny_name():
|
||||
return "atria-soft"
|
||||
|
||||
def get_maintainer():
|
||||
return "authors.txt"
|
||||
|
||||
def get_version():
|
||||
return "version.txt"
|
||||
|
||||
def configure(target, my_module):
|
||||
my_module.add_extra_flags()
|
||||
# add the file to compile:
|
||||
my_module.add_src_file([
|
||||
'ethread/tools.cpp',
|
||||
])
|
||||
|
||||
my_module.add_header_file([
|
||||
'ethread/tools.hpp',
|
||||
'ethread/Thread.hpp',
|
||||
'ethread/Mutex.hpp',
|
||||
'ethread/MutexRecursive.hpp',
|
||||
'ethread/Semaphore.hpp',
|
||||
])
|
||||
|
||||
if "Windows" in target.get_type():
|
||||
my_module.add_src_file([
|
||||
'ethread/Mutex.Windows.cpp',
|
||||
'ethread/MutexRecursive.Windows.cpp',
|
||||
'ethread/Thread.Windows.cpp',
|
||||
'ethread/Semaphore.Windows.cpp',
|
||||
])
|
||||
else:
|
||||
my_module.add_src_file([
|
||||
'ethread/Mutex.pthread.cpp',
|
||||
'ethread/MutexRecursive.pthread.cpp',
|
||||
'ethread/Thread.pthread.cpp',
|
||||
'ethread/Semaphore.pthread.cpp',
|
||||
])
|
||||
my_module.add_depend([
|
||||
'pthread',
|
||||
])
|
||||
# build in C++ mode
|
||||
my_module.compile_version("c++", 2011)
|
||||
# add dependency of the generic C++ library:
|
||||
my_module.add_depend([
|
||||
'etk-core',
|
||||
])
|
||||
#pthread is not availlable on Windows
|
||||
if "Linux" in target.get_type() \
|
||||
or "Android" in target.get_type():
|
||||
my_module.add_depend([
|
||||
'pthread'
|
||||
])
|
||||
|
||||
return True
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
#!/usr/bin/python
|
||||
import lutin.module as module
|
||||
import realog.debug as debug
|
||||
import lutin.tools as tools
|
||||
|
||||
|
||||
@ -7,10 +7,10 @@ def get_type():
|
||||
return "LIBRARY"
|
||||
|
||||
def get_desc():
|
||||
return "Ewol thread tools"
|
||||
return "Ewol thread"
|
||||
|
||||
def get_licence():
|
||||
return "APACHE-2"
|
||||
return "MPL-2"
|
||||
|
||||
def get_compagny_type():
|
||||
return "com"
|
||||
@ -19,29 +19,41 @@ def get_compagny_name():
|
||||
return "atria-soft"
|
||||
|
||||
def get_maintainer():
|
||||
return ["Mr DUPIN Edouard <yui.heero@gmail.com>"]
|
||||
return "authors.txt"
|
||||
|
||||
def get_version():
|
||||
return [0,1,"dev"]
|
||||
return "version.txt"
|
||||
|
||||
def create(target, module_name):
|
||||
my_module = module.Module(__file__, module_name, get_type())
|
||||
my_module.add_extra_compile_flags()
|
||||
def configure(target, my_module):
|
||||
my_module.add_extra_flags()
|
||||
# add the file to compile:
|
||||
my_module.add_src_file([
|
||||
'ethread/tools.cpp',
|
||||
'ethread/debug.cpp',
|
||||
'ethread/Future.cpp',
|
||||
'ethread/Promise.cpp',
|
||||
'ethread/Pool.cpp',
|
||||
'ethread/PoolAction.cpp',
|
||||
'ethread/PoolExecutor.cpp',
|
||||
])
|
||||
|
||||
my_module.add_header_file([
|
||||
'ethread/tools.h',
|
||||
'ethread/Future.hpp',
|
||||
'ethread/Promise.hpp',
|
||||
'ethread/Pool.hpp',
|
||||
'ethread/PoolAction.hpp',
|
||||
'ethread/PoolExecutor.hpp',
|
||||
])
|
||||
|
||||
# build in C++ mode
|
||||
my_module.compile_version("c++", 2011)
|
||||
# add dependency of the generic C++ library:
|
||||
my_module.add_module_depend('cxx')
|
||||
|
||||
my_module.add_path(tools.get_current_path(__file__))
|
||||
return my_module
|
||||
my_module.add_depend([
|
||||
'elog',
|
||||
'etk',
|
||||
'ethread-tools',
|
||||
'echrono',
|
||||
'ememory'
|
||||
])
|
||||
return True
|
||||
|
||||
|
||||
|
1
version.txt
Normal file
1
version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.0.0
|
Loading…
x
Reference in New Issue
Block a user