2 Copyright 2005-2014 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5 you can redistribute it and/or modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation. Threading Building Blocks is
7 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 See the GNU General Public License for more details. You should have received a copy of
10 the GNU General Public License along with Threading Building Blocks; if not, write to the
11 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13 As a special exception, you may use this file as part of a free software library without
14 restriction. Specifically, if other files instantiate templates or use macros or inline
15 functions from this file, or you compile this file and link it with other files to produce
16 an executable, this file does not by itself cause the resulting executable to be covered
17 by the GNU General Public License. This exception does not however invalidate any other
18 reasons why the executable file might be covered by the GNU General Public License.
21 #ifndef __TBB_mutex_padding_H
22 #define __TBB_mutex_padding_H
24 // wrapper for padding mutexes to be alone on a cache line, without requiring they be allocated
25 // from a pool. Because we allow them to be defined anywhere they must be two cache lines in size.
29 namespace interface7 {
32 static const size_t cache_line_size = 64;
34 // Pad a mutex to occupy a number of full cache lines sufficient to avoid false sharing
35 // with other data; space overhead is up to 2*cache_line_size-1.
36 template<typename Mutex, bool is_rw> class padded_mutex;
38 template<typename Mutex>
39 class padded_mutex<Mutex,false> : tbb::internal::mutex_copy_deprecated_and_disabled {
40 typedef long pad_type;
41 pad_type my_pad[((sizeof(Mutex)+cache_line_size-1)/cache_line_size+1)*cache_line_size/sizeof(pad_type)];
43 Mutex *impl() { return (Mutex *)((uintptr_t(this)|(cache_line_size-1))+1);}
46 static const bool is_rw_mutex = Mutex::is_rw_mutex;
47 static const bool is_recursive_mutex = Mutex::is_recursive_mutex;
48 static const bool is_fair_mutex = Mutex::is_fair_mutex;
50 padded_mutex() { new(impl()) Mutex(); }
51 ~padded_mutex() { impl()->~Mutex(); }
53 //! Represents acquisition of a mutex.
54 class scoped_lock : tbb::internal::no_copy {
55 typename Mutex::scoped_lock my_scoped_lock;
57 scoped_lock() : my_scoped_lock() {}
58 scoped_lock( padded_mutex& m ) : my_scoped_lock(*m.impl()) { }
61 void acquire( padded_mutex& m ) { my_scoped_lock.acquire(*m.impl()); }
62 bool try_acquire( padded_mutex& m ) { return my_scoped_lock.try_acquire(*m.impl()); }
63 void release() { my_scoped_lock.release(); }
67 template<typename Mutex>
68 class padded_mutex<Mutex,true> : tbb::internal::mutex_copy_deprecated_and_disabled {
69 typedef long pad_type;
70 pad_type my_pad[((sizeof(Mutex)+cache_line_size-1)/cache_line_size+1)*cache_line_size/sizeof(pad_type)];
72 Mutex *impl() { return (Mutex *)((uintptr_t(this)|(cache_line_size-1))+1);}
75 static const bool is_rw_mutex = Mutex::is_rw_mutex;
76 static const bool is_recursive_mutex = Mutex::is_recursive_mutex;
77 static const bool is_fair_mutex = Mutex::is_fair_mutex;
79 padded_mutex() { new(impl()) Mutex(); }
80 ~padded_mutex() { impl()->~Mutex(); }
82 //! Represents acquisition of a mutex.
83 class scoped_lock : tbb::internal::no_copy {
84 typename Mutex::scoped_lock my_scoped_lock;
86 scoped_lock() : my_scoped_lock() {}
87 scoped_lock( padded_mutex& m, bool write = true ) : my_scoped_lock(*m.impl(),write) { }
90 void acquire( padded_mutex& m, bool write = true ) { my_scoped_lock.acquire(*m.impl(),write); }
91 bool try_acquire( padded_mutex& m, bool write = true ) { return my_scoped_lock.try_acquire(*m.impl(),write); }
92 bool upgrade_to_writer() { return my_scoped_lock.upgrade_to_writer(); }
93 bool downgrade_to_reader() { return my_scoped_lock.downgrade_to_reader(); }
94 void release() { my_scoped_lock.release(); }
98 } // namespace internal
99 } // namespace interface7
102 #endif /* __TBB_mutex_padding_H */