Panda3D
contextSwitch.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file contextSwitch.h
10  * @author drose
11  * @date 2007-06-21
12  */
13 
14 #ifndef CONTEXTSWITCH_H
15 #define CONTEXTSWITCH_H
16 
17 #include "pandabase.h"
18 #include "selectThreadImpl.h"
19 
20 /* This file defines the code to perform fundamental context switches
21  between different threads of execution within user space code. It
22  does this by saving and restoring the register state, including
23  transferring to a new stack. */
24 
25 /* The code in this file is all written in C code, rather than C++, to
26  reduce possible conflicts from longjmp implementations that attempt
27  to be smart with respect to C++ destructors and exception
28  handling. */
29 
30 #ifdef THREAD_SIMPLE_IMPL
31 
32 struct ThreadContext;
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 typedef void ContextFunction(struct ThreadContext *from_context, void *);
39 typedef void ThreadFunction(void *);
40 
41 extern const int needs_stack_prealloc;
42 extern const int is_os_threads;
43 
44 /* Call this to fill in the appropriate values in context. If
45  needs_stack_prealloc (above) is true, the stack must already have
46  been allocated; if needs_stack_prealloc is false, the stack pointer
47  is not used and may be NULL. The context will be initialized so
48  that when switch_to_thread_context() is called, it will begin
49  executing thread_func(data), which should not return. This
50  function will return normally. */
51 void init_thread_context(struct ThreadContext *context,
52  unsigned char *stack, size_t stack_size,
53  ThreadFunction *thread_func, void *data);
54 
55 /* Call this to save the current thread context. This function does
56  not return until switch_to_thread_context() is called. Instead it
57  immediately calls next_context(data), which should not return. */
58 void save_thread_context(struct ThreadContext *context,
59  ContextFunction *next_context, void *data);
60 
61 /* Call this to resume executing a previously saved context.
62  from_context must be the currently-executing context, and
63  to_context is the context to resume. When called, it will return
64  from save_thread_context() in the saved stack (or begin executing
65  thread_func()). */
66 void switch_to_thread_context(struct ThreadContext *from_context,
67  struct ThreadContext *to_context);
68 
69 /* Use this pair of functions to transparently allocate and destroy an
70  opaque ThreadContext object of the appropriate size. These
71  functions allocate memory, and initialize the context as
72  appropriate for the main thread. See init_main_context() to finish
73  the initialization for a new thread. */
74 struct ThreadContext *alloc_thread_context();
75 void free_thread_context(struct ThreadContext *context);
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
81 #endif /* THREAD_SIMPLE_IMPL */
82 
83 #endif /* CONTEXTSWITCH_H */
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.