Bug 201664

Summary: [build] cannot build 'sort' using WITH_SORT_THREADS=yes
Product: Base System Reporter: Anthony Cornehl <accornehl>
Component: binAssignee: Mark Johnston <markj>
Status: Closed FIXED    
Severity: Affects Only Me CC: driesm, gcooper, markj, zakharov.vv
Priority: --- Keywords: patch
Version: CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
Fix building 'sort' with threads support none

Description Anthony Cornehl 2015-07-17 22:33:56 UTC
I hit this error using a -CURRENT checkout from today. All that's needed to reproduce this is building usr.bin/sort with "WITH_SORT_THREADS=yes" in the environment or /etc/src.conf

# clang -v
FreeBSD clang version 3.6.0 (tags/RELEASE_360/final 230434) 20150225
Target: x86_64-unknown-freebsd11.0
Thread model: posix

--- radixsort.o ---
cc  -O2 -pipe   -DSORT_THREADS -g -std=gnu99 -fstack-protector -Wsystem-headers -Werror -Wall -Wno-format-y2k -W -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls -Wold-style-definition -Wno-pointer-sign -Wmissing-variable-declarations -Wthread-safety -Wno-empty-body -Wno-string-plus-int -Wno-unused-const-variable -Qunused-arguments -c /usr/home/acornehl/git/freebsd/usr.bin/sort/radixsort.c -o radixsort.o
/usr/home/acornehl/git/freebsd/usr.bin/sort/radixsort.c:144:17: error: mutex 'g_ls_mutex' is not held on every path through here [-Werror,-Wthread-safety-analysis]
        new_ls->next = g_ls;
                       ^
/usr/home/acornehl/git/freebsd/usr.bin/sort/radixsort.c:141:3: note: mutex acquired here
                pthread_mutex_lock(&g_ls_mutex);
                ^
/usr/home/acornehl/git/freebsd/usr.bin/sort/radixsort.c:149:3: error: releasing mutex 'g_ls_mutex' that was not held [-Werror,-Wthread-safety-analysis]
                pthread_mutex_unlock(&g_ls_mutex);
                ^
2 errors generated.
*** [radixsort.o] Error code 1

make[4]: stopped in /usr/home/acornehl/git/freebsd/usr.bin/sort
1 error
Comment 1 Vladimir Zakharov 2016-03-01 16:38:14 UTC
Created attachment 167612 [details]
Fix building 'sort' with threads support
Comment 2 Vladimir Zakharov 2016-03-01 16:38:59 UTC
It seems to be ThreadSafetyAnasysis false positive [1]. Attached patch removes "-Werror" when sort is being built with threading support.

Another way around is to lock/unlock mutex unconditionally.

1: http://clang.llvm.org/docs/ThreadSafetyAnalysis.html#conditional-locks
Comment 3 Vladimir Zakharov 2016-12-02 12:27:01 UTC
Review: https://reviews.freebsd.org/D8694
Comment 4 Dries Michiels freebsd_committer freebsd_triage 2018-01-08 21:04:23 UTC
This is still an issue in 11.1-RELEASE.
Comment 5 commit-hook freebsd_committer freebsd_triage 2018-02-07 20:37:26 UTC
A commit references this bug:

Author: markj
Date: Wed Feb  7 20:36:38 UTC 2018
New revision: 328995
URL: https://svnweb.freebsd.org/changeset/base/328995

Log:
  Fix the WITH_SORT_THREADS build.

  PR:		201664
  MFC after:	1 week

Changes:
  head/usr.bin/sort/radixsort.c
Comment 6 commit-hook freebsd_committer freebsd_triage 2018-02-14 00:32:33 UTC
A commit references this bug:

Author: markj
Date: Wed Feb 14 00:32:20 UTC 2018
New revision: 329255
URL: https://svnweb.freebsd.org/changeset/base/329255

Log:
  MFC r328995:
  Fix the WITH_SORT_THREADS build.

  PR:	201664

Changes:
_U  stable/11/
  stable/11/usr.bin/sort/radixsort.c