diff options
-rw-r--r-- | Makefile | 87 | ||||
-rw-r--r-- | PKGBUILD | 8 | ||||
-rw-r--r-- | lem/postgres.c (renamed from postgres.c) | 26 | ||||
l--------- | lem/postgres.so | 1 | ||||
-rw-r--r--[l---------] | lem/postgres/queued.lua | 94 | ||||
-rwxr-xr-x | qtest.lua | 3 | ||||
-rw-r--r-- | queued.lua | 96 |
7 files changed, 158 insertions, 157 deletions
@@ -1,57 +1,62 @@ -CC = gcc -CFLAGS ?= -O2 -pipe -Wall -Wextra -Wno-variadic-macros -Wno-strict-aliasing -PKGCONFIG = pkg-config +CC = gcc -std=gnu99 +CFLAGS ?= -O2 -pipe -Wall -Wextra +PKG_CONFIG = pkg-config +PG_CONFIG = pg_config STRIP = strip INSTALL = install +UNAME = uname + +OS = $(shell $(UNAME)) +CFLAGS += $(shell $(PKG_CONFIG) --cflags lem) +lmoddir = $(shell $(PKG_CONFIG) --variable=INSTALL_LMOD lem) +cmoddir = $(shell $(PKG_CONFIG) --variable=INSTALL_CMOD lem) + +ifeq ($(OS),Darwin) +SHARED = -dynamiclib -Wl,-undefined,dynamic_lookup +STRIP += -x +else +SHARED = -shared +endif -CFLAGS += $(shell $(PKGCONFIG) --cflags lem) -LUA_PATH = $(shell $(PKGCONFIG) --variable=path lem) -LUA_CPATH = $(shell $(PKGCONFIG) --variable=cpath lem) - -programs = postgres.so -scripts = queued.lua +llibs = lem/postgres/queued.lua +clibs = lem/postgres.so -ifdef NDEBUG -CFLAGS += -DNDEBUG +ifdef V +E=@\# +Q= +else +E=@echo +Q=@ endif -.PHONY: all strip install clean -.PRECIOUS: %.o +.PHONY: all debug strip install clean -all: $(programs) +all: CFLAGS += -DNDEBUG +all: $(clibs) -%.o: %.c - @echo ' CC $@' - @$(CC) $(CFLAGS) -fPIC -nostartfiles -c $< -o $@ +debug: $(clibs) -postgres.so: CFLAGS += -I$(shell pg_config --includedir) -postgres.so: postgres.o - @echo ' LD $@' - @$(CC) -shared -L$(shell pg_config --libdir) -lpq $(LDFLAGS) $^ -o $@ +lem/postgres.so: CFLAGS += -I$(shell $(PG_CONFIG) --includedir) +lem/postgres.so: LDFLAGS += -L$(shell $(PG_CONFIG) --libdir) +lem/postgres.so: LIBS += -lpq +lem/postgres.so: lem/postgres.c + $E ' CCLD $@' + $Q$(CC) $(CFLAGS) -fPIC -nostartfiles $(SHARED) $^ -o $@ $(LDFLAGS) $(LIBS) %-strip: % - @echo ' STRIP $<' - @$(STRIP) $< - -strip: $(programs:%=%-strip) - -path-install: - @echo " INSTALL -d $(LUA_PATH)/lem/postgres" - @$(INSTALL) -d $(DESTDIR)$(LUA_PATH)/lem/postgres - -%.lua-install: %.lua path-install - @echo " INSTALL $<" - @$(INSTALL) -m644 $< $(DESTDIR)$(LUA_PATH)/lem/postgres/$< + $E ' STRIP $<' + $Q$(STRIP) $< -cpath-install: - @echo " INSTALL -d $(LUA_CPATH)/lem" - @$(INSTALL) -d $(DESTDIR)$(LUA_CPATH)/lem +strip: $(clibs:%=%-strip) -%.so-install: %.so cpath-install - @echo " INSTALL $<" - @$(INSTALL) $< $(DESTDIR)$(LUA_CPATH)/lem/$< +$(DESTDIR)$(lmoddir)/% $(DESTDIR)$(cmoddir)/%: % + $E ' INSTALL $@' + $Q$(INSTALL) -d $(dir $@) + $Q$(INSTALL) -m 644 $< $@ -install: $(programs:%=%-install) $(scripts:%=%-install) +install: \ + $(llibs:%=$(DESTDIR)$(lmoddir)/%) \ + $(clibs:%=$(DESTDIR)$(cmoddir)/%) clean: - rm -f $(programs) *.o *.c~ *.h~ + rm -f $(clibs) @@ -1,11 +1,11 @@ # Maintainer: Emil Renner Berthing <esmil@mailme.dk> pkgname=lem-postgres -pkgver=0.1 +pkgver=0.3 pkgrel=1 -pkgdesc="PostgreSQL library for the Lua Event Machine" +pkgdesc='PostgreSQL library for the Lua Event Machine' arch=('i686' 'x86_64' 'armv5tel' 'armv7l') -url="https://github.com/esmil/lem-postgres" +url='https://github.com/esmil/lem-postgres' license=('GPL') depends=('lem' 'postgresql-libs') source=() @@ -13,7 +13,7 @@ source=() build() { cd "$startdir" - make NDEBUG=1 + make } package() { diff --git a/postgres.c b/lem/postgres.c index 16f2de8..2d28cf5 100644 --- a/postgres.c +++ b/lem/postgres.c @@ -50,7 +50,7 @@ connection_gc(lua_State *T) if (c->conn == NULL) return 0; - ev_io_stop(EV_G_ &c->w); + ev_io_stop(LEM_ &c->w); PQfinish(c->conn); return 0; } @@ -75,7 +75,7 @@ connection_close(lua_State *T) c->T = NULL; } - ev_io_stop(EV_G_ &c->w); + ev_io_stop(LEM_ &c->w); PQfinish(c->conn); c->conn = NULL; @@ -181,7 +181,7 @@ connection_connect(lua_State *T) } c->T = T; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_replace(T, 1); lua_settop(T, 1); @@ -241,7 +241,7 @@ connection_reset(lua_State *T) } c->T = T; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(T, 1); return lua_yield(T, 1); @@ -467,7 +467,7 @@ connection_exec(lua_State *T) c->T = T; c->w.cb = exec_handler; c->w.events = EV_READ; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(T, 1); return lua_yield(T, 1); @@ -505,7 +505,7 @@ connection_prepare(lua_State *T) c->T = T; c->w.cb = exec_handler; c->w.events = EV_READ; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(T, 1); return lua_yield(T, 1); @@ -560,7 +560,7 @@ connection_run(lua_State *T) c->T = T; c->w.cb = exec_handler; c->w.events = EV_READ; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(T, 1); return lua_yield(T, 1); @@ -641,7 +641,7 @@ connection_put(lua_State *T) c->T = T; c->w.cb = put_handler; c->w.events = EV_WRITE; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(T, 2); return lua_yield(T, 2); @@ -709,7 +709,7 @@ connection_done(lua_State *T) c->T = T; c->w.cb = exec_handler; c->w.events = EV_READ; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(c->T, 1); return lua_yield(c->T, 1); @@ -725,7 +725,7 @@ connection_done(lua_State *T) c->T = T; c->w.cb = done_handler; c->w.events = EV_WRITE; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); if (error == NULL) { lua_settop(T, 1); @@ -810,7 +810,7 @@ connection_get(lua_State *T) c->T = T; c->w.cb = get_handler; c->w.events = EV_READ; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); lua_settop(T, 1); return lua_yield(T, 1); @@ -827,13 +827,13 @@ connection_get(lua_State *T) c->T = T; c->w.cb = exec_handler; c->w.events = EV_READ; - ev_io_start(EV_G_ &c->w); + ev_io_start(LEM_ &c->w); /* TODO: it is necessary but kinda ugly to call * the exec_handler directly from here. * find a better solution... */ lua_settop(T, 1); - exec_handler(EV_G_ &c->w, 0); + exec_handler(LEM_ &c->w, 0); return lua_yield(T, 1); } diff --git a/lem/postgres.so b/lem/postgres.so deleted file mode 120000 index d0ce981..0000000 --- a/lem/postgres.so +++ /dev/null @@ -1 +0,0 @@ -../postgres.so
\ No newline at end of file diff --git a/lem/postgres/queued.lua b/lem/postgres/queued.lua index 94dfe13..2f597f4 120000..100644 --- a/lem/postgres/queued.lua +++ b/lem/postgres/queued.lua @@ -1 +1,93 @@ -../../queued.lua
\ No newline at end of file +-- +-- This file is part of lem-postgres. +-- Copyright 2011 Emil Renner Berthing +-- +-- lem-postgres is free software: you can redistribute it and/or +-- modify it under the terms of the GNU General Public License as +-- published by the Free Software Foundation, either version 3 of +-- the License, or (at your option) any later version. +-- +-- lem-postgres is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with lem-postgres. If not, see <http://www.gnu.org/licenses/>. +-- + +local utils = require 'lem.utils' +local postgres = require 'lem.postgres' + +local remove = table.remove +local thisthread, suspend, resume = utils.thisthread, utils.suspend, utils.resume + +local QConnection = {} +QConnection.__index = QConnection + +function QConnection:close() + local ok, err = self.conn:close() + for i = 1, self.n - 1 do + resume(self[i]) + self[i] = nil + end + return ok, err +end +QConnection.__gc = QConnection.close + +local function lock(self) + local n = self.n + if n == 0 then + self.n = 1 + else + self[n] = thisthread() + self.n = n + 1 + suspend() + end + return self.conn +end +QConnection.lock = lock + +local function unlock(self, ...) + local n = self.n - 1 + self.n = n + if n > 0 then + resume(remove(self, 1)) + end + return ... +end +QConnection.unlock = unlock + +local function wrap(method) + return function(self, ...) + return unlock(self, method(lock(self), ...)) + end +end + +local Connection = postgres.Connection +QConnection.exec = wrap(Connection.exec) +QConnection.prepare = wrap(Connection.prepare) +QConnection.run = wrap(Connection.run) + +local qconnect +do + local setmetatable = setmetatable + local connect = postgres.connect + + function qconnect(...) + local conn, err = connect(...) + if not conn then return nil, err end + + return setmetatable({ + n = 0, + conn = conn, + }, QConnection) + end +end + +return { + QConnection = QConnection, + connect = qconnect, +} + +-- vim: ts=2 sw=2 noet: @@ -105,7 +105,8 @@ do db:unlock() end -utils.timer(1.0, function() +utils.spawn(function() + utils.sleeper():sleep(1.0) assert(db:exec('DROP TABLE mytable')) end) diff --git a/queued.lua b/queued.lua deleted file mode 100644 index d244e61..0000000 --- a/queued.lua +++ /dev/null @@ -1,96 +0,0 @@ --- --- This file is part of lem-postgres. --- Copyright 2011 Emil Renner Berthing --- --- lem-postgres is free software: you can redistribute it and/or --- modify it under the terms of the GNU General Public License as --- published by the Free Software Foundation, either version 3 of --- the License, or (at your option) any later version. --- --- lem-postgres is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License --- along with lem-postgres. If not, see <http://www.gnu.org/licenses/>. --- - -local utils = require 'lem.utils' -local postgres = require 'lem.postgres' - -local QConnection = {} -QConnection.__index = QConnection - -function QConnection:close() - local ok, err = self.conn:close() - for i = 1, self.n - 1 do - self[i]:wakeup() - self[i] = nil - end - return ok, err -end -QConnection.__gc = QConnection.close - -do - local remove = table.remove - local newsleeper = utils.sleeper - - local function lock(self) - local n = self.n - if n == 0 then - self.n = 1 - else - local sleeper = newsleeper() - self[n] = sleeper - self.n = n + 1 - sleeper:sleep() - end - return self.conn - end - QConnection.lock = lock - - local function unlock(self, ...) - local n = self.n - 1 - self.n = n - if n > 0 then - remove(self, 1):wakeup() - end - return ... - end - QConnection.unlock = unlock - - local function wrap(method) - return function(self, ...) - return unlock(self, method(lock(self), ...)) - end - end - - local Connection = postgres.Connection - QConnection.exec = wrap(Connection.exec) - QConnection.prepare = wrap(Connection.prepare) - QConnection.run = wrap(Connection.run) -end - -local qconnect -do - local setmetatable = setmetatable - local connect = postgres.connect - - function qconnect(...) - local conn, err = connect(...) - if not conn then return nil, err end - - return setmetatable({ - n = 0, - conn = conn, - }, QConnection) - end -end - -return { - QConnection = QConnection, - connect = qconnect, -} - --- vim: ts=2 sw=2 noet: |