Committing Intel(R) TBB 2018 source code
[platform/upstream/tbb.git] / src / tbb / spin_mutex.cpp
1 /*
2     Copyright (c) 2005-2017 Intel Corporation
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15
16
17
18
19 */
20
21 #include "tbb/tbb_machine.h"
22 #include "tbb/spin_mutex.h"
23 #include "itt_notify.h"
24 #include "tbb_misc.h"
25
26 namespace tbb {
27
28 void spin_mutex::scoped_lock::internal_acquire( spin_mutex& m ) {
29     __TBB_ASSERT( !my_mutex, "already holding a lock on a spin_mutex" );
30     ITT_NOTIFY(sync_prepare, &m);
31     __TBB_LockByte(m.flag);
32     my_mutex = &m;
33     ITT_NOTIFY(sync_acquired, &m);
34 }
35
36 void spin_mutex::scoped_lock::internal_release() {
37     __TBB_ASSERT( my_mutex, "release on spin_mutex::scoped_lock that is not holding a lock" );
38
39     ITT_NOTIFY(sync_releasing, my_mutex);
40     __TBB_UnlockByte(my_mutex->flag);
41     my_mutex = NULL;
42 }
43
44 bool spin_mutex::scoped_lock::internal_try_acquire( spin_mutex& m ) {
45     __TBB_ASSERT( !my_mutex, "already holding a lock on a spin_mutex" );
46     bool result = bool( __TBB_TryLockByte(m.flag) );
47     if( result ) {
48         my_mutex = &m;
49         ITT_NOTIFY(sync_acquired, &m);
50     }
51     return result;
52 }
53
54 void spin_mutex::internal_construct() {
55     ITT_SYNC_CREATE(this, _T("tbb::spin_mutex"), _T(""));
56 }
57
58 } // namespace tbb