critical_section.h
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "internal/_deprecated_header_message_guard.h"
#if !defined(__TBB_show_deprecation_message_critical_section_H) && defined(__TBB_show_deprecated_header_message)
#define __TBB_show_deprecation_message_critical_section_H
#pragma message("TBB Warning: tbb/critical_section.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
#endif
#if defined(__TBB_show_deprecated_header_message)
#undef __TBB_show_deprecated_header_message
#endif
#ifndef _TBB_CRITICAL_SECTION_H_
#define _TBB_CRITICAL_SECTION_H_
#define __TBB_critical_section_H_include_area
#include "internal/_warning_suppress_enable_notice.h"
#if _WIN32||_WIN64
#include "machine/windows_api.h"
#else
#include <pthread.h>
#include <errno.h>
#endif // _WIN32||WIN64
#include "tbb_stddef.h"
#include "tbb_thread.h"
#include "tbb_exception.h"
#include "tbb_profiling.h"
namespace tbb {
namespace internal {
class critical_section_v4 : internal::no_copy {
#if _WIN32||_WIN64
CRITICAL_SECTION my_impl;
#else
pthread_mutex_t my_impl;
#endif
tbb_thread::id my_tid;
public:
void __TBB_EXPORTED_METHOD internal_construct();
critical_section_v4() {
#if _WIN32||_WIN64
InitializeCriticalSectionEx( &my_impl, 4000, 0 );
#else
pthread_mutex_init(&my_impl, NULL);
#endif
internal_construct();
}
~critical_section_v4() {
__TBB_ASSERT(my_tid == tbb_thread::id(), "Destroying a still-held critical section");
#if _WIN32||_WIN64
DeleteCriticalSection(&my_impl);
#else
pthread_mutex_destroy(&my_impl);
#endif
}
class scoped_lock : internal::no_copy {
private:
critical_section_v4 &my_crit;
public:
scoped_lock( critical_section_v4& lock_me) :my_crit(lock_me) {
my_crit.lock();
}
~scoped_lock() {
my_crit.unlock();
}
};
void lock() {
tbb_thread::id local_tid = this_tbb_thread::get_id();
if(local_tid == my_tid) throw_exception( eid_improper_lock );
#if _WIN32||_WIN64
EnterCriticalSection( &my_impl );
#else
int rval = pthread_mutex_lock(&my_impl);
__TBB_ASSERT_EX(!rval, "critical_section::lock: pthread_mutex_lock failed");
#endif
__TBB_ASSERT(my_tid == tbb_thread::id(), NULL);
my_tid = local_tid;
}
bool try_lock() {
bool gotlock;
tbb_thread::id local_tid = this_tbb_thread::get_id();
if(local_tid == my_tid) return false;
#if _WIN32||_WIN64
gotlock = TryEnterCriticalSection( &my_impl ) != 0;
#else
int rval = pthread_mutex_trylock(&my_impl);
// valid returns are 0 (locked) and [EBUSY]
__TBB_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed");
gotlock = rval == 0;
#endif
if(gotlock) {
my_tid = local_tid;
}
return gotlock;
}
void unlock() {
__TBB_ASSERT(this_tbb_thread::get_id() == my_tid, "thread unlocking critical_section is not thread that locked it");
my_tid = tbb_thread::id();
#if _WIN32||_WIN64
LeaveCriticalSection( &my_impl );
#else
int rval = pthread_mutex_unlock(&my_impl);
__TBB_ASSERT_EX(!rval, "critical_section::unlock: pthread_mutex_unlock failed");
#endif
}
static const bool is_rw_mutex = false;
static const bool is_recursive_mutex = false;
static const bool is_fair_mutex = true;
}; // critical_section_v4
} // namespace internal
__TBB_DEPRECATED_VERBOSE_MSG("tbb::critical_section is deprecated, use std::mutex") typedef internal::critical_section_v4 critical_section;
__TBB_DEFINE_PROFILING_SET_NAME(critical_section)
} // namespace tbb
#include "internal/_warning_suppress_disable_notice.h"
#undef __TBB_critical_section_H_include_area
#endif // _TBB_CRITICAL_SECTION_H_
tbb::__TBB_DEPRECATED_VERBOSE_MSG
class __TBB_DEPRECATED_VERBOSE_MSG("tbb::aligned_space is deprecated, use std::aligned_storage") aligned_space
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:43
tbb::internal::eid_improper_lock
Definition: tbb_exception.h:76
tbb_thread.h
_deprecated_header_message_guard.h
internal
Definition: _flow_graph_async_msg_impl.h:24
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
id
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id id
Definition: ittnotify_static.h:172
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::internal::throw_exception
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
Definition: tbb_exception.h:105
tbb::internal::critical_section_v4::scoped_lock
Definition: critical_section.h:79
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal::critical_section_v4::lock
void lock()
Definition: critical_section.h:92
windows_api.h
tbb::this_tbb_thread::get_id
__TBB_DEPRECATED_VERBOSE tbb_thread::id get_id()
Definition: tbb_thread.h:331
tbb_profiling.h
tbb::internal::critical_section_v4::unlock
void unlock()
Definition: critical_section.h:123
tbb::internal::critical_section_v4::scoped_lock::my_crit
critical_section_v4 & my_crit
Definition: critical_section.h:81
tbb::internal::critical_section_v4::internal_construct
void __TBB_EXPORTED_METHOD internal_construct()
Definition: critical_section.cpp:23
tbb_exception.h
tbb::internal::critical_section_v4::scoped_lock::~scoped_lock
~scoped_lock()
Definition: critical_section.h:87
tbb::internal::critical_section_v4::is_rw_mutex
static const bool is_rw_mutex
Definition: critical_section.h:134
tbb::internal::critical_section_v4::critical_section_v4
critical_section_v4()
Definition: critical_section.h:61
tbb::internal::critical_section_v4::~critical_section_v4
~critical_section_v4()
Definition: critical_section.h:70
tbb::internal::critical_section_v4
Definition: critical_section.h:50
tbb::internal::critical_section_v4::is_recursive_mutex
static const bool is_recursive_mutex
Definition: critical_section.h:135
tbb::internal::critical_section_v4::my_impl
pthread_mutex_t my_impl
Definition: critical_section.h:54
tbb_stddef.h
__TBB_DEFINE_PROFILING_SET_NAME
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
Definition: tbb_profiling.h:115
_warning_suppress_enable_notice.h
__TBB_EXPORTED_METHOD
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
tbb::internal::critical_section_v4::try_lock
bool try_lock()
Definition: critical_section.h:105
tbb::internal::critical_section_v4::my_tid
tbb_thread::id my_tid
Definition: critical_section.h:56
tbb::internal::critical_section_v4::scoped_lock::scoped_lock
scoped_lock(critical_section_v4 &lock_me)
Definition: critical_section.h:83
tbb::internal::critical_section_v4::is_fair_mutex
static const bool is_fair_mutex
Definition: critical_section.h:136
__TBB_ASSERT_EX
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:167

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.