00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #define ACEDIA_REDUCED_ANNOUNCE
00031
00032 #include "context.hpp"
00033 #include "acedia_atomic.hpp"
00034
00035 #ifdef ACEDIA_UCONTEXT_IMPL
00036
00037
00038
00039
00040
00041 #include "exceptions.hpp"
00042
00043 namespace acedia
00044 {
00045 namespace details
00046 {
00047 struct SNode
00048 {
00049 SNode *next;
00050 void *s;
00051 inline SNode() :
00052 next(NULL) , s(mmap(0, STACK_SIZE,
00053 PROT_EXEC | PROT_READ | PROT_WRITE,
00054 MAP_PRIVATE | MAP_ANON,
00055 -1, 0))
00056 {
00057 }
00058 inline ~SNode()
00059 {
00060 munmap(s, STACK_SIZE);
00061 }
00062 };
00063
00064 struct StackFactory
00065 {
00066
00067 static volatile SNode *head;
00068 static volatile boost::int32_t count;
00069
00070 static SNode *get()
00071 {
00072 for (;;)
00073 {
00074 SNode *result = const_cast<SNode*>(head);
00075 if (result)
00076 {
00077 if (atomic::compareAndSwap(&head, result, result->next))
00078 {
00079 (void) atomic::addAndFetch(&count, -1);
00080 return result;
00081 }
00082 }
00083 else return new SNode;
00084 }
00085 }
00086
00087 static void release(SNode *n)
00088 {
00089 if (atomic::addAndFetch(&count, 1) < 16)
00090 {
00091 for (;;)
00092 {
00093 n->next = const_cast<SNode*>(head);
00094 if (atomic::compareAndSwap(&head, n->next, n)) return;
00095 }
00096 }
00097 else
00098 {
00099 (void) atomic::addAndFetch(&count, -1);
00100 delete n;
00101 }
00102 }
00103
00104 };
00105
00106 }
00107
00108 }
00109
00110 volatile acedia::details::SNode *acedia::details::StackFactory::head = NULL;
00111 volatile boost::int32_t acedia::details::StackFactory::count = 0;
00112
00113 namespace acedia
00114 {
00115 void Context::init()
00116 {
00117 stack = reinterpret_cast<void*>(details::StackFactory::get());
00118 getcontext(&ctx);
00119 ctx.uc_stack.ss_sp = reinterpret_cast<details::SNode*>(stack)->s;
00120 ctx.uc_stack.ss_size = STACK_SIZE;
00121 ctx.uc_link = 0;
00122 }
00123
00124 Context::Context() throw() : stack(NULL)
00125 {
00126 getcontext(&ctx);
00127 }
00128
00129 Context::Context(void (*func)(void), int arg1) : stack(NULL)
00130 {
00131 init();
00132 makecontext(&ctx, func, 1, arg1);
00133 }
00134
00135 Context::~Context()
00136 {
00137 if (stack) details::StackFactory::release((details::SNode*) stack);
00138 }
00139
00140 }
00141
00142 #elif defined(ACEDIA_FIBER_IMPL)
00143
00144
00145
00146
00147
00148 #include <cassert>
00149
00150 namespace acedia
00151 {
00152 Context::Context() : fiber(NULL), isConvertedThread(true)
00153 {
00154 fiber = ConvertThreadToFiber(NULL);
00155 assert(fiber != NULL);
00156 }
00157
00158 Context::Context(LPFIBER_START_ROUTINE func, LPVOID arg1) :
00159 fiber(NULL), isConvertedThread(false)
00160 {
00161 fiber = CreateFiber(0, func, arg1);
00162 assert(fiber != NULL);
00163 }
00164
00165 Context::~Context()
00166 {
00167 if (fiber && !isConvertedThread) DeleteFiber(fiber);
00168 }
00169
00170 }
00171
00172 #endif