2 * Copyright (c) 2011, Mark Heily <mark@heily.com>
3 * Copyright (c) 2009, Stacey Son <sson@freebsd.org>
4 * Copyright (c) 2000-2008, Apple Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef _PTWQ_PRIVATE_H
31 #define _PTWQ_PRIVATE_H 1
41 # include "windows/platform.h"
43 # include "posix/platform.h"
46 #include "pthread_workqueue.h"
49 /* The maximum number of workqueues that can be created.
50 This is based on libdispatch only needing 6 workqueues.
52 #define PTHREAD_WORKQUEUE_MAX 31
54 /* The total number of priority levels. */
55 #define WORKQ_NUM_PRIOQUEUE 3
57 /* Signatures/magic numbers. */
58 #define PTHREAD_WORKQUEUE_SIG 0xBEBEBEBE
59 #define PTHREAD_WORKQUEUE_ATTR_SIG 0xBEBEBEBE
61 /* Whether to use real-time threads for the workers if available */
63 extern unsigned int PWQ_RT_THREADS;
64 extern time_t PWQ_SPIN_USEC;
65 extern unsigned int PWQ_SPIN_THREADS;
67 /* A limit of the number of cpu:s that we view as available, useful when e.g. using processor sets */
68 extern unsigned int PWQ_ACTIVE_CPU;
71 #define fastpath(x) ((__typeof__(x))__builtin_expect((long)(x), ~0l))
72 #define slowpath(x) ((__typeof__(x))__builtin_expect((long)(x), 0l))
74 #define fastpath(x) (x)
75 #define slowpath(x) (x)
78 #define CACHELINE_SIZE 64
79 #define ROUND_UP_TO_CACHELINE_SIZE(x) (((x) + (CACHELINE_SIZE - 1)) & ~(CACHELINE_SIZE - 1))
82 * The work item cache, has three different optional implementations:
83 * 1. No cache, just normal malloc/free using the standard malloc library in use
84 * 2. Libumem based object cache, requires linkage with libumem - for non-Solaris see http://labs.omniti.com/labs/portableumem
85 * this is the most balanced cache supporting migration across threads of allocated/freed witems
86 * 3. TSD based cache, modelled on libdispatch continuation implementation, can lead to imbalance with assymetric
87 * producer/consumer threads as allocated memory is cached by the thread freeing it
90 #define WITEM_CACHE_TYPE 1 // Set to 1, 2 or 3 to specify witem cache implementation to use
93 STAILQ_ENTRY(work) item_entry;
97 unsigned int gencount;
98 #if (WITEM_CACHE_TYPE == 3)
99 struct work *volatile wi_next;
103 struct _pthread_workqueue {
104 unsigned int sig; /* Unique signature for this structure */
108 unsigned int wqlist_index;
109 STAILQ_HEAD(,work) item_listhead;
110 pthread_spinlock_t mtx;
111 #ifdef WORKQUEUE_PLATFORM_SPECIFIC
112 WORKQUEUE_PLATFORM_SPECIFIC;
117 int manager_init(void);
118 unsigned long manager_peek(const char *);
119 void manager_workqueue_create(struct _pthread_workqueue *);
120 void manager_workqueue_additem(struct _pthread_workqueue *, struct work *);
122 struct work *witem_alloc(void (*func)(void *), void *func_arg); // returns a properly initialized witem
123 void witem_free(struct work *wi);
124 int witem_cache_init(void);
125 void witem_cache_cleanup(void *value);
127 #endif /* _PTWQ_PRIVATE_H */