From owner-svn-src-head@FreeBSD.ORG Mon Sep 1 18:27:04 2014 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id D196B28B; Mon, 1 Sep 2014 18:27:04 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id BB31B1EF1; Mon, 1 Sep 2014 18:27:04 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id s81IR4a8081938; Mon, 1 Sep 2014 18:27:04 GMT (envelope-from ed@FreeBSD.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id s81IR4iD081937; Mon, 1 Sep 2014 18:27:04 GMT (envelope-from ed@FreeBSD.org) Message-Id: <201409011827.s81IR4iD081937@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: ed set sender to ed@FreeBSD.org using -f From: Ed Schouten Date: Mon, 1 Sep 2014 18:27:04 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r270941 - head/sys/sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Sep 2014 18:27:05 -0000 Author: ed Date: Mon Sep 1 18:27:04 2014 New Revision: 270941 URL: http://svnweb.freebsd.org/changeset/base/270941 Log: Add lock annotations to . Clang has support for annotating mutexes and code that uses mutexes to validate certain aspects of thread safety: - Whether acquiring/releasing locks is done properly (e.g., whether you unlock a mutex before leaving a function). - Whether a lock is held while reading/writing data from/to memory. Analysis is performed at the function level. Functions can be annotated to indicate they: - (try to) pick up a lock, - release a lock, - can only be called when (not) holding a lock, - assert that a lock is held. Variables and structure members can be annotated to indicate that they are guarded by a certain lock. In C++, these annotations can refer to both global variables, but also other class/structure members. In C, it is only possible to refer to global variables. This change adds wrappers for the annotations used by Clang to . They currently have no effect, but this is on purpose. This change will be merged back to FreeBSD 9 and 10, which means we can safely experiment with these annotations on HEAD without making it harder to port changes back. Reviewed by: announced on arch@ and toolchain@ MFC after: 3 weeks Modified: head/sys/sys/cdefs.h Modified: head/sys/sys/cdefs.h ============================================================================== --- head/sys/sys/cdefs.h Mon Sep 1 18:25:49 2014 (r270940) +++ head/sys/sys/cdefs.h Mon Sep 1 18:27:04 2014 (r270941) @@ -739,4 +739,60 @@ #define __NO_TLS 1 #endif +/* + * Lock annotations. + * + * Clang provides support for doing basic thread-safety tests at + * compile-time, by marking which locks will/should be held when + * entering/leaving a functions. + * + * Furthermore, it is also possible to annotate variables and structure + * members to enforce that they are only accessed when certain locks are + * held. + * + * Note: These annotations have no effect on this version of FreeBSD. + * They are merely provided for forward compatibilty. + */ + +#define __lock_annotate(x) + +/* Structure implements a lock. */ +#define __lockable __lock_annotate(lockable) + +/* Function acquires an exclusive or shared lock. */ +#define __locks_exclusive(...) \ + __lock_annotate(exclusive_lock_function(__VA_ARGS__)) +#define __locks_shared(...) \ + __lock_annotate(shared_lock_function(__VA_ARGS__)) + +/* Function attempts to acquire an exclusive or shared lock. */ +#define __trylocks_exclusive(...) \ + __lock_annotate(exclusive_trylock_function(__VA_ARGS__)) +#define __trylocks_shared(...) \ + __lock_annotate(shared_trylock_function(__VA_ARGS__)) + +/* Function releases a lock. */ +#define __unlocks(...) __lock_annotate(unlock_function(__VA_ARGS__)) + +/* Function asserts that an exclusive or shared lock is held. */ +#define __asserts_exclusive(...) \ + __lock_annotate(assert_exclusive_lock(__VA_ARGS__)) +#define __asserts_shared(...) \ + __lock_annotate(assert_shared_lock(__VA_ARGS__)) + +/* Function requires that an exclusive or shared lock is or is not held. */ +#define __requires_exclusive(...) \ + __lock_annotate(exclusive_locks_required(__VA_ARGS__)) +#define __requires_shared(...) \ + __lock_annotate(shared_locks_required(__VA_ARGS__)) +#define __requires_unlocked(...) \ + __lock_annotate(locks_excluded(__VA_ARGS__)) + +/* Function should not be analyzed. */ +#define __no_lock_analysis __lock_annotate(no_thread_safety_analysis) + +/* Guard variables and structure members by lock. */ +#define __guarded_by(x) __lock_annotate(guarded_by(x)) +#define __pt_guarded_by(x) __lock_annotate(pt_guarded_by(x)) + #endif /* !_SYS_CDEFS_H_ */