Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/threads-ffi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoumith Chintala <soumith@gmail.com>2016-05-14 21:07:48 +0300
committerSoumith Chintala <soumith@gmail.com>2016-05-14 21:07:48 +0300
commit251411d95da1d03fe6a8664001a4941a61e7642d (patch)
tree05af1c0fc7bbc37565210ea1ce24df7e44472052
parent628ab68498f4e3095709ee60a1bbd83cc4f0b556 (diff)
parent18e0c0698526d7f9400e44a35374e0d227e07414 (diff)
Merge pull request #64 from lake4790k/master
support sharedserialize of tds.AtomicCounter
-rw-r--r--.travis.yml1
-rw-r--r--README.md6
-rw-r--r--sharedserialize.lua16
-rw-r--r--test/test-atomic.lua30
4 files changed, 53 insertions, 0 deletions
diff --git a/.travis.yml b/.travis.yml
index bf854c8..c9904f5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -59,3 +59,4 @@ script:
- ${TESTLUA} test-threads-shared.lua
- ${TESTLUA} test-traceback.lua
- ${TESTLUA} test-threads-coroutine.lua
+- ${TESTLUA} test-atomic.lua
diff --git a/README.md b/README.md
index 633db10..08203cb 100644
--- a/README.md
+++ b/README.md
@@ -134,6 +134,7 @@ The library provides different low-level and high-level threading capabilities.
* [Thread](#thread): a single thread with no artifice ;
* [Mutex](#mutex): a thread mutex ;
* [Condition](#condition): a condition variable.
+ * [Atomic counter](#atomic): lock free atomic counter
Soon some more high-level features will be proposed, built on top of Threads.
@@ -474,3 +475,8 @@ Raise the condition signal.
#### Condition.free() ####
Free given condition.
+
+<a name ='atomic'>
+### Atomic counter ###
+
+`tds.AtomicCounter` has been implemented to be used with `sharedserialize` to provide fast and safe lockless counting of progress (steps) between threads. See [example](test/test-atomic.lua) for usage.
diff --git a/sharedserialize.lua b/sharedserialize.lua
index 17d18b6..c34684e 100644
--- a/sharedserialize.lua
+++ b/sharedserialize.lua
@@ -59,6 +59,22 @@ if tds then
function mt.__read(self, f)
end
typenames['tds.Vec'] = mt
+
+ -- atomic
+ local mt = {}
+ function mt.__factory(f)
+ local self = deserializePointer(f)
+ self = ffi.cast('tds_atomic_counter&', self)
+ ffi.gc(self, tds.C.tds_atomic_free)
+ return self
+ end
+ function mt.__write(self, f)
+ serializePointer(self, f)
+ tds.C.tds_atomic_retain(self)
+ end
+ function mt.__read(self, f)
+ end
+ typenames['tds.AtomicCounter'] = mt
end
-- tensor support
diff --git a/test/test-atomic.lua b/test/test-atomic.lua
new file mode 100644
index 0000000..89c268c
--- /dev/null
+++ b/test/test-atomic.lua
@@ -0,0 +1,30 @@
+local threads = require 'threads'
+threads.Threads.serialization('threads.sharedserialize')
+
+local status, tds = pcall(require, 'tds')
+tds = status and tds or nil
+if not status then return end
+
+local atomic = tds.AtomicCounter()
+local numOfThreads = 10
+
+local pool = threads.Threads(numOfThreads)
+
+local steps = 100000
+
+for t=1,numOfThreads do
+ pool:addjob(function()
+ for i=1,steps do
+ atomic:inc()
+ end
+ end)
+end
+
+pool:synchronize()
+
+print(atomic)
+assert(atomic:get() == numOfThreads * steps)
+
+pool:terminate()
+
+print('PASSED') \ No newline at end of file