Skip to content

Commit 952d091

Browse files
author
Jason Kridner
committed
pollpri basic functionality kinda working
I ended up putting in a poll loop within a thread, rather than adding an event watcher to the main event loop. This will likely be notably slower, but I haven't figured out how to get the watcher to behave properly.
1 parent e0cf403 commit 952d091

3 files changed

Lines changed: 143 additions & 79 deletions

File tree

.settings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<settings version="0.0.4" ><auto><configurations debug="0" ><config path="blinkled.js" name="blinkled" extension="js" args="" /><config path="bone101.js" name="bone101" extension="js" args="" /><config path="footest.js" name="footest" extension="" args="" /><config path="input.js" name="input" extension="" args="" /></configurations><panel>{"ext/editors/editors":{"parent":{"visible":2,"flex":1},"visible":2,"flex":1},"ext/tree/tree":{"parent":{"visible":true,"width":"200"},"visible":true,"flex":1,"state":"normal"},"ext/console/console":{"parent":{"visible":2,"height":200},"visible":true,"flex":1},"ext/settings/settings":{"parent":{"visible":true,"width":"200"},"visible":false,"flex":1,"state":"normal"},"ext/openfiles/openfiles":{"parent":{"visible":true,"width":"200"},"visible":false,"flex":1,"state":"normal"}}</panel><tree_selection path="/workspace/bonescript/misctest.js" type="file"/><tree>["folder[1]","folder[1]/folder[2]"]</tree><console><input>this.subdir
1+
<settings version="0.0.4" ><auto><configurations debug="0" ><config path="blinkled.js" name="blinkled" extension="js" args="" /><config path="bone101.js" name="bone101" extension="js" args="" /><config path="footest.js" name="footest" extension="" args="" /><config path="input.js" name="input" extension="" args="" /></configurations><panel>{"ext/editors/editors":{"parent":{"visible":2,"flex":1},"visible":2,"flex":1},"ext/tree/tree":{"parent":{"visible":true,"width":"200"},"visible":true,"flex":1,"state":"normal"},"ext/console/console":{"parent":{"visible":2,"height":200},"visible":true,"flex":1},"ext/settings/settings":{"parent":{"visible":true,"width":"200"},"visible":false,"flex":1,"state":"normal"},"ext/openfiles/openfiles":{"parent":{"visible":true,"width":"200"},"visible":false,"flex":1,"state":"normal"}}</panel><tree_selection path="/workspace/polltest.c" type="file"/><tree>["folder[1]","folder[1]/folder[2]"]</tree><console><input>this.subdir
22

33

4-
</input></console><customtypes/><zen>0.7</zen><recentfiles>[{"caption":"misc.cpp","value":"/workspace/bonescript/misc.cpp"},{"caption":"misctest.js","value":"/workspace/bonescript/misctest.js"},{"caption":"epolltest.c","value":"/workspace/epolltest.c"},{"caption":"blinkled.js","value":"/workspace/blinkled.js"},{"caption":"input.js","value":"/workspace/input.js"},{"caption":"index.js","value":"/workspace/bonescript/index.js"},{"caption":"footest.js","value":"/workspace/footest.js"},{"caption":"polltest.c","value":"/workspace/polltest.c"},{"caption":"wscript","value":"/workspace/bonescript/wscript"},{"caption":"LICENSE","value":"/workspace/LICENSE"}]</recentfiles><extensions/><files active="/workspace/bonescript/misc.cpp"><file path="/workspace/bonescript/index.js" type="file" size="19586" name="index.js" contenttype="application/javascript; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/bonescript/index.js" state="{&quot;scrolltop&quot;:200,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:0,&quot;column&quot;:0},&quot;end&quot;:{&quot;row&quot;:0,&quot;column&quot;:0}}}"/><file path="/workspace/input.js" type="file" size="745" name="input.js" contenttype="application/javascript; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/input.js" state="{&quot;scrolltop&quot;:200,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:0,&quot;column&quot;:0},&quot;end&quot;:{&quot;row&quot;:0,&quot;column&quot;:0}}}"/><file path="/workspace/blinkled.js" type="file" size="364" name="blinkled.js" contenttype="application/javascript; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/blinkled.js"/><file path="/workspace/bonescript/misc.cpp" type="file" size="1372" name="misc.cpp" contenttype="text/x-c; charset=utf-8" modifieddate="Fri, 04 May 2012 17:24:24 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/bonescript/misc.cpp" changed="0" state="{&quot;scrolltop&quot;:200,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:24,&quot;column&quot;:34},&quot;end&quot;:{&quot;row&quot;:24,&quot;column&quot;:34}}}"/><file path="/workspace/epolltest.c" type="file" size="1012" name="epolltest.c" contenttype="text/x-c; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/epolltest.c" changed="0" state="{&quot;scrolltop&quot;:200,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:28,&quot;column&quot;:0},&quot;end&quot;:{&quot;row&quot;:29,&quot;column&quot;:0}}}"/><file type="file" name="misctest.js" path="/workspace/bonescript/misctest.js" scriptname="/var/lib/cloud9/bonescript/misctest.js" changed="0" modifieddate="Fri, 04 May 2012 17:10:17 +0100" state="{&quot;scrolltop&quot;:200,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:7,&quot;column&quot;:64},&quot;end&quot;:{&quot;row&quot;:7,&quot;column&quot;:64}}}"/></files></auto><general name="General" saveallbeforerun="true" openfiles="true"/><beautify name="Beautify"><jsbeautify name="JS Beautify" preserveemptylines="true" keeparrayindentation="false" braces="end-expand"/></beautify><editors name="Editor"><code name="Code Editor" overwrite="false" selectstyle="line" activeline="true" showinvisibles="false" showprintmargin="true" printmargincolumn="80" softtabs="true" tabsize="4" scrollspeed="2" fontsize="12" wrapmode="false" wraplimitmin="" wraplimitmax="" gutter="true" highlightselectedword="true" autohidehorscrollbar="true"/></editors><search><word key="attachInterrupt"/><word key="rx"/><word key="analogRead"/><word key="digitalWrite"/><word key="HIGH"/><word key="output"/><word key="socket"/><word key="readFile"/><word key="pinmode"/><word key="eeproms"/><word key="eeprom"/><word key="eeproms"/><word key="socket.emit"/><word key="muxOption"/><word key="ex"/><word key="export"/><word key="getpinmode"/><word key="omap_mux"/><word key="readout"/><word key="omap_mux"/><word key="muxstruct"/><word key="socket.emit"/><word key="mode"/><word key="socket"/><word key="spawn"/><word key="socket.on"/><word key="fibers"/><word key="pinObject"/><word key="pull"/><word key="pinmode"/><word key="events"/><word key="gpmc_ben1"/><word key="opensync"/></search><breakpoints /></settings>
4+
</input></console><customtypes/><zen>0.7</zen><recentfiles>[{"caption":"misc.cpp","value":"/workspace/bonescript/misc.cpp"},{"caption":"epolltest.c","value":"/workspace/epolltest.c"},{"caption":"misctest.js","value":"/workspace/bonescript/misctest.js"},{"caption":"polltest.c","value":"/workspace/polltest.c"},{"caption":"blinkled.js","value":"/workspace/blinkled.js"},{"caption":"input.js","value":"/workspace/input.js"},{"caption":"index.js","value":"/workspace/bonescript/index.js"},{"caption":"footest.js","value":"/workspace/footest.js"},{"caption":"wscript","value":"/workspace/bonescript/wscript"},{"caption":"LICENSE","value":"/workspace/LICENSE"}]</recentfiles><extensions/><files active="/workspace/bonescript/misc.cpp"><file path="/workspace/bonescript/index.js" type="file" size="19586" name="index.js" contenttype="application/javascript; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/bonescript/index.js" state="{&quot;scrolltop&quot;:1443,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:0,&quot;column&quot;:0},&quot;end&quot;:{&quot;row&quot;:0,&quot;column&quot;:0}}}"/><file path="/workspace/input.js" type="file" size="745" name="input.js" contenttype="application/javascript; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/input.js" state="{&quot;scrolltop&quot;:1443,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:0,&quot;column&quot;:0},&quot;end&quot;:{&quot;row&quot;:0,&quot;column&quot;:0}}}"/><file path="/workspace/blinkled.js" type="file" size="364" name="blinkled.js" contenttype="application/javascript; charset=utf-8" modifieddate="Fri, 04 May 2012 16:41:38 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/blinkled.js"/><file path="/workspace/bonescript/misc.cpp" type="file" size="1372" name="misc.cpp" contenttype="text/x-c; charset=utf-8" modifieddate="Fri, 04 May 2012 22:15:44 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/bonescript/misc.cpp" changed="0" state="{&quot;scrolltop&quot;:1443,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:123,&quot;column&quot;:8},&quot;end&quot;:{&quot;row&quot;:123,&quot;column&quot;:8}}}"/><file path="/workspace/epolltest.c" type="file" size="1012" name="epolltest.c" contenttype="text/x-c; charset=utf-8" modifieddate="Fri, 04 May 2012 22:08:59 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/epolltest.c" changed="0" state="{&quot;scrolltop&quot;:1443,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:28,&quot;column&quot;:24},&quot;end&quot;:{&quot;row&quot;:28,&quot;column&quot;:24}}}"/><file type="file" name="misctest.js" path="/workspace/bonescript/misctest.js" scriptname="/var/lib/cloud9/bonescript/misctest.js" changed="0" modifieddate="Fri, 04 May 2012 22:05:55 +0100" state="{&quot;scrolltop&quot;:1443,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:3,&quot;column&quot;:23},&quot;end&quot;:{&quot;row&quot;:3,&quot;column&quot;:23}}}"/><file path="/workspace/polltest.c" type="file" size="6849" name="polltest.c" contenttype="text/x-c; charset=utf-8" modifieddate="Thu, 03 May 2012 18:14:14 +0100" creationdate="" lockable="false" hidden="false" executable="false" scriptname="/var/lib/cloud9/polltest.c" changed="0" state="{&quot;scrolltop&quot;:1443,&quot;scrollleft&quot;:0,&quot;selection&quot;:{&quot;start&quot;:{&quot;row&quot;:262,&quot;column&quot;:3},&quot;end&quot;:{&quot;row&quot;:262,&quot;column&quot;:3}}}"/></files></auto><general name="General" saveallbeforerun="true" openfiles="true"/><beautify name="Beautify"><jsbeautify name="JS Beautify" preserveemptylines="true" keeparrayindentation="false" braces="end-expand"/></beautify><editors name="Editor"><code name="Code Editor" overwrite="false" selectstyle="line" activeline="true" showinvisibles="false" showprintmargin="true" printmargincolumn="80" softtabs="true" tabsize="4" scrollspeed="2" fontsize="12" wrapmode="false" wraplimitmin="" wraplimitmax="" gutter="true" highlightselectedword="true" autohidehorscrollbar="true"/></editors><search><word key="epoll_ctl"/><word key="Pollpri"/><word key="pollpri_event"/><word key="sleep"/><word key="event"/><word key="attachInterrupt"/><word key="rx"/><word key="analogRead"/><word key="digitalWrite"/><word key="HIGH"/><word key="output"/><word key="socket"/><word key="readFile"/><word key="pinmode"/><word key="eeproms"/><word key="eeprom"/><word key="eeproms"/><word key="socket.emit"/><word key="muxOption"/><word key="ex"/><word key="export"/><word key="getpinmode"/><word key="omap_mux"/><word key="readout"/><word key="omap_mux"/><word key="muxstruct"/><word key="socket.emit"/><word key="mode"/><word key="socket"/><word key="spawn"/><word key="socket.on"/><word key="fibers"/><word key="pinObject"/><word key="pull"/><word key="pinmode"/><word key="events"/><word key="gpmc_ben1"/><word key="opensync"/></search><breakpoints /></settings>

bonescript/misc.cpp

Lines changed: 133 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,148 @@ using namespace std;
1313
using namespace node;
1414
using namespace v8;
1515

16-
static Handle<Value> pollpri(const Arguments&);
17-
static int pollpri_thread(eio_req *);
18-
static int pollpri_after(eio_req *);
19-
//extern "C" void pollpri_event(struct ev_loop* loop, ev_io* req, int revents);
2016
extern "C" void pollpri_event(ev_io* req, int revents);
2117
extern "C" void init(Handle<Object>);
2218

23-
struct pollpri_request {
24-
Persistent<Function> callback;
25-
char path[1];
26-
};
19+
class Pollpri: ObjectWrap {
20+
private:
21+
int fd, epfd;
22+
char *path;
23+
public:
24+
struct pollpri_request {
25+
Pollpri *p;
26+
Persistent<Function> cb;
27+
};
2728

28-
class Pollpri : public ObjectWrap {
29-
};
29+
static Persistent<FunctionTemplate> ct;
30+
31+
static void Init(Handle<Object> target) {
32+
HandleScope scope;
33+
Local<FunctionTemplate> t = FunctionTemplate::New(New);
34+
ct = Persistent<FunctionTemplate>::New(t);
35+
ct->InstanceTemplate()->SetInternalFieldCount(1);
36+
ct->SetClassName(String::NewSymbol("Pollpri"));
37+
38+
NODE_SET_PROTOTYPE_METHOD(ct, "pollpri", pollpri);
39+
40+
target->Set(String::NewSymbol("Pollpri"), ct->GetFunction());
41+
}
3042

31-
static Handle<Value> pollpri(const Arguments& args) {
32-
HandleScope scope;
33-
const char *usage = "usage: pollpri(path, cb)";
34-
if(args.Length() != 2) {
35-
return ThrowException(Exception::Error(String::New(usage)));
43+
Pollpri() {
44+
epfd = 0;
45+
fd = 0;
3646
}
37-
String::Utf8Value path(args[0]);
38-
Local<Function> cb = Local<Function>::Cast(args[1]);
3947

40-
pollpri_request *pr = (pollpri_request *)
41-
malloc(sizeof(struct pollpri_request) + path.length() + 1);
42-
43-
pr->cb = Persistent<Function>::New(cb);
44-
strncpy(pr->path, *path, path.length() + 1);
48+
~Pollpri() {
49+
if(epfd) close(epfd);
50+
if(fd) close(fd);
51+
}
4552

46-
eio_custom(pollpri_thread, EIO_PRI_DEFAULT, pollpri_after, pr);
47-
ev_ref(EV_DEFAULT_UC);
48-
printf("Leaving pollpri\n");
49-
return(Undefined());
50-
}
51-
52-
static int pollpri_thread(eio_req *req) {
53-
printf("Entered pollpri_thread\n");
54-
struct pollpri_request * pr = (struct pollpri_request *)req->data;
55-
int epfd = epoll_create(1);
56-
int fd = open(pr->path, O_RDWR | O_NONBLOCK);
57-
printf("open(%s) returned %d: %s\n", pr->path, fd, strerror(errno));
58-
struct epoll_event ev;
59-
ev.events = EPOLLPRI;
60-
ev.data.fd = fd;
61-
int n = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
62-
printf("epoll_ctl(%d) returned %d (%d): %s\n", fd, n, epfd, strerror(errno));
63-
ev_io pollpri_watcher;
64-
ev_init(&pollpri_watcher, pollpri_event);
65-
pollpri_watcher.data = pr;
66-
ev_io_set(&pollpri_watcher, epfd, EV_READ | EV_WRITE);
67-
ev_io_start(EV_DEFAULT_ &pollpri_watcher);
68-
//struct epoll_event events;
69-
//n = epoll_wait(epfd, &events, 1, -1);
70-
//printf("epoll_wait(%d) returned %d: %s\n", epfd, n, strerror(errno));
71-
printf("Leaving pollpri_thread\n");
72-
return(0);
73-
}
53+
static Handle<Value> New(const Arguments &args) {
54+
printf("Entered New\n");
55+
HandleScope scope;
56+
const char *usage = "usage: new Pollpri(path)";
57+
if(args.Length() != 1) {
58+
return ThrowException(Exception::Error(String::New(usage)));
59+
}
60+
String::Utf8Value path(args[0]);
61+
Pollpri *p = new Pollpri();
62+
p->Wrap(args.This());
63+
p->path = (char *)malloc(path.length() + 1);
64+
strncpy(p->path, *path, path.length() + 1);
65+
printf("Leaving New\n");
66+
return(args.This());
67+
}
7468

75-
extern "C" void pollpri_event(ev_io* req, int revents) {
76-
printf("Entered pollpri_event\n");
77-
printf("Leaving pollpri_event\n");
78-
}
69+
static Handle<Value> pollpri(const Arguments& args) {
70+
printf("Entered pollpri\n");
71+
HandleScope scope;
72+
const char *usage = "usage: pollpri(cb)";
73+
if(args.Length() != 1) {
74+
return ThrowException(Exception::Error(String::New(usage)));
75+
}
76+
Local<Function> cb = Local<Function>::Cast(args[0]);
77+
78+
pollpri_request* pr =
79+
(pollpri_request *)malloc(sizeof(struct pollpri_request));
80+
Pollpri* p = ObjectWrap::Unwrap<Pollpri>(args.This());
81+
82+
pr->p = p;
83+
pr->cb = Persistent<Function>::New(cb);
84+
85+
eio_custom(pollpri_thread, EIO_PRI_DEFAULT, pollpri_after, pr);
86+
ev_ref(EV_DEFAULT_UC);
87+
printf("Leaving pollpri\n");
88+
return(Undefined());
89+
}
90+
91+
static int pollpri_thread(eio_req *req) {
92+
printf("Entered pollpri_thread\n");
93+
struct pollpri_request * pr = (struct pollpri_request *)req->data;
94+
Pollpri *p = pr->p;
95+
int epfd = p->epfd;
96+
int fd = p->fd;
97+
if(!fd) {
98+
fd = open(p->path, O_RDWR | O_NONBLOCK);
99+
pr->p->fd = fd;
100+
printf("open(%s) returned %d: %s\n", p->path, fd, strerror(errno));
101+
}
102+
if(!epfd) {
103+
epfd = epoll_create(1);
104+
pr->p->epfd = epfd;
105+
printf("epoll_create(1) returned %d: %s\n", epfd, strerror(errno));
106+
struct epoll_event ev;
107+
ev.events = EPOLLPRI;
108+
ev.data.fd = fd;
109+
int n = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
110+
printf("epoll_ctl(%d) returned %d (%d): %s\n", fd, n, epfd, strerror(errno));
111+
}
112+
//ev_io pollpri_watcher;
113+
//ev_init(&pollpri_watcher, pollpri_event);
114+
//pollpri_watcher.data = pr;
115+
//ev_io_set(&pollpri_watcher, epfd, EV_READ | EV_WRITE);
116+
//ev_io_start(EV_DEFAULT_ &pollpri_watcher);
117+
struct epoll_event events;
118+
printf("Calling epoll_wait\n");
119+
sleep(1);
120+
int m = epoll_wait(epfd, &events, 1, -1);
121+
printf("epoll_wait(%d) returned %d: %s\n", epfd, m, strerror(errno));
122+
//close(epfd);
123+
//close(fd);
124+
printf("Leaving pollpri_thread\n");
125+
return(0);
126+
}
127+
128+
static int pollpri_after(eio_req *req) {
129+
printf("Entered pollpri_after\n");
130+
HandleScope scope;
131+
ev_unref(EV_DEFAULT_UC);
132+
struct pollpri_request * pr = (struct pollpri_request *)req->data;
133+
Local<Value> argv[2];
134+
argv[0] = Local<Value>::New(Null());
135+
argv[1] = String::New(pr->p->path);
136+
pr->cb->Call(Context::GetCurrent()->Global(), 2, argv);
137+
pr->cb.Dispose();
138+
//close(pr->p->epfd);
139+
//close(pr->p->fd);
140+
free(pr);
141+
printf("Leaving pollpri_after\n");
142+
return(0);
143+
}
144+
145+
};
79146

80-
static int pollpri_after(eio_req *req) {
81-
printf("Entered pollpri_after\n");
82-
HandleScope scope;
83-
//ev_unref(EV_DEFAULT_UC);
84-
struct pollpri_request * pr = (struct pollpri_request *)req->data;
85-
Local<Value> argv[2];
86-
argv[0] = Local<Value>::New(Null());
87-
argv[1] = String::New(pr->path);
88-
pr->cb->Call(Context::GetCurrent()->Global(), 2, argv);
89-
//pr->cb.Dispose();
90-
//free(pr);
91-
printf("Leaving pollpri_after\n");
92-
return(0);
93-
}
147+
Persistent<FunctionTemplate> Pollpri::ct;
94148

95-
extern "C" void init(Handle<Object> target) {
96-
HandleScope scope;
97-
NODE_SET_METHOD(target, "pollpri", pollpri);
149+
extern "C" {
150+
void init(Handle<Object> target) {
151+
Pollpri::Init(target);
152+
}
153+
154+
void pollpri_event(ev_io* req, int revents) {
155+
printf("Entered pollpri_event\n");
156+
printf("Leaving pollpri_event\n");
157+
}
158+
159+
NODE_MODULE(pollpri, init);
98160
}

bonescript/misctest.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
var misc = require('./misc');
21
var fs = require('fs');
3-
42
var filename = '/sys/class/gpio/gpio34/value';
3+
var misc = require('./misc');
4+
var pollpri = new misc.Pollpri(filename);
5+
6+
var readdata = function(err, data) {
7+
console.log(data + ': ' + fs.readFileSync(data, 'utf-8'));
8+
pollpri.pollpri(readdata);
9+
};
510

6-
console.log('Calling pollpri');
7-
misc.pollpri(filename, function(err, data) {
8-
console.log(data + ': ' + fs.readFileSync(filename, 'utf-8'));
9-
});
11+
pollpri.pollpri(readdata);

0 commit comments

Comments
 (0)