1 /* -*- mode: c; tab-width: 4; fill-column: 78 -*- */
2 /* vi: set ts=4 tw=128: */
5 thread_util.c, Copyright (c) 2014 Dave Odell <dmo2118@gmail.com>
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
11 documentation. No representations are made about the suitability of this
12 software for any purpose. It is provided "as is" without express or
16 #include "thread_util.h"
18 #include "aligned_malloc.h"
19 #include "resources.h"
29 #include <stdio.h> /* Only used by thread_memory_alignment(). */
40 #if defined __MACH__ && defined __APPLE__ /* OS X, iOS */
41 # include <sys/sysctl.h>
45 #define IS_POWER_OF_2(x) ((x) > 0 && !((x) & ((x) - 1)))
48 arraysize(a). Also known as countof(x), XtNumber(x), NELEMS(x), LEN(x),
49 NUMOF(x), ARRAY_SIZE(x), etc., since the fine folks behind C never got
50 around to including this incredibly useful macro in the standard library,
51 which is where it belongs.
53 Much of the code here assumes that multiple processors in a system all use
54 the same cache line size...which might be wrong on occasion.
57 #define arraysize(a) (sizeof(a) / sizeof(*(a)))
58 #define arrayend(a) ((a) + arraysize(a))
61 These numbers are from:
62 - Linux: arch/(arch name)/include/asm/cache.h, note
63 L1_CACHE_BYTES/L1_CACHE_SHIFT/SMP_CACHE_BYTES.
64 - FreeBSD: sys/(sys name)/include/param.h, note
65 CACHE_LINE_SHIFT/CACHE_LINE_SIZE.
67 Preprocessor symbols come from:
68 - TARGET_CPU_CPP_BUILTINS() in the GNU C preprocessor
69 <http://code.ohloh.net/?s=%22TARGET_CPU_CPP_BUILTINS%22&fp=304413>
70 - http://predef.sourceforge.net/
74 Several architectures need preprocessor symbols.
76 Qualcomm Hexagon: 1 << 5
77 Imagination Technologies META: 1 << 6
78 OpenRISC: 16 (Linux has the cache line size as a todo.)
85 # error unistd.h must be present whenever pthread.h is.
88 # if defined __MACH__ && defined __APPLE__ /* OS X, iOS */
89 # include <TargetConditionals.h> /* For TARGET_OS_IPHONE. */
90 # ifdef TARGET_OS_IPHONE
91 # define _CACHE_LINE_SIZE 64
95 # if defined __FreeBSD__ && !defined _CACHE_LINE_SIZE
96 # include <machine/param.h>
97 # ifdef CACHE_LINE_SIZE
98 # define _CACHE_LINE_SIZE CACHE_LINE_SIZE
102 # if !defined _CACHE_LINE_SIZE
103 # if defined __alpha || defined __alpha__
105 # define _CACHE_LINE_SIZE 64 /* EV6 and above. EV4 and EV5 use 32 bytes. */
106 # elif defined __arm__
107 /* ARM architecture */
108 # define _CACHE_LINE_SIZE (1 << 6)
109 # elif defined __AVR || defined __AVR__
111 # define _CACHE_LINE_SIZE (1 << 5)
112 # elif defined __bfin || defined __BFIN__
113 /* Analog Devices Blackfin */
114 # define _CACHE_LINE_SIZE (1 << 5)
115 # elif defined _TMS320C6X || defined __TMS320C6X__
116 /* Texas Instruments TMS320C6x */
117 # define _CACHE_LINE_SIZE (1 << 7) /* From L2. L1 data cache line is 1 << 6. */
118 # elif defined __cris
119 /* Axis Communications ETRAX CRIS */
120 # define _CACHE_LINE_SIZE 32
121 # elif defined __ia64__ || defined _IA64
123 # define _CACHE_LINE_SIZE (1 << 7)
124 # elif defined __M32R__ || defined __m32r__
125 /* Mitsubishi/Renesas M32R */
126 # define _CACHE_LINE_SIZE (1 << 4)
127 # elif defined __m68k__ || defined M68000 || defined __MC68K__
129 # define _CACHE_LINE_SIZE (1 << 4)
130 # elif defined __MICROBLAZE__ || defined __microblaze__
131 /* Xilinx MicroBlaze */
132 # define _CACHE_LINE_SIZE (1 << 5)
133 # elif defined __mips__ || defined __mips || defined __MIPS__
135 # define _CACHE_LINE_SIZE (1 << 6)
136 # elif defined __mn10300__ || defined __MN10300__
137 /* Matsushita/Panasonic MN103 */
138 # define _CACHE_LINE_SIZE 32 /* MN103E010 has 16 bytes. */
139 # elif defined __hppa || defined __hppa__
140 /* Hewlett-Packard PA-RISC */
141 # define _CACHE_LINE_SIZE 64 /* PA-RISC 2.0 uses 64 bytes, PA-RISC 1.1 uses 32. */
142 # elif defined __powerpc || defined _ARCH_PPC
143 /* Power Architecture (a.k.a. PowerPC) */
144 # define _CACHE_LINE_SIZE (1 << 7) /* Linux has a list of PPC models with associated L1_CACHE_SHIFT values. */
145 # elif defined __s390__ || defined __370__ || defined __zarch__ || defined __SYSC_ZARCH__
147 # define _CACHE_LINE_SIZE 256
148 # elif defined SUNPLUS || defined __SCORE__ || defined __score__
150 # define _CACHE_LINE_SIZE (1 << 4)
151 # elif defined __sh__
153 # define _CACHE_LINE_SIZE (1 << 5) /* SH3 and earlier used 1 << 4. */
154 # elif defined __sparc__ || defined __sparc
156 # define _CACHE_LINE_SIZE (1 << 7) /* Linux and FreeBSD disagree as to what this should be. */
157 # elif defined __tile__
158 /* Tilera TILE series */
159 # define _CACHE_LINE_SIZE (1 << 6) /* TILEPro uses different sizes for L1 and L2. */
160 # elif defined __i386 || defined __x86_64
162 # define _CACHE_LINE_SIZE (1 << 7)
163 # elif defined __xtensa__ || defined __XTENSA__
164 /* Cadence Design Systems/Tensilica Xtensa */
165 # define _CACHE_LINE_SIZE (1 << 5) /* 1 << 4 on some models. */
167 # endif /* !defined _CACHE_LINE_SIZE */
169 # if defined __NetBSD__ && !defined _CACHE_LINE_SIZE
171 NetBSD defines COHERENCY_UNIT to be 32 on MIPS, and 64 for all other platforms -- which is wrong. Still, this is what the kernel
172 uses; if this value didn't work, the system wouldn't run.
174 # include <sys/param.h>
175 # ifdef COHERENCY_UNIT
176 # define _CACHE_LINE_SIZE COHERENCY_UNIT
180 # ifndef _CACHE_LINE_SIZE
181 # define _CACHE_LINE_SIZE 256 /* Fallback cache line size. */
184 static unsigned _get_cache_line_size(void)
188 - Try to get the actual cache line size from the operating system.
189 - In the interest of keeping things simple, this only checks with
191 - A few other methods that could be added:
192 - Query x86 CPUs directly with the CPUID instruction.
193 - Query various ELF systems through the auxillary vector.
194 (Power, Alpha, SuperH)
195 - Query Linux through
196 /sys/devices/system/cpu/cpu?/cache/index?/coherency_line_size
198 - Query Linux through cache_alignment in /proc/cpuinfo
199 - Query Solaris through PICL.
200 - If that fails, return a value appropriate for the current CPU
202 - Otherwise, return a sufficiently large number.
206 sysconf(3) is not a syscall, it's a glibc call that, for cache line sizes,
207 uses CPUID on x86 and returns 0 on other platforms. If it were to work on
208 most other platforms, it would have to get cache information from the
209 kernel, since that information is usually made available by the processor
210 only in privileged mode.
211 https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/i386/sysconf.c;hb=HEAD
214 /* uClibc, newlib, dietlibc, musl, Bionic do not have this. */
216 # if HAVE_UNISTD_H && ( \
217 defined _SC_LEVEL1_DCACHE_LINESIZE || \
218 defined _SC_LEVEL2_CACHE_LINESIZE || \
219 defined _SC_LEVEL3_CACHE_LINESIZE || \
220 defined _SC_LEVEL4_CACHE_LINESIZE)
222 static const int names[] =
224 # ifdef _SC_LEVEL1_DCACHE_LINESIZE
225 _SC_LEVEL1_DCACHE_LINESIZE,
227 # ifdef _SC_LEVEL2_CACHE_LINESIZE
228 _SC_LEVEL2_CACHE_LINESIZE,
230 # ifdef _SC_LEVEL3_CACHE_LINESIZE
231 _SC_LEVEL3_CACHE_LINESIZE,
233 # ifdef _SC_LEVEL4_CACHE_LINESIZE
234 _SC_LEVEL4_CACHE_LINESIZE
241 for(name = names; name != arrayend(names); ++name)
243 long sysconf_result = sysconf(*name); /* Can return -1 or 0 on
246 if(sysconf_result > result)
247 result = sysconf_result;
253 /* Currently, this fails for every platform that isn't x86. Perhaps
254 future versions will support other processors? */
258 # if defined __MACH__ && defined __APPLE__
260 uint32_t result; /* sysctl.h says that hw.cachelinesize is a
262 size_t size = sizeof(result);
263 static const int name[] = {CTL_HW, HW_CACHELINE};
265 if(!sysctl((int *)name, 2, &result, &size, NULL, 0)) /* (int *) is for OS X. */
267 assert(size == sizeof(result));
273 /* Guess based on the CPU type. */
274 return _CACHE_LINE_SIZE;
277 const pthread_mutex_t mutex_initializer =
278 # if defined _GNU_SOURCE && !defined NDEBUG
279 PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
281 PTHREAD_MUTEX_INITIALIZER
285 const pthread_cond_t cond_initializer = PTHREAD_COND_INITIALIZER;
287 static long _has_pthread = 0; /* Initialize on first threadpool/barrier_create. */
288 static int _cache_line_size = sizeof(void *);
290 #endif /* HAVE_PTHREAD */
292 static void _thread_util_init(Display *dpy)
295 /* This is maybe not thread-safe, but: this should -- and generally will --
296 be called before the program launches its second thread. */
301 _has_pthread = _POSIX_THREADS;
303 _has_pthread = sysconf(_SC_THREADS);
306 if(_has_pthread >= 0)
308 if(get_boolean_resource(dpy, "useThreads", "Boolean"))
310 _cache_line_size = _get_cache_line_size();
311 assert(_cache_line_size >= sizeof(void *));
312 assert(IS_POWER_OF_2(_cache_line_size));
324 hardware_concurrency() -
326 Various platforms offer various statistics that look like they should be
327 useful: sysconf(_SC_NPROCESSORS_ONLN) (i.e. the number of 'online'
328 processors) in particular is available on many Unixes, and is frequently
329 used for functions like hardware_concurrency(). But 'online' is somewhat
330 ambiguous; it can mean:
332 1. The number of CPU cores that are not (temporarily) asleep. (e.g. Android
333 can sometimes put cores to sleep if they aren't being used, and this is
334 reflected in _SC_NPROCESSORS_ONLN.)
336 2. The maximum number of CPU cores that can be provided to this application,
337 as currently set by the system administrator. (2) is the one that
338 hardware_concurrency() ultimately needs.
342 Shamelessly plagarized from Boost.Thread and Stack Overflow
343 <http://stackoverflow.com/q/150355>. GNU libstdc++ has some of this too,
344 see thread::hardware_concurrency() in thread.cc.
345 http://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/src/c%2B%2B11/thread.cc?view=markup
347 This might not work right on less common systems for various reasons.
351 # if defined __APPLE__ && defined __MACH__ || \
352 defined __FreeBSD__ || \
353 defined __OpenBSD__ || \
354 defined __NetBSD__ || \
355 defined __DragonFly__ || \
359 BSD Unixes use sysctl(3) for this.
360 Some BSDs also support sysconf(3) for this, but in each case this was added
362 Linux: sysctl is present, but strongly deprecated.
363 Minix uses the NetBSD userspace, so it has both this and sysconf(3).
364 QNX: sysctl is present for kern.* and net.*, but it doesn't say anything
368 /* __APPLE__ without __MACH__ is OS 9 or earlier. __APPLE__ with __MACH__ is OS X. */
371 The usual thing to do here is for sysctl(3) to call __sysctl(2).
372 http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/gen/sysctl.c?only_with_tag=HEAD
373 http://svnweb.freebsd.org/base/head/lib/libc/gen/sysctl.c?view=markup
377 OS X: Xcode Instruments (as of Xcode 4; Apple likes to move things like
378 this around) can disable CPUs as a debugging tool.
379 Instruments -> Preferences... (Command-,) -> General -> Active Processor Cores
380 FreeBSD, OpenBSD: It doesn't look like CPUs can be disabled.
381 NetBSD: CPUs can be disabled manually through cpuctl(8).
386 /* FreeBSD: sys/sysctl.h needs sys/types.h, but the one doesn't bring the
387 other in automatically. */
388 # include <sys/types.h>
389 # include <sys/sysctl.h>
391 static unsigned _hardware_concurrency(void)
394 size_t size = sizeof(count);
396 # if defined __APPLE__ && defined __MACH__
397 /* Apple sez: sysctl("hw.logicalcpu") is affected by the "current power
398 management mode", so use hw.logicalcpu_max. */
399 /* https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/sysctl.3.html */
400 if(!sysctlbyname("hw.logicalcpu_max", &count, &size, NULL, 0)) /* Preferred on more recent Darwin. */
402 assert(size == sizeof(count));
407 # if defined HW_NCPUONLINE
408 /* NetBSD has this. */
410 static const int name[] = {CTL_HW, HW_NCPUONLINE};
411 if(!sysctl(name, 2, &count, &size, NULL, 0))
413 assert(size == sizeof(count));
420 static const int name[] = {CTL_HW, HW_NCPU};
421 if(!sysctl((int *)name, 2, &count, &size, NULL, 0)) /* (int *) is for OS X. */
423 assert(size == sizeof(count));
431 # elif HAVE_UNISTD_H && defined _SC_NPROCESSORS_ONLN
435 Linux 2.0 was the first version to provide SMP support via clone(2).
436 (e)glibc on Linux provides this, which in turn uses get_nprocs().
437 get_nprocs in turn uses /sys/devices/system/cpu/online, /proc/stat, or /proc/cpuinfo, whichever's available.
438 https://sourceware.org/git/?p=glibc.git;a=blob;f=posix/sysconf.c;hb=HEAD
439 https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/getsysstats.c;hb=HEAD
440 Linux usually isn't configured to auto-enable/disable cores.
441 SunOS (Solaris), sometime between 4.1.3 and 5.5.1.
442 This includes all open source derivatives of 5.10. (Illumos, OpenIndiana)
443 sysconf(_SC_NPROCESSORS_ONLN) call _sysconfig(2).
444 Not sure if CPU power management (enabled by default, see cpupm and
445 cpu_deep_idle in power.conf(4)) affects this.
446 psradm(1M) can bring up/down CPU cores, which affects
447 sysconf(_SC_NPROCESSORS_ONLN).
448 http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libc/port/gen/sysconf.c
449 Minix 3.2, at the latest. (This is the first version to support SMP.)
450 AIX 7.1, probably earlier.
453 Mac OS X apparently has this on 10.5+.
454 FreeBSD 5.0, NetBSD 5.0 also have this. They both call sysctl(3).
455 http://svnweb.freebsd.org/base/head/lib/libc/gen/sysconf.c?view=markup
456 http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/gen/sysconf.c?only_with_tag=HEAD
458 QNX has sysconf(3), but it doesn't have _SC_NPROCESSORS_*.
461 static unsigned _hardware_concurrency(void)
463 long count = sysconf(_SC_NPROCESSORS_ONLN);
464 return count > 0 ? count : 1;
469 static unsigned _hardware_concurrency(void)
471 return 1; /* Fallback for unknown systems. */
477 unsigned hardware_concurrency(Display *dpy)
479 _thread_util_init(dpy);
481 if(_has_pthread >= 0)
482 return _hardware_concurrency();
487 /* thread_memory_alignment() */
489 unsigned thread_memory_alignment(Display *dpy)
491 _thread_util_init(dpy);
493 return _cache_line_size;
495 return sizeof(void *);
501 static unsigned _threadpool_count_serial(struct threadpool *self)
504 assert(_has_pthread);
505 if(_has_pthread >= 0)
506 return self->count ? 1 : 0;
511 static void _serial_destroy(struct threadpool *self)
513 void *thread = self->serial_threads;
514 unsigned i, count = _threadpool_count_serial(self);
516 for(i = 0; i != count; ++i)
518 self->thread_destroy(thread);
519 thread = (char *)thread + self->thread_size;
522 free(self->serial_threads);
527 static void _parallel_abort(struct threadpool *self)
529 assert(self->count > 1);
530 self->count = self->parallel_unfinished + 1 /* The '+ 1' should technically be _threadpool_count_serial(self). */;
531 PTHREAD_VERIFY(pthread_cond_broadcast(&self->cond));
534 struct _parallel_startup_type
536 struct threadpool *parent;
537 int (*thread_create)(void *self, struct threadpool *pool, unsigned id);
541 static unsigned _threadpool_count_parallel(struct threadpool *self)
543 assert(_has_pthread);
544 assert(self->count >= 1);
545 return self->count - 1 /* The '- 1' should technically be _threadpool_count_serial(self). */;
548 static void *_start_routine(void *startup_raw);
550 /* Tricky lock sequence: _add_next_thread unlocks on error. */
551 static void _add_next_thread(struct _parallel_startup_type *self)
553 assert(!self->last_errno);
555 if(self->parent->parallel_unfinished == _threadpool_count_parallel(self->parent))
557 PTHREAD_VERIFY(pthread_cond_broadcast(&self->parent->cond));
561 pthread_t *thread = self->parent->parallel_threads + self->parent->parallel_unfinished;
562 self->last_errno = pthread_create(thread, NULL, _start_routine, self);
564 _parallel_abort(self->parent);
568 static void *_thread_free_and_unlock(struct threadpool *self, void *thread)
570 PTHREAD_VERIFY(pthread_mutex_unlock(&self->mutex));
577 static void *_thread_destroy_and_unlock(struct threadpool *self, void *thread)
579 self->thread_destroy(thread);
580 return _thread_free_and_unlock(self, thread);
583 /* At one point, one of the threads refused to destroy itself at the end. Why?! And why won't it happen again? */
585 static void *_start_routine(void *startup_raw)
587 struct _parallel_startup_type *startup = (struct _parallel_startup_type *)startup_raw;
589 struct threadpool *parent = startup->parent;
593 PTHREAD_VERIFY(pthread_mutex_lock(&parent->mutex));
594 ++parent->parallel_unfinished;
597 /* Ideally, the thread object goes on the thread's stack. This guarantees no false sharing with other threads, and in a NUMA
598 configuration, ensures that the thread object is using memory from the right node. */
599 thread = alloca(parent->thread_size);
601 startup->last_errno = thread_malloc(&thread, NULL, parent->thread_size);
602 if(startup->last_errno)
604 _parallel_abort(parent);
605 PTHREAD_VERIFY(pthread_mutex_unlock(&parent->mutex));
610 /* Setting thread affinity for threads running in lock-step can cause delays
611 and jumpiness. Ideally, there would be some way to recommend (but not
612 require) that a thread run on a certain core/set of cores. */
614 /* Neither Linux nor libnuma seem to support the concept of a preferred/ideal
615 CPU for a thread/process. */
621 CPU_SET(&cpu_set, &parent._threads_unfinished);
622 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set);
625 startup->last_errno = startup->thread_create(thread, parent, parent->parallel_unfinished);
626 if(startup->last_errno)
628 _parallel_abort(parent);
629 return _thread_free_and_unlock(parent, thread); /* Tail calls make everything better. */
632 assert(!startup->last_errno);
633 _add_next_thread(startup); /* Calls _parallel_abort() on failure. */
634 if(startup->last_errno)
635 return _thread_destroy_and_unlock(parent, thread);
642 This must come before the '.threads' check, otherwise if
643 threadpool_destroy is called immediately after a run starts, then
644 it's possible that not all threads would be launched for the final
645 run. This can cause deadlock in conjunction with things like
648 if(parent->parallel_pending)
649 break; /* Start a run. */
651 if(!parent->parallel_threads)
652 return _thread_destroy_and_unlock(parent, thread); /* Threads are shutting down. */
654 PTHREAD_VERIFY(pthread_cond_wait(&parent->cond, &parent->mutex));
657 --parent->parallel_pending;
658 if(!parent->parallel_pending)
659 PTHREAD_VERIFY(pthread_cond_broadcast(&parent->cond));
660 /* All threads have started processing, other threads can finish. */
662 PTHREAD_VERIFY(pthread_mutex_unlock(&parent->mutex));
664 parent->thread_run(thread);
666 PTHREAD_VERIFY(pthread_mutex_lock(&parent->mutex));
668 if(!parent->parallel_threads) /* I don't think this is necessary anymore. */
671 /* Don't loop around until all other threads have begun processing. */
673 /* I suspect it doesn't matter whether this comes before or after the threads_unfinished check. */
674 while(parent->parallel_pending)
675 PTHREAD_VERIFY(pthread_cond_wait(&parent->cond, &parent->mutex));
677 --parent->parallel_unfinished;
678 if(!parent->parallel_unfinished)
679 PTHREAD_VERIFY(pthread_cond_broadcast(&parent->cond)); /* All threads done for now. */
682 /* return _thread_destroy_and_unlock(parent, thread); */
685 static void _unlock_and_destroy(struct threadpool *self)
689 threads = self->parallel_threads;
690 self->parallel_threads = NULL;
693 PTHREAD_VERIFY(pthread_cond_broadcast(&self->cond));
695 PTHREAD_VERIFY(pthread_mutex_unlock(&self->mutex));
701 unsigned i, count = _threadpool_count_parallel(self);
702 for(i = 0; i != count; ++i)
703 PTHREAD_VERIFY(pthread_join(threads[i], NULL));
707 PTHREAD_VERIFY(pthread_cond_destroy(&self->cond));
708 PTHREAD_VERIFY(pthread_mutex_destroy(&self->mutex));
710 _serial_destroy(self);
713 #endif /* HAVE_PTHREAD */
715 int threadpool_create(struct threadpool *self, const struct threadpool_class *cls, Display *dpy, unsigned count)
717 _thread_util_init(dpy);
721 /* If threads are not present, run each "thread" in sequence on the calling
722 thread. Otherwise, only run the first thread on the main thread. */
726 self->thread_size = cls->size;
727 self->thread_destroy = cls->destroy;
731 unsigned i, count_serial = _threadpool_count_serial(self);
735 thread = malloc(cls->size * count_serial);
741 /* Might as well skip the malloc. */
745 self->serial_threads = thread;
747 for(i = 0; i != count_serial; ++i)
749 int error = cls->create(thread, self, i);
753 _serial_destroy(self);
757 thread = (char *)thread + self->thread_size;
762 assert(_has_pthread); /* _has_pthread should be either -1 or >0. */
763 if(_has_pthread >= 0)
765 unsigned count_parallel = _threadpool_count_parallel(self);
766 self->mutex = mutex_initializer;
767 self->cond = cond_initializer;
768 self->parallel_pending = 0;
769 self->parallel_unfinished = 0;
772 self->parallel_threads = NULL;
776 self->parallel_threads = malloc(sizeof(pthread_t) * count_parallel);
777 if(!self->parallel_threads)
781 struct _parallel_startup_type startup;
782 startup.parent = self;
783 startup.thread_create = cls->create;
784 startup.last_errno = 0;
786 PTHREAD_VERIFY(pthread_mutex_lock(&self->mutex));
787 _add_next_thread(&startup);
789 if(!startup.last_errno)
791 while(self->parallel_unfinished != count_parallel && self->parallel_threads)
792 PTHREAD_VERIFY(pthread_cond_wait(&self->cond, &self->mutex));
795 /* This must come after the if(!startup.last_errno). */
796 if(startup.last_errno)
798 _unlock_and_destroy(self);
802 self->parallel_unfinished = 0;
803 PTHREAD_VERIFY(pthread_mutex_unlock(&self->mutex));
806 return startup.last_errno;
814 void threadpool_destroy(struct threadpool *self)
817 if(_has_pthread >= 0)
819 PTHREAD_VERIFY(pthread_mutex_lock(&self->mutex));
820 _unlock_and_destroy(self);
825 _serial_destroy(self);
828 void threadpool_run(struct threadpool *self, void (*func)(void *))
831 if(_has_pthread >= 0)
833 unsigned count = _threadpool_count_parallel(self);
834 PTHREAD_VERIFY(pthread_mutex_lock(&self->mutex));
836 /* Do not call threadpool_run() twice without a threadpool_wait() in the middle. */
837 assert(!self->parallel_pending);
838 assert(!self->parallel_unfinished);
840 self->parallel_pending = count;
841 self->parallel_unfinished = count;
842 self->thread_run = func;
843 PTHREAD_VERIFY(pthread_cond_broadcast(&self->cond));
844 PTHREAD_VERIFY(pthread_mutex_unlock(&self->mutex));
848 /* It's perfectly valid to move this to the beginning of threadpool_wait(). */
850 void *thread = self->serial_threads;
851 unsigned i, count = _threadpool_count_serial(self);
852 for(i = 0; i != count; ++i)
855 thread = (char *)thread + self->thread_size;
860 void threadpool_wait(struct threadpool *self)
863 if(_has_pthread >= 0)
865 PTHREAD_VERIFY(pthread_mutex_lock(&self->mutex));
866 while(self->parallel_unfinished)
867 PTHREAD_VERIFY(pthread_cond_wait(&self->cond, &self->mutex));
868 PTHREAD_VERIFY(pthread_mutex_unlock(&self->mutex));