src/ThreadUtil.jl

Mon, 06 Dec 2021 11:52:10 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 06 Dec 2021 11:52:10 +0200
changeset 33
a60d2f12ef93
permissions
-rw-r--r--

Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.

33
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 ###########################
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Some threading utilities
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 ###########################
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 __precompile__()
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 module ThreadUtil
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 ##############
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 # Our exports
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 ##############
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 export @threadsif,
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 @background,
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 @backgroundif
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 ##########
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 # Threads
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 ##########
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 macro threadsif(threads, loop)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 return esc(:(if $threads
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 Threads.@threads $loop
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 else
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 $loop
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 end))
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 macro background(bgtask, fgtask)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 return :(t = Threads.@spawn $(esc(bgtask));
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 $(esc(fgtask));
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 wait(t))
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 macro backgroundif(threads, bgtask, fgtask)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 return :(if $(esc(threads))
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 @background $(esc(bgtask)) $(esc(fgtask))
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 else
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 $(esc(bgtask))
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 $(esc(fgtask))
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 end)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 end # Module
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46

mercurial