Multi-threaded testing
author | Gareth Rees |
copyright | See section Copyright and License. |
date | 2014-10-21 |
index terms | pair: threads; testing |
revision | //info.ravenbrook.com/project/mps/master/design/testthr.txt#8 |
status | complete design |
tag | design.mps.testthr |
Introduction
.intro: This is the design of the multi-threaded testing module in the Memory Pool System.
.readership: Any MPS developer.
.overview: The MPS is designed to work in a multi-threaded environment (see design.mps.thread-safety) and this needs to be tested on all supported platforms. The multi-threaded testing module provides an interface for creating and joining threads, so that multi-threaded test cases are portable to all platforms on which the MPS runs.
Requirements
.req.create: The module must provide an interface for creating threads and running code in them. (Because there is no such interface in the Standard C Library.)
.req.join: The module must provide an interface for joining a running thread: that is, waiting for the thread to finish and collecting a result. (Because we want to be able to test that the MPS behaves correctly when interacting with a finished thread.)
.req.portable: The module must be easily portable to all the platforms on which the MPS runs.
.req.usable: The module must be simple to use, not requiring elaborate setup or tear-down or error handling. (Because we want test cases to be easy to write.)
Implementation
.impl.posix: To meet .req.portable and .req.usable, the module presents an interface that is essentially identical to the POSIX Threads interface [pthreads], except for the names. On POSIX platforms the implementation is trivial; on Windows it is necessary to translate the concepts back and forth.
.impl.storage: To meet .req.usable, the module defines the testthr_t type in the header testthr.h (even though this requires an #if), so that test cases can easily declare variables and allocate storage for thread identifiers.
.impl.error: To meet .req.usable, the module does not propagate error codes, but calls error() from the test library if anything goes wrong. There is thus no need for the test cases to check result codes.
Interface
typedef testthr_t
The type of thread identifiers.
typedef void *(*testthr_routine_t)(void *)
The type of a function that can be called when a thread is created.
void testthr_create(testthr_t *thread_o, testthr_routine_t start, void *arg)
Create a thread. Store the identifier of the newly created thread in *thread_o, and call start(), passing arg as the single parameter.
void testthr_join(testthr_t *thread, void **result_o)
Wait for a thread to complete. Suspend execution of the calling thread until the target thread terminates (if necessary), and if result_o is non-NULL, update *result_o with the return value of the thread's start() function.
References
[pthreads] | The Open Group; "The Single UNIX Specification, Version 2---Threads"; <https://pubs.opengroup.org/onlinepubs/7990989775/xsh/threads.html> |
Document History
- 2014-10-21 GDR Initial draft.
Copyright and License
Copyright © 2014–2020 Ravenbrook Limited.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.