summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-03-12 02:55:09 +0100
committerMattias Andrée <maandree@operamail.com>2014-03-12 02:55:09 +0100
commitbf99c560ef952901caa381b07959865189925c2a (patch)
tree2d9f248b2c784b4fc1e2703b5318eab0da7c7f50
parenttype + update todo, drm looks like the solution for not requiring X/Wayland/Mir (diff)
downloadblueshift-bf99c560ef952901caa381b07959865189925c2a.tar.gz
blueshift-bf99c560ef952901caa381b07959865189925c2a.tar.bz2
blueshift-bf99c560ef952901caa381b07959865189925c2a.tar.xz
improve thread safety
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--examples/threaded15
1 files changed, 11 insertions, 4 deletions
diff --git a/examples/threaded b/examples/threaded
index 7b37cce..e70d33e 100644
--- a/examples/threaded
+++ b/examples/threaded
@@ -22,7 +22,7 @@ gamma_green = [1.15, 1.16]
gamma_blue = [1.11, 1.10]
-# Make colour curves thread local
+# Make colour curves thread local.
class threadlocal:
def __init__(self, obj):
self.default = obj
@@ -56,19 +56,24 @@ r_curve = threadlocal([i / (i_size - 1) for i in range(i_size)])
g_curve = threadlocal([i / (i_size - 1) for i in range(i_size)])
b_curve = threadlocal([i / (i_size - 1) for i in range(i_size)])
-## cmf_10deg uses non-thread safe cache, run once in advance so it is not done by the threads
+## cmf_10deg uses non-thread safe cache, run once in advance so it is not done by the threads.
cmf_10deg(0)
-# Thread synchronisation barrier
+# Thread synchronisation barrier.
barrier = threading.Barrier(len(gamma_red) + 1)
# Help functions for colour interpolation.
interpol, purify = None, None
-# Parameters in `periodically`
+# Parameters in `periodically`.
fade_ = None
+# randr is not threadsafe, and absolutely not if you have multiple screens.
+# This semaphore is used to make sure that two threads are not accessing randr at the same time.
+flush_semaphore = threading.Semaphore()
+
+
def adjust(m):
'''
Adjust monitor colours
@@ -97,7 +102,9 @@ def adjust(m):
gamma(gamma_red[m], gamma_green[m], gamma_blue[m])
# Flush settings to monitor.
+ flush_semaphore.acquire()
randr(m)
+ flush_semaphore.release()
# Signal thread completion.
barrier.wait()