Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
82f4ae7f
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
82f4ae7f
编写于
3月 27, 2017
作者:
L
Liu Yiqun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add alternative implementation of SpinLock and ThreadBarrier,
using the same implemetation as mac os.
上级
38fa74ed
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
95 addition
and
48 deletion
+95
-48
cmake/flags.cmake
cmake/flags.cmake
+12
-0
paddle/utils/arch/linux/Locks.cpp
paddle/utils/arch/linux/Locks.cpp
+83
-48
未找到文件。
cmake/flags.cmake
浏览文件 @
82f4ae7f
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
include
(
CheckCXXCompilerFlag
)
include
(
CheckCXXCompilerFlag
)
include
(
CheckCCompilerFlag
)
include
(
CheckCCompilerFlag
)
include
(
CheckCXXSymbolExists
)
include
(
CheckCXXSymbolExists
)
include
(
CheckTypeSize
)
function
(
CheckCompilerCXX11Flag
)
function
(
CheckCompilerCXX11Flag
)
if
(
CMAKE_CXX_COMPILER_ID STREQUAL
"GNU"
)
if
(
CMAKE_CXX_COMPILER_ID STREQUAL
"GNU"
)
...
@@ -83,6 +84,17 @@ if(NOT UINT64_MAX_EXISTS)
...
@@ -83,6 +84,17 @@ if(NOT UINT64_MAX_EXISTS)
endif
()
endif
()
endif
()
endif
()
SET
(
CMAKE_EXTRA_INCLUDE_FILES
"pthread.h"
)
CHECK_TYPE_SIZE
(
pthread_spinlock_t SPINLOCK_FOUND
)
CHECK_TYPE_SIZE
(
pthread_barrier_t BARRIER_FOUND
)
if
(
SPINLOCK_FOUND
)
add_definitions
(
-DPADDLE_USE_PTHREAD_SPINLOCK
)
endif
(
SPINLOCK_FOUND
)
if
(
BARRIER_FOUND
)
add_definitions
(
-DPADDLE_USE_PTHREAD_BARRIER
)
endif
(
BARRIER_FOUND
)
SET
(
CMAKE_EXTRA_INCLUDE_FILES
""
)
# Common flags. the compiler flag used for C/C++ sources whenever release or debug
# Common flags. the compiler flag used for C/C++ sources whenever release or debug
# Do not care if this flag is support for gcc.
# Do not care if this flag is support for gcc.
set
(
COMMON_FLAGS
set
(
COMMON_FLAGS
...
...
paddle/utils/arch/linux/Locks.cpp
浏览文件 @
82f4ae7f
...
@@ -15,6 +15,7 @@ limitations under the License. */
...
@@ -15,6 +15,7 @@ limitations under the License. */
#include "paddle/utils/Locks.h"
#include "paddle/utils/Locks.h"
#include <semaphore.h>
#include <semaphore.h>
#include <unistd.h>
#include <unistd.h>
#include "paddle/utils/Logging.h"
namespace
paddle
{
namespace
paddle
{
class
SemaphorePrivate
{
class
SemaphorePrivate
{
...
@@ -26,7 +27,10 @@ Semaphore::Semaphore(int initValue) : m(new SemaphorePrivate()) {
...
@@ -26,7 +27,10 @@ Semaphore::Semaphore(int initValue) : m(new SemaphorePrivate()) {
sem_init
(
&
m
->
sem
,
0
,
initValue
);
sem_init
(
&
m
->
sem
,
0
,
initValue
);
}
}
Semaphore
::~
Semaphore
()
{
sem_destroy
(
&
m
->
sem
);
}
Semaphore
::~
Semaphore
()
{
sem_destroy
(
&
m
->
sem
);
delete
m
;
}
bool
Semaphore
::
timeWait
(
struct
timespec
*
ts
)
{
bool
Semaphore
::
timeWait
(
struct
timespec
*
ts
)
{
return
(
0
==
sem_timedwait
(
&
m
->
sem
,
ts
));
return
(
0
==
sem_timedwait
(
&
m
->
sem
,
ts
));
...
@@ -36,70 +40,101 @@ void Semaphore::wait() { sem_wait(&m->sem); }
...
@@ -36,70 +40,101 @@ void Semaphore::wait() { sem_wait(&m->sem); }
void
Semaphore
::
post
()
{
sem_post
(
&
m
->
sem
);
}
void
Semaphore
::
post
()
{
sem_post
(
&
m
->
sem
);
}
#ifdef PADDLE_USE_PTHREAD_SPINLOCK
class
SpinLockPrivate
{
class
SpinLockPrivate
{
public:
public:
inline
SpinLockPrivate
()
{
inline
SpinLockPrivate
()
{
pthread_spin_init
(
&
lock_
,
0
);
}
#ifndef __ANDROID__
inline
~
SpinLockPrivate
()
{
pthread_spin_destroy
(
&
lock_
);
}
pthread_spin_init
(
&
lock_
,
0
);
inline
void
lock
()
{
pthread_spin_lock
(
&
lock_
);
}
inline
void
unlock
()
{
pthread_spin_unlock
(
&
lock_
);
}
pthread_spinlock_t
lock_
;
char
padding_
[
64
-
sizeof
(
pthread_spinlock_t
)];
};
#else
#else
lock_
=
0
;
#endif
#include <atomic>
class
SpinLockPrivate
{
public:
inline
void
lock
()
{
while
(
lock_
.
test_and_set
(
std
::
memory_order_acquire
))
{
}
}
inline
~
SpinLockPrivate
()
{
#ifndef __ANDROID__
pthread_spin_destroy
(
&
lock_
);
#endif
}
}
#ifndef __ANDROID__
inline
void
unlock
()
{
lock_
.
clear
(
std
::
memory_order_release
);
}
pthread_spinlock_t
lock_
;
#else
std
::
atomic_flag
lock_
=
ATOMIC_FLAG_INIT
;
unsigned
long
lock_
;
char
padding_
[
64
-
sizeof
(
lock_
)];
// Padding to cache line size
#endif
char
padding_
[
64
-
sizeof
(
lock_
)];
};
};
SpinLock
::
SpinLock
()
:
m
(
new
SpinLockPrivate
())
{}
#endif
SpinLock
::
SpinLock
()
:
m
(
new
SpinLockPrivate
())
{}
SpinLock
::~
SpinLock
()
{
delete
m
;
}
SpinLock
::~
SpinLock
()
{
delete
m
;
}
void
SpinLock
::
lock
()
{
m
->
lock
();
}
void
SpinLock
::
unlock
()
{
m
->
unlock
();
}
void
SpinLock
::
lock
()
{
#ifdef PADDLE_USE_PTHREAD_BARRIER
#ifndef __ANDROID__
pthread_spin_lock
(
&
m
->
lock_
);
#endif
}
void
SpinLock
::
unlock
()
{
#ifndef __ANDROID__
pthread_spin_unlock
(
&
m
->
lock_
);
#endif
}
class
ThreadBarrierPrivate
{
class
ThreadBarrierPrivate
{
public:
public:
#ifndef __ANDROID__
pthread_barrier_t
barrier_
;
pthread_barrier_t
barrier_
;
#else
unsigned
long
barrier_
;
inline
explicit
ThreadBarrierPrivate
(
int
count
)
{
#endif
pthread_barrier_init
(
&
barrier_
,
nullptr
,
count
);
}
inline
~
ThreadBarrierPrivate
()
{
pthread_barrier_destroy
(
&
barrier_
);
}
inline
void
wait
()
{
pthread_barrier_wait
(
&
barrier_
);
}
};
};
ThreadBarrier
::
ThreadBarrier
(
int
count
)
:
m
(
new
ThreadBarrierPrivate
())
{
#else
#ifndef __ANDROID__
pthread_barrier_init
(
&
m
->
barrier_
,
nullptr
,
count
);
#endif
}
ThreadBarrier
::~
ThreadBarrier
()
{
class
ThreadBarrierPrivate
{
#ifndef __ANDROID__
public:
pthread_barrier_destroy
(
&
m
->
barrier_
);
pthread_mutex_t
mutex_
;
#endif
pthread_cond_t
cond_
;
delete
m
;
int
count_
;
}
int
tripCount_
;
inline
explicit
ThreadBarrierPrivate
(
int
cnt
)
:
count_
(
0
),
tripCount_
(
cnt
)
{
CHECK_NE
(
cnt
,
0
);
CHECK_GE
(
pthread_mutex_init
(
&
mutex_
,
0
),
0
);
CHECK_GE
(
pthread_cond_init
(
&
cond_
,
0
),
0
);
}
inline
~
ThreadBarrierPrivate
()
{
pthread_cond_destroy
(
&
cond_
);
pthread_mutex_destroy
(
&
mutex_
);
}
/**
* @brief wait
* @return true if the last wait
*/
inline
bool
wait
()
{
pthread_mutex_lock
(
&
mutex_
);
++
count_
;
if
(
count_
>=
tripCount_
)
{
count_
=
0
;
pthread_cond_broadcast
(
&
cond_
);
pthread_mutex_unlock
(
&
mutex_
);
return
true
;
}
else
{
pthread_cond_wait
(
&
cond_
,
&
mutex_
);
pthread_mutex_unlock
(
&
mutex_
);
return
false
;
}
}
};
void
ThreadBarrier
::
wait
()
{
#ifndef __ANDROID__
pthread_barrier_wait
(
&
m
->
barrier_
);
#endif
#endif
}
ThreadBarrier
::
ThreadBarrier
(
int
count
)
:
m
(
new
ThreadBarrierPrivate
(
count
))
{}
ThreadBarrier
::~
ThreadBarrier
()
{
delete
m
;
}
void
ThreadBarrier
::
wait
()
{
m
->
wait
();
}
}
// namespace paddle
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录