show version
[tridge/junkcode.git] / critical.c
1 volatile int count1=0;
2 volatile int count2=0;
3
4 volatile int in_critical_section = 0;
5
6 void sig_handler()
7 {
8   if (in_critical_section) {
9     count1++;
10   } else {
11     do_signal_work();
12   }
13 }
14
15
16 void mainline(void)
17 {
18   in_critical_section = 1;
19
20   do_normal_work();
21
22   while (count1 != count2) {
23     do_signal_work();
24     count2++;
25   }
26   in_critical_section = 0;
27
28   while (count1 != count2) {
29     /* here we have to block signals, but we expect it almost never to
30      * happen.  it will only happen if a signal arrives between the
31      * end of the first while loop and the unsetting of the
32      * in_critical_section flag, which should be so rare that the
33      * performance hit will be tiny
34      */
35     block_signals();
36     do_signal_work();
37     count2++;
38     unblock_signals();
39   }
40 }