File: vm_sys_posix.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (154 lines) | stat: -rw-r--r-- 3,164 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
   FALCON - The Falcon Programming Language.
   FILE: vm_sys_posix.cpp

   System specifics for the falcon VM - POSIX compliant systems.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Fri, 25 Apr 2008 17:30:00 +0200

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/


#include <falcon/vm_sys.h>
#include <falcon/vm_sys_posix.h>
#include <falcon/memory.h>
#include <falcon/signals.h>
#include <unistd.h>
#include <poll.h>
#include <stdio.h>

// if poll.h does not define POLLIN, then it's in stropts.
#ifndef POLLIN
   #include <stropts.h>
#endif
#include <errno.h>

namespace Falcon {
namespace Sys {

SystemData::SystemData(VMachine *vm)
{
   m_sysData = (struct VM_SYS_DATA*) memAlloc( sizeof( struct VM_SYS_DATA ) );

   m_vm = vm;
   m_sysData->isSignalTarget = false;

   // create the dup'd suspend pipe.
   if( pipe( m_sysData->interruptPipe ) != 0 )
   {
      printf( "Falcon: fatal allocation error in creating pipe at %s:%d\n", __FILE__, __LINE__ );
      exit(1);
   }
}


SystemData::~SystemData()
{
   // close the pipe
   close( m_sysData->interruptPipe[0] );
   close( m_sysData->interruptPipe[1] );

   // delete the structure
   memFree( m_sysData );
}


bool SystemData::interrupted() const
{
   int res;
   struct pollfd fds[1];
   fds[0].events = POLLIN;
   fds[0].fd = m_sysData->interruptPipe[0];

   while( (res = poll( fds, 1, 0 ) ) == EAGAIN );
   // If we have one event, the we have to read...
   return res == 1;
}


void SystemData::interrupt()
{
   if( write( m_sysData->interruptPipe[1], "0", 1 ) != 1 )
   {
      printf( "Falcon: fatal error in writing to the interrupt pipe at %s:%d\n", __FILE__, __LINE__ );
      exit(1);
   }

}

void SystemData::resetInterrupt()
{
   int res;
   struct pollfd fds[1];
   fds[0].events = POLLIN;
   fds[0].fd = m_sysData->interruptPipe[0];

   do {
      res = poll( fds, 1, 0 );
      if( res == 1 )
      {
         char dt;
         if( read( m_sysData->interruptPipe[0], &dt, 1 ) < 0 )
         {
            printf( "Falcon: fatal error in reading from the interrupt pipe at %s:%d\n", __FILE__, __LINE__ );
            exit(1);
         }
         continue;
      }

   } while( res == EAGAIN );

}


bool SystemData::sleep( numeric seconds ) const
{
   int ms = (int) (seconds * 1000.0);
   struct pollfd fds[1];
   fds[0].events = POLLIN;
   fds[0].fd = m_sysData->interruptPipe[0];

   int res;
   while( ( res = poll( fds, 1, ms )) == EAGAIN );

   // if res is 0, then we completed the wait.
   return res == 0;
}


const char *SystemData::getSystemType()
{
   return "POSIX";
}


bool SystemData::becomeSignalTarget()
{
   if ( 0 != signalReceiver )
      return false;

   m_sysData->isSignalTarget = true;
   signalReceiver = new SignalReceiver(m_vm);
   signalReceiver->start();

   return true;
}


void SystemData::earlyCleanup()
{
   if ( m_sysData->isSignalTarget ) {
      delete signalReceiver;
      signalReceiver = 0;
   }
}

}
}

/* end of vm_sys_posix.cpp */