Skip to content

Commit

Permalink
Support __thread keyword when compiling with Clang on OS X.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Jul 20, 2012
1 parent 3aa07ec commit 81101ee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
28 changes: 3 additions & 25 deletions ext/oxt/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OXT - OS eXtensions for boosT
* Provides important functionality necessary for writing robust server software.
*
* Copyright (c) 2010 Phusion
* Copyright (c) 2010, 2011, 2012 Phusion
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -29,6 +29,7 @@
#include <sstream>
#include <cstring>
#include "backtrace.hpp"
#include "macros.hpp"

namespace oxt {

Expand All @@ -54,31 +55,8 @@ namespace {
/*
* boost::thread_specific_storage is pretty expensive. So we use the __thread
* keyword whenever possible - that's almost free.
* GCC supports the __thread keyword on x86 since version 3.3, but versions earlier
* than 4.1.2 have bugs (http://gcc.gnu.org/ml/gcc-bugs/2006-09/msg02275.html).
*/

#ifndef OXT_GCC_VERSION
#define OXT_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#endif

/*
* FreeBSD 5 supports the __thread keyword, and everything works fine in
* micro-tests, but in mod_passenger the thread-local variables are initialized
* to unaligned addresses for some weird reason, thereby causing bus errors.
*
* GCC on OpenBSD supports __thread, but any access to such a variable
* results in a segfault.
*
* Solaris does support __thread, but often it's not compiled into default GCC
* packages (not to mention it's not available for Sparc). Playing it safe...
*
* MacOS X doesn't support __thread at all.
*/
#if OXT_GCC_VERSION >= 40102 && !defined(__FreeBSD__) && \
!defined(__SOLARIS__) && !defined(__OpenBSD__) && !defined(__APPLE__)
#ifdef OXT_THREAD_LOCAL_KEYWORD_SUPPORTED
static __thread backtrace_data *thread_specific_backtrace_data;

void
Expand Down
33 changes: 32 additions & 1 deletion ext/oxt/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OXT - OS eXtensions for boosT
* Provides important functionality necessary for writing robust server software.
*
* Copyright (c) 2010 Phusion
* Copyright (c) 2010, 2011, 2012 Phusion
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,6 +32,10 @@
* so often by application programmers.
*/

#define OXT_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)

#if (defined(__GNUC__) && (__GNUC__ > 2) && !defined(OXT_DEBUG)) || defined(IN_DOXYGEN)
/**
* Indicate that the given expression is likely to be true.
Expand All @@ -55,4 +59,31 @@
#define OXT_UNLIKELY(expr) expr
#endif

/*
* GCC supports the __thread keyword on x86 since version 3.3, but versions earlier
* than 4.1.2 have bugs (http://gcc.gnu.org/ml/gcc-bugs/2006-09/msg02275.html).
*
* FreeBSD 5 supports the __thread keyword, and everything works fine in
* micro-tests, but in mod_passenger the thread-local variables are initialized
* to unaligned addresses for some weird reason, thereby causing bus errors.
*
* GCC on OpenBSD supports __thread, but any access to such a variable
* results in a segfault.
*
* Solaris does support __thread, but often it's not compiled into default GCC
* packages (not to mention it's not available for Sparc). Playing it safe...
*
* On MacOS X, neither gcc nor llvm-gcc support the __thread keyword, but Clang
* does. It works on at least clang >= 3.0.
*/
#if defined(__APPLE__)
#if defined(__clang__) && __clang_major__ >= 3
#define OXT_THREAD_LOCAL_KEYWORD_SUPPORTED
#endif
#elif defined(__GNUC__) && OXT_GCC_VERSION >= 40102
#if !defined(__SOLARIS__) && !defined(__OpenBSD__)
#define OXT_THREAD_LOCAL_KEYWORD_SUPPORTED
#endif
#endif

#endif /* _OXT_MACROS_HPP_ */
8 changes: 2 additions & 6 deletions ext/oxt/spin_lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef _OXT_SPIN_LOCK_HPP_
#define _OXT_SPIN_LOCK_HPP_

#include "macros.hpp"

// At the time of writing (July 22, 2008), these operating systems don't
// support pthread spin locks:
// - OpenBSD 4.3
Expand All @@ -34,12 +36,6 @@
#define OXT_NO_PTHREAD_SPINLOCKS
#endif

#ifndef OXT_GCC_VERSION
#define OXT_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCH_LEVEL__)
#endif

#if defined(__APPLE__)
#include "detail/spin_lock_darwin.hpp"
#elif (OXT_GCC_VERSION > 40100 && (defined(__i386__) || defined(__x86_64__))) || defined(IN_DOXYGEN)
Expand Down

0 comments on commit 81101ee

Please sign in to comment.