Support float and function ptr args better, -W warning fixes.
[wine/wine64.git] / scheduler / fiber.c
blob7a816ce82e5150f8e1ca290cb72f03605f34de49
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 <setjmp.h>
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "wine/exception.h"
29 #include "thread.h"
31 struct fiber_data
33 LPVOID param; /* 00 fiber param */
34 void *except; /* 04 saved exception handlers list */
35 void *stack_top; /* 08 top of fiber stack */
36 void *stack_low; /* 0c fiber stack low-water mark */
37 void *stack_base; /* 10 base of the fiber stack */
38 jmp_buf jmpbuf; /* 14 setjmp buffer (on Windows: CONTEXT) */
39 DWORD flags; /* fiber flags */
40 LPFIBER_START_ROUTINE start; /* start routine */
44 /* call the fiber initial function once we have switched stack */
45 static void start_fiber( void *arg )
47 struct fiber_data *fiber = arg;
48 LPFIBER_START_ROUTINE start = fiber->start;
50 __TRY
52 fiber->start = NULL;
53 start( fiber->param );
54 ExitThread( 1 );
56 __EXCEPT(UnhandledExceptionFilter)
58 TerminateThread( GetCurrentThread(), GetExceptionCode() );
60 __ENDTRY
64 /***********************************************************************
65 * CreateFiber (KERNEL32.@)
67 LPVOID WINAPI CreateFiber( SIZE_T stack, LPFIBER_START_ROUTINE start, LPVOID param )
69 return CreateFiberEx( stack, 0, 0, start, param );
73 /***********************************************************************
74 * CreateFiberEx (KERNEL32.@)
76 LPVOID WINAPI CreateFiberEx( SIZE_T stack_commit, SIZE_T stack_reserve, DWORD flags,
77 LPFIBER_START_ROUTINE start, LPVOID param )
79 struct fiber_data *fiber;
81 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
83 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
84 return NULL;
87 /* FIXME: should use the thread stack allocation routines here */
88 if (!stack_reserve) stack_reserve = 1024*1024;
89 if(!(fiber->stack_base = VirtualAlloc( 0, stack_reserve, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
91 HeapFree( GetProcessHeap(), 0, fiber );
92 return NULL;
94 fiber->stack_top = (char *)fiber->stack_base + stack_reserve;
95 fiber->stack_low = fiber->stack_base;
96 fiber->param = param;
97 fiber->except = (void *)-1;
98 fiber->start = start;
99 fiber->flags = flags;
100 return fiber;
104 /***********************************************************************
105 * DeleteFiber (KERNEL32.@)
107 void WINAPI DeleteFiber( LPVOID fiber_ptr )
109 struct fiber_data *fiber = fiber_ptr;
111 if (!fiber) return;
112 if (fiber == NtCurrentTeb()->fiber)
114 HeapFree( GetProcessHeap(), 0, fiber );
115 ExitThread(1);
117 VirtualFree( fiber->stack_base, 0, MEM_RELEASE );
118 HeapFree( GetProcessHeap(), 0, fiber );
122 /***********************************************************************
123 * ConvertThreadToFiber (KERNEL32.@)
125 LPVOID WINAPI ConvertThreadToFiber( LPVOID param )
127 return ConvertThreadToFiberEx( param, 0 );
131 /***********************************************************************
132 * ConvertThreadToFiberEx (KERNEL32.@)
134 LPVOID WINAPI ConvertThreadToFiberEx( LPVOID param, DWORD flags )
136 struct fiber_data *fiber;
138 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
140 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
141 return NULL;
143 fiber->param = param;
144 fiber->except = NtCurrentTeb()->except;
145 fiber->stack_top = NtCurrentTeb()->stack_top;
146 fiber->stack_low = NtCurrentTeb()->stack_low;
147 fiber->stack_base = NtCurrentTeb()->stack_base;
148 fiber->start = NULL;
149 fiber->flags = flags;
150 NtCurrentTeb()->fiber = fiber;
151 return fiber;
155 /***********************************************************************
156 * ConvertFiberToThread (KERNEL32.@)
158 BOOL WINAPI ConvertFiberToThread(void)
160 struct fiber_data *fiber = NtCurrentTeb()->fiber;
162 if (fiber)
164 NtCurrentTeb()->fiber = NULL;
165 HeapFree( GetProcessHeap(), 0, fiber );
167 return TRUE;
171 /***********************************************************************
172 * SwitchToFiber (KERNEL32.@)
174 void WINAPI SwitchToFiber( LPVOID fiber )
176 struct fiber_data *new_fiber = fiber;
177 struct fiber_data *current_fiber = NtCurrentTeb()->fiber;
179 current_fiber->except = NtCurrentTeb()->except;
180 current_fiber->stack_low = NtCurrentTeb()->stack_low;
181 /* stack_base and stack_top never change */
183 /* FIXME: should save floating point context if requested in fiber->flags */
184 if (!setjmp( current_fiber->jmpbuf ))
186 NtCurrentTeb()->fiber = new_fiber;
187 NtCurrentTeb()->except = new_fiber->except;
188 NtCurrentTeb()->stack_top = new_fiber->stack_top;
189 NtCurrentTeb()->stack_low = new_fiber->stack_low;
190 NtCurrentTeb()->stack_base = new_fiber->stack_base;
191 if (new_fiber->start) /* first time */
192 SYSDEPS_SwitchToThreadStack( start_fiber, new_fiber );
193 else
194 longjmp( new_fiber->jmpbuf, 1 );