kernel32: Get rid of DOSMEM_AllocSelector.
[wine/multimedia.git] / dlls / kernel / fiber.c
blobc8e4068ac0aca5a580cae5c7e2df236bfaa3ce38
1 /*
2 * Fiber support
4 * Copyright 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FIXME:
21 * - proper handling of 16-bit stack and signal stack
24 #include "config.h"
25 #include "wine/port.h"
27 #include <setjmp.h>
28 #include <stdarg.h>
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winternl.h"
35 #include "wine/exception.h"
36 #include "wine/library.h"
38 struct fiber_data
40 LPVOID param; /* 00 fiber param */
41 void *except; /* 04 saved exception handlers list */
42 void *stack_base; /* 08 top of fiber stack */
43 void *stack_limit; /* 0c fiber stack low-water mark */
44 void *stack_allocation; /* 10 base of the fiber stack allocation */
45 sigjmp_buf jmpbuf; /* 14 setjmp buffer (on Windows: CONTEXT) */
46 DWORD flags; /* fiber flags */
47 LPFIBER_START_ROUTINE start; /* start routine */
51 /* call the fiber initial function once we have switched stack */
52 static void start_fiber( void *arg )
54 struct fiber_data *fiber = arg;
55 LPFIBER_START_ROUTINE start = fiber->start;
57 __TRY
59 fiber->start = NULL;
60 start( fiber->param );
61 ExitThread( 1 );
63 __EXCEPT(UnhandledExceptionFilter)
65 TerminateThread( GetCurrentThread(), GetExceptionCode() );
67 __ENDTRY
71 /***********************************************************************
72 * CreateFiber (KERNEL32.@)
74 LPVOID WINAPI CreateFiber( SIZE_T stack, LPFIBER_START_ROUTINE start, LPVOID param )
76 return CreateFiberEx( stack, 0, 0, start, param );
80 /***********************************************************************
81 * CreateFiberEx (KERNEL32.@)
83 LPVOID WINAPI CreateFiberEx( SIZE_T stack_commit, SIZE_T stack_reserve, DWORD flags,
84 LPFIBER_START_ROUTINE start, LPVOID param )
86 struct fiber_data *fiber;
88 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
90 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
91 return NULL;
94 /* FIXME: should use the thread stack allocation routines here */
95 if (!stack_reserve) stack_reserve = 1024*1024;
96 if(!(fiber->stack_allocation = VirtualAlloc( 0, stack_reserve, MEM_COMMIT, PAGE_READWRITE )))
98 HeapFree( GetProcessHeap(), 0, fiber );
99 return NULL;
101 fiber->stack_base = (char *)fiber->stack_allocation + stack_reserve;
102 fiber->stack_limit = fiber->stack_allocation;
103 fiber->param = param;
104 fiber->except = (void *)-1;
105 fiber->start = start;
106 fiber->flags = flags;
107 return fiber;
111 /***********************************************************************
112 * DeleteFiber (KERNEL32.@)
114 void WINAPI DeleteFiber( LPVOID fiber_ptr )
116 struct fiber_data *fiber = fiber_ptr;
118 if (!fiber) return;
119 if (fiber == NtCurrentTeb()->Tib.u.FiberData)
121 HeapFree( GetProcessHeap(), 0, fiber );
122 ExitThread(1);
124 VirtualFree( fiber->stack_allocation, 0, MEM_RELEASE );
125 HeapFree( GetProcessHeap(), 0, fiber );
129 /***********************************************************************
130 * ConvertThreadToFiber (KERNEL32.@)
132 LPVOID WINAPI ConvertThreadToFiber( LPVOID param )
134 return ConvertThreadToFiberEx( param, 0 );
138 /***********************************************************************
139 * ConvertThreadToFiberEx (KERNEL32.@)
141 LPVOID WINAPI ConvertThreadToFiberEx( LPVOID param, DWORD flags )
143 struct fiber_data *fiber;
145 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
147 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
148 return NULL;
150 fiber->param = param;
151 fiber->except = NtCurrentTeb()->Tib.ExceptionList;
152 fiber->stack_base = NtCurrentTeb()->Tib.StackBase;
153 fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
154 fiber->stack_allocation = NtCurrentTeb()->DeallocationStack;
155 fiber->start = NULL;
156 fiber->flags = flags;
157 NtCurrentTeb()->Tib.u.FiberData = fiber;
158 return fiber;
162 /***********************************************************************
163 * ConvertFiberToThread (KERNEL32.@)
165 BOOL WINAPI ConvertFiberToThread(void)
167 struct fiber_data *fiber = NtCurrentTeb()->Tib.u.FiberData;
169 if (fiber)
171 NtCurrentTeb()->Tib.u.FiberData = NULL;
172 HeapFree( GetProcessHeap(), 0, fiber );
174 return TRUE;
178 /***********************************************************************
179 * SwitchToFiber (KERNEL32.@)
181 void WINAPI SwitchToFiber( LPVOID fiber )
183 struct fiber_data *new_fiber = fiber;
184 struct fiber_data *current_fiber = NtCurrentTeb()->Tib.u.FiberData;
186 current_fiber->except = NtCurrentTeb()->Tib.ExceptionList;
187 current_fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
188 /* stack_allocation and stack_base never change */
190 /* FIXME: should save floating point context if requested in fiber->flags */
191 if (!sigsetjmp( current_fiber->jmpbuf, 1 ))
193 NtCurrentTeb()->Tib.u.FiberData = new_fiber;
194 NtCurrentTeb()->Tib.ExceptionList = new_fiber->except;
195 NtCurrentTeb()->Tib.StackBase = new_fiber->stack_base;
196 NtCurrentTeb()->Tib.StackLimit = new_fiber->stack_limit;
197 NtCurrentTeb()->DeallocationStack = new_fiber->stack_allocation;
198 if (new_fiber->start) /* first time */
199 wine_switch_to_stack( start_fiber, new_fiber, new_fiber->stack_base );
200 else
201 siglongjmp( new_fiber->jmpbuf, 1 );