aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/makefile2
-rw-r--r--common/threads/driver.cxx146
-rw-r--r--common/threads/makefile82
-rw-r--r--common/threads/test.hxx31
-rw-r--r--common/threads/test.std0
5 files changed, 261 insertions, 0 deletions
diff --git a/common/makefile b/common/makefile
index ad3b0a4..87488be 100644
--- a/common/makefile
+++ b/common/makefile
@@ -11,6 +11,8 @@ template \
lifecycle \
query
+tests += threads
+
default := $(out_base)/
test := $(out_base)/.test
clean := $(out_base)/.clean
diff --git a/common/threads/driver.cxx b/common/threads/driver.cxx
new file mode 100644
index 0000000..9ae5b50
--- /dev/null
+++ b/common/threads/driver.cxx
@@ -0,0 +1,146 @@
+// file : common/threads/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test operations in a multi-threaded environment.
+//
+
+#include <vector>
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <odb/shared-ptr.hxx>
+#include <odb/details/thread.hxx>
+
+#include <common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+using odb::shared_ptr;
+using odb::details::thread;
+
+const size_t thread_count = 32;
+const size_t iteration_count = 100;
+
+struct task
+{
+ task (database& db, size_t n)
+ : db_ (db), n_ (n)
+ {
+ }
+
+ void
+ execute ()
+ {
+ try
+ {
+ for (size_t i (0); i < iteration_count; ++i)
+ {
+ unsigned long id ((n_ * iteration_count + i) * 3);
+
+ {
+ object o1 (id, "frist object");
+ object o2 (id + 1, "second object");
+ object o3 (id + 2, "third object");
+
+ transaction t (db_.begin_transaction ());
+ db_.persist (o1);
+ db_.persist (o2);
+ db_.persist (o3);
+ t.commit ();
+ }
+
+ {
+ transaction t (db_.begin_transaction ());
+ auto_ptr<object> o (db_.load<object> (id));
+ assert (o->str_ == "frist object");
+ o->str_ = "another value";
+ db_.store (*o);
+ t.commit ();
+ }
+
+ {
+ typedef odb::query<object> query;
+ typedef odb::result<object> result;
+
+ transaction t (db_.begin_transaction ());
+ result r (db_.query<object> (query::str == "another value"));
+
+ bool found (false);
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ if (i->id_ == id)
+ {
+ found = true;
+ break;
+ }
+ }
+ assert (found);
+ t.commit ();
+ }
+
+ {
+ transaction t (db_.begin_transaction ());
+ db_.erase<object> (id);
+ t.commit ();
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ }
+ }
+
+ static void*
+ execute (void* arg)
+ {
+ static_cast<task*> (arg)->execute ();
+ return 0;
+ }
+
+ database& db_;
+ size_t n_;
+};
+
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ vector<shared_ptr<thread> > threads;
+ vector<shared_ptr<task> > tasks;
+
+ for (size_t i (0); i < thread_count; ++i)
+ {
+ shared_ptr<task> t (new (shared) task (*db, i));
+ tasks.push_back (t);
+
+ threads.push_back (
+ shared_ptr<thread> (
+ new (shared) thread (&task::execute, t.get ())));
+ }
+
+ for (size_t i (0); i < thread_count; ++i)
+ threads[i]->join ();
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+
+ // pthread_exit (0);
+}
diff --git a/common/threads/makefile b/common/threads/makefile
new file mode 100644
index 0000000..af03a93
--- /dev/null
+++ b/common/threads/makefile
@@ -0,0 +1,82 @@
+# file : common/threads/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+# license : GNU GPL v2; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+odb_hdr := test.hxx
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+common.l := $(out_root)/libcommon/common.l
+common.l.cpp-options := $(out_root)/libcommon/common.l.cpp-options
+
+driver := $(out_base)/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Import.
+#
+$(call import,\
+ $(scf_root)/import/odb/stub.make,\
+ odb: odb,odb-rules: odb_rules)
+
+# Build.
+#
+$(driver): $(cxx_obj) $(common.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+$(cxx_obj) $(cxx_od): $(common.l.cpp-options)
+
+genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(odb)
+$(gen): odb := $(odb)
+$(gen): odb_options += --database $(db_id) --generate-schema --generate-query
+$(gen): cpp_options := -I$(out_base)
+$(gen): $(common.l.cpp-options)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Test.
+#
+$(test): $(driver) $(src_base)/test.std
+ $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql)
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options \
+| diff -u $(src_base)/test.std -)
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean))
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(odb_rules))
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/o-e.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/libcommon/makefile)
diff --git a/common/threads/test.hxx b/common/threads/test.hxx
new file mode 100644
index 0000000..d02fdfd
--- /dev/null
+++ b/common/threads/test.hxx
@@ -0,0 +1,31 @@
+// file : common/template/test.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <string>
+
+#include <odb/core.hxx>
+
+#pragma odb object
+struct object
+{
+ object (unsigned long id, const std::string& str)
+ : id_ (id), str_ (str)
+ {
+ }
+
+ object ()
+ {
+ }
+
+ #pragma odb id
+ unsigned long id_;
+
+ std::string str_;
+};
+
+#endif // TEST_HXX
diff --git a/common/threads/test.std b/common/threads/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/common/threads/test.std