Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
be481c16
Commit
be481c16
authored
Mar 14, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add a preliminary implementation of futex in macos
parent
fe1c989e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
123 additions
and
12 deletions
+123
-12
CMakeLists.txt
example/echo_c++/CMakeLists.txt
+26
-2
sys_futex.cpp
src/bthread/sys_futex.cpp
+92
-0
sys_futex.h
src/bthread/sys_futex.h
+5
-10
No files found.
example/echo_c++/CMakeLists.txt
View file @
be481c16
...
...
@@ -66,6 +66,16 @@ if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
endif
()
include_directories
(
${
LEVELDB_INCLUDE_PATH
}
)
find_library
(
SSL_LIB NAMES ssl
)
if
(
NOT SSL_LIB
)
message
(
FATAL_ERROR
"Fail to find ssl"
)
endif
()
find_library
(
CRYPTO_LIB NAMES crypto
)
if
(
NOT CRYPTO_LIB
)
message
(
FATAL_ERROR
"Fail to find crypto"
)
endif
()
add_executable
(
echo_client client.cpp
${
PROTO_SRC
}
${
PROTO_HEADER
}
)
add_executable
(
echo_server server.cpp
${
PROTO_SRC
}
${
PROTO_HEADER
}
)
...
...
@@ -74,10 +84,24 @@ set(DYNAMIC_LIB
${
GFLAGS_LIBRARY
}
${
PROTOBUF_LIBRARIES
}
${
LEVELDB_LIB
}
ssl
crypto
${
SSL_LIB
}
${
CRYPTO_LIB
}
dl
)
if
(
CMAKE_SYSTEM_NAME STREQUAL
"Darwin"
)
set
(
DYNAMIC_LIB
${
DYNAMIC_LIB
}
pthread
"-framework CoreFoundation"
"-framework CoreGraphics"
"-framework CoreData"
"-framework CoreText"
"-framework Security"
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
)
endif
()
target_link_libraries
(
echo_client
${
BRPC_LIB
}
${
DYNAMIC_LIB
}
)
target_link_libraries
(
echo_server
${
BRPC_LIB
}
${
DYNAMIC_LIB
}
)
src/bthread/sys_futex.cpp
0 → 100644
View file @
be481c16
// bthread - A M:N threading library to make applications more concurrent.
// Copyright (c) 2012 Baidu, Inc.
//
// 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.
// Author: Zhu,Jiashun (zhujiahun@baidu.com)
// Date: Wed Mar 14 17:44:58 CST 2018
#include "bthread/sys_futex.h"
#include "butil/scoped_lock.h"
#include <map>
#include <pthread.h>
#if defined(OS_MACOSX)
namespace
bthread
{
struct
SimuFutex
{
pthread_mutex_t
lock
;
pthread_cond_t
cond
;
SimuFutex
()
{
pthread_mutex_init
(
&
lock
,
NULL
);
pthread_cond_init
(
&
cond
,
NULL
);
}
~
SimuFutex
()
{
pthread_mutex_destroy
(
&
lock
);
pthread_cond_destroy
(
&
cond
);
}
};
// TODO: use a more efficient way. Current impl doesn't delete SimuFutex at all.
static
std
::
map
<
void
*
,
SimuFutex
>
s_futex_map
;
static
pthread_mutex_t
s_futex_map_mutex
=
PTHREAD_MUTEX_INITIALIZER
;
int
futex_wait_private
(
void
*
addr1
,
int
expected
,
const
timespec
*
timeout
)
{
std
::
unique_lock
<
pthread_mutex_t
>
mu
(
s_futex_map_mutex
);
SimuFutex
&
simu_futex
=
s_futex_map
[
addr1
];
mu
.
unlock
();
int
rc
=
pthread_mutex_lock
(
&
simu_futex
.
lock
);
if
(
rc
<
0
)
{
return
rc
;
}
if
(
static_cast
<
butil
::
atomic
<
int
>*>
(
addr1
)
->
load
()
==
expected
)
{
pthread_cond_wait
(
&
simu_futex
.
cond
,
&
simu_futex
.
lock
);
}
rc
=
pthread_mutex_unlock
(
&
simu_futex
.
lock
);
if
(
rc
<
0
)
{
return
rc
;
}
return
0
;
}
int
futex_wake_private
(
void
*
addr1
,
int
nwake
)
{
std
::
unique_lock
<
pthread_mutex_t
>
mu
(
s_futex_map_mutex
);
SimuFutex
&
simu_futex
=
s_futex_map
[
addr1
];
mu
.
unlock
();
int
rc
=
pthread_mutex_lock
(
&
simu_futex
.
lock
);
if
(
rc
<
0
)
{
return
rc
;
}
for
(
int
i
=
0
;
i
<
nwake
;
++
i
)
{
rc
=
pthread_cond_signal
(
&
simu_futex
.
cond
);
if
(
rc
<
0
)
{
return
rc
;
}
}
rc
=
pthread_mutex_unlock
(
&
simu_futex
.
lock
);
if
(
rc
<
0
)
{
return
rc
;
}
return
0
;
}
int
futex_requeue_private
(
void
*
addr1
,
int
nwake
,
void
*
addr2
)
{
// TODO
return
-
1
;
}
}
// namespace bthread
#endif
src/bthread/sys_futex.h
View file @
be481c16
...
...
@@ -53,18 +53,13 @@ inline int futex_requeue_private(void* addr1, int nwake, void* addr2) {
#elif defined(OS_MACOSX)
namespace
bthread
{
inline
int
futex_wait_private
(
void
*
addr1
,
int
expected
,
const
timespec
*
timeout
)
{
return
-
1
;
}
inline
int
futex_wake_private
(
void
*
addr1
,
int
nwake
)
{
return
-
1
;
}
int
futex_wait_private
(
void
*
addr1
,
int
expected
,
const
timespec
*
timeout
);
int
futex_wake_private
(
void
*
addr1
,
int
nwake
);
int
futex_requeue_private
(
void
*
addr1
,
int
nwake
,
void
*
addr2
);
inline
int
futex_requeue_private
(
void
*
addr1
,
int
nwake
,
void
*
addr2
)
{
return
-
1
;
}
}
// namespace bthread
#else
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment