Skip to content

Commit

Permalink
Fix OpenBSD build. (#4108)
Browse files Browse the repository at this point in the history
Also in case fiber context stack creation via mmap is enabled, adding MAP_STACK flag for this platform.
  • Loading branch information
devnexen authored Mar 22, 2021
1 parent 823e1a5 commit 1e5a17c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ if test "$PHP_SWOOLE" != "no"; then
AC_CHECK_LIB(pthread, pthread_spin_lock, AC_DEFINE(HAVE_SPINLOCK, 1, [have pthread_spin_lock]))
AC_CHECK_LIB(pthread, pthread_mutex_timedlock, AC_DEFINE(HAVE_MUTEX_TIMEDLOCK, 1, [have pthread_mutex_timedlock]))
AC_CHECK_LIB(pthread, pthread_barrier_init, AC_DEFINE(HAVE_PTHREAD_BARRIER, 1, [have pthread_barrier_init]))
AC_CHECK_LIB(pthread, pthread_mutexattr_setpshared, AC_DEFINE(HAVE_PTHREAD_MUTEXATTR_SETPSHARED, 1, [have pthread_mutexattr_setpshared]))
AC_CHECK_LIB(pthread, pthread_mutexattr_setrobust, AC_DEFINE(HAVE_PTHREAD_MUTEXATTR_SETROBUST, 1, [have pthread_mutexattr_setrobust]))
AC_CHECK_LIB(pthread, pthread_mutex_consistent, AC_DEFINE(HAVE_PTHREAD_MUTEX_CONSISTENT, 1, [have pthread_mutex_consistent]))
AC_CHECK_LIB(pcre, pcre_compile, AC_DEFINE(HAVE_PCRE, 1, [have pcre]))
Expand Down
17 changes: 11 additions & 6 deletions src/coroutine/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/

#include "swoole_coroutine_context.h"
#if __linux__
#ifdef SW_CONTEXT_PROTECT_STACK_PAGE
#include <sys/mman.h>
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#define MAP_ANONYMOUS MAP_ANON
#endif
#endif

#ifndef SW_USE_THREAD_CONTEXT

#define MAGIC_STRING "swoole_coroutine#5652a7fb2b38be"
#define START_OFFSET (64 * 1024)

#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#define MAP_ANONYMOUS MAP_ANON
#endif

namespace swoole {
namespace coroutine {

Expand All @@ -36,7 +35,13 @@ Context::Context(size_t stack_size, const coroutine_func_t &fn, void *private_da
end_ = false;

#ifdef SW_CONTEXT_PROTECT_STACK_PAGE
stack_ = (char *) ::mmap(0, stack_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
int mapflags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef __OpenBSD__
// no-op for Linux and NetBSD, not to enable on FreeBSD as the semantic differs.
// However necessary on OpenBSD.
mapflags |= MAP_STACK;
#endif
stack_ = (char *) ::mmap(0, stack_size_, PROT_READ | PROT_WRITE, mapflags, -1, 0);
#else
stack_ = (char *) sw_malloc(stack_size_);
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/lock/mutex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ Mutex::Mutex(int flags) : Lock() {
pthread_mutexattr_init(&impl->attr_);

if (flags & PROCESS_SHARED) {
#ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST
pthread_mutexattr_setpshared(&impl->attr_, PTHREAD_PROCESS_SHARED);
#else
swWarn("PTHREAD_MUTEX_PSHARED is not supported");
#endif
}

if (flags & ROBUST) {
Expand Down

0 comments on commit 1e5a17c

Please sign in to comment.