Permalink
Browse files

adding async.h from node-sqlite3 and update LICENSE

  • Loading branch information...
japj committed May 13, 2011
1 parent 71d77d1 commit 82f090791a85325307380120a9863d7ac64aee43
Showing with 105 additions and 4 deletions.
  1. +43 −4 LICENSE
  2. +62 −0 src/async.h
View
47 LICENSE
@@ -1,3 +1,7 @@
+node-sdlmixer's license follows:
+
+====
+
Copyright (c) 2011, Jeroen Janssen <jeroen.janssen@gmail.com>
All rights reserved.
@@ -18,9 +22,44 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
----
-The audio files in wavs/sfx and wavs/drums originate from:
-http://github.com/Marak/play.js
+====
+
+This license applies to all parts of node-sdlmixer that are not externally
+maintained.
+
+The externally maintained items used by node-sdlmixer are:
+
+ - The example audio files in wavs/sfx and wavs/drums originate from:
+ http://github.com/Marak/play.js
+
+ - src/async.h originates from:
+ https://github.com/developmentseed/node-sqlite3 and is distributed
+ under the following license:
----
+ Copyright (c) 2011, Konstantin Käfer <kkaefer@gmail.com>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification,
+ are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ * Neither the name of "Development Seed" nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL Konstantin Käfer BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+====
View
@@ -0,0 +1,62 @@
+#ifndef NODE_SQLITE3_SRC_ASYNC_H
+#define NODE_SQLITE3_SRC_ASYNC_H
+
+
+// Generic ev_async handler.
+template <class Item, class Parent> class Async {
+ typedef void (*Callback)(Parent* parent, Item* item);
+
+protected:
+ ev_async watcher;
+ pthread_mutex_t mutex;
+ std::vector<Item*> data;
+ Callback callback;
+public:
+ Parent* parent;
+
+public:
+ inline Async(Parent* parent_, Callback cb_)
+ : callback(cb_), parent(parent_) {
+ watcher.data = this;
+ ev_async_init(&watcher, listener);
+ ev_async_start(EV_DEFAULT_UC_ &watcher);
+ pthread_mutex_init(&mutex, NULL);
+ }
+
+ static void listener(EV_P_ ev_async *w, int revents) {
+ Async* async = static_cast<Async*>(w->data);
+ std::vector<Item*> rows;
+ pthread_mutex_lock(&async->mutex);
+ rows.swap(async->data);
+ pthread_mutex_unlock(&async->mutex);
+ for (unsigned int i = 0, size = rows.size(); i < size; i++) {
+ ev_unref(EV_DEFAULT_UC);
+ async->callback(async->parent, rows[i]);
+ }
+ }
+
+ inline void add(Item* item) {
+ // Make sure node runs long enough to deliver the messages.
+ ev_ref(EV_DEFAULT_UC);
+ pthread_mutex_lock(&mutex);
+ data.push_back(item);
+ pthread_mutex_unlock(&mutex);
+ }
+
+ inline void send() {
+ ev_async_send(EV_DEFAULT_ &watcher);
+ }
+
+ inline void send(Item* item) {
+ add(item);
+ send();
+ }
+
+ inline ~Async() {
+ ev_invoke(&watcher, ev_async_pending(&watcher));
+ pthread_mutex_destroy(&mutex);
+ ev_async_stop(EV_DEFAULT_UC_ &watcher);
+ }
+};
+
+#endif

0 comments on commit 82f0907

Please sign in to comment.