Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial support for node 0.6.x (using libuv), not fully working yet

  • Loading branch information...
commit 5d9165892e7bf58000eb72404d2e2111fd0fbc44 1 parent fd11b9f
Jeroen Janssen authored
4 CHANGES
... ... @@ -1,3 +1,7 @@
  1 +Unreleased
  2 +* node 0.6.x (libuv) support
  3 + 'known' issue atm: SEGV at end of program (not good!)
  4 +
1 5 2011.05.14, Version 0.0.9
2 6
3 7 * changed npm package to 'sdlmixer'
13 sdlmixer.js
... ... @@ -1,3 +1,14 @@
1   -var sdlmixer_lib = require('./build/default/node-sdlmixer');
  1 +var sdlmixer_lib;
  2 +var node_version = process.version.split('.');
  3 +if ((node_version[0] == "v0") && (node_version[1] == "4"))
  4 +{
  5 +// console.log("v0.4.x");
  6 + sdlmixer_lib = require('./build/default/node-sdlmixer');
  7 +}
  8 +else
  9 +{
  10 +// console.log("!v0.4.x");
  11 + sdlmixer_lib = require('./build/Release/node-sdlmixer');
  12 +}
2 13
3 14 module.exports = new sdlmixer_lib.SDLMixer();
0  src/async.h → src/async_ev.h
File renamed without changes
75 src/async_uv.h
... ... @@ -0,0 +1,75 @@
  1 +#ifndef NODE_SQLITE3_SRC_ASYNC_H
  2 +#define NODE_SQLITE3_SRC_ASYNC_H
  3 +
  4 +
  5 +// Generic uv_async handler.
  6 +template <class Item, class Parent> class Async {
  7 + typedef void (*Callback)(Parent* parent, Item* item);
  8 +
  9 +protected:
  10 + uv_async_t watcher;
  11 + pthread_mutex_t mutex;
  12 + std::vector<Item*> data;
  13 + Callback callback;
  14 +public:
  15 + Parent* parent;
  16 +
  17 +public:
  18 + Async(Parent* parent_, Callback cb_)
  19 + : callback(cb_), parent(parent_) {
  20 + watcher.data = this;
  21 + pthread_mutex_init(&mutex, NULL);
  22 + uv_async_init(uv_default_loop(), &watcher, listener);
  23 + }
  24 +
  25 + static void listener(uv_async_t* handle, int status) {
  26 + Async* async = static_cast<Async*>(handle->data);
  27 + std::vector<Item*> rows;
  28 + pthread_mutex_lock(&async->mutex);
  29 + rows.swap(async->data);
  30 + pthread_mutex_unlock(&async->mutex);
  31 + for (unsigned int i = 0, size = rows.size(); i < size; i++) {
  32 + uv_unref(uv_default_loop());
  33 + async->callback(async->parent, rows[i]);
  34 + }
  35 + }
  36 +
  37 + static void close(uv_handle_t* handle) {
  38 + assert(handle != NULL);
  39 + assert(handle->data != NULL);
  40 + Async* async = static_cast<Async*>(handle->data);
  41 + delete async;
  42 + handle->data = NULL;
  43 + }
  44 +
  45 + void finish() {
  46 + // Need to call the listener again to ensure all items have been
  47 + // processed. Is this a bug in uv_async? Feels like uv_close
  48 + // should handle that.
  49 + listener(&watcher, 0);
  50 + uv_close((uv_handle_t*)&watcher, close);
  51 + }
  52 +
  53 + void add(Item* item) {
  54 + // Make sure node runs long enough to deliver the messages.
  55 + uv_ref(uv_default_loop());
  56 + pthread_mutex_lock(&mutex);
  57 + data.push_back(item);
  58 + pthread_mutex_unlock(&mutex);
  59 + }
  60 +
  61 + void send() {
  62 + uv_async_send(&watcher);
  63 + }
  64 +
  65 + void send(Item* item) {
  66 + add(item);
  67 + send();
  68 + }
  69 +
  70 + ~Async() {
  71 + pthread_mutex_destroy(&mutex);
  72 + }
  73 +};
  74 +
  75 +#endif
26 src/node-sdlmixer.cc
@@ -30,7 +30,20 @@ static void releaseAudioChannel(int channel) {
30 30 availableChannels.push_back(channel);
31 31 }
32 32
33   -static int DoPlay(eio_req *req) {
  33 +#ifdef UV_VERSION_MAJOR
  34 +static void UV_Play(uv_work_t *req)
  35 +{
  36 + struct playInfo * pi = (struct playInfo *) req->data;
  37 +
  38 + /* Load the requested wave file */
  39 + pi->wave = Mix_LoadWAV(pi->name);
  40 +
  41 + //printf("Playing [%s] on channel[%d]\n", pi->name, pi->channel);
  42 + /* Play and then exit */
  43 + Mix_PlayChannel(pi->channel, pi->wave, 0);
  44 +}
  45 +#else
  46 +static int EIO_Play(eio_req *req) {
34 47 struct playInfo * pi = (struct playInfo *) req->data;
35 48
36 49 /* Load the requested wave file */
@@ -43,12 +56,13 @@ static int DoPlay(eio_req *req) {
43 56 return 0;
44 57 }
45 58
46   -int SDLMixer::NotifyPlayed(eio_req *req) {
  59 +int EIO_AfterPlay(eio_req *req) {
47 60 HandleScope scope;
48 61 ev_unref( EV_DEFAULT_UC);
49 62 //printf("SDLMixer::NotifyPlayed\n");
50 63 return 0;
51 64 }
  65 +#endif
52 66
53 67 Handle<Value> SDLMixer::Play(const Arguments& args) {
54 68 HandleScope scope;
@@ -86,8 +100,14 @@ Handle<Value> SDLMixer::Play(const Arguments& args) {
86 100 playDoneEvent = new AsyncPlayDone(sm, PlayDoneCallback);
87 101 }
88 102
89   - eio_custom(DoPlay, EIO_PRI_DEFAULT, NotifyPlayed, pi);
  103 +#ifdef UV_VERSION_MAJOR
  104 + uv_work_t* work_req = (uv_work_t *) (calloc(1, sizeof(uv_work_t)));
  105 + work_req->data = pi;
  106 + uv_queue_work(uv_default_loop(), work_req, UV_Play, NULL);
  107 +#else
  108 + eio_custom(EIO_Play, EIO_PRI_DEFAULT, EIO_AfterPlay, pi);
90 109 ev_ref( EV_DEFAULT_UC);
  110 +#endif
91 111
92 112 return scope.Close(args[0]);
93 113 }
7 src/node-sdlmixer.h
@@ -11,7 +11,11 @@
11 11 #include "SDL_mixer.h"
12 12
13 13 #include <pthread.h>
14   -#include "async.h"
  14 +#ifdef UV_VERSION_MAJOR
  15 +#include "async_uv.h"
  16 +#else
  17 +#include "async_ev.h"
  18 +#endif
15 19
16 20 using namespace v8;
17 21 using namespace node;
@@ -36,7 +40,6 @@ class SDLMixer: public ObjectWrap {
36 40 typedef Async<playInfo, SDLMixer> AsyncPlayDone;
37 41 protected:
38 42
39   - static int NotifyPlayed(eio_req *req);
40 43 static Handle<Value> Play(const Arguments& args);
41 44
42 45 static void ChannelFinished(int channel);
6 test/simple.js
... ... @@ -1,6 +1,6 @@
1 1 var sdlmixer = require("../sdlmixer"),
2   - sys = require("sys"),
3   - puts = sys.puts;
  2 + util = require("util"),
  3 + puts = util.puts;
4 4
5 5 puts("Opened audio at " + sdlmixer.audioRate + " Hz " + sdlmixer.audioFormat + " bit " +
6 6 sdlmixer.audioChannels + " and " + sdlmixer.numberOfAudioChannels + " channels.")
@@ -9,4 +9,4 @@ puts("Opened audio at " + sdlmixer.audioRate + " Hz " + sdlmixer.audioFormat + "
9 9
10 10 sdlmixer.play(__dirname + "/../wavs/sfx/alarm.wav", function () {
11 11 puts("done")
12   -});
  12 +});
4 test/test.js
... ... @@ -1,6 +1,6 @@
1 1 var sdlmixer = require("../sdlmixer"),
2   - sys = require("sys"),
3   - puts = sys.puts;
  2 + util = require("util"),
  3 + puts = util.puts;
4 4
5 5 puts("Opened audio at " + sdlmixer.audioRate + " Hz " + sdlmixer.audioFormat + " bit " +
6 6 sdlmixer.audioChannels + " and " + sdlmixer.numberOfAudioChannels + " channels.")

0 comments on commit 5d91658

Please sign in to comment.
Something went wrong with that request. Please try again.