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