]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_partikle/fosa_long_jump.c
446207474bac4d68b022a5d57c8ac6ef88f99ad3
[frescor/fosa.git] / src_partikle / fosa_long_jump.c
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FOSA (Frsh Operating System Abstraction)
32 //
33 // FOSA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FOSA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FOSA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 //fosa_long_jump.h
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      //
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65
66 #ifdef CONFIG_LONGJUMP
67
68 #include <fosa_long_jump.h>
69
70 enum {
71   LONGJMP_MAGIC = 0x01234567,
72   LONGJMP_NSIG = 3,
73   LONGJMP_FIRSTSIG = SIGRTMIN + 1,
74 };
75
76 int jmp_used_signals [LONGJMP_NSIG] = {[0 ... (LONGJMP_NSIG - 1)] = 0};
77
78
79 int fosa_long_jump_save_context
80    (fosa_long_jump_context_t * context)
81 {
82   if (!context)
83     return FOSA_EINVAL;
84
85   // Save the actual signal mask & mark the jump as still not performed
86   pthread_sigmask (SIG_SETMASK, NULL, &(context -> jmp_sigmask));
87   context -> jmp_hasexecuted  = false;
88
89   if (setjmp (context -> jmp_context) == LONGJMP_MAGIC) {
90 #ifndef CONFIG_LONGJUMP_FREE_SIGNALS
91     pthread_sigmask (SIG_SETMASK, &(context -> jmp_sigmask), NULL);
92 #endif 
93     context -> jmp_hasexecuted = true;
94   }
95   return 0;
96 }
97
98
99 int fosa_long_jump_was_performed
100     (const fosa_long_jump_context_t * context, int * jumped)
101 {
102   if (!context && !jumped)
103     return FOSA_EINVAL;
104
105   *jumped = context -> jmp_hasexecuted? 1:0;
106   return 0;
107 }
108
109
110 void jmp_handler (int signo, siginfo_t *info, void *context)
111 {
112   fosa_long_jump_context_t *jmp_info = (fosa_long_jump_context_t *) info -> si_value.sival_ptr;
113   sigset_t set;
114
115 #ifdef CONFIG_LONGJUMP_FREE_SIGNALS
116   // Restore the signal mask beofre the context save
117   pthread_sigmask (SIG_SETMASK, &(context -> jmp_sigmask), NULL);
118
119   // Free this signal
120   sigemptyset (&set);
121   sigaddset (&set, signo);
122   pthread_sigmask (SIG_BLOCK, &sigmask, NULL);
123
124   pthread_mutex_lock ();
125   jmp_used_signals [signo] = 0;
126   pthread_mutex_unlock ();
127 #endif
128
129   // Restore the saved context
130   longjmp (jmp_info -> jmp_context, LONGJMP_MAGIC);
131   return;
132 }
133
134
135 int fosa_long_jump_install_handler
136     (fosa_signal_t *signal, fosa_thread_id_t *handler)
137 {
138   int i;
139   struct sigaction sa;
140   sigset_t sigmask;
141
142   // Find the first usable signal
143   //  FIXME: Should be atomical
144   for (i = 0; i < LONGJMP_NSIG && jmp_used_signals [i]; i++);
145   if (i >= LONGJMP_NSIG)
146     return FOSA_ENOMEM;
147
148   jmp_used_signals [i] = 1;
149
150   *signal = LONGJMP_FIRSTSIG + i;
151   *handler = pthread_self ();
152
153   // intall the jump handler
154   sa.sa_flags = SA_SIGINFO;
155   sigfillset (&sa.sa_mask);
156   sa.sa_sigaction = jmp_handler;
157   sigaction (*signal, &sa, NULL);
158
159   // unmask the signal for the calling thread
160   sigemptyset (&sigmask);
161   sigaddset (&sigmask, *signal);
162   pthread_sigmask (SIG_UNBLOCK, &sigmask, NULL);
163
164   return 0;
165 }
166
167 #endif /* CONFIG_LONGJUMP */