1 /*
2  * libmowgli: A collection of useful routines for programming.
3  * thread.h: Cross-platform threading helper routines.
4  *
5  * Copyright (c) 2011 Wilcox Technologies, LLC <awilcox -at- wilcox-tech.com>
6  * Copyright (c) 2011, 2012 William Pitcock <nenolod@dereferenced.org>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice is present in all copies.
11  *
12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
13  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
16  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
20  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
21  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22  * POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #ifndef __MOWGLI_THREAD_H__
26 #define __MOWGLI_THREAD_H__
27 
28 #ifdef MOWGLI_OS_UNIX_TYPE
29 # include <thread.h>
30 # define MOWGLI_FEATURE_HAVE_NATIVE_THREADS
31 # ifdef MOWGLI_OS_THREADS_SOLARIS
32 #  define  MOWGLI_NATIVE_THREAD_DECL(name) pthread_t(name)
33 # else
34 #  define MOWGLI_NATIVE_THREAD_DECL(name) thread_t(name)
35 # endif
36 #elif defined MOWGLI_OS_WIN
37 # define MOWGLI_FEATURE_HAVE_NATIVE_THREADS
38 # define MOWGLI_NATIVE_THREAD_DECL(name) HANDLE(name)
39 #else
40 # include <pthread.h>
41 #endif
42 
43 typedef struct
44 {
45 #ifdef MOWGLI_FEATURE_HAVE_NATIVE_THREADS
46 	MOWGLI_NATIVE_THREAD_DECL(thread);
47 #else
48 	pthread_t thread;
49 #endif
50 } mowgli_thread_t;
51 
52 #ifdef MOWGLI_NATIVE_THREAD_DECL
53 # undef MOWGLI_NATIVE_THREAD_DECL
54 #endif
55 
56 typedef void *(*mowgli_thread_start_fn_t)(mowgli_thread_t *thread, void *userdata);
57 
58 /*
59  * Note: we should keep our thread interface light and minimal for best possible
60  * portability.  Creating, ending, killing and cleanup functions are presently implemented,
61  * and cover approximately 99.999% of uses of thread APIs.  --nenolod
62  */
63 typedef struct
64 {
65 	int (*thread_create)(mowgli_thread_t *thread, mowgli_thread_start_fn_t start_fn, void *userdata);
66 	void (*thread_exit)(mowgli_thread_t *thread);
67 	void *(*thread_join)(mowgli_thread_t * thread);
68 	void (*thread_kill)(mowgli_thread_t *thread);
69 	void (*thread_destroy)(mowgli_thread_t *thread);
70 } mowgli_thread_ops_t;
71 
72 int mowgli_thread_create(mowgli_thread_t *thread, mowgli_thread_start_fn_t start_fn, void *userdata);
73 void mowgli_thread_exit(mowgli_thread_t *thread);
74 void *mowgli_thread_join(mowgli_thread_t *thread);
75 void mowgli_thread_kill(mowgli_thread_t *thread);
76 void mowgli_thread_destroy(mowgli_thread_t *thread);
77 
78 typedef enum
79 {
80 	MOWGLI_THREAD_POLICY_DEFAULT,
81 	MOWGLI_THREAD_POLICY_DISABLED,
82 } mowgli_thread_policy_t;
83 
84 #endif
85