Tizen 2.1 base
[platform/upstream/gcd.git] / pthread_workqueue-0.8.2 / src / private.h
1 /*-
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.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  *
28  */
29
30 #ifndef _PTWQ_PRIVATE_H
31 #define _PTWQ_PRIVATE_H 1
32
33 #include <errno.h>
34 #include <limits.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #if defined(_WIN32)
41 # include "windows/platform.h"
42 #else
43 # include "posix/platform.h"
44 #endif
45
46 #include "pthread_workqueue.h"
47 #include "debug.h"
48
49 /* The maximum number of workqueues that can be created.
50    This is based on libdispatch only needing 6 workqueues.
51    */
52 #define PTHREAD_WORKQUEUE_MAX 31
53
54 /* The total number of priority levels. */
55 #define WORKQ_NUM_PRIOQUEUE 3
56
57 /* Signatures/magic numbers.  */
58 #define PTHREAD_WORKQUEUE_SIG       0xBEBEBEBE
59 #define PTHREAD_WORKQUEUE_ATTR_SIG  0xBEBEBEBE 
60
61 /* Whether to use real-time threads for the workers if available */
62
63 extern unsigned int PWQ_RT_THREADS;
64 extern time_t PWQ_SPIN_USEC;
65 extern unsigned int PWQ_SPIN_THREADS;
66
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;
69
70 #if __GNUC__
71 #define fastpath(x)     ((__typeof__(x))__builtin_expect((long)(x), ~0l))
72 #define slowpath(x)     ((__typeof__(x))__builtin_expect((long)(x), 0l))
73 #else
74 #define fastpath(x) (x)
75 #define slowpath(x) (x)
76 #endif
77
78 #define CACHELINE_SIZE  64
79 #define ROUND_UP_TO_CACHELINE_SIZE(x)   (((x) + (CACHELINE_SIZE - 1)) & ~(CACHELINE_SIZE - 1))
80
81 /*
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
88  */
89
90 #define WITEM_CACHE_TYPE 1 // Set to 1, 2 or 3 to specify witem cache implementation to use
91
92 struct work {
93     STAILQ_ENTRY(work)   item_entry; 
94     void               (*func)(void *);
95     void                *func_arg;
96     unsigned int         flags;
97     unsigned int         gencount;
98 #if (WITEM_CACHE_TYPE == 3)
99         struct work *volatile wi_next;
100 #endif
101 };
102
103 struct _pthread_workqueue {
104     unsigned int         sig;    /* Unique signature for this structure */
105     unsigned int         flags;
106     int                  queueprio;
107     int                  overcommit;
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;
113 #endif
114 };
115
116 /* manager.c */
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 *);
121
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);
126
127 #endif  /* _PTWQ_PRIVATE_H */