mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-03 19:22:27 +00:00
Eliminate cyclic locks in runtime
This change introduces a new deadlock detector for Cosmo's POSIX threads implementation. Error check mutexes will now track a DAG of nested locks and report EDEADLK when a deadlock is theoretically possible. These will occur rarely, but it's important for production hardening your code. You don't even need to change your mutexes to use the POSIX error check mode because `cosmocc -mdbg` will enable error checking on mutexes by default globally. When cycles are found, an error message showing your demangled symbols describing the strongly connected component are printed and then the SIGTRAP is raised, which means you'll also get a backtrace if you're using ShowCrashReports() too. This new error checker is so low-level and so pure that it's able to verify the relationships of every libc runtime lock, including those locks upon which the mutex implementation depends.
This commit is contained in:
parent
26c051c297
commit
af7bd80430
141 changed files with 2094 additions and 1601 deletions
|
@ -16,20 +16,38 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/cxxabi.h"
|
||||
#include "libc/stdio/internal.h"
|
||||
|
||||
/**
|
||||
* Blocks until data from stream buffer is written out.
|
||||
*
|
||||
* @param f is the stream handle, or 0 for all streams
|
||||
* @return is 0 on success or -1 on error
|
||||
* @return is 0 on success or EOF on error
|
||||
*/
|
||||
int fflush(FILE *f) {
|
||||
int rc;
|
||||
if (f)
|
||||
if (f) {
|
||||
flockfile(f);
|
||||
rc = fflush_unlocked(f);
|
||||
if (f)
|
||||
rc = fflush_unlocked(f);
|
||||
funlockfile(f);
|
||||
} else {
|
||||
__stdio_lock();
|
||||
struct Dll *e, *e2;
|
||||
for (rc = 0, e = dll_last(__stdio.files); e; e = e2) {
|
||||
f = FILE_CONTAINER(e);
|
||||
__stdio_ref(f);
|
||||
__stdio_unlock();
|
||||
rc |= fflush(FILE_CONTAINER(e));
|
||||
__stdio_lock();
|
||||
e2 = dll_prev(__stdio.files, e);
|
||||
__stdio_unref_unlocked(f);
|
||||
}
|
||||
__stdio_unlock();
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
__attribute__((__constructor__(60))) static textstartup void fflush_init(void) {
|
||||
__cxa_atexit((void *)fflush, 0, 0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue