Commit 3e87f670 authored by zhujiashun's avatar zhujiashun

Link succeeds with fake epoll and partial kqueue

parent d070170e
......@@ -147,7 +147,17 @@ set(DYNAMIC_LIB
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(DYNAMIC_LIB ${DYNAMIC_LIB} rt)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(DYNAMIC_LIB ${DYNAMIC_LIB} pthread)
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()
# for *.so
......@@ -226,7 +236,6 @@ set(BUTIL_SOURCES
${CMAKE_SOURCE_DIR}/src/butil/strings/string_util.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/string_util_constants.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/stringprintf.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/sys_string_conversions_posix.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/utf_offset_string_conversions.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/utf_string_conversion_utils.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/utf_string_conversions.cc
......@@ -274,11 +283,17 @@ set(BUTIL_SOURCES
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(BUTIL_SOURCES ${BUTIL_SOURCES}
${CMAKE_SOURCE_DIR}/src/butil/file_util_linux.cc
${CMAKE_SOURCE_DIR}/src/butil/threading/platform_thread_linux.cc)
${CMAKE_SOURCE_DIR}/src/butil/threading/platform_thread_linux.cc
${CMAKE_SOURCE_DIR}/src/butil/strings/sys_string_conversions_posix.cc)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(BUTIL_SOURCES ${BUTIL_SOURCES}
${CMAKE_SOURCE_DIR}/src/butil/mac/bundle_locations.mm
${CMAKE_SOURCE_DIR}/src/butil/mac/foundation_util.mm)
${CMAKE_SOURCE_DIR}/src/butil/mac/foundation_util.mm
${CMAKE_SOURCE_DIR}/src/butil/file_util_mac.mm
${CMAKE_SOURCE_DIR}/src/butil/threading/platform_thread_mac.mm
${CMAKE_SOURCE_DIR}/src/butil/strings/sys_string_conversions_mac.mm
${CMAKE_SOURCE_DIR}/src/butil/time/time_mac.cc
${CMAKE_SOURCE_DIR}/src/butil/mac/scoped_mach_port.cc)
endif()
file(GLOB_RECURSE BVAR_SOURCES "${CMAKE_SOURCE_DIR}/src/bvar/*.cpp")
......
......@@ -19,12 +19,7 @@ set_property(TARGET ${BUTIL_LIB} PROPERTY POSITION_INDEPENDENT_CODE 1)
add_library(brpc SHARED $<TARGET_OBJECTS:BUTIL_LIB> $<TARGET_OBJECTS:OBJ_LIB>)
add_library(brpc_static STATIC $<TARGET_OBJECTS:BUTIL_LIB> $<TARGET_OBJECTS:OBJ_LIB>)
target_link_libraries(brpc ${DYNAMIC_LIB}
"-framework CoreFoundation"
"-framework CoreGraphics"
"-framework CoreData"
"-framework CoreText"
"-framework Security")
target_link_libraries(brpc ${DYNAMIC_LIB})
if(WITH_GLOG)
target_link_libraries(brpc ${GLOG_LIB})
......
......@@ -34,7 +34,9 @@
#include "butil/fd_guard.h"
extern "C" {
#if defined(OS_LINUX)
extern char *program_invocation_name;
#endif
int __attribute__((weak)) ProfilerStart(const char* fname);
void __attribute__((weak)) ProfilerStop();
}
......@@ -453,7 +455,11 @@ static void LoadSymbols() {
info.start_addr = 0;
info.end_addr = std::numeric_limits<uintptr_t>::max();
info.offset = 0;
#if defined(OS_LINUX)
info.path = program_invocation_name;
#elif defined(OS_MACOSX)
info.path = getprogname();
#endif
ExtractSymbolsFromBinary(symbol_map, info);
butil::Timer tm2;
......
......@@ -25,9 +25,17 @@ BAIDU_REGISTER_ERRNO(ESTOP, "The structure is stopping")
extern "C" {
#if defined(OS_LINUX)
extern int *__errno_location() __attribute__((__const__));
int *bthread_errno_location() {
return __errno_location();
}
#elif defined(OS_MACOSX)
// TODO(zhujiashun): find workaround
int *bthread_errno_location() {
return &errno;
}
#endif
} // extern "C"
......@@ -402,10 +402,16 @@ static pthread_once_t init_sys_mutex_lock_once = PTHREAD_ONCE_INIT;
// Call _dl_sym which is a private function in glibc to workaround the malloc
// causing deadlock temporarily. This fix is hardly portable.
static void init_sys_mutex_lock() {
#if defined(OS_LINUX)
// TODO: may need dlvsym when GLIBC has multiple versions of a same symbol.
// http://blog.fesnel.com/blog/2009/08/25/preloading-with-multiple-symbol-versions
sys_pthread_mutex_lock = (MutexOp)_dl_sym(RTLD_NEXT, "pthread_mutex_lock", (void*)init_sys_mutex_lock);
sys_pthread_mutex_unlock = (MutexOp)_dl_sym(RTLD_NEXT, "pthread_mutex_unlock", (void*)init_sys_mutex_lock);
#elif defined(OS_MACOSX)
// TODO: look workaround for dlsym on mac
sys_pthread_mutex_lock = (MutexOp)dlsym(RTLD_NEXT, "pthread_mutex_lock");
sys_pthread_mutex_unlock = (MutexOp)dlsym(RTLD_NEXT, "pthread_mutex_unlock");
#endif
}
// Make sure pthread functions are ready before main().
......
......@@ -100,11 +100,11 @@ __BEGIN_DECLS
The "size" parameter is a hint specifying the number of file
descriptors to be associated with the new instance. The fd
returned by epoll_create() should be closed with close(). */
extern int epoll_create (int __size);
inline int epoll_create (int __size) { return 0;}
/* Same as epoll_create but with an FLAGS parameter. The unused SIZE
parameter has been dropped. */
extern int epoll_create1 (int __flags);
inline int epoll_create1 (int __flags) {return 0;}
/* Manipulate an epoll instance "epfd". Returns 0 in case of success,
......@@ -113,8 +113,8 @@ extern int epoll_create1 (int __flags);
constants defined above. The "fd" parameter is the target of the
operation. The "event" parameter describes which events the caller
is interested in and any associated user data. */
extern int epoll_ctl (int __epfd, int __op, int __fd,
struct epoll_event *__event);
inline int epoll_ctl (int __epfd, int __op, int __fd,
struct epoll_event *__event) {return 0;}
/* Wait for events on an epoll instance "epfd". Returns the number of
triggered events returned in "events" buffer. Or -1 in case of
......@@ -126,8 +126,8 @@ extern int epoll_ctl (int __epfd, int __op, int __fd,
This function is a cancellation point and therefore not marked with
. */
extern int epoll_wait (int __epfd, struct epoll_event *__events,
int __maxevents, int __timeout);
inline int epoll_wait (int __epfd, struct epoll_event *__events,
int __maxevents, int __timeout) { return 0;}
__END_DECLS
......
......@@ -2,18 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_util.h"
#include "butil/file_util.h"
#import <Foundation/Foundation.h>
#include <copyfile.h>
#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/mac/foundation_util.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "butil/basictypes.h"
#include "butil/files/file_path.h"
#include "butil/mac/foundation_util.h"
#include "butil/strings/string_util.h"
#include "butil/threading/thread_restrictions.h"
namespace base {
namespace butil {
namespace internal {
bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
......@@ -24,18 +24,18 @@ bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
} // namespace internal
bool GetTempDir(base::FilePath* path) {
bool GetTempDir(butil::FilePath* path) {
NSString* tmp = NSTemporaryDirectory();
if (tmp == nil)
return false;
*path = base::mac::NSStringToFilePath(tmp);
*path = butil::mac::NSStringToFilePath(tmp);
return true;
}
FilePath GetHomeDir() {
NSString* tmp = NSHomeDirectory();
if (tmp != nil) {
FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp);
FilePath mac_home_dir = butil::mac::NSStringToFilePath(tmp);
if (!mac_home_dir.empty())
return mac_home_dir;
}
......@@ -49,4 +49,4 @@ FilePath GetHomeDir() {
return FilePath("/tmp");
}
} // namespace base
} // namespace butil
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "butil/mac/scoped_mach_port.h"
#include "butil/logging.h"
namespace butil {
namespace mac {
namespace internal {
// static
void SendRightTraits::Free(mach_port_t port) {
kern_return_t kr = mach_port_deallocate(mach_task_self(), port);
LOG_IF(ERROR, kr != KERN_SUCCESS) << "Fail to call mach_port_deallocate";
}
// static
void ReceiveRightTraits::Free(mach_port_t port) {
kern_return_t kr =
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
LOG_IF(ERROR, kr != KERN_SUCCESS) << "Fail to call mach_port_mod_refs";
}
// static
void PortSetTraits::Free(mach_port_t port) {
kern_return_t kr =
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_PORT_SET, -1);
LOG_IF(ERROR, kr != KERN_SUCCESS) << "Fail to call mach_port_mod_refs";
}
} // namespace internal
} // namespace mac
} // namespace butil
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_MAC_SCOPED_MACH_PORT_H_
#define BASE_MAC_SCOPED_MACH_PORT_H_
#include <mach/mach.h>
#include "butil/base_export.h"
#include "butil/scoped_generic.h"
namespace butil {
namespace mac {
namespace internal {
struct SendRightTraits {
static mach_port_t InvalidValue() {
return MACH_PORT_NULL;
}
static void Free(mach_port_t port);
};
struct ReceiveRightTraits {
static mach_port_t InvalidValue() {
return MACH_PORT_NULL;
}
static void Free(mach_port_t port);
};
struct PortSetTraits {
static mach_port_t InvalidValue() {
return MACH_PORT_NULL;
}
static void Free(mach_port_t port);
};
} // namespace internal
// A scoper for handling a Mach port that names a send right. Send rights are
// reference counted, and this takes ownership of the right on construction
// and then removes a reference to the right on destruction. If the reference
// is the last one on the right, the right is deallocated.
using ScopedMachSendRight =
ScopedGeneric<mach_port_t, internal::SendRightTraits>;
// A scoper for handling a Mach port's receive right. There is only one
// receive right per port. This takes ownership of the receive right on
// construction and then destroys the right on destruction, turning all
// outstanding send rights into dead names.
using ScopedMachReceiveRight =
ScopedGeneric<mach_port_t, internal::ReceiveRightTraits>;
// A scoper for handling a Mach port set. A port set can have only one
// reference. This takes ownership of that single reference on construction and
// destroys the port set on destruction. Destroying a port set does not destroy
// the receive rights that are members of the port set.
using ScopedMachPortSet = ScopedGeneric<mach_port_t, internal::PortSetTraits>;
} // namespace mac
} // namespace butil
#endif // BASE_MAC_SCOPED_MACH_PORT_H_
......@@ -2,17 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/sys_string_conversions.h"
#include "butil/strings/sys_string_conversions.h"
#import <Foundation/Foundation.h>
#include <vector>
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/strings/string_piece.h"
#include "butil/mac/foundation_util.h"
#include "butil/mac/scoped_cftyperef.h"
#include "butil/strings/string_piece.h"
namespace base {
namespace butil {
namespace {
......@@ -78,7 +78,7 @@ static OutStringType STLStringToSTLStringWithEncodingsT(
if (in_length == 0)
return OutStringType();
base::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy(
butil::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy(
NULL,
reinterpret_cast<const UInt8*>(in.data()),
in_length * sizeof(typename InStringType::value_type),
......@@ -152,12 +152,12 @@ CFStringRef SysUTF16ToCFStringRef(const string16& utf16) {
}
NSString* SysUTF8ToNSString(const std::string& utf8) {
return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease(
return (NSString*)butil::mac::CFTypeRefToNSObjectAutorelease(
SysUTF8ToCFStringRef(utf8));
}
NSString* SysUTF16ToNSString(const string16& utf16) {
return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease(
return (NSString*)butil::mac::CFTypeRefToNSObjectAutorelease(
SysUTF16ToCFStringRef(utf16));
}
......@@ -183,4 +183,4 @@ string16 SysNSStringToUTF16(NSString* nsstring) {
return SysCFStringRefToUTF16(reinterpret_cast<CFStringRef>(nsstring));
}
} // namespace base
} // namespace butil
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/platform_thread.h"
#include "butil/threading/platform_thread.h"
#import <Foundation/Foundation.h>
#include <mach/mach.h>
......@@ -12,13 +12,12 @@
#include <algorithm>
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/threading/thread_id_name_manager.h"
#include "base/tracked_objects.h"
#include "butil/lazy_instance.h"
#include "butil/logging.h"
#include "butil/threading/thread_id_name_manager.h"
//#include "butil/tracked_objects.h"
namespace base {
namespace butil {
// If Cocoa is to be used on more than one thread, it must know that the
// application is multithreaded. Since it's possible to enter Cocoa code
......@@ -44,7 +43,8 @@ void InitThreading() {
// static
void PlatformThread::SetName(const char* name) {
ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
tracked_objects::ThreadData::InitializeThreadContext(name);
// TODO: add tracked_objects related headers
//tracked_objects::ThreadData::InitializeThreadContext(name);
// Mac OS X does not expose the length limit of the name, so
// hardcode it.
......@@ -69,7 +69,7 @@ void SetPriorityNormal(mach_port_t mach_thread_id) {
THREAD_STANDARD_POLICY_COUNT);
if (result != KERN_SUCCESS)
MACH_DVLOG(1, result) << "thread_policy_set";
DVLOG(1) << "Fail to call thread_policy_set";
}
// Enables time-contraint policy and priority suitable for low-latency,
......@@ -91,7 +91,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
reinterpret_cast<thread_policy_t>(&policy),
THREAD_EXTENDED_POLICY_COUNT);
if (result != KERN_SUCCESS) {
MACH_DVLOG(1, result) << "thread_policy_set";
DVLOG(1) << "Fail to call thread_policy_set";
return;
}
......@@ -103,7 +103,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
reinterpret_cast<thread_policy_t>(&precedence),
THREAD_PRECEDENCE_POLICY_COUNT);
if (result != KERN_SUCCESS) {
MACH_DVLOG(1, result) << "thread_policy_set";
DVLOG(1) << "Fail to call thread_policy_set";
return;
}
......@@ -147,7 +147,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
THREAD_TIME_CONSTRAINT_POLICY,
reinterpret_cast<thread_policy_t>(&time_constraints),
THREAD_TIME_CONSTRAINT_POLICY_COUNT);
MACH_DVLOG_IF(1, result != KERN_SUCCESS, result) << "thread_policy_set";
DVLOG_IF(1, result != KERN_SUCCESS) << "Fail to call thread_policy_set";
return;
}
......@@ -214,4 +214,4 @@ void InitOnThread() {
void TerminateOnThread() {
}
} // namespace base
} // namespace butil
......@@ -15,7 +15,6 @@
#include "butil/basictypes.h"
#include "butil/logging.h"
#include "butil/mac/mach_logging.h"
#include "butil/mac/scoped_cftyperef.h"
#include "butil/mac/scoped_mach_port.h"
......@@ -47,7 +46,7 @@ uint64_t ComputeCurrentTicks() {
// whether mach_timebase_info has already been called. This is
// recommended by Apple's QA1398.
kern_return_t kr = mach_timebase_info(&timebase_info);
MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info";
DCHECK(kr == KERN_SUCCESS) << "Fail to call mach_timebase_info";
}
// mach_absolute_time is it when it comes to ticks on the Mac. Other calls
......@@ -82,11 +81,11 @@ uint64_t ComputeThreadTicks() {
}
kern_return_t kr = thread_info(
thread,
thread.get(),
THREAD_BASIC_INFO,
reinterpret_cast<thread_info_t>(&thread_info_data),
&thread_info_count);
MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
DCHECK(kr == KERN_SUCCESS) << "Fail to call thread_info";
return (thread_info_data.user_time.seconds *
butil::Time::kMicrosecondsPerSecond) +
......
......@@ -236,7 +236,7 @@ int main(int argc, char* argv[]) {
tids.resize(FLAGS_thread_num);
if (!FLAGS_use_bthread) {
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (pthread_create(&tids[i], NULL, replay_thread, &chan_group) != 0) {
if (pthread_create((pthread_t*)&tids[i], NULL, replay_thread, &chan_group) != 0) {
LOG(ERROR) << "Fail to create pthread";
return -1;
}
......@@ -263,7 +263,7 @@ int main(int argc, char* argv[]) {
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (!FLAGS_use_bthread) {
pthread_join(tids[i], NULL);
pthread_join((pthread_t)tids[i], NULL);
} else {
bthread_join(tids[i], NULL);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment