Skip to content

Commit

Permalink
Implement thread local storage for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeryKopylov committed Apr 16, 2015
1 parent 7262617 commit 25dbb11
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
11 changes: 11 additions & 0 deletions Rx/v2/src/rxcpp/rx-includes.hpp
Expand Up @@ -86,6 +86,13 @@
#define _VARIADIC_MAX 10
#endif

#if defined(__APPLE__) && defined(__MACH__)
#include <TargetConditionals.h>
#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
#define RXCPP_ON_IOS
#endif
#endif

#pragma push_macro("min")
#pragma push_macro("max")
#undef min
Expand Down Expand Up @@ -120,6 +127,10 @@
#include <typeinfo>
#include <tuple>

#if defined(RXCPP_ON_IOS)
#include <pthread.h>
#endif

#include "rx-util.hpp"
#include "rx-predef.hpp"
#include "rx-subscription.hpp"
Expand Down
43 changes: 42 additions & 1 deletion Rx/v2/src/rxcpp/rx-util.hpp
Expand Up @@ -7,7 +7,7 @@

#include "rx-includes.hpp"

#if !defined(RXCPP_THREAD_LOCAL)
#if !defined(RXCPP_ON_IOS) && !defined(RXCPP_THREAD_LOCAL)
#if defined(_MSC_VER)
#define RXCPP_THREAD_LOCAL __declspec(thread)
#else
Expand Down Expand Up @@ -634,6 +634,47 @@ class unwinder

}

#if !defined(RXCPP_THREAD_LOCAL)
template<typename T>
class thread_local_storage
{
private:
pthread_key_t key;

public:
thread_local_storage()
{
pthread_key_create(&key, NULL);
}

~thread_local_storage()
{
pthread_key_delete(key);
}

thread_local_storage& operator =(T* p)
{
pthread_setspecific(key, p);
return *this;
}

bool operator !()
{
return pthread_getspecific(key) == NULL;
}

T* operator ->()
{
return static_cast<T*>(pthread_getspecific(key));
}

T* get()
{
return static_cast<T*>(pthread_getspecific(key));
}
};
#endif

}
namespace rxu=util;

Expand Down
22 changes: 16 additions & 6 deletions Rx/v2/src/rxcpp/schedulers/rx-currentthread.hpp
Expand Up @@ -31,10 +31,17 @@ struct action_queue
};

private:
static current_thread_queue_type*& current_thread_queue() {
static RXCPP_THREAD_LOCAL current_thread_queue_type* queue;
#if defined(RXCPP_THREAD_LOCAL)
static current_thread_queue_type*& current_thread_queue() {
static RXCPP_THREAD_LOCAL current_thread_queue_type* queue;
return queue;
}
#else
static rxu::thread_local_storage<current_thread_queue_type>& current_thread_queue() {
static rxu::thread_local_storage<current_thread_queue_type> queue;
return queue;
}
#endif

public:

Expand All @@ -60,7 +67,7 @@ struct action_queue
return current_thread_queue()->queue.top();
}
static void pop() {
auto state = current_thread_queue();
auto& state = current_thread_queue();
if (!state) {
abort();
}
Expand All @@ -71,7 +78,7 @@ struct action_queue
}
}
static void push(item_type item) {
auto state = current_thread_queue();
auto& state = current_thread_queue();
if (!state) {
abort();
}
Expand Down Expand Up @@ -110,12 +117,15 @@ struct action_queue
if (!current_thread_queue()) {
abort();
}
destroy(current_thread_queue());
#if defined(RXCPP_THREAD_LOCAL)
destroy(current_thread_queue());
#else
destroy(current_thread_queue().get());
#endif
current_thread_queue() = nullptr;
}
};


}

struct current_thread : public scheduler_interface
Expand Down

0 comments on commit 25dbb11

Please sign in to comment.