Panda3D
|
00001 /* Filename: contextSwitch.h 00002 * Created by: drose (21Jun07) 00003 * 00004 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00005 * 00006 * PANDA 3D SOFTWARE 00007 * Copyright (c) Carnegie Mellon University. All rights reserved. 00008 * 00009 * All use of this software is subject to the terms of the revised BSD 00010 * license. You should have received a copy of this license along 00011 * with this source code in a file named "LICENSE." 00012 * 00013 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00014 00015 #ifndef CONTEXTSWITCH_H 00016 #define CONTEXTSWITCH_H 00017 00018 #include "pandabase.h" 00019 #include "selectThreadImpl.h" 00020 00021 /* This file defines the code to perform fundamental context switches 00022 between different threads of execution within user space code. It 00023 does this by saving and restoring the register state, including 00024 transferring to a new stack. */ 00025 00026 /* The code in this file is all written in C code, rather than C++, to 00027 reduce possible conflicts from longjmp implementations that attempt 00028 to be smart with respect to C++ destructors and exception 00029 handling. */ 00030 00031 #ifdef THREAD_SIMPLE_IMPL 00032 00033 struct ThreadContext; 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 typedef void ContextFunction(struct ThreadContext *from_context, void *); 00040 typedef void ThreadFunction(void *); 00041 00042 extern const int needs_stack_prealloc; 00043 extern const int is_os_threads; 00044 00045 /* Call this to fill in the appropriate values in context. If 00046 needs_stack_prealloc (above) is true, the stack must already have 00047 been allocated; if needs_stack_prealloc is false, the stack pointer 00048 is not used and may be NULL. The context will be initialized so 00049 that when switch_to_thread_context() is called, it will begin 00050 executing thread_func(data), which should not return. This 00051 function will return normally. */ 00052 void init_thread_context(struct ThreadContext *context, 00053 unsigned char *stack, size_t stack_size, 00054 ThreadFunction *thread_func, void *data); 00055 00056 /* Call this to save the current thread context. This function does 00057 not return until switch_to_thread_context() is called. Instead it 00058 immediately calls next_context(data), which should not return. */ 00059 void save_thread_context(struct ThreadContext *context, 00060 ContextFunction *next_context, void *data); 00061 00062 /* Call this to resume executing a previously saved context. 00063 from_context must be the currently-executing context, and 00064 to_context is the context to resume. When called, it will return 00065 from save_thread_context() in the saved stack (or begin executing 00066 thread_func()). */ 00067 void switch_to_thread_context(struct ThreadContext *from_context, 00068 struct ThreadContext *to_context); 00069 00070 /* Use this pair of functions to transparently allocate and destroy an 00071 opaque ThreadContext object of the appropriate size. These 00072 functions allocate memory, and initialize the context as 00073 appropriate for the main thread. See init_main_context() to finish 00074 the initialization for a new thread. */ 00075 struct ThreadContext *alloc_thread_context(); 00076 void free_thread_context(struct ThreadContext *context); 00077 00078 #ifdef __cplusplus 00079 } 00080 #endif 00081 00082 #endif /* THREAD_SIMPLE_IMPL */ 00083 00084 #endif /* CONTEXTSWITCH_H */ 00085