1 /*
2     Copyright 2005-2014 Intel Corporation.  All Rights Reserved.
3 
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
12 
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.
19 */
20 
21 #ifndef __TBB_mutex_padding_H
22 #define __TBB_mutex_padding_H
23 
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.
26 
27 
28 namespace tbb {
29 namespace interface7 {
30 namespace internal {
31 
32 static const size_t cache_line_size = 64;
33 
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;
37 
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)];
42 
impl()43     Mutex *impl() { return (Mutex *)((uintptr_t(this)|(cache_line_size-1))+1);}
44 
45 public:
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;
49 
padded_mutex()50     padded_mutex() { new(impl()) Mutex(); }
~padded_mutex()51     ~padded_mutex() { impl()->~Mutex(); }
52 
53     //! Represents acquisition of a mutex.
54     class scoped_lock :  tbb::internal::no_copy {
55         typename Mutex::scoped_lock my_scoped_lock;
56     public:
scoped_lock()57         scoped_lock() : my_scoped_lock() {}
scoped_lock(padded_mutex & m)58         scoped_lock( padded_mutex& m ) : my_scoped_lock(*m.impl()) { }
~scoped_lock()59         ~scoped_lock() {  }
60 
acquire(padded_mutex & m)61         void acquire( padded_mutex& m ) { my_scoped_lock.acquire(*m.impl()); }
try_acquire(padded_mutex & m)62         bool try_acquire( padded_mutex& m ) { return my_scoped_lock.try_acquire(*m.impl()); }
release()63         void release() { my_scoped_lock.release(); }
64     };
65 };
66 
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)];
71 
impl()72     Mutex *impl() { return (Mutex *)((uintptr_t(this)|(cache_line_size-1))+1);}
73 
74 public:
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;
78 
padded_mutex()79     padded_mutex() { new(impl()) Mutex(); }
~padded_mutex()80     ~padded_mutex() { impl()->~Mutex(); }
81 
82     //! Represents acquisition of a mutex.
83     class scoped_lock :  tbb::internal::no_copy {
84         typename Mutex::scoped_lock my_scoped_lock;
85     public:
scoped_lock()86         scoped_lock() : my_scoped_lock() {}
87         scoped_lock( padded_mutex& m, bool write = true ) : my_scoped_lock(*m.impl(),write) { }
~scoped_lock()88         ~scoped_lock() {  }
89 
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); }
upgrade_to_writer()92         bool upgrade_to_writer() { return my_scoped_lock.upgrade_to_writer(); }
downgrade_to_reader()93         bool downgrade_to_reader() { return my_scoped_lock.downgrade_to_reader(); }
release()94         void release() { my_scoped_lock.release(); }
95     };
96 };
97 
98 } // namespace internal
99 } // namespace interface7
100 } // namespace tbb
101 
102 #endif /* __TBB_mutex_padding_H */
103