Skip to content

Commit e967d7f

Browse files
committed
ucontext facility can be enforced by version=ucontext_Posix
Available only on Posix platforms. Useful for testing purposes.
1 parent d3ce2ff commit e967d7f

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

druntime/src/core/thread/fiber/package.d

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ package
153153
version (AsmX86_64_Posix) {} else
154154
version (AsmExternal) {} else
155155
{
156-
// NOTE: The ucontext implementation requires architecture specific
157-
// data definitions to operate so testing for it must be done
158-
// by checking for the existence of ucontext_t rather than by
159-
// a version identifier. Please note that this is considered
160-
// an obsolescent feature according to the POSIX spec, so a
161-
// custom solution is still preferred.
162-
import core.sys.posix.ucontext : getcontext, makecontext, MINSIGSTKSZ, swapcontext, ucontext_t;
156+
version = ucontext_Posix;
163157
}
164158
}
159+
160+
version (ucontext_Posix)
161+
{
162+
// NOTE: Please note that ucontext is considered an obsolescent
163+
// feature according to the POSIX spec, so a custom
164+
// solution is still preferred.
165+
import core.sys.posix.ucontext : getcontext, makecontext, MINSIGSTKSZ, swapcontext, ucontext_t;
166+
}
165167
}
166168

167169
///////////////////////////////////////////////////////////////////////////////
@@ -188,7 +190,16 @@ package
188190
// default stack created by Fiber.initStack or the initial
189191
// switch into a new context will fail.
190192

191-
version (AsmX86_Windows)
193+
version (ucontext_Posix)
194+
{
195+
Fiber cfib = Fiber.getThis();
196+
void* ucur = cfib.m_ucur;
197+
198+
*oldp = &ucur;
199+
swapcontext( **(cast(ucontext_t***) oldp),
200+
*(cast(ucontext_t**) newp) );
201+
}
202+
else version (AsmX86_Windows)
192203
{
193204
asm pure nothrow @nogc
194205
{
@@ -363,15 +374,6 @@ package
363374
jmp RCX;
364375
}
365376
}
366-
else static if ( __traits( compiles, ucontext_t ) )
367-
{
368-
Fiber cfib = Fiber.getThis();
369-
void* ucur = cfib.m_ucur;
370-
371-
*oldp = &ucur;
372-
swapcontext( **(cast(ucontext_t***) oldp),
373-
*(cast(ucontext_t**) newp) );
374-
}
375377
else
376378
static assert(0, "Not implemented");
377379
}
@@ -866,7 +868,17 @@ protected:
866868
}
867869
}
868870

869-
version (AsmX86_Windows)
871+
version (ucontext_Posix)
872+
{
873+
getcontext( &m_utxt );
874+
m_utxt.uc_stack.ss_sp = m_pmem;
875+
m_utxt.uc_stack.ss_size = m_size;
876+
makecontext( &m_utxt, &fiber_entryPoint, 0 );
877+
// NOTE: If ucontext is being used then the top of the stack will
878+
// be a pointer to the ucontext_t struct for that fiber.
879+
push( cast(size_t) &m_utxt );
880+
}
881+
else version (AsmX86_Windows)
870882
{
871883
version (StackGrowsDown) {} else static assert( false );
872884

@@ -1232,16 +1244,6 @@ protected:
12321244
*/
12331245
pstack += int.sizeof * 1;
12341246
}
1235-
else static if ( __traits( compiles, ucontext_t ) )
1236-
{
1237-
getcontext( &m_utxt );
1238-
m_utxt.uc_stack.ss_sp = m_pmem;
1239-
m_utxt.uc_stack.ss_size = m_size;
1240-
makecontext( &m_utxt, &fiber_entryPoint, 0 );
1241-
// NOTE: If ucontext is being used then the top of the stack will
1242-
// be a pointer to the ucontext_t struct for that fiber.
1243-
push( cast(size_t) &m_utxt );
1244-
}
12451247
else
12461248
static assert(0, "Not implemented");
12471249
}

0 commit comments

Comments
 (0)